dom/tests/mochitest/webcomponents/test_document_register.html

branch
TOR_BUG_9701
changeset 15
b8a032363ba2
equal deleted inserted replaced
-1:000000000000 0:395c0b3dbb06
1 <!DOCTYPE HTML>
2 <html>
3 <!--
4 https://bugzilla.mozilla.org/show_bug.cgi?id=783129
5 -->
6 <head>
7 <title>Test for document.registerElement using custom prototype</title>
8 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
9 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
10 </head>
11 <body>
12 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=783129">Bug 783129</a>
13 <div>
14 <x-unresolved id="unresolved"></x-unresolved>
15 </div>
16
17 <script>
18
19 function testRegisterExtend(tag, extend, proto, expectException) {
20 try {
21 document.registerElement(tag, { prototype: proto, extends: extend });
22 ok(!expectException, "Registered " + tag + " extending " + extend + " containing " + proto + " in proto chain.");
23 } catch (ex) {
24 ok(expectException, "Did not register " + tag + " extending " + extend + " containing " + proto + " in proto chain.");
25 }
26 }
27
28 function testRegisterSimple(tag, proto, expectException) {
29 try {
30 document.registerElement(tag, { prototype: proto });
31 ok(!expectException, "Registered " + tag + " containing " + proto + " in proto chain.");
32 } catch (ex) {
33 ok(expectException, "Did not register " + tag + " containing " + proto + " in proto chain.");
34 }
35 }
36
37 function startTest() {
38 // Test registering some simple prototypes.
39 testRegisterSimple("x-html-obj-elem", Object.create(HTMLElement.prototype), false);
40 testRegisterSimple("x-html-obj-p", Object.create(HTMLParagraphElement.prototype), false);
41
42 // If prototype is an interface prototype object for any interface object,
43 // registration will throw.
44 testRegisterSimple("x-html-elem", HTMLElement.prototype, true);
45 testRegisterSimple("x-html-select", HTMLSelectElement.prototype, true);
46 testRegisterSimple("some-elem", HTMLElement.prototype, true);
47 testRegisterSimple("x-html-p", HTMLParagraphElement.prototype, true);
48 testRegisterSimple("x-html-span", HTMLSpanElement.prototype, true);
49 testRegisterSimple("x-svg-proto", SVGElement.prototype, true);
50
51 // Make sure the prototype on unresolved elements is HTMLElement not HTMLUnknownElement.
52 var unresolved = document.getElementById("unresolved");
53 is(unresolved.__proto__, HTMLElement.prototype, "Unresolved custom elements should have HTMLElement as prototype.");
54
55 var anotherUnresolved = document.createElement("maybe-custom-element");
56 is(anotherUnresolved.__proto__, HTMLElement.prototype, "Unresolved custom elements should have HTMLElement as prototype.");
57
58 // Registering without a prototype should automatically create one inheriting from HTMLElement.
59 testRegisterSimple("x-elem-no-proto", null, false);
60 var simpleElem = document.createElement("x-elem-no-proto");
61 is(simpleElem.__proto__.__proto__, HTMLElement.prototype, "Default prototype should inherit from HTMLElement");
62
63 var simpleProto = Object.create(HTMLElement.prototype);
64 testRegisterSimple("x-elem-simple-proto", simpleProto, false);
65 var simpleProtoElem = document.createElement("x-elem-simple-proto");
66 is(simpleProtoElem.__proto__, simpleProto, "Custom element should use registered prototype.");
67 var anotherSimpleElem = document.createElementNS("http://www.w3.org/1999/xhtml", "x-elem-simple-proto");
68 is(anotherSimpleElem.__proto__, simpleProto, "Custom element should use registered prototype.");
69
70 // Test registering some invalid prototypes.
71 testRegisterSimple("x-invalid-number", 42, true);
72 testRegisterSimple("x-invalid-boolean", false, true);
73 testRegisterSimple("x-invalid-float", 1.0, true);
74 // Can not register with a prototype that inherits from SVGElement
75 // without extending an existing element type.
76 testRegisterSimple("x-html-obj-svg", Object.create(SVGElement.prototype), true);
77 // A prototype with a non-configurable "constructor" property must throw.
78 var nonConfigProto = Object.create(HTMLElement.prototype,
79 { constructor: { configurable: false, value: function() {} } });
80 testRegisterSimple("x-non-config-proto", nonConfigProto, true);
81
82 // Test invalid custom element names.
83 testRegisterSimple("invalid", Object.create(HTMLElement.prototype), true);
84 testRegisterSimple("annotation-xml", Object.create(HTMLElement.prototype), true);
85 testRegisterSimple("color-profile", Object.create(HTMLElement.prototype), true);
86 testRegisterSimple("font-face", Object.create(HTMLElement.prototype), true);
87 testRegisterSimple("font-face-src", Object.create(HTMLElement.prototype), true);
88 testRegisterSimple("font-face-uri", Object.create(HTMLElement.prototype), true);
89 testRegisterSimple("font-face-format", Object.create(HTMLElement.prototype), true);
90 testRegisterSimple("font-face-name", Object.create(HTMLElement.prototype), true);
91 testRegisterSimple("missing-glyph", Object.create(HTMLElement.prototype), true);
92
93 // Test registering elements that extend from an existing element.
94 testRegisterExtend("x-extend-span", "span", Object.create(HTMLElement.prototype), false);
95 testRegisterExtend("x-extend-span-caps", "SPAN", Object.create(HTMLElement.prototype), false);
96
97 // Test registering elements that extend from a non-existing element.
98 testRegisterExtend("x-extend-span-nonexist", "nonexisting", Object.create(HTMLElement.prototype), true);
99 testRegisterExtend("x-extend-svg-nonexist", "nonexisting", Object.create(SVGElement.prototype), true);
100
101 // Test registration with duplicate type.
102 testRegisterSimple("x-dupe-me", Object.create(HTMLElement.prototype), false);
103 testRegisterSimple("x-dupe-me", Object.create(HTMLElement.prototype), true);
104 testRegisterSimple("X-DUPE-ME", Object.create(HTMLElement.prototype), true);
105 testRegisterSimple("x-dupe-me", null, true);
106 testRegisterExtend("x-dupe-me", "span", Object.create(HTMLElement.prototype), true);
107 testRegisterExtend("x-dupe-me", "shape", Object.create(SVGElement.prototype), true);
108
109 testRegisterExtend("x-svg-dupe-me", "circle", Object.create(SVGElement.prototype), false);
110 testRegisterSimple("x-svg-dupe-me", Object.create(HTMLElement.prototype), true);
111 testRegisterSimple("X-SVG-DUPE-ME", Object.create(HTMLElement.prototype), true);
112 testRegisterSimple("x-svg-dupe-me", null, true);
113 testRegisterExtend("x-svg-dupe-me", "span", Object.create(HTMLElement.prototype), true);
114 testRegisterExtend("x-svg-dupe-me", "shape", Object.create(SVGElement.prototype), true);
115
116 // document.createElement with extended type.
117 var extendedProto = Object.create(HTMLButtonElement.prototype);
118 var buttonConstructor = document.registerElement("x-extended-button", { prototype: extendedProto, extends: "button" });
119 var extendedButton = document.createElement("button", "x-extended-button");
120 is(extendedButton.tagName, "BUTTON", "Created element should have local name of BUTTON");
121 is(extendedButton.__proto__, extendedProto, "Created element should have the prototype of the extended type.");
122 is(extendedButton.getAttribute("is"), "x-extended-button", "The |is| attribute of the created element should be the extended type.");
123
124 // document.createElementNS with different namespace than definition.
125 var svgButton = document.createElementNS("http://www.w3.org/2000/svg", "button", "x-extended-button");
126 isnot(svgButton.__proto__, extendedProto, "Definition for element is in html namespace, registration should not apply for SVG elements.");
127
128 // document.createElementNS with no namespace.
129 var noNamespaceButton = document.createElementNS("", "button", "x-extended-button");
130 isnot(noNamespaceButton.__proto__, extendedProto, "Definition for element is in html namespace, registration should not apply for elements with no namespace.");
131
132 // document.createElement with non-existant extended type.
133 var normalButton = document.createElement("button", "x-non-existant");
134 is(normalButton.__proto__, HTMLButtonElement.prototype, "When the extended type doesn't exist, prototype should not change.");
135
136 // document.createElement with exteneded type that does not match with local name of element.
137 var normalDiv = document.createElement("div", "x-extended-button");
138 is(normalDiv.__proto__, HTMLDivElement.prototype, "Prototype should not change when local name of extended type defintion does not match.");
139
140 // Custom element constructor.
141 var constructedButton = new buttonConstructor();
142 is(constructedButton.tagName, "BUTTON", "Created element should have local name of BUTTON");
143 is(constructedButton.__proto__, extendedProto, "Created element should have the prototype of the extended type.");
144 is(constructedButton.getAttribute("is"), "x-extended-button", "The |is| attribute of the created element should be the extended type.");
145
146 // document.createElementNS with extended type.
147 var svgExtendedProto = Object.create(SVGTextElement.prototype);
148 var svgConstructor = document.registerElement("x-extended-text", { prototype: svgExtendedProto, extends: "text"});
149 var extendedText = document.createElementNS("http://www.w3.org/2000/svg", "text", "x-extended-text");
150 is(extendedText.tagName, "text", "Created element should have a local name of |text|.");
151 is(extendedText.__proto__, svgExtendedProto, "Created element have the registered prototype.");
152 is(extendedText.getAttribute("is"), "x-extended-text", "The |is| attribute of the created element should be the extended type.");
153
154 // document.createElement with different namespace than definition for extended element.
155 var htmlText = document.createElement("text", "x-extended-text");
156 isnot(htmlText.__proto__, svgExtendedProto, "Definition for element in SVG namespace should not apply to HTML elements.");
157
158 // Custom element constructor for a SVG element.
159 var constructedText = new svgConstructor();
160 is(constructedText.tagName, "text", "Created element should have a local name of |text|.");
161 is(constructedText.__proto__, svgExtendedProto, "Created element have the registered prototype.");
162 is(constructedText.getAttribute("is"), "x-extended-text", "The |is| attribute of the created element should be the extended type.");
163
164 // Try creating an element with a custom element name, but not in the html namespace.
165 var htmlNamespaceProto = Object.create(HTMLElement.prototype);
166 document.registerElement("x-in-html-namespace", { prototype: htmlNamespaceProto });
167 var wrongNamespaceElem = document.createElementNS("http://www.w3.org/2000/svg", "x-in-html-namespace");
168 isnot(wrongNamespaceElem.__proto__, htmlNamespaceProto, "Definition for element in html namespace should not apply to SVG elements.");
169 }
170
171 startTest();
172
173 </script>
174 </body>
175 </html>

mercurial