Skip to content
This repository was archived by the owner on Mar 13, 2018. It is now read-only.

Commit 76028bf

Browse files
committed
Merge pull request #356 from arv/dom-invalidation
Fix issue where the DOM tree could get out of sync.
2 parents 2864330 + 8eded52 commit 76028bf

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/wrappers/Node.js

+16
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,18 @@
184184
return df;
185185
}
186186

187+
function clearChildNodes(wrapper) {
188+
if (wrapper.firstChild_ !== undefined) {
189+
var child = wrapper.firstChild_;
190+
while (child) {
191+
var tmp = child;
192+
child = child.nextSibling_;
193+
tmp.parentNode_ = tmp.previousSibling_ = tmp.nextSibling_ = undefined;
194+
}
195+
}
196+
wrapper.firstChild_ = wrapper.lastChild_ = undefined;
197+
}
198+
187199
function removeAllChildNodes(wrapper) {
188200
if (wrapper.invalidateShadowRenderer()) {
189201
var childWrapper = wrapper.firstChild;
@@ -325,6 +337,7 @@
325337

326338
if (useNative) {
327339
ensureSameOwnerDocument(this, childWrapper);
340+
clearChildNodes(this);
328341
originalInsertBefore.call(this.impl, unwrap(childWrapper), refNode);
329342
} else {
330343
if (!previousNode)
@@ -402,6 +415,7 @@
402415
childWrapper.previousSibling_ = childWrapper.nextSibling_ =
403416
childWrapper.parentNode_ = undefined;
404417
} else {
418+
clearChildNodes(this);
405419
removeChildOriginalHelper(this.impl, childNode);
406420
}
407421

@@ -467,6 +481,7 @@
467481
}
468482
} else {
469483
ensureSameOwnerDocument(this, newChildWrapper);
484+
clearChildNodes(this);
470485
originalReplaceChild.call(this.impl, unwrap(newChildWrapper),
471486
oldChildNode);
472487
}
@@ -559,6 +574,7 @@
559574
this.appendChild(textNode);
560575
}
561576
} else {
577+
clearChildNodes(this);
562578
this.impl.textContent = textContent;
563579
}
564580

test/js/test.js

+24
Original file line numberDiff line numberDiff line change
@@ -491,4 +491,28 @@ suite('Shadow DOM', function() {
491491
assert.equal(count, 0);
492492
});
493493
*/
494+
495+
test('moving nodes from light to shadow - issue 48', function() {
496+
var div = document.createElement('div');
497+
div.innerHTML = '<a></a><b></b>';
498+
var a = div.firstChild;
499+
var b = div.lastChild;
500+
501+
var sr = div.createShadowRoot();
502+
sr.innerHTML = '<c></c>';
503+
var c = sr.firstChild;
504+
505+
assert.equal(getVisualInnerHtml(div), '<c></c>');
506+
507+
c.appendChild(a);
508+
assert.equal(getVisualInnerHtml(div), '<c><a></a></c>');
509+
510+
c.textContent = '';
511+
assert.equal(getVisualInnerHtml(div), '<c></c>');
512+
513+
c.appendChild(b);
514+
assert.equal(getVisualInnerHtml(div), '<c><b></b></c>');
515+
516+
});
517+
494518
});

0 commit comments

Comments
 (0)