OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 <title>Custom Elements: defineElement</title> |
| 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"> |
| 5 <script src="../../resources/testharness.js"></script> |
| 6 <script src="../../resources/testharness-helpers.js"></script> |
| 7 <script src="../../resources/testharnessreport.js"></script> |
| 8 <script src="resources/custom-elements-helpers.js"></script> |
| 9 <body> |
| 10 <script> |
| 11 // TODO(dominicc): Merge these tests with |
| 12 // https://github.com/w3c/web-platform-tests/pull/2940 |
| 13 |
| 14 'use strict'; |
| 15 |
| 16 test_with_window((w) => { |
| 17 assert_throws(TypeError.prototype, () => { |
| 18 w.customElements.define('a-a', 42); |
| 19 }, 'defining a non-constructor should throw a TypeError'); |
| 20 }, 'A "constructor" that is not a constructor'); |
| 21 |
| 22 test_with_window((w) => { |
| 23 // https://html.spec.whatwg.org/multipage/scripting.html#valid-custom-element-
name |
| 24 let invalid_names = [ |
| 25 'annotation-xml', |
| 26 'color-profile', |
| 27 'font-face', |
| 28 'font-face-src', |
| 29 'font-face-uri', |
| 30 'font-face-format', |
| 31 'font-face-name', |
| 32 'missing-glyph', |
| 33 'div', 'p', |
| 34 'nothtmlbutnohyphen', |
| 35 '-not-initial-a-z', '0not-initial-a-z', 'Not-initial-a-z', |
| 36 'intermediate-UPPERCASE-letters', |
| 37 'bad-\u00b6', 'bad-\u00b8', 'bad-\u00bf', 'bad-\u00d7', 'bad-\u00f7', |
| 38 'bad-\u037e', 'bad-\u037e', 'bad-\u2000', 'bad-\u200e', 'bad-\u203e', |
| 39 'bad-\u2041', 'bad-\u206f', 'bad-\u2190', 'bad-\u2bff', 'bad-\u2ff0', |
| 40 'bad-\u3000', 'bad-\ud800', 'bad-\uf8ff', 'bad-\ufdd0', 'bad-\ufdef', |
| 41 'bad-\ufffe', 'bad-\uffff', 'bad-' + String.fromCodePoint(0xf0000) |
| 42 ]; |
| 43 class X extends w.HTMLElement {} |
| 44 invalid_names.forEach((name) => { |
| 45 assert_throws('SYNTAX_ERR', () => { |
| 46 w.customElements.define(name, X); |
| 47 }, `defining an element named "${name}" should throw a SyntaxError`); |
| 48 }); |
| 49 }, 'Invalid names'); |
| 50 |
| 51 test_with_window((w) => { |
| 52 class X extends w.HTMLElement {} |
| 53 class Y extends w.HTMLElement {} |
| 54 w.customElements.define('a-a', X); |
| 55 assert_throws('NotSupportedError', () => { |
| 56 w.customElements.define('a-a', Y); |
| 57 }, 'defining an element with a name that is already defined should throw ' + |
| 58 'a NotSupportedError'); |
| 59 }, 'Duplicate name'); |
| 60 |
| 61 test_with_window((w) => { |
| 62 class X extends w.HTMLElement {} |
| 63 w.customElements.define('a-a', X); |
| 64 assert_throws('NotSupportedError', () => { |
| 65 w.customElements.define('a-b', X); |
| 66 }, 'defining an element with a constructor that is already in the ' + |
| 67 'registry should throw a NotSupportedError'); |
| 68 }, 'Reused constructor'); |
| 69 |
| 70 test_with_window((w) => { |
| 71 function F() {} |
| 72 F.prototype = 42; |
| 73 assert_throws(TypeError.prototype, () => { |
| 74 w.customElements.define('a-a', F); |
| 75 }, 'defining an element with a constructor with a prototype that is not an ' + |
| 76 'object should throw a TypeError'); |
| 77 }, 'Retrieved prototype is a non-object'); |
| 78 |
| 79 test_with_window((w) => { |
| 80 assert_throws(TypeError.prototype, () => { |
| 81 let not_a_constructor = new Object(); |
| 82 let invalid_name = 'annotation-xml'; |
| 83 // TODO(dominicc): When V8 supports IsConstructor, replace this with a |
| 84 // function. |
| 85 w.customElements.define(invalid_name, not_a_constructor); |
| 86 }, 'Defining an element with an invalid name and invalid constructor ' + |
| 87 'should throw a TypeError for the constructor and not a SyntaxError'); |
| 88 |
| 89 class C extends w.HTMLElement {} |
| 90 w.customElements.define('a-a', C); |
| 91 assert_throws('SYNTAX_ERR', () => { |
| 92 let invalid_name = 'annotation-xml'; |
| 93 let reused_constructor = C; |
| 94 w.customElements.define(invalid_name, reused_constructor); |
| 95 }, 'Defining an element with an invalid name and a reused constructor ' + |
| 96 'should throw a SyntaxError for the name and not a NotSupportedError'); |
| 97 }, 'Order of checks'); |
| 98 </script> |
OLD | NEW |