| OLD | NEW |
| (Empty) |
| 1 /* Use this script when you want to test APIs that use vendor prefixes | |
| 2 and define which objects need to be checked for prefixed versions, à la | |
| 3 <script src="vendor-prefix.js" | |
| 4 data-prefixed-objects='[{"ancestors":["navigator"], "name":"getUserMedia"}]
' | |
| 5 data-prefixed-prototypes='[{"ancestors":["HTMLMediaElement"],"name":"srcObjec
t"}]'></script> | |
| 6 data-prefixed-objects lets prefix objects in the global space | |
| 7 data-prefixed-prototypes adds prefixes to interfaces, for objects that | |
| 8 get created during the tests | |
| 9 | |
| 10 NB: vendor prefixes are expected to go away in favor of putting | |
| 11 new features behind flag, so hopefully there will be only limited | |
| 12 need to use this | |
| 13 */ | |
| 14 | |
| 15 (function () { | |
| 16 var aliases = {}; | |
| 17 var documentingPrefixUsage = document.createElement('div'); | |
| 18 var vendorPrefixes = ["moz", "ms", "o", "webkit", "Moz", "MS", "O", "WebKit"
, "op"]; | |
| 19 | |
| 20 function getParentObject(ancestors) { | |
| 21 var parent = window; | |
| 22 var currentName = ""; | |
| 23 ancestors.forEach(function (p) { | |
| 24 currentName = currentName ? currentName + "." + p : p; | |
| 25 if (parent[p] === undefined) { | |
| 26 throw currentName + " is undefined, cannot set prefix alias on c
hild object"; | |
| 27 } | |
| 28 parent = parent[p]; | |
| 29 }); | |
| 30 return parent; | |
| 31 } | |
| 32 | |
| 33 function prependPrefix(prefix, name) { | |
| 34 var newName = name[0].toUpperCase() + name.substr(1, name.length); | |
| 35 return prefix + newName; | |
| 36 } | |
| 37 | |
| 38 function setPrototypeAlias(obj) { | |
| 39 var parent = getParentObject(obj.ancestors); | |
| 40 if (!parent.prototype.hasOwnProperty(obj.name)) { | |
| 41 vendorPrefixes.forEach(function (prefix) { | |
| 42 if (parent.prototype.hasOwnProperty(prependPrefix(prefix, obj.na
me))) { | |
| 43 Object.defineProperty(parent.prototype, obj.name, | |
| 44 {get: function() {return this[prependP
refix(prefix, obj.name)];}, | |
| 45 set: function(v) {this[prependPrefix(
prefix, obj.name)] = v;} | |
| 46 }); | |
| 47 aliases[obj.ancestors.join(".") + ".prototype." + obj.name]
= obj.ancestors.join(".") + ".prototype." + prependPrefix(prefix, obj.name); | |
| 48 return; | |
| 49 } | |
| 50 }); | |
| 51 } | |
| 52 } | |
| 53 | |
| 54 function setAlias(obj) { | |
| 55 var parent = getParentObject(obj.ancestors); | |
| 56 if (parent[obj.name] === undefined) { | |
| 57 vendorPrefixes.forEach(function (prefix) { | |
| 58 if (parent[prependPrefix(prefix, obj.name)] !== undefined) { | |
| 59 parent[obj.name] = parent[prependPrefix(prefix, obj.name)]; | |
| 60 aliases[obj.ancestors.join(".") + "." + obj.name] = obj.ance
stors.join(".") + "." + prependPrefix(prefix, obj.name); | |
| 61 return; | |
| 62 } | |
| 63 }); | |
| 64 } | |
| 65 } | |
| 66 | |
| 67 if (location.search.indexOf('usePrefixes=1') !== -1) { | |
| 68 if (document.querySelector("script[data-prefixed-objects]")) { | |
| 69 var prefixObjectsData = document.querySelector("script[data-prefixed
-objects]").dataset["prefixedObjects"]; | |
| 70 try { | |
| 71 var prefixedObjects = JSON.parse(prefixObjectsData); | |
| 72 } catch (e) { | |
| 73 throw "couldn't parse data-prefixed-objects as JSON:" + e; | |
| 74 } | |
| 75 prefixedObjects.forEach(setAlias); | |
| 76 } | |
| 77 if (document.querySelector("script[data-prefixed-prototypes]")) { | |
| 78 var prefixProtoData = document.querySelector("script[data-prefixed-p
rototypes]").dataset["prefixedPrototypes"]; | |
| 79 try { | |
| 80 var prefixedPrototypes = JSON.parse(prefixProtoData); | |
| 81 } catch (e) { | |
| 82 throw "couldn't parse data-prefixed-prototypes as JSON:" + e; | |
| 83 } | |
| 84 prefixedPrototypes.forEach(setPrototypeAlias); | |
| 85 } | |
| 86 var ul = document.createElement("ul"); | |
| 87 Object.keys(aliases).forEach(function (alias) { | |
| 88 var li = document.createElement("li"); | |
| 89 li.appendChild(document.createTextNode(alias + " has been set to be
an alias of vendor-prefixed " + aliases[alias])); | |
| 90 ul.appendChild(li); | |
| 91 }); | |
| 92 documentingPrefixUsage.appendChild(ul); | |
| 93 } else { | |
| 94 // Document that the test can be run with prefixes enabled | |
| 95 | |
| 96 var a = document.createElement('a'); | |
| 97 var link = ""; | |
| 98 if (location.search) { | |
| 99 link = location.search + "&usePrefixes=1"; | |
| 100 } else { | |
| 101 link = "?usePrefixes=1"; | |
| 102 } | |
| 103 a.setAttribute("href", link); | |
| 104 a.appendChild(document.createTextNode("with vendor prefixes enabled")); | |
| 105 documentingPrefixUsage.appendChild(document.createTextNode("The feature(
s) tested here are known to have been made available via vendor prefixes; you ca
n run this test ")); | |
| 106 documentingPrefixUsage.appendChild(a); | |
| 107 documentingPrefixUsage.appendChild(document.createTextNode(".")); | |
| 108 } | |
| 109 var log = document.getElementById('log'); | |
| 110 if (log) { | |
| 111 log.parentNode.insertBefore(documentingPrefixUsage, log); | |
| 112 } else { | |
| 113 document.body.appendChild(documentingPrefixUsage); | |
| 114 } | |
| 115 })(); | |
| OLD | NEW |