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