Skip to content

Commit b2c5875

Browse files
mfreed7chromium-wpt-export-bot
authored andcommitted
Add ElementInternals.shadowRoot which returns both open and closed roots
See [1] for context. This CL adds the shadowRoot attribute to ElementInternals, which allows custom elements to access their own shadow roots, including in the case that the shadowRoot is closed. Without this capability, custom elements that use closed declarative shadow roots would have no ability to retrieve the contents of their own shadow root. Bug: 1042130 [1] WICG/webcomponents#871 (comment) Change-Id: I33606144bec3c27600d2e67f108d0344f7696dd2
1 parent 3fce2a5 commit b2c5875

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<!DOCTYPE html>
2+
<meta charset="utf-8">
3+
<title>ElementInternals.shadowRoot</title>
4+
<link rel="author" title="Mason Freed" href="mailto:[email protected]">
5+
<link rel="help" href="https://github.com/w3c/webcomponents/issues/871">
6+
<script src="/resources/testharness.js"></script>
7+
<script src="/resources/testharnessreport.js"></script>
8+
9+
<script>
10+
11+
test(() => {
12+
let constructed = false;
13+
customElements.define('custom-open', class extends HTMLElement {
14+
constructor() {
15+
super();
16+
const elementInternals = this.attachInternals();
17+
assert_equals(elementInternals.shadowRoot, null);
18+
const shadow = this.attachShadow({mode: 'open'});
19+
assert_equals(elementInternals.shadowRoot, shadow);
20+
constructed = true;
21+
}
22+
});
23+
const element = document.createElement('custom-open');
24+
assert_true(constructed);
25+
}, 'ElementInternals.shadowRoot allows access to open shadow root');
26+
27+
test(() => {
28+
let constructed = false;
29+
customElements.define('custom-closed', class extends HTMLElement {
30+
constructor() {
31+
super();
32+
const elementInternals = this.attachInternals();
33+
assert_equals(elementInternals.shadowRoot, null);
34+
const shadow = this.attachShadow({mode: 'closed'});
35+
assert_equals(elementInternals.shadowRoot, shadow);
36+
assert_equals(this.shadowRoot, null);
37+
constructed = true;
38+
}
39+
});
40+
const element = document.createElement('custom-closed');
41+
assert_true(constructed);
42+
}, 'ElementInternals.shadowRoot allows access to closed shadow root');
43+
44+
</script>

0 commit comments

Comments
 (0)