From 8eded5286c819c9ca9e7b84ebd7216595ead1c15 Mon Sep 17 00:00:00 2001 From: Erik Arvidsson Date: Thu, 16 Jan 2014 18:28:10 -0500 Subject: [PATCH] Fix issue where the DOM tree could get out of sync. The problem occurs when a node that requires invalidation is moved into anohter node that does not require invalidation. The internal pointers are no updated as expected. Fixes https://github.com/Polymer/platform/issues/48 --- src/wrappers/Node.js | 16 ++++++++++++++++ test/js/test.js | 24 ++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/wrappers/Node.js b/src/wrappers/Node.js index fac1b17..2766aa2 100644 --- a/src/wrappers/Node.js +++ b/src/wrappers/Node.js @@ -184,6 +184,18 @@ return df; } + function clearChildNodes(wrapper) { + if (wrapper.firstChild_ !== undefined) { + var child = wrapper.firstChild_; + while (child) { + var tmp = child; + child = child.nextSibling_; + tmp.parentNode_ = tmp.previousSibling_ = tmp.nextSibling_ = undefined; + } + } + wrapper.firstChild_ = wrapper.lastChild_ = undefined; + } + function removeAllChildNodes(wrapper) { if (wrapper.invalidateShadowRenderer()) { var childWrapper = wrapper.firstChild; @@ -325,6 +337,7 @@ if (useNative) { ensureSameOwnerDocument(this, childWrapper); + clearChildNodes(this); originalInsertBefore.call(this.impl, unwrap(childWrapper), refNode); } else { if (!previousNode) @@ -402,6 +415,7 @@ childWrapper.previousSibling_ = childWrapper.nextSibling_ = childWrapper.parentNode_ = undefined; } else { + clearChildNodes(this); removeChildOriginalHelper(this.impl, childNode); } @@ -467,6 +481,7 @@ } } else { ensureSameOwnerDocument(this, newChildWrapper); + clearChildNodes(this); originalReplaceChild.call(this.impl, unwrap(newChildWrapper), oldChildNode); } @@ -559,6 +574,7 @@ this.appendChild(textNode); } } else { + clearChildNodes(this); this.impl.textContent = textContent; } diff --git a/test/js/test.js b/test/js/test.js index f8ebaf8..317a1ff 100644 --- a/test/js/test.js +++ b/test/js/test.js @@ -491,4 +491,28 @@ suite('Shadow DOM', function() { assert.equal(count, 0); }); */ + + test('moving nodes from light to shadow - issue 48', function() { + var div = document.createElement('div'); + div.innerHTML = ''; + var a = div.firstChild; + var b = div.lastChild; + + var sr = div.createShadowRoot(); + sr.innerHTML = ''; + var c = sr.firstChild; + + assert.equal(getVisualInnerHtml(div), ''); + + c.appendChild(a); + assert.equal(getVisualInnerHtml(div), ''); + + c.textContent = ''; + assert.equal(getVisualInnerHtml(div), ''); + + c.appendChild(b); + assert.equal(getVisualInnerHtml(div), ''); + + }); + }); \ No newline at end of file