Skip to content

Commit

Permalink
Bug 1663100 [wpt PR 25402] - Restrict attachInternals() to be run ins…
Browse files Browse the repository at this point in the history
…ide or after the constructor, a=testonly

Automatic update from web-platform-tests
Restrict attachInternals() to be run inside or after the constructor

Per the discussion at [1], the intention of this change is to prevent
calls to attachInternals() prior to the constructor of the custom
element having a chance to do so. The spec PR is at [2].

This change is gated behind the DeclarativeShadowDOM flag. With the
flag disabled (the default), a use counter is added for checking on
the web compatibility of this change. The use counter will measure
the cases where attachInternals() is being called in a to-be-outlawed
way.

[1] WICG/webcomponents#871 (comment)
[2] whatwg/html#5909

Bug: 1042130
Change-Id: Iacf97a49133b5f7f44710e5c0287f01cfebe4c44
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2392975
Reviewed-by: Alexei Svitkine <asvitkinechromium.org>
Reviewed-by: Kouhei Ueno <kouheichromium.org>
Commit-Queue: Mason Freed <masonfreedchromium.org>
Auto-Submit: Mason Freed <masonfreedchromium.org>
Cr-Commit-Position: refs/heads/master{#806830}

--

wpt-commits: df5b354d012f3bb0db1524bfaf8a4e16b4b01cc2
wpt-pr: 25402

UltraBlame original commit: 56afece076c057fd659c3c180e1673c5a409a614
  • Loading branch information
marco-c committed Sep 28, 2020
1 parent b1f5d7a commit b72c2e7
Showing 1 changed file with 69 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,73 @@
assert_true(constructed);
}, 'ElementInternals.shadowRoot allows access to closed shadow root');

test(() => {
let constructed = false;
const element = document.createElement('x-1');
assert_throws_dom('NotSupportedError', () => element.attachInternals(),'attachInternals cannot be called before definition exists');
customElements.define('x-1', class extends HTMLElement {
constructor() {
super();
assert_true(!!this.attachInternals());
constructed = true;
}
});
assert_false(constructed);
assert_throws_dom('NotSupportedError', () => element.attachInternals(),'attachInternals cannot be called before constructor');
customElements.upgrade(element);
assert_true(constructed);
assert_throws_dom('NotSupportedError', () => element.attachInternals(),'attachInternals already called');
}, 'ElementInternals cannot be called before constructor, upgrade case');

test(() => {
let constructed = false;
const element = document.createElement('x-2');
customElements.define('x-2', class extends HTMLElement {
constructor() {
super();
// Don't attachInternals() here
constructed = true;
}
});
assert_throws_dom('NotSupportedError', () => element.attachInternals(),'attachInternals cannot be called before constructor');
assert_false(constructed);
customElements.upgrade(element);
assert_true(constructed);
assert_true(!!element.attachInternals(),'After the constructor, ok to call from outside');
}, 'ElementInternals *can* be called after constructor, upgrade case');

test(() => {
let constructed = false;
customElements.define('x-3', class extends HTMLElement {
constructor() {
super();
assert_true(!!this.attachInternals());
constructed = true;
}
});
const element = document.createElement('x-3');
assert_true(constructed);
assert_throws_dom('NotSupportedError', () => element.attachInternals(), 'attachInternals already called');
}, 'ElementInternals cannot be called after constructor calls it, create case');

test(() => {
let constructed = false;
const element = document.createElement('x-5');
customElements.define('x-5', class extends HTMLElement {
static disabledFeatures = [ 'internals' ];
constructor() {
super();
assert_throws_dom('NotSupportedError', () => this.attachInternals(), 'attachInternals forbidden by disabledFeatures, constructor');
constructed = true;
}
});
assert_false(constructed);
assert_throws_dom('NotSupportedError', () => element.attachInternals(), 'attachInternals forbidden by disabledFeatures, pre-upgrade');
customElements.upgrade(element);
assert_true(constructed);
assert_throws_dom('NotSupportedError', () => element.attachInternals(), 'attachInternals forbidden by disabledFeatures, post-upgrade');
}, 'ElementInternals disabled by disabledFeatures');



</script>

0 comments on commit b72c2e7

Please sign in to comment.