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> | 6 <script src="../../resources/testharness-helpers.js"></script> |
7 <script src="../../resources/testharnessreport.js"></script> | 7 <script src="../../resources/testharnessreport.js"></script> |
8 <script src="resources/custom-elements-helpers.js"></script> | 8 <script src="resources/custom-elements-helpers.js"></script> |
9 <body> | 9 <body> |
10 <script> | 10 <script> |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 test_with_window((w) => { | 57 test_with_window((w) => { |
58 class X extends w.HTMLElement {} | 58 class X extends w.HTMLElement {} |
59 class Y extends w.HTMLElement {} | 59 class Y extends w.HTMLElement {} |
60 w.customElements.define('a-a', X); | 60 w.customElements.define('a-a', X); |
61 assert_throws('NotSupportedError', () => { | 61 assert_throws('NotSupportedError', () => { |
62 w.customElements.define('a-a', Y); | 62 w.customElements.define('a-a', Y); |
63 }, 'defining an element with a name that is already defined should throw ' + | 63 }, 'defining an element with a name that is already defined should throw ' + |
64 'a NotSupportedError'); | 64 'a NotSupportedError'); |
65 }, 'Duplicate name'); | 65 }, 'Duplicate name'); |
66 | 66 |
| 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) => { |
| 71 class Y extends w.HTMLElement {} |
| 72 let X = (function () {}).bind({}); |
| 73 Object.defineProperty(X, 'prototype', { |
| 74 get() { |
| 75 assert_throws('NotSupportedError', () => { |
| 76 w.customElements.define('a-a', Y); |
| 77 }, 'defining an element with a name that is being defined should ' + |
| 78 'throw a NotSupportedError'); |
| 79 return new Object(); |
| 80 } |
| 81 }); |
| 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); |
| 89 }, 'Duplicate name defined recursively'); |
| 90 |
67 test_with_window((w) => { | 91 test_with_window((w) => { |
68 class X extends w.HTMLElement {} | 92 class X extends w.HTMLElement {} |
69 w.customElements.define('a-a', X); | 93 w.customElements.define('a-a', X); |
70 assert_throws('NotSupportedError', () => { | 94 assert_throws('NotSupportedError', () => { |
71 w.customElements.define('a-b', X); | 95 w.customElements.define('a-b', X); |
72 }, 'defining an element with a constructor that is already in the ' + | 96 }, 'defining an element with a constructor that is already in the ' + |
73 'registry should throw a NotSupportedError'); | 97 'registry should throw a NotSupportedError'); |
74 }, 'Reused constructor'); | 98 }, 'Reused constructor'); |
75 | 99 |
| 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) => { |
| 104 let X = (function () {}).bind({}); |
| 105 Object.defineProperty(X, 'prototype', { |
| 106 get() { |
| 107 assert_throws('NotSupportedError', () => { |
| 108 w.customElements.define('second-name', X); |
| 109 }, 'defining an element with a constructor that is being defined ' + |
| 110 'should throw a NotSupportedError'); |
| 111 return new Object(); |
| 112 } |
| 113 }); |
| 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); |
| 121 }, 'Reused constructor recursively'); |
| 122 |
76 test_with_window((w) => { | 123 test_with_window((w) => { |
77 function F() {} | 124 function F() {} |
78 F.prototype = 42; | 125 F.prototype = 42; |
79 assert_throws(TypeError.prototype, () => { | 126 assert_throws(TypeError.prototype, () => { |
80 w.customElements.define('a-a', F); | 127 w.customElements.define('a-a', F); |
81 }, 'defining an element with a constructor with a prototype that is not an ' + | 128 }, 'defining an element with a constructor with a prototype that is not an ' + |
82 'object should throw a TypeError'); | 129 'object should throw a TypeError'); |
83 }, 'Retrieved prototype is a non-object'); | 130 }, 'Retrieved prototype is a non-object'); |
84 | 131 |
85 test_with_window((w) => { | 132 test_with_window((w) => { |
86 assert_throws(TypeError.prototype, () => { | 133 assert_throws(TypeError.prototype, () => { |
87 let not_a_constructor = () => {}; | 134 let not_a_constructor = () => {}; |
88 let invalid_name = 'annotation-xml'; | 135 let invalid_name = 'annotation-xml'; |
89 w.customElements.define(invalid_name, not_a_constructor); | 136 w.customElements.define(invalid_name, not_a_constructor); |
90 }, 'Defining an element with an invalid name and invalid constructor ' + | 137 }, 'Defining an element with an invalid name and invalid constructor ' + |
91 'should throw a TypeError for the constructor and not a SyntaxError'); | 138 'should throw a TypeError for the constructor and not a SyntaxError'); |
92 | 139 |
93 class C extends w.HTMLElement {} | 140 class C extends w.HTMLElement {} |
94 w.customElements.define('a-a', C); | 141 w.customElements.define('a-a', C); |
95 assert_throws('SYNTAX_ERR', () => { | 142 assert_throws('SYNTAX_ERR', () => { |
96 let invalid_name = 'annotation-xml'; | 143 let invalid_name = 'annotation-xml'; |
97 let reused_constructor = C; | 144 let reused_constructor = C; |
98 w.customElements.define(invalid_name, reused_constructor); | 145 w.customElements.define(invalid_name, reused_constructor); |
99 }, 'Defining an element with an invalid name and a reused constructor ' + | 146 }, 'Defining an element with an invalid name and a reused constructor ' + |
100 'should throw a SyntaxError for the name and not a NotSupportedError'); | 147 'should throw a SyntaxError for the name and not a NotSupportedError'); |
101 }, 'Order of checks'); | 148 }, 'Order of checks'); |
102 </script> | 149 </script> |
OLD | NEW |