| TAMURA, Kent | 4c70b5a | 2019-02-21 10:06:44 | [diff] [blame] | 1 | <!DOCTYPE html> |
| 2 | <body> |
| 3 | <script src="/resources/testharness.js"></script> |
| 4 | <script src="/resources/testharnessreport.js"></script> |
| 5 | <link rel="help" content="https://html.spec.whatwg.org/multipage/custom-elements.html#dom-attachinternals"> |
| 6 | <div id="container"></div> |
| 7 | <script> |
| 8 | test(() => { |
| 9 | class MyElement1 extends HTMLElement { |
| 10 | } |
| 11 | customElements.define('my-element1', MyElement1); |
| 12 | const container = document.querySelector('#container'); |
| 13 | |
| 14 | let element = new MyElement1(); |
| 15 | assert_true(element.attachInternals() instanceof ElementInternals, |
| 16 | 'New - 1st call'); |
| Stephen McGruer | d510304 | 2020-01-23 21:45:45 | [diff] [blame] | 17 | assert_throws_dom('NotSupportedError', () => { element.attachInternals(); }, |
| Stephen McGruer | 63c2f71 | 2020-01-25 14:26:16 | [diff] [blame] | 18 | 'New - 2nd call'); |
| TAMURA, Kent | 4c70b5a | 2019-02-21 10:06:44 | [diff] [blame] | 19 | |
| 20 | element = document.createElement('my-element1'); |
| 21 | assert_true(element.attachInternals() instanceof ElementInternals, |
| 22 | 'createElement - 1st call'); |
| Stephen McGruer | d510304 | 2020-01-23 21:45:45 | [diff] [blame] | 23 | assert_throws_dom('NotSupportedError', () => { element.attachInternals(); }, |
| Stephen McGruer | 63c2f71 | 2020-01-25 14:26:16 | [diff] [blame] | 24 | 'createElement - 2nd call'); |
| TAMURA, Kent | 4c70b5a | 2019-02-21 10:06:44 | [diff] [blame] | 25 | |
| 26 | container.innerHTML = '<my-element1></my-element1>'; |
| 27 | assert_true(container.firstChild.attachInternals() instanceof ElementInternals, |
| 28 | 'Parser - 1st call'); |
| Stephen McGruer | d510304 | 2020-01-23 21:45:45 | [diff] [blame] | 29 | assert_throws_dom('NotSupportedError', () => { |
| TAMURA, Kent | 4c70b5a | 2019-02-21 10:06:44 | [diff] [blame] | 30 | container.firstChild.attachInternals(); |
| 31 | }, 'Parser - 2nd call'); |
| 32 | }, 'Successful attachInternals() and the second call.'); |
| 33 | |
| 34 | test(() => { |
| 35 | class MyDiv extends HTMLDivElement {} |
| 36 | customElements.define('my-div', MyDiv, { extends: 'div' }); |
| 37 | const customizedBuiltin = document.createElement('div', { is: 'my-div'}); |
| Stephen McGruer | d510304 | 2020-01-23 21:45:45 | [diff] [blame] | 38 | assert_throws_dom('NotSupportedError', () => { customizedBuiltin.attachInternals() }); |
| TAMURA, Kent | 4c70b5a | 2019-02-21 10:06:44 | [diff] [blame] | 39 | }, 'attachInternals() throws a NotSupportedError if it is called for ' + |
| 40 | 'a customized built-in element'); |
| 41 | |
| 42 | test(() => { |
| 43 | const builtin = document.createElement('div'); |
| Stephen McGruer | d510304 | 2020-01-23 21:45:45 | [diff] [blame] | 44 | assert_throws_dom('NotSupportedError', () => { builtin.attachInternals() }); |
| TAMURA, Kent | 4c70b5a | 2019-02-21 10:06:44 | [diff] [blame] | 45 | |
| 46 | const doc = document.implementation.createDocument('foo', null); |
| 47 | const span = doc.appendChild(doc.createElementNS('http://www.w3.org/1999/xhtml', 'html:span')); |
| 48 | assert_true(span instanceof HTMLElement); |
| Stephen McGruer | d510304 | 2020-01-23 21:45:45 | [diff] [blame] | 49 | assert_throws_dom('NotSupportedError', () => { span.attachInternals(); }); |
| TAMURA, Kent | 4c70b5a | 2019-02-21 10:06:44 | [diff] [blame] | 50 | |
| 51 | const undefinedCustom = document.createElement('undefined-element'); |
| Stephen McGruer | d510304 | 2020-01-23 21:45:45 | [diff] [blame] | 52 | assert_throws_dom('NotSupportedError', () => { undefinedCustom.attachInternals() }); |
| TAMURA, Kent | 4c70b5a | 2019-02-21 10:06:44 | [diff] [blame] | 53 | }, 'If a custom element definition for the local name of the element doesn\'t' + |
| Domenic Denicola | dc4519e | 2019-05-15 23:30:20 | [diff] [blame] | 54 | ' exist, throw an NotSupportedError'); |
| TAMURA, Kent | 4c70b5a | 2019-02-21 10:06:44 | [diff] [blame] | 55 | |
| 56 | test(() => { |
| 57 | class MyElement2 extends HTMLElement { |
| 58 | static get disabledFeatures() { return ['internals']; } |
| 59 | } |
| 60 | customElements.define('my-element2', MyElement2); |
| 61 | const container = document.querySelector('#container'); |
| 62 | |
| Stephen McGruer | d510304 | 2020-01-23 21:45:45 | [diff] [blame] | 63 | assert_throws_dom('NotSupportedError', () => { |
| TAMURA, Kent | 4c70b5a | 2019-02-21 10:06:44 | [diff] [blame] | 64 | (new MyElement2).attachInternals(); |
| 65 | }); |
| Stephen McGruer | d510304 | 2020-01-23 21:45:45 | [diff] [blame] | 66 | assert_throws_dom('NotSupportedError', () => { |
| TAMURA, Kent | 4c70b5a | 2019-02-21 10:06:44 | [diff] [blame] | 67 | document.createElement('my-element2').attachInternals(); |
| 68 | }); |
| Stephen McGruer | d510304 | 2020-01-23 21:45:45 | [diff] [blame] | 69 | assert_throws_dom('NotSupportedError', () => { |
| TAMURA, Kent | 4c70b5a | 2019-02-21 10:06:44 | [diff] [blame] | 70 | container.innerHTML = '<my-element2></my-element2>'; |
| 71 | container.firstChild.attachInternals(); |
| 72 | }); |
| 73 | |
| 74 | class MyElement3 extends HTMLElement { |
| 75 | static get disabledFeatures() { return ['INTERNALS']; } |
| 76 | } |
| 77 | customElements.define('my-element3', MyElement3); |
| 78 | assert_true((new MyElement3).attachInternals() instanceof ElementInternals); |
| 79 | }, 'If a custom element definition for the local name of the element has ' + |
| 80 | 'disable internals flag, throw a NotSupportedError'); |
| 81 | </script> |
| 82 | </body> |