Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / resources / vendor-prefix.js
1 /*
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].
5  *
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
9  * */
10
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).  */
14
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
23
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
26    need to use this
27 */
28
29 (function () {
30     var aliases = {};
31     var documentingPrefixUsage = document.createElement('div');
32     var vendorPrefixes = ["moz", "ms", "o", "webkit", "Moz", "MS", "O", "WebKit", "op"];
33
34     function getParentObject(ancestors) {
35         var parent = window;
36         var currentName = "";
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";
41             }
42             parent = parent[p];
43         });
44         return parent;
45     }
46
47     function prependPrefix(prefix, name) {
48         var newName = name[0].toUpperCase() + name.substr(1, name.length);
49         return prefix + newName;
50     }
51
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;}
60                                           });
61                     aliases[obj.ancestors.join(".") + ".prototype." + obj.name] = obj.ancestors.join(".") + ".prototype." + prependPrefix(prefix, obj.name);
62                     return;
63                 }
64             });
65         }
66     }
67
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);
75                     return;
76                 }
77             });
78         }
79     }
80
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"];
84         try {
85             var prefixedObjects = JSON.parse(prefixObjectsData);
86         } catch (e) {
87             throw "couldn't parse data-prefixed-objects as JSON:" + e;
88         }
89         prefixedObjects.forEach(setAlias);
90     }
91     if (document.querySelector("script[data-prefixed-prototypes]")) {
92         var prefixProtoData = document.querySelector("script[data-prefixed-prototypes]").dataset["prefixedPrototypes"];
93         try {
94             var prefixedPrototypes = JSON.parse(prefixProtoData);
95         } catch (e) {
96             throw "couldn't parse data-prefixed-prototypes as JSON:" + e;
97         }
98         prefixedPrototypes.forEach(setPrototypeAlias);
99     }
100 })();