Skip to content

Commit a01431a

Browse files
mfreed7chromium-wpt-export-bot
authored andcommitted
Change the behavior of clonable to be more opt-in
See the discussion here: whatwg/html#10107 (comment) The new consensus is that the old behavior was likely web- incompatible, plus not very developer-desirable. The new behavior adds a `shadowrootclonable` attribute for declarative shadow dom to opt-in to clonable shadow roots. This is a slight behavior change from the existing shipped behavior, in that before the `clonable` concept was introduced, *any* declarative shadow root within a `<template>` would be cloned. Now, that behavior is opt in. So: old: <template> <div> <template shadowrootmode=open> I do NOT get cloned! </template> </div> </template> new: <template> <div> <template shadowrootmode=open shadowrootclonable> I get cloned! </template> </div> </template> See these three spec PRs: whatwg/dom#1246 whatwg/html#10069 whatwg/html#10117 Bug: 1510466 Change-Id: Ice7c7579094eb08b882c4bb44f93045f23b8f222
1 parent 755d6d2 commit a01431a

5 files changed

+59
-23
lines changed

resources/declarative-shadow-dom-polyfill.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ function polyfill_declarative_shadow_dom(root) {
1717
root.querySelectorAll("template[shadowrootmode]").forEach(template => {
1818
const mode = template.getAttribute("shadowrootmode");
1919
const delegatesFocus = template.hasAttribute("shadowrootdelegatesfocus");
20-
const shadowRoot = template.parentNode.attachShadow({ mode, delegatesFocus });
20+
const clonable = template.hasAttribute("shadowrootclonable");
21+
const shadowRoot = template.parentNode.attachShadow({ mode, delegatesFocus, clonable });
2122
shadowRoot.appendChild(template.content);
2223
template.remove();
2324
polyfill_declarative_shadow_dom(shadowRoot);

shadow-dom/declarative/declarative-shadow-dom-basic.html

+23
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,29 @@
143143
}, 'Declarative Shadow DOM: delegates focus attribute');
144144
</script>
145145

146+
test(() => {
147+
const div = document.createElement('div');
148+
div.setHTMLUnsafe(`
149+
<div id="host">
150+
<template shadowrootmode="open" shadowrootclonable>
151+
</template>
152+
</div>
153+
`);
154+
var host = div.querySelector('#host');
155+
assert_true(!!host.shadowRoot,"No shadow root found");
156+
assert_true(host.shadowRoot.clonable,"clonable should be true");
157+
div.setHTMLUnsafe(`
158+
<div id="host">
159+
<template shadowrootmode="open">
160+
</template>
161+
</div>
162+
`);
163+
host = div.querySelector('#host');
164+
assert_true(!!host.shadowRoot,"No shadow root found");
165+
assert_false(host.shadowRoot.clonable,"clonable should be false without the shadowrootclonable attribute");
166+
}, 'Declarative Shadow DOM: clonable attribute');
167+
</script>
168+
146169
<div id="multi-host" style="display:none">
147170
<template shadowrootmode="open">
148171
<span>root 1</span>

shadow-dom/declarative/declarative-shadow-dom-repeats.html

+6-8
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@
4949
</script>
5050

5151
<div id=open2>
52-
<template shadowrootmode=open shadowrootdelegatesfocus>
53-
Open, delegates focus (not the default),
54-
named slot assignment (the default), clonable (the default for declarative)
52+
<template shadowrootmode=open shadowrootdelegatesfocus shadowrootclonable>
53+
Open, delegates focus (not the default), clonable (not the default)
54+
named slot assignment (the default)
5555
</template>
5656
</div>
5757

@@ -66,11 +66,9 @@
6666
assert_throws_dom("NotSupportedError",() => {
6767
open2.attachShadow({mode: "open", delegatesFocus: true, slotAssignment: "manual", clonable: true});
6868
},'Mismatched shadow root slotAssignment should throw');
69-
// See https://github.com/whatwg/html/issues/10107: the behavior of the
70-
// clonable flag is still being discussed.
71-
// assert_throws_dom("NotSupportedError",() => {
72-
// open2.attachShadow({mode: "open", delegatesFocus: true, slotAssignment: "named", clonable: false});
73-
// },'Mismatched shadow root clonable should throw');
69+
assert_throws_dom("NotSupportedError",() => {
70+
open2.attachShadow({mode: "open", delegatesFocus: true, slotAssignment: "named", clonable: false});
71+
},'Mismatched shadow root clonable should throw');
7472

7573
const initialShadow = open2.shadowRoot;
7674
const shadow = open2.attachShadow({mode: "open", delegatesFocus: true, slotAssignment: "named", clonable: true}); // Shouldn't throw

shadow-dom/declarative/gethtml.tentative.html

+12-8
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<body>
1010

1111
<script>
12-
function testElementType(allowsShadowDom, elementType, runGetHTMLOnShadowRoot, declarativeShadowDom, mode, delegatesFocus, serializable) {
12+
function testElementType(allowsShadowDom, elementType, runGetHTMLOnShadowRoot, declarativeShadowDom, mode, delegatesFocus, serializable, clonable) {
1313
const t = test(t => {
1414
// Create and attach element
1515
let wrapper;
@@ -27,7 +27,7 @@
2727

2828
let shadowRoot;
2929
const isOpen = mode === 'open';
30-
let initDict = {mode: mode, delegatesFocus: delegatesFocus};
30+
let initDict = {mode: mode, delegatesFocus: delegatesFocus, clonable};
3131
let expectedSerializable = null;
3232
switch (serializable) {
3333
case undefined: expectedSerializable = false; break;
@@ -37,9 +37,10 @@
3737
}
3838
const delegatesAttr = delegatesFocus ? ' shadowrootdelegatesfocus=""' : '';
3939
const serializableAttr = expectedSerializable ? ' serializable=""' : '';
40+
const clonableAttr = clonable ? ' shadowrootclonable=""' : '';
4041

4142
if (allowsShadowDom && declarativeShadowDom) {
42-
const html = `<${elementType}><template shadowrootmode=${mode}${delegatesAttr}${serializableAttr}>`;
43+
const html = `<${elementType}><template shadowrootmode=${mode}${delegatesAttr}${serializableAttr}${clonableAttr}>`;
4344
wrapper.setHTMLUnsafe(html);
4445
if (isOpen) {
4546
shadowRoot = wrapper.firstElementChild.shadowRoot;
@@ -58,11 +59,12 @@
5859
assert_true(!allowsShadowDom || !!shadowRoot);
5960

6061
if (allowsShadowDom) {
61-
const correctShadowHtml = `<template shadowrootmode="${mode}"${delegatesAttr}${serializableAttr}><slot></slot></template>`;
62+
const correctShadowHtml = `<template shadowrootmode="${mode}"${delegatesAttr}${serializableAttr}${clonableAttr}><slot></slot></template>`;
6263
const correctHtml = `<${elementType}>${correctShadowHtml}</${elementType}>`;
6364
assert_equals(shadowRoot.mode,mode);
6465
assert_equals(shadowRoot.delegatesFocus,delegatesFocus);
6566
assert_equals(shadowRoot.serializable,expectedSerializable);
67+
assert_equals(shadowRoot.clonable,clonable);
6668
shadowRoot.appendChild(document.createElement('slot'));
6769
const emptyElement = `<${elementType}></${elementType}>`;
6870
if (isOpen) {
@@ -91,7 +93,7 @@
9193
// ...and that the default for includeShadowRoots is false.
9294
assert_equals(wrapper.getHTML(),wrapper.innerHTML,'The default for includeShadowRoots should be false');
9395

94-
}, `${runGetHTMLOnShadowRoot ? 'ShadowRoot' : 'Element'}.getHTML() on <${elementType}>${allowsShadowDom ? `, with ${declarativeShadowDom ? 'declarative' : 'imperative'} shadow, mode=${mode}, delegatesFocus=${delegatesFocus}, serializable=${serializable}.` : ''}`);
96+
}, `${runGetHTMLOnShadowRoot ? 'ShadowRoot' : 'Element'}.getHTML() on <${elementType}>${allowsShadowDom ? `, with ${declarativeShadowDom ? 'declarative' : 'imperative'} shadow, mode=${mode}, delegatesFocus=${delegatesFocus}, serializable=${serializable}, clonable=${clonable}.` : ''}`);
9597
}
9698

9799
function runAllTests() {
@@ -103,9 +105,11 @@
103105
if (allowsShadowDom) {
104106
for (const declarativeShadowDom of [false, true]) {
105107
for (const delegatesFocus of [false, true]) {
106-
for (const mode of ['open', 'closed']) {
107-
for (const serializable of [undefined, 'false', 'true']) {
108-
testElementType(true, elementName, runGetHTMLOnShadowRoot, declarativeShadowDom, mode, delegatesFocus, serializable);
108+
for (const clonable of [false, true]) {
109+
for (const mode of ['open', 'closed']) {
110+
for (const serializable of [undefined, 'false', 'true']) {
111+
testElementType(true, elementName, runGetHTMLOnShadowRoot, declarativeShadowDom, mode, delegatesFocus, serializable, clonable);
112+
}
109113
}
110114
}
111115
}

shadow-dom/shadow-root-clonable.html

+16-6
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,26 @@
4747
div.setHTMLUnsafe('<div><template shadowrootmode=open><input></template></div>');
4848
const root = div.firstElementChild.shadowRoot;
4949
assert_true(!!root);
50-
assert_true(root.clonable, "clonable is automatically true for declarative shadow root");
50+
assert_true(!root.clonable, "clonable is *not* automatically true for declarative shadow root");
51+
52+
const clone = div.cloneNode(true);
53+
const clonedRoot = clone.firstElementChild.shadowRoot;
54+
assert_true(!clonedRoot,'no shadow root gets cloned');
55+
}, "declarative shadow roots do *not* get clonable: true automatically");
56+
57+
test(() => {
58+
const div = document.createElement("div");
59+
div.setHTMLUnsafe('<div><template shadowrootmode=open shadowrootclonable><input></template></div>');
60+
const root = div.firstElementChild.shadowRoot;
61+
assert_true(!!root);
62+
assert_true(root.clonable, "clonable gets added when shadowrootclonable is present");
5163

5264
const clone = div.cloneNode(true);
5365
const clonedRoot = clone.firstElementChild.shadowRoot;
5466
assert_true(!!clonedRoot);
5567
assert_equals(clonedRoot.children.length, 1, "children count");
5668
assert_equals(clonedRoot.children[0].localName, "input", "children content");
57-
}, "declarative shadow roots get clonable: true automatically");
69+
}, "declarative shadow roots can opt in to clonable with shadowrootclonable");
5870
</script>
5971

6072
<template id="test">
@@ -70,8 +82,6 @@
7082
assert_true(!!root);
7183
const clone = template.content.cloneNode(true);
7284
const clonedRoot = clone.querySelector('#host').shadowRoot;
73-
assert_true(!!clonedRoot);
74-
assert_equals(clonedRoot.children.length, 1, "children count");
75-
assert_equals(clonedRoot.children[0].localName, "input", "children content");
76-
}, "declarative shadow roots inside templates also get cloned automatically");
85+
assert_true(!clonedRoot,'no shadow root gets cloned');
86+
}, "declarative shadow roots inside templates do *not* get cloned automatically");
7787
</script>

0 commit comments

Comments
 (0)