OLD | NEW |
---|---|
1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
2 <title>Custom Elements: defineElement</title> | 2 <title>Custom Elements: defineElement</title> |
3 <link rel="help" href="https://html.spec.whatwg.org/multipage/scripting.html#cus tomelementsregistry"> | 3 <link rel="help" href="https://html.spec.whatwg.org/multipage/scripting.html#cus tomelementsregistry"> |
4 <meta name="author" title="Dominic Cooney" href="mailto:dominicc@chromium.org"> | 4 <meta name="author" title="Dominic Cooney" href="mailto:dominicc@chromium.org"> |
5 <script src="../../resources/testharness.js"></script> | 5 <script src="../../resources/testharness.js"></script> |
6 <script src="../../resources/testharness-helpers.js"></script> | |
7 <script src="../../resources/testharnessreport.js"></script> | 6 <script src="../../resources/testharnessreport.js"></script> |
8 <script src="resources/custom-elements-helpers.js"></script> | 7 <script src="resources/custom-elements-helpers.js"></script> |
9 <body> | 8 <body> |
10 <script> | 9 <script> |
11 // TODO(dominicc): Merge these tests with | 10 // TODO(dominicc): Merge these tests with |
12 // https://github.com/w3c/web-platform-tests/pull/2940 | 11 // https://github.com/w3c/web-platform-tests/pull/2940 |
13 | 12 |
14 'use strict'; | 13 'use strict'; |
15 | 14 |
15 // Since testharness cannot test that e instance of the appropriate interface, | |
dominicc (has gone to gerrit)
2016/07/06 01:34:42
The grammar of this comment isn't right, Since/bec
| |
16 // because testharness does not know what window it was created. | |
17 function assert_throws_dom_exception(global_context, code, func, description) { | |
18 let exception; | |
19 assert_throws(code, () => { | |
20 try { | |
21 func.call(this); | |
22 } catch(e) { | |
23 exception = e; | |
24 throw e; | |
25 } | |
26 }, description); | |
27 assert_true(exception instanceof global_context.DOMException, 'DOMException on the appropriate window'); | |
28 } | |
29 | |
16 test_with_window((w) => { | 30 test_with_window((w) => { |
17 assert_throws(TypeError.prototype, () => { | 31 assert_throws(TypeError.prototype, () => { |
18 w.customElements.define('a-a', 42); | 32 w.customElements.define('a-a', 42); |
19 }, 'defining a number "constructor" should throw a TypeError'); | 33 }, 'defining a number "constructor" should throw a TypeError'); |
20 assert_throws(TypeError.prototype, () => { | 34 assert_throws(TypeError.prototype, () => { |
21 w.customElements.define('a-a', () => {}); | 35 w.customElements.define('a-a', () => {}); |
22 }, 'defining an arrow function "constructor" should throw a TypeError'); | 36 }, 'defining an arrow function "constructor" should throw a TypeError'); |
23 assert_throws(TypeError.prototype, () => { | 37 assert_throws(TypeError.prototype, () => { |
24 w.customElements.define('a-a', { m() {} }.m); | 38 w.customElements.define('a-a', { m() {} }.m); |
25 }, 'defining a concise method "constructor" should throw a TypeError'); | 39 }, 'defining a concise method "constructor" should throw a TypeError'); |
(...skipping 15 matching lines...) Expand all Loading... | |
41 '-not-initial-a-z', '0not-initial-a-z', 'Not-initial-a-z', | 55 '-not-initial-a-z', '0not-initial-a-z', 'Not-initial-a-z', |
42 'intermediate-UPPERCASE-letters', | 56 'intermediate-UPPERCASE-letters', |
43 'bad-\u00b6', 'bad-\u00b8', 'bad-\u00bf', 'bad-\u00d7', 'bad-\u00f7', | 57 'bad-\u00b6', 'bad-\u00b8', 'bad-\u00bf', 'bad-\u00d7', 'bad-\u00f7', |
44 'bad-\u037e', 'bad-\u037e', 'bad-\u2000', 'bad-\u200e', 'bad-\u203e', | 58 'bad-\u037e', 'bad-\u037e', 'bad-\u2000', 'bad-\u200e', 'bad-\u203e', |
45 'bad-\u2041', 'bad-\u206f', 'bad-\u2190', 'bad-\u2bff', 'bad-\u2ff0', | 59 'bad-\u2041', 'bad-\u206f', 'bad-\u2190', 'bad-\u2bff', 'bad-\u2ff0', |
46 'bad-\u3000', 'bad-\ud800', 'bad-\uf8ff', 'bad-\ufdd0', 'bad-\ufdef', | 60 'bad-\u3000', 'bad-\ud800', 'bad-\uf8ff', 'bad-\ufdd0', 'bad-\ufdef', |
47 'bad-\ufffe', 'bad-\uffff', 'bad-' + String.fromCodePoint(0xf0000) | 61 'bad-\ufffe', 'bad-\uffff', 'bad-' + String.fromCodePoint(0xf0000) |
48 ]; | 62 ]; |
49 class X extends w.HTMLElement {} | 63 class X extends w.HTMLElement {} |
50 invalid_names.forEach((name) => { | 64 invalid_names.forEach((name) => { |
51 assert_throws('SYNTAX_ERR', () => { | 65 assert_throws_dom_exception(w, 'SYNTAX_ERR', () => { |
52 w.customElements.define(name, X); | 66 w.customElements.define(name, X); |
53 }, `defining an element named "${name}" should throw a SyntaxError`); | 67 }) |
54 }); | 68 }); |
55 }, 'Invalid names'); | 69 }, 'Invalid names'); |
56 | 70 |
57 test_with_window((w) => { | 71 test_with_window((w) => { |
58 class X extends w.HTMLElement {} | 72 class X extends w.HTMLElement {} |
59 class Y extends w.HTMLElement {} | 73 class Y extends w.HTMLElement {} |
60 w.customElements.define('a-a', X); | 74 w.customElements.define('a-a', X); |
61 assert_throws('NotSupportedError', () => { | 75 assert_throws_dom_exception(w, 'NotSupportedError', () => { |
62 w.customElements.define('a-a', Y); | 76 w.customElements.define('a-a', Y); |
63 }, 'defining an element with a name that is already defined should throw ' + | 77 }, 'defining an element with a name that is already defined should throw ' + |
64 'a NotSupportedError'); | 78 'a NotSupportedError'); |
65 }, 'Duplicate name'); | 79 }, 'Duplicate name'); |
66 | 80 |
67 // TODO(dominicc): Update this (perhaps by removing this comment) when | |
68 // https://github.com/whatwg/html/pull/1333 lands/issue | |
69 // https://github.com/whatwg/html/issues/1329 is closed. | |
70 test_with_window((w) => { | 81 test_with_window((w) => { |
71 class Y extends w.HTMLElement {} | 82 class Y extends w.HTMLElement {} |
72 let X = (function () {}).bind({}); | 83 let X = (function () {}).bind({}); |
73 Object.defineProperty(X, 'prototype', { | 84 Object.defineProperty(X, 'prototype', { |
74 get() { | 85 get() { |
75 assert_throws('NotSupportedError', () => { | 86 assert_throws_dom_exception(w, 'NotSupportedError', () => { |
76 w.customElements.define('a-a', Y); | 87 w.customElements.define('a-a', Y); |
77 }, 'defining an element with a name that is being defined should ' + | 88 }, 'defining an element with a name that is being defined should ' + |
78 'throw a NotSupportedError'); | 89 'throw a NotSupportedError'); |
79 return new Object(); | 90 return new Object(); |
80 } | 91 } |
81 }); | 92 }); |
82 // TODO(dominicc): When callback retrieval is implemented, change this | |
83 // to pass a valid constructor and recursively call define when retrieving | |
84 // callbacks instead; then it is possible to assert the first definition | |
85 // worked: | |
86 // let element = Reflect.construct(HTMLElement, [], X); | |
87 // assert_equals(element.localName, 'a-a'); | |
88 w.customElements.define('a-a', X); | 93 w.customElements.define('a-a', X); |
94 assert_equals(w.customElements.get('a-a'), X, 'the first definition should hav e worked'); | |
89 }, 'Duplicate name defined recursively'); | 95 }, 'Duplicate name defined recursively'); |
90 | 96 |
91 test_with_window((w) => { | 97 test_with_window((w) => { |
92 class X extends w.HTMLElement {} | 98 class X extends w.HTMLElement {} |
93 w.customElements.define('a-a', X); | 99 w.customElements.define('a-a', X); |
94 assert_throws('NotSupportedError', () => { | 100 assert_throws_dom_exception(w, 'NotSupportedError', () => { |
95 w.customElements.define('a-b', X); | 101 w.customElements.define('a-b', X); |
96 }, 'defining an element with a constructor that is already in the ' + | 102 }, 'defining an element with a constructor that is already in the ' + |
97 'registry should throw a NotSupportedError'); | 103 'registry should throw a NotSupportedError'); |
98 }, 'Reused constructor'); | 104 }, 'Reused constructor'); |
99 | 105 |
100 // TODO(dominicc): Update this (perhaps by removing this comment) when | |
101 // https://github.com/whatwg/html/pull/1333 lands/issue | |
102 // https://github.com/whatwg/html/issues/1329 is closed. | |
103 test_with_window((w) => { | 106 test_with_window((w) => { |
104 let X = (function () {}).bind({}); | 107 let X = (function () {}).bind({}); |
105 Object.defineProperty(X, 'prototype', { | 108 Object.defineProperty(X, 'prototype', { |
106 get() { | 109 get() { |
107 assert_throws('NotSupportedError', () => { | 110 assert_throws_dom_exception(w, 'NotSupportedError', () => { |
108 w.customElements.define('second-name', X); | 111 w.customElements.define('second-name', X); |
109 }, 'defining an element with a constructor that is being defined ' + | 112 }, 'defining an element with a constructor that is being defined ' + |
110 'should throw a NotSupportedError'); | 113 'should throw a NotSupportedError'); |
111 return new Object(); | 114 return new Object(); |
112 } | 115 } |
113 }); | 116 }); |
114 // TODO(dominicc): When callback retrieval is implemented, change this | |
115 // to pass a valid constructor and recursively call define when retrieving | |
116 // callbacks instead; then it is possible to assert the first definition | |
117 // worked: | |
118 // let element = Reflect.construct(HTMLElement, [], X); | |
119 // assert_equals(element.localName, 'a-a'); | |
120 w.customElements.define('first-name', X); | 117 w.customElements.define('first-name', X); |
118 assert_equals(w.customElements.get('first-name'), X, 'the first definition sho uld have worked'); | |
121 }, 'Reused constructor recursively'); | 119 }, 'Reused constructor recursively'); |
122 | 120 |
123 test_with_window((w) => { | 121 test_with_window((w) => { |
124 function F() {} | |
125 F.prototype = 42; | |
126 assert_throws(TypeError.prototype, () => { | |
127 w.customElements.define('a-a', F); | |
128 }, 'defining an element with a constructor with a prototype that is not an ' + | |
129 'object should throw a TypeError'); | |
130 }, 'Retrieved prototype is a non-object'); | |
131 | |
132 test_with_window((w) => { | |
133 assert_throws(TypeError.prototype, () => { | 122 assert_throws(TypeError.prototype, () => { |
134 let not_a_constructor = () => {}; | 123 let not_a_constructor = () => {}; |
135 let invalid_name = 'annotation-xml'; | 124 let invalid_name = 'annotation-xml'; |
136 w.customElements.define(invalid_name, not_a_constructor); | 125 w.customElements.define(invalid_name, not_a_constructor); |
137 }, 'defining an element with an invalid name and invalid constructor ' + | 126 }, 'defining an element with an invalid name and invalid constructor ' + |
138 'should throw a TypeError for the constructor and not a SyntaxError'); | 127 'should throw a TypeError for the constructor and not a SyntaxError'); |
139 | 128 |
140 class C extends w.HTMLElement {} | 129 class C extends w.HTMLElement {} |
141 w.customElements.define('a-a', C); | 130 w.customElements.define('a-a', C); |
142 assert_throws('SYNTAX_ERR', () => { | 131 assert_throws('SYNTAX_ERR', () => { |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
202 constructor() { | 191 constructor() { |
203 super(); | 192 super(); |
204 invocations.push(this); | 193 invocations.push(this); |
205 } | 194 } |
206 } | 195 } |
207 w.customElements.define('a-a', C); | 196 w.customElements.define('a-a', C); |
208 assert_array_equals([a], invocations, | 197 assert_array_equals([a], invocations, |
209 'the constructor should have been invoked once for the ' + | 198 'the constructor should have been invoked once for the ' + |
210 'elements in the shadow tree'); | 199 'elements in the shadow tree'); |
211 }, 'Upgrade: shadow tree'); | 200 }, 'Upgrade: shadow tree'); |
201 | |
202 // Final step in Step 14 | |
203 // 14. Finally, if the first set of steps threw an exception, then rethrow that exception, | |
204 // and terminate this algorithm. | |
205 test_with_window((w) => { | |
206 class Y extends w.HTMLElement {} | |
207 let X = (function () {}).bind({}); | |
208 Object.defineProperty(X, 'prototype', { | |
209 get() { throw { name: 42 }; } | |
210 }); | |
211 assert_throws({ name: 42 }, () => { | |
212 w.customElements.define('a-a', X); | |
213 }, 'should rethrow constructor exception'); | |
214 w.customElements.define('a-a', Y); | |
215 assert_equals(w.customElements.get('a-a'), Y, 'the same name can be registered after failure'); | |
216 }, 'If an exception is thrown, rethrow that exception and terminate the algorith m'); | |
217 | |
218 // 14.1 Let prototype be Get(constructor, "prototype"). Rethrow any exceptions. | |
219 test_with_window((w) => { | |
220 let X = (function () {}).bind({}); | |
221 Object.defineProperty(X, 'prototype', { | |
222 get() { throw { name: 'prototype throws' }; } | |
223 }); | |
224 assert_throws({ name: 'prototype throws' }, () => { | |
225 w.customElements.define('a-a', X); | |
226 }, 'Exception from Get(constructor, prototype) should be rethrown'); | |
227 }, 'Rethrow any exceptions thrown while getting prototype'); | |
228 | |
229 // 14.2 If Type(prototype) is not Object, then throw a TypeError exception. | |
230 test_with_window((w) => { | |
231 function F() {} | |
232 F.prototype = 42; | |
233 assert_throws(TypeError.prototype, () => { | |
234 w.customElements.define('a-a', F); | |
235 }, 'defining an element with a constructor with a prototype that is not an ' + | |
236 'object should throw a TypeError'); | |
237 }, 'Retrieved prototype is a non-object'); | |
238 | |
239 // 14.3 Let connectedCallback be Get(prototype, "connectedCallback"). Rethrow an y exceptions. | |
240 // 14.5 Let disconnectedCallback be Get(prototype, "disconnectedCallback"). Reth row any exceptions. | |
241 // 14.7 Let attributeChangedCallback be Get(prototype, "attributeChangedCallback "). Rethrow any exceptions. | |
242 // Note that this test implicitly tests order of callback retrievals. | |
243 // Callbacks are defined in reverse order. | |
244 let callbacks_in_reverse = ['attributeChangedCallback', 'disconnectedCallback', 'connectedCallback']; | |
245 function F_for_callbacks_in_reverse() {}; | |
246 callbacks_in_reverse.forEach((callback) => { | |
247 test_with_window((w) => { | |
248 Object.defineProperty(F_for_callbacks_in_reverse.prototype, callback, { | |
249 get() { throw { name: callback }; } | |
250 }); | |
251 assert_throws({ name: callback }, () => { | |
252 w.customElements.define('a-a', F_for_callbacks_in_reverse); | |
253 }, 'Exception from Get(prototype, callback) should be rethrown'); | |
254 }, 'Rethrow any exceptions thrown while retrieving ' + callback); | |
255 }); | |
256 | |
257 // 14.4 If connectedCallback is not undefined, and IsCallable(connectedCallback) is false, | |
258 // then throw a TypeError exception. | |
259 // 14.6 If disconnectedCallback is not undefined, and IsCallable(disconnectedCal lback) is false, | |
260 // then throw a TypeError exception. | |
261 // 14.9. If attributeChangedCallback is not undefined, then | |
262 // 1. If IsCallable(attributeChangedCallback) is false, then throw a TypeE rror exception. | |
263 callbacks_in_reverse.forEach((callback) => { | |
264 test_with_window((w) => { | |
265 function F() {} | |
266 Object.defineProperty(F.prototype, callback, { | |
267 get() { return new Object(); } | |
268 }); | |
269 assert_throws(TypeError.prototype, () => { | |
270 w.customElements.define('a-a', F); | |
271 }, 'defining an element with a constructor with a callback that is ' + | |
272 'not undefined and not callable should throw a TypeError'); | |
273 }, 'If retrieved callback '+ callback + ' is not undefined and not callable, t hrow TypeError'); | |
274 }); | |
275 | |
276 // 14.9.2 Let observedAttributesIterable be Get(constructor, "observedAttributes "). | |
277 // Rethrow any exceptions. | |
278 test_with_window((w) => { | |
279 class X extends w.HTMLElement{ | |
280 constructor() { super(); } | |
281 attributeChangedCallback() {} | |
282 static get observedAttributes() { throw { name: 'observedAttributes throws' }; } | |
283 } | |
284 assert_throws({ name: 'observedAttributes throws' }, () => { | |
285 w.customElements.define('a-a', X); | |
286 }, 'Exception from Get(constructor, observedAttributes) should be rethrown'); | |
287 }, 'Rethrow any exceptions thrown while getting observedAttributes'); | |
288 | |
289 // 14.9.3 If observedAttributesIterable is not undefined, then set observedAttri butes | |
290 // to the result of converting observedAttributesIterable to a sequence<D OMString>. | |
291 // Rethrow any exceptions. | |
292 test_with_window((w) => { | |
293 class X extends w.HTMLElement{ | |
294 constructor() { super(); } | |
295 attributeChangedCallback() {} | |
296 static get observedAttributes() { return new RegExp(); } | |
297 } | |
298 assert_throws(TypeError.prototype, () => { | |
299 w.customElements.define('a-a', X); | |
300 }, 'converting RegExp to sequence<DOMString> should throw TypeError'); | |
301 }, 'exception thrown while converting observedAttributes to sequence<DOMString> ' + | |
302 'should be rethrown'); | |
303 | |
304 // 14.9.2 test Get(constructor, observedAttributes) does not throw if | |
305 // attributeChangedCallback is undefined. | |
306 test_with_window((w) => { | |
307 let observedAttributes_invoked = false; | |
308 let X = (function () {}).bind({}); | |
309 Object.defineProperty(X, 'observedAttributes', { | |
310 get() { observedAttributes_invoked = true; } | |
311 }); | |
312 assert_false( observedAttributes_invoked, 'Get(constructor, observedAttributes ) should not be invoked'); | |
313 }, 'Get(constructor, observedAttributes)' + | |
314 'should not execute if attributeChangedCallback is undefined'); | |
315 | |
316 // step 2 | |
317 // 2. If constructor is an interface object whose corresponding interface either is | |
318 // HTMLElement or has HTMLElement in its set of inherited interfaces, throw | |
319 // a TypeError and abort these steps. | |
320 // 3. If name is not a valid custom element name, then throw a "SyntaxError" DOM Exception | |
321 // and abort these steps. | |
322 test_with_window((w) => { | |
323 let invalid_name = 'annotation-xml'; | |
324 // TODO(davaajav): change this to TypeError, when we add a failure expectation to this file | |
325 assert_throws('SYNTAX_ERR', () => { | |
326 w.customElements.define(invalid_name, HTMLElement); | |
327 }, 'defining a constructor that is an interface object whose interface is HTML Element' + | |
328 'should throw TypeError not SyntaxError'); | |
329 }, 'Invalid constructor'); | |
330 | |
331 // step 2 | |
332 test_with_window((w) => { | |
333 let invalid_name = 'annotation-xml'; | |
334 assert_throws_dom_exception(w, 'SYNTAX_ERR', () => { | |
335 w.customElements.define(invalid_name, HTMLButtonElement); | |
336 }, 'defining a constructor that is an interface object who has HTMLElement' + | |
337 'in its set of inhertied interfaces should throw TypeError not SyntaxErro r'); | |
338 }, 'Invalid constructor'); | |
339 | |
340 // step 2 | |
341 test_with_window((w) => { | |
342 let invalid_name = 'annotation-xml'; | |
343 assert_throws_dom_exception(w, 'SYNTAX_ERR', () => { | |
344 w.customElements.define(invalid_name, class extends HTMLElement {}); | |
345 }, 'defining author-defined custom element constructor' + | |
346 'should pass this step without throwing TypeError'); | |
347 }, 'Invalid constructor'); | |
212 </script> | 348 </script> |
349 </body> | |
OLD | NEW |