Mock div.style through document.createElement hack instead of exposing method in vendorPrefix.js

This commit is contained in:
Gregers Gram Rygg
2012-06-27 16:49:59 +02:00
parent 1ba6aa75d7
commit d71e7a7beb
2 changed files with 23 additions and 34 deletions

View File

@@ -126,9 +126,6 @@ OpenLayers.Util.vendorPrefix = (function() {
// used for testing
cssCache: cssCache,
jsCache: jsCache,
_mockStyle: function(mock) {
divStyle = mock;
}
jsCache: jsCache
};
}());

View File

@@ -3,6 +3,14 @@
<head>
<title>vendorPrefix.js Tests</title>
<script>
var div = document.createElement("div");
var style = div.style,
orgCreateElement = document.createElement;
// wrap document.createElement to control property values
document.createElement = function(type) {
return div;
};
// dependencies for tests
var OpenLayers = [
@@ -19,22 +27,8 @@
*/
function test_vendor_prefixes(t) {
t.plan(20);
var o = {
prop: null,
val: null
}, err;
var err;
/*var orgCreateElement = document.createElement;
// wrap document.createElement to control property values
document.createElement = function(type) {
var el = orgCreateElement.call(document, type);
// Since o is an object we can change prop for each test
if(o.prop) {
el.style[o.prop] = o.val;
}
return el;
};*/
function clearCache(type) {
var cache = OpenLayers.Util.vendorPrefix[type.replace("style", "js") + "Cache"];
for (var key in cache) {
@@ -42,20 +36,27 @@
}
}
function setStyleMockProp(prop, value) {
if (prop && value === undefined) {
delete style[prop];
} else if (prop) {
style[prop] = value;
}
}
function curryTestPrefix(type) {
return function(standardProp, expectedPrefix, msg) {
var prefixedProp, err;
try {
clearCache(type);
var fakeStyle = { cssText: "" };
if (o.prop != null) {
fakeStyle[o.prop] = o.val;
}
OpenLayers.Util.vendorPrefix._mockStyle(fakeStyle);
setStyleMockProp(expectedPrefix, "");
prefixedProp = OpenLayers.Util.vendorPrefix[type](standardProp);
} catch(e) {
err = e;
} finally {
setStyleMockProp(expectedPrefix, undefined);
}
if(!err) {
t.eq(prefixedProp, expectedPrefix, msg);
} else {
@@ -66,33 +67,24 @@
var testDomPrefix = curryTestPrefix("style"),
testCssPrefix = curryTestPrefix("css");
o.prop = undefined;
o.val = undefined;
testDomPrefix("unsupported", null, "DOM vendor prefix - unsupported");
testCssPrefix("unsupported", null, "CSS vendor prefix - unsupported");
o.prop = "test";
o.val = "";
testDomPrefix("test", "test", "DOM vendor prefix - single word");
testCssPrefix("test", "test", "CSS vendor prefix - single word");
o.prop = "testMultiWord";
testDomPrefix("testMultiWord", "testMultiWord", "DOM vendor prefix - multiple words");
testCssPrefix("test-multi-word", "test-multi-word", "CSS vendor prefix - multiple words");
o.prop = "WebkitMultiWord";
testDomPrefix("multiWord", "WebkitMultiWord", "DOM vendor prefix - multiple words for WebKit");
testCssPrefix("multi-word", "-webkit-multi-word", "CSS vendor prefix - multiple words for WebKit");
o.prop = "MozMultiWord";
testDomPrefix("multiWord", "MozMultiWord", "DOM vendor prefix - multiple words for Mozilla");
testCssPrefix("multi-word", "-moz-multi-word", "CSS vendor prefix - multiple words for Mozilla");
o.prop = "OMultiWord";
testDomPrefix("multiWord", "OMultiWord", "DOM vendor prefix - multiple words for Opera");
testCssPrefix("multi-word", "-o-multi-word", "CSS vendor prefix - multiple words for Opera");
o.prop = "msMultiWord";
testDomPrefix("multiWord", "msMultiWord", "DOM vendor prefix - multiple words for Internet Explorer");
testCssPrefix("multi-word", "-ms-multi-word", "CSS vendor prefix - multiple words for Internet Explorer");
@@ -116,7 +108,7 @@
t.eq( OpenLayers.Util.vendorPrefix.js( { "webkitTest": true }, "test" ), "webkitTest", "Standard object property");
// unwrap document.createElement
//document.createElement = orgCreateElement;
document.createElement = orgCreateElement;
}
</script>