Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* Use this script when you want to test APIs that use vendor prefixes | |
|
Dirk Pranke
2014/07/02 16:09:02
Is this file actually cloned from the w3c somewher
| |
| 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 // For this version of vendor-prefixes.js, always replace the prefixes. | |
| 68 if (document.querySelector("script[data-prefixed-objects]")) { | |
| 69 var prefixObjectsData = document.querySelector("script[data-prefixed-obj ects]").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-proto types]").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 })(); | |
| OLD | NEW |