| OLD | NEW |
| (Empty) |
| 1 <!DOCTYPE html> | |
| 2 <title>Custom Elements: Element definition</title> | |
| 3 <script src="/resources/testharness.js"></script> | |
| 4 <script src="/resources/testharnessreport.js"></script> | |
| 5 <body> | |
| 6 <div id="log"></div> | |
| 7 <iframe id="iframe"></iframe> | |
| 8 <script> | |
| 9 'use strict'; | |
| 10 (() => { | |
| 11 // Element definition | |
| 12 // https://html.spec.whatwg.org/multipage/scripting.html#element-definition | |
| 13 | |
| 14 // Use window from iframe to isolate the test. | |
| 15 const testWindow = iframe.contentDocument.defaultView; | |
| 16 const customElements = testWindow.customElements; | |
| 17 | |
| 18 let testable = false; | |
| 19 test(() => { | |
| 20 assert_true('customElements' in testWindow, '"window.customElements" exists'
); | |
| 21 assert_true('define' in customElements, '"window.customElements.define" exis
ts'); | |
| 22 testable = true; | |
| 23 }, '"window.customElements.define" should exists'); | |
| 24 if (!testable) | |
| 25 return; | |
| 26 | |
| 27 const expectTypeError = TypeError.prototype; | |
| 28 // Following errors are DOMException, not JavaScript errors. | |
| 29 const expectSyntaxError = 'SYNTAX_ERR'; | |
| 30 const expectNotSupportedError = 'NOT_SUPPORTED_ERR'; | |
| 31 | |
| 32 // 1. If IsConstructor(constructor) is false, | |
| 33 // then throw a TypeError and abort these steps. | |
| 34 test(() => { | |
| 35 assert_throws(expectTypeError, () => { | |
| 36 customElements.define(); | |
| 37 }); | |
| 38 }, 'If no arguments, should throw a TypeError'); | |
| 39 test(() => { | |
| 40 assert_throws(expectTypeError, () => { | |
| 41 customElements.define('test-define-one-arg'); | |
| 42 }); | |
| 43 }, 'If one argument, should throw a TypeError'); | |
| 44 [ | |
| 45 [ 'undefined', undefined ], | |
| 46 [ 'null', null ], | |
| 47 [ 'object', {} ], | |
| 48 [ 'string', 'string' ], | |
| 49 [ 'arrow function', () => {} ], // IsConstructor returns false for arrow fun
ctions | |
| 50 [ 'method', ({ m() { } }).m ], // IsConstructor returns false for methods | |
| 51 ].forEach(t => { | |
| 52 test(() => { | |
| 53 assert_throws(expectTypeError, () => { | |
| 54 customElements.define(`test-define-constructor-${t[0]}`, t[1]); | |
| 55 }); | |
| 56 }, `If constructor is ${t[0]}, should throw a TypeError`); | |
| 57 }); | |
| 58 | |
| 59 // 2. If name is not a valid custom element name, | |
| 60 // then throw a SyntaxError and abort these steps. | |
| 61 let validCustomElementNames = [ | |
| 62 // [a-z] (PCENChar)* '-' (PCENChar)* | |
| 63 // https://html.spec.whatwg.org/multipage/scripting.html#valid-custom-elemen
t-name | |
| 64 'a-', | |
| 65 'a-a', | |
| 66 'aa-', | |
| 67 'aa-a', | |
| 68 'a-.-_', | |
| 69 'a-0123456789', | |
| 70 'a-\u6F22\u5B57', // Two CJK Unified Ideographs | |
| 71 'a-\uD840\uDC0B', // Surrogate pair U+2000B | |
| 72 ]; | |
| 73 let invalidCustomElementNames = [ | |
| 74 undefined, | |
| 75 null, | |
| 76 '', | |
| 77 '-', | |
| 78 'a', | |
| 79 'input', | |
| 80 'mycustomelement', | |
| 81 'A', | |
| 82 'A-', | |
| 83 '0-', | |
| 84 'a-A', | |
| 85 'a-Z', | |
| 86 'A-a', | |
| 87 'a-a\u00D7', | |
| 88 'a-a\u3000', | |
| 89 'a-a\uDB80\uDC00', // Surrogate pair U+F0000 | |
| 90 // name must not be any of the hyphen-containing element names. | |
| 91 'annotation-xml', | |
| 92 'color-profile', | |
| 93 'font-face', | |
| 94 'font-face-src', | |
| 95 'font-face-uri', | |
| 96 'font-face-format', | |
| 97 'font-face-name', | |
| 98 'missing-glyph', | |
| 99 ]; | |
| 100 validCustomElementNames.forEach(name => { | |
| 101 test(() => { | |
| 102 customElements.define(name, class {}); | |
| 103 }, `Element names: defining an element named ${name} should succeed`); | |
| 104 }); | |
| 105 invalidCustomElementNames.forEach(name => { | |
| 106 test(() => { | |
| 107 assert_throws(expectSyntaxError, () => { | |
| 108 customElements.define(name, class {}); | |
| 109 }); | |
| 110 }, `Element names: defining an element named ${name} should throw a SyntaxEr
ror`); | |
| 111 }); | |
| 112 | |
| 113 // 3. If this CustomElementsRegistry contains an entry with name name, | |
| 114 // then throw a NotSupportedError and abort these steps. | |
| 115 test(() => { | |
| 116 customElements.define('test-define-dup-name', class {}); | |
| 117 assert_throws(expectNotSupportedError, () => { | |
| 118 customElements.define('test-define-dup-name', class {}); | |
| 119 }); | |
| 120 }, 'If the name is already defined, should throw a NotSupportedError'); | |
| 121 | |
| 122 // 5. If this CustomElementsRegistry contains an entry with constructor constr
uctor, | |
| 123 // then throw a NotSupportedError and abort these steps. | |
| 124 test(() => { | |
| 125 class TestDupConstructor {}; | |
| 126 customElements.define('test-define-dup-constructor', TestDupConstructor); | |
| 127 assert_throws(expectNotSupportedError, () => { | |
| 128 customElements.define('test-define-dup-ctor2', TestDupConstructor); | |
| 129 }); | |
| 130 }, 'If the constructor is already defined, should throw a NotSupportedError'); | |
| 131 | |
| 132 // 9.1. If extends is a valid custom element name, | |
| 133 // then throw a NotSupportedError. | |
| 134 validCustomElementNames.forEach(name => { | |
| 135 test(() => { | |
| 136 assert_throws(expectNotSupportedError, () => { | |
| 137 customElements.define('test-define-extend-valid-name', class {}, { exten
ds: name }); | |
| 138 }); | |
| 139 }, `If extends is ${name}, should throw a NotSupportedError`); | |
| 140 }); | |
| 141 | |
| 142 // 9.2. If the element interface for extends and the HTML namespace is HTMLUnk
nownElement | |
| 143 // (e.g., if extends does not indicate an element definition in this specifica
tion), | |
| 144 // then throw a NotSupportedError. | |
| 145 [ | |
| 146 // https://html.spec.whatwg.org/multipage/dom.html#elements-in-the-dom:htmlu
nknownelement | |
| 147 'bgsound', | |
| 148 'blink', | |
| 149 'isindex', | |
| 150 'multicol', | |
| 151 'nextid', | |
| 152 'spacer', | |
| 153 'elementnametobeunknownelement', | |
| 154 ].forEach(name => { | |
| 155 test(() => { | |
| 156 assert_throws(expectNotSupportedError, () => { | |
| 157 customElements.define('test-define-extend-' + name, class {}, { extends:
name }); | |
| 158 }); | |
| 159 }, `If extends is ${name}, should throw a NotSupportedError`); | |
| 160 }); | |
| 161 | |
| 162 // 12.1. Let prototype be Get(constructor, "prototype"). Rethrow any exception
s. | |
| 163 function assert_rethrown(func, description) { | |
| 164 assert_throws({ name: 'rethrown' }, func, description); | |
| 165 } | |
| 166 function throw_rethrown_error() { | |
| 167 const e = new Error('check this is rethrown'); | |
| 168 e.name = 'rethrown'; | |
| 169 throw e; | |
| 170 } | |
| 171 test(() => { | |
| 172 // Hack for prototype to throw while IsConstructor is true. | |
| 173 const BadConstructor = (function () { }).bind({}); | |
| 174 Object.defineProperty(BadConstructor, 'prototype', { | |
| 175 get() { throw_rethrown_error(); } | |
| 176 }); | |
| 177 assert_rethrown(() => { | |
| 178 customElements.define('test-define-constructor-prototype-rethrow', BadCons
tructor); | |
| 179 }); | |
| 180 }, 'If constructor.prototype throws, should rethrow'); | |
| 181 | |
| 182 // 12.2. If Type(prototype) is not Object, | |
| 183 // then throw a TypeError exception. | |
| 184 test(() => { | |
| 185 const c = (function () { }).bind({}); // prototype is undefined. | |
| 186 assert_throws(expectTypeError, () => { | |
| 187 customElements.define('test-define-constructor-prototype-undefined', c); | |
| 188 }); | |
| 189 }, 'If Type(constructor.prototype) is undefined, should throw a TypeError'); | |
| 190 test(() => { | |
| 191 function c() {}; | |
| 192 c.prototype = 'string'; | |
| 193 assert_throws(expectTypeError, () => { | |
| 194 customElements.define('test-define-constructor-prototype-string', c); | |
| 195 }); | |
| 196 }, 'If Type(constructor.prototype) is string, should throw a TypeError'); | |
| 197 | |
| 198 // 12.3. Let lifecycleCallbacks be a map with the four keys "connectedCallback
", | |
| 199 // "disconnectedCallback", "adoptedCallback", and "attributeChangedCallback", | |
| 200 // each of which belongs to an entry whose value is null. | |
| 201 // 12.4. For each of the four keys callbackName in lifecycleCallbacks: | |
| 202 // 12.4.1. Let callbackValue be Get(prototype, callbackName). Rethrow any exce
ptions. | |
| 203 // 12.4.2. If callbackValue is not undefined, then set the value of the entry
in | |
| 204 // lifecycleCallbacks with key callbackName to the result of converting callba
ckValue | |
| 205 // to the Web IDL Function callback type. Rethrow any exceptions from the conv
ersion. | |
| 206 [ | |
| 207 'connectedCallback', | |
| 208 'disconnectedCallback', | |
| 209 'adoptedCallback', | |
| 210 'attributeChangedCallback', | |
| 211 ].forEach(name => { | |
| 212 test(() => { | |
| 213 class C { | |
| 214 get [name]() { throw_rethrown_error(); } | |
| 215 } | |
| 216 assert_rethrown(() => { | |
| 217 customElements.define(`test-define-${name.toLowerCase()}-rethrow`, C); | |
| 218 }); | |
| 219 }, `If constructor.prototype.${name} throws, should rethrow`); | |
| 220 | |
| 221 [ | |
| 222 { name: 'undefined', value: undefined, success: true }, | |
| 223 { name: 'function', value: function () { }, success: true }, | |
| 224 { name: 'null', value: null, success: false }, | |
| 225 { name: 'object', value: {}, success: false }, | |
| 226 { name: 'integer', value: 1, success: false }, | |
| 227 ].forEach(data => { | |
| 228 test(() => { | |
| 229 class C { }; | |
| 230 C.prototype[name] = data.value; | |
| 231 if (data.success) { | |
| 232 customElements.define(`test-define-${name.toLowerCase()}-${data.name}`
, C); | |
| 233 } else { | |
| 234 assert_throws(expectTypeError, () => { | |
| 235 customElements.define(`test-define-${name.toLowerCase()}-${data.name
}`, C); | |
| 236 }); | |
| 237 } | |
| 238 }, `If constructor.prototype.${name} is ${data.name}, should ${data.succes
s ? 'succeed' : 'throw a TypeError'}`); | |
| 239 }); | |
| 240 }); | |
| 241 })(); | |
| 242 </script> | |
| 243 </body> | |
| OLD | NEW |