2 * Distributed under both the W3C Test Suite License [1] and the W3C
3 * 3-clause BSD License [2]. To contribute to a W3C Test Suite, see the
4 * policies and contribution forms [3].
6 * [1] http://www.w3.org/Consortium/Legal/2008/04-testsuite-license
7 * [2] http://www.w3.org/Consortium/Legal/2008/03-bsd-license
8 * [3] http://www.w3.org/2004/10/27-testcases
11 /* Source: https://github.com/w3c/web-platform-tests/blob/master/common/vendor-prefix.js
12 * The file has been modified to always be on (i.e. does not require usePrefixes=1 to
13 * start replacing prefixes). */
15 /* Use this script when you want to test APIs that use vendor prefixes
16 and define which objects need to be checked for prefixed versions, à la
17 <script src="vendor-prefix.js"
18 data-prefixed-objects='[{"ancestors":["navigator"], "name":"getUserMedia"}]'
19 data-prefixed-prototypes='[{"ancestors":["HTMLMediaElement"],"name":"srcObject"}]'></script>
20 data-prefixed-objects lets prefix objects in the global space
21 data-prefixed-prototypes adds prefixes to interfaces, for objects that
22 get created during the tests
24 NB: vendor prefixes are expected to go away in favor of putting
25 new features behind flag, so hopefully there will be only limited
31 var documentingPrefixUsage = document.createElement('div');
32 var vendorPrefixes = ["moz", "ms", "o", "webkit", "Moz", "MS", "O", "WebKit", "op"];
34 function getParentObject(ancestors) {
37 ancestors.forEach(function (p) {
38 currentName = currentName ? currentName + "." + p : p;
39 if (parent[p] === undefined) {
40 throw currentName + " is undefined, cannot set prefix alias on child object";
47 function prependPrefix(prefix, name) {
48 var newName = name[0].toUpperCase() + name.substr(1, name.length);
49 return prefix + newName;
52 function setPrototypeAlias(obj) {
53 var parent = getParentObject(obj.ancestors);
54 if (!parent.prototype.hasOwnProperty(obj.name)) {
55 vendorPrefixes.forEach(function (prefix) {
56 if (parent.prototype.hasOwnProperty(prependPrefix(prefix, obj.name))) {
57 Object.defineProperty(parent.prototype, obj.name,
58 {get: function() {return this[prependPrefix(prefix, obj.name)];},
59 set: function(v) {this[prependPrefix(prefix, obj.name)] = v;}
61 aliases[obj.ancestors.join(".") + ".prototype." + obj.name] = obj.ancestors.join(".") + ".prototype." + prependPrefix(prefix, obj.name);
68 function setAlias(obj) {
69 var parent = getParentObject(obj.ancestors);
70 if (parent[obj.name] === undefined) {
71 vendorPrefixes.forEach(function (prefix) {
72 if (parent[prependPrefix(prefix, obj.name)] !== undefined) {
73 parent[obj.name] = parent[prependPrefix(prefix, obj.name)];
74 aliases[obj.ancestors.join(".") + "." + obj.name] = obj.ancestors.join(".") + "." + prependPrefix(prefix, obj.name);
81 // For this version of vendor-prefixes.js, always replace the prefixes.
82 if (document.querySelector("script[data-prefixed-objects]")) {
83 var prefixObjectsData = document.querySelector("script[data-prefixed-objects]").dataset["prefixedObjects"];
85 var prefixedObjects = JSON.parse(prefixObjectsData);
87 throw "couldn't parse data-prefixed-objects as JSON:" + e;
89 prefixedObjects.forEach(setAlias);
91 if (document.querySelector("script[data-prefixed-prototypes]")) {
92 var prefixProtoData = document.querySelector("script[data-prefixed-prototypes]").dataset["prefixedPrototypes"];
94 var prefixedPrototypes = JSON.parse(prefixProtoData);
96 throw "couldn't parse data-prefixed-prototypes as JSON:" + e;
98 prefixedPrototypes.forEach(setPrototypeAlias);