OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 <title>Custom Elements: Constructor Tests</title> |
| 3 <link rel="help" href="https://html.spec.whatwg.org/multipage/dom.html#elements-
in-the-dom"> |
| 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 'use strict'; |
| 12 |
| 13 test_with_window((w) => { |
| 14 assert_throws(TypeError.prototype, () => { |
| 15 w.HTMLElement(); |
| 16 }, 'calling the HTMLElement constructor should throw a TypeError'); |
| 17 }, 'HTMLElement constructor, invoke'); |
| 18 |
| 19 test_with_window((w) => { |
| 20 assert_throws(TypeError.prototype, () => { |
| 21 new w.HTMLElement(); |
| 22 }, 'invoking the HTMLElement constructor with a construct call should ' + |
| 23 'throw a TypeError'); |
| 24 }, 'HTMLElement constructor, construct'); |
| 25 |
| 26 test_with_window((w) => { |
| 27 class X extends w.HTMLElement {} |
| 28 w.customElements.define('a-a', X); |
| 29 assert_throws(TypeError.prototype, () => { |
| 30 X(); |
| 31 }, 'calling a custom element constructor should throw a TypeError'); |
| 32 }, 'Custom element constructor, invoke'); |
| 33 |
| 34 test_with_window((w) => { |
| 35 var num_constructor_invocations = 0; |
| 36 class C extends w.HTMLElement { |
| 37 constructor() { |
| 38 super(); |
| 39 num_constructor_invocations++; |
| 40 } |
| 41 } |
| 42 w.customElements.define('a-a', C); |
| 43 let e = new C(); |
| 44 assert_true(e instanceof w.HTMLElement, |
| 45 'the element should be an HTMLElement'); |
| 46 assert_equals(e.localName, |
| 47 'a-a', |
| 48 'the element tag name should be "a-a"'); |
| 49 assert_equals(e.namespaceURI, |
| 50 'http://www.w3.org/1999/xhtml', |
| 51 'the element should be in the HTML namespace'); |
| 52 assert_equals(e.prefix, |
| 53 null, |
| 54 'the element name should not have a prefix'); |
| 55 assert_equals(e.ownerDocument, |
| 56 w.document, |
| 57 'the element should be owned by the registry\'s associated ' + |
| 58 'document'); |
| 59 assert_equals(num_constructor_invocations, |
| 60 1, |
| 61 'the constructor should have been invoked once'); |
| 62 }, 'Custom element constructor, construct'); |
| 63 |
| 64 test_with_window((w) => { |
| 65 class C extends w.HTMLElement { } |
| 66 class D extends C {} |
| 67 w.customElements.define('a-a', C); // Note: Not D. |
| 68 assert_throws(TypeError.prototype, () => { |
| 69 new D(); |
| 70 }, 'constructing a custom element with a new.target with no registry ' + |
| 71 'entry should throw a TypeError'); |
| 72 |
| 73 assert_throws(TypeError.prototype, () => { |
| 74 Reflect.construct(C, [], 42); |
| 75 }, 'constructing a custom element with a non-object new.target should ' + |
| 76 'throw a TypeError'); |
| 77 }, 'Custom element constructor, construct invalid new.target'); |
| 78 </script> |
OLD | NEW |