Skip to content

Commit b76d81e

Browse files
author
Steven Orvell
committed
Enhance robustness by replacing slot with a comment
This change makes the slot not depend on its nextSibling being stable in dom which is problematic if, for example, its nextSibling is a restamping dom-if which becomes false.
1 parent 82e798c commit b76d81e

3 files changed

Lines changed: 29 additions & 16 deletions

File tree

lib/utils/templatize.html

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,15 +176,18 @@
176176
} else if (n.localName === 'slot') {
177177
if (hide) {
178178
n.__polymerParent__ = n.parentNode;
179-
n.__polymerSibling__ = n.nextSibling;
180-
n.parentNode.removeChild(n);
179+
n.__polymerReplaced__ = new Comment('hidden-slot');
180+
n.parentNode.replaceChild(n.__polymerReplaced__, n);
181181
} else {
182182
const parent = n.__polymerParent__;
183-
if (parent) {
184-
parent.insertBefore(n, n.__polymerSibling__ || null);
183+
const replace = n.__polymerReplaced__;
184+
if (parent && replace) {
185+
parent.replaceChild(n, n.__polymerReplaced__);
185186
}
186187
}
187-
} else if (n.style) {
188+
}
189+
190+
else if (n.style) {
188191
if (hide) {
189192
n.__polymerDisplay__ = n.style.display;
190193
n.style.display = 'none';

test/unit/dom-if-elements.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,11 @@
193193
<dom-module id="x-slot">
194194
<template>
195195
<template id="domIf" is="dom-if" if>
196-
<div>stuff</div>
197-
<slot name="one"></slot>
198-
<slot name="two"></slot>
196+
<div class="stuff">stuff</div>
197+
<slot id="one" name="one"></slot><template id="innerIf" is="dom-if" if restamp>hi</template>
198+
<slot id="two" name="two"></slot>
199199
{{text}}
200-
<slot name="three"></slot>
200+
<slot id="three" name="three"></slot>
201201
</template>
202202
</template>
203203
<script>

test/unit/dom-if.html

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -564,21 +564,31 @@
564564
document.body.appendChild(x);
565565
x.$.domIf.render();
566566
let stamped = x.shadowRoot.childNodes;
567-
assert.equal(stamped.length, 12);
567+
assert.equal(stamped.length, 14);
568568
assert.equal(stamped[4].assignedNodes()[0], one);
569-
assert.equal(stamped[6].assignedNodes()[0], two);
570-
assert.equal(stamped[8].assignedNodes()[0], three);
569+
assert.equal(stamped[8].assignedNodes()[0], two);
570+
assert.equal(stamped[10].assignedNodes()[0], three);
571571
x.$.domIf.if = false;
572572
x.$.domIf.render();
573573
stamped = x.shadowRoot.childNodes;
574-
assert.equal(stamped.length, 9);
574+
assert.equal(stamped.length, 14);
575575
x.$.domIf.if = true;
576576
x.$.domIf.render();
577577
stamped = x.shadowRoot.childNodes;
578-
assert.equal(stamped.length, 12);
578+
assert.equal(stamped.length, 14);
579+
assert.equal(stamped[4].assignedNodes()[0], one);
580+
assert.equal(stamped[8].assignedNodes()[0], two);
581+
assert.equal(stamped[10].assignedNodes()[0], three);
582+
x.$.domIf.if = false;
583+
x.$.domIf.render();
584+
const innerIf = x.shadowRoot.querySelector('#innerIf');
585+
innerIf.if = false;
586+
innerIf.render();
587+
x.$.domIf.if = true;
588+
x.$.domIf.render();
579589
assert.equal(stamped[4].assignedNodes()[0], one);
580-
assert.equal(stamped[6].assignedNodes()[0], two);
581-
assert.equal(stamped[8].assignedNodes()[0], three);
590+
assert.equal(stamped[7].assignedNodes()[0], two);
591+
assert.equal(stamped[9].assignedNodes()[0], three);
582592
document.body.removeChild(x);
583593
});
584594

0 commit comments

Comments
 (0)