|
41 | 41 | assert_true(constructed);
|
42 | 42 | }, 'ElementInternals.shadowRoot allows access to closed shadow root');
|
43 | 43 |
|
| 44 | +test(() => { |
| 45 | + let constructed = false; |
| 46 | + const element = document.createElement('x-1'); |
| 47 | + assert_throws_dom('NotSupportedError', () => element.attachInternals(),'attachInternals cannot be called before definition exists'); |
| 48 | + customElements.define('x-1', class extends HTMLElement { |
| 49 | + constructor() { |
| 50 | + super(); |
| 51 | + assert_true(!!this.attachInternals()); |
| 52 | + constructed = true; |
| 53 | + } |
| 54 | + }); |
| 55 | + assert_false(constructed); |
| 56 | + assert_throws_dom('NotSupportedError', () => element.attachInternals(),'attachInternals cannot be called before constructor'); |
| 57 | + customElements.upgrade(element); |
| 58 | + assert_true(constructed); |
| 59 | + assert_throws_dom('NotSupportedError', () => element.attachInternals(),'attachInternals already called'); |
| 60 | +}, 'ElementInternals cannot be called before constructor, upgrade case'); |
| 61 | + |
| 62 | +test(() => { |
| 63 | + let constructed = false; |
| 64 | + const element = document.createElement('x-2'); |
| 65 | + customElements.define('x-2', class extends HTMLElement { |
| 66 | + constructor() { |
| 67 | + super(); |
| 68 | + // Don't attachInternals() here |
| 69 | + constructed = true; |
| 70 | + } |
| 71 | + }); |
| 72 | + assert_throws_dom('NotSupportedError', () => element.attachInternals(),'attachInternals cannot be called before constructor'); |
| 73 | + assert_false(constructed); |
| 74 | + customElements.upgrade(element); |
| 75 | + assert_true(constructed); |
| 76 | + assert_true(!!element.attachInternals(),'After the constructor, ok to call from outside'); |
| 77 | +}, 'ElementInternals *can* be called after constructor, upgrade case'); |
| 78 | + |
| 79 | +test(() => { |
| 80 | + let constructed = false; |
| 81 | + customElements.define('x-3', class extends HTMLElement { |
| 82 | + constructor() { |
| 83 | + super(); |
| 84 | + assert_true(!!this.attachInternals()); |
| 85 | + constructed = true; |
| 86 | + } |
| 87 | + }); |
| 88 | + const element = document.createElement('x-3'); |
| 89 | + assert_true(constructed); |
| 90 | + assert_throws_dom('NotSupportedError', () => element.attachInternals(), 'attachInternals already called'); |
| 91 | +}, 'ElementInternals cannot be called after constructor calls it, create case'); |
| 92 | + |
| 93 | +test(() => { |
| 94 | + let constructed = false; |
| 95 | + const element = document.createElement('x-5'); |
| 96 | + customElements.define('x-5', class extends HTMLElement { |
| 97 | + static disabledFeatures = [ 'internals' ]; |
| 98 | + constructor() { |
| 99 | + super(); |
| 100 | + assert_throws_dom('NotSupportedError', () => this.attachInternals(), 'attachInternals forbidden by disabledFeatures, constructor'); |
| 101 | + constructed = true; |
| 102 | + } |
| 103 | + }); |
| 104 | + assert_false(constructed); |
| 105 | + assert_throws_dom('NotSupportedError', () => element.attachInternals(), 'attachInternals forbidden by disabledFeatures, pre-upgrade'); |
| 106 | + customElements.upgrade(element); |
| 107 | + assert_true(constructed); |
| 108 | + assert_throws_dom('NotSupportedError', () => element.attachInternals(), 'attachInternals forbidden by disabledFeatures, post-upgrade'); |
| 109 | +}, 'ElementInternals disabled by disabledFeatures'); |
| 110 | + |
| 111 | + |
| 112 | + |
44 | 113 | </script>
|
0 commit comments