diff --git a/src/lib/dom-api.html b/src/lib/dom-api.html index b18e69607c..929c1ac912 100644 --- a/src/lib/dom-api.html +++ b/src/lib/dom-api.html @@ -65,11 +65,15 @@ // 3. node is (host of container needs distribution) appendChild: function(node) { var handled; + // if a is added, make sure it's parent has logical info. + this._ensureContentLogicalInfo(node); this._removeNodeFromHost(node, true); if (this._nodeIsInLogicalTree(this.node)) { this._addLogicalInfo(node, this.node); this._addNodeToHost(node); handled = this._maybeDistribute(node, this.node); + } else { + this._addNodeToHost(node); } // if not distributing and not adding to host, do a fast path addition if (!handled && !this._tryRemoveUndistributedNode(node)) { @@ -86,9 +90,10 @@ return this.appendChild(node); } var handled; + // if a is added, make sure it's parent has logical info. + this._ensureContentLogicalInfo(node); this._removeNodeFromHost(node, true); if (this._nodeIsInLogicalTree(this.node)) { - saveLightChildrenIfNeeded(this.node); var children = this.childNodes; var index = children.indexOf(ref_node); if (index < 0) { @@ -98,6 +103,8 @@ this._addLogicalInfo(node, this.node, index); this._addNodeToHost(node); handled = this._maybeDistribute(node, this.node); + } else { + this._addNodeToHost(node); } // if not distributing and not adding to host, do a fast path addition if (!handled && !this._tryRemoveUndistributedNode(node)) { @@ -125,6 +132,8 @@ if (this._nodeIsInLogicalTree(this.node)) { this._removeNodeFromHost(node); handled = this._maybeDistribute(node, this.node); + } else { + this._removeNodeFromHost(node); } if (!handled) { // if removing from a shadyRoot, remove form host instead @@ -223,18 +232,37 @@ }, _updateInsertionPoints: function(host) { - host.shadyRoot._insertionPoints = + var i$ = host.shadyRoot._insertionPoints = factory(host.shadyRoot).querySelectorAll(CONTENT); + // ensure 's and their parents have logical dom info. + for (var i=0, c; i < i$.length; i++) { + c = i$[i]; + saveLightChildrenIfNeeded(c); + saveLightChildrenIfNeeded(factory(c).parentNode); + } }, // a node is in a shadyRoot, is a shadyRoot, // or has a lightParent _nodeIsInLogicalTree: function(node) { return Boolean((node._lightParent !== undefined) || node._isShadyRoot || - this._ownerShadyRootForNode(node) || node.shadyRoot); }, + _ensureContentLogicalInfo: function(node) { + if (node.nodeType === Node.DOCUMENT_FRAGMENT_NODE) { + saveLightChildrenIfNeeded(this.node); + var c$ = Array.prototype.slice.call(node.childNodes); + for (var i=0, n; (i +
+ + 1 + 2 +
+ diff --git a/test/unit/polymer-dom.html b/test/unit/polymer-dom.html index 6b8240d4ff..16671e5a7d 100644 --- a/test/unit/polymer-dom.html +++ b/test/unit/polymer-dom.html @@ -47,6 +47,12 @@ +
+ + 1 + 2 +
+ diff --git a/test/unit/polymer-dom.js b/test/unit/polymer-dom.js index 5ba436b50d..9d26612fa9 100644 --- a/test/unit/polymer-dom.js +++ b/test/unit/polymer-dom.js @@ -424,6 +424,34 @@ suite('Polymer.dom', function() { assert.equal(Polymer.dom(rere.root).querySelectorAll('span').length, 0); }); + test('appendChild interacts with unmanaged parent tree', function() { + var container = document.querySelector('#container'); + var echo = Polymer.dom(container).firstElementChild; + assert.equal(echo.localName, 'x-echo'); + var s1 = Polymer.dom(echo).nextElementSibling; + assert.equal(s1.textContent, '1'); + var s2 = Polymer.dom(s1).nextElementSibling; + assert.equal(s2.textContent, '2'); + assert.equal(Polymer.dom(container).children.length, 3); + Polymer.dom(echo).appendChild(s1); + Polymer.dom.flush(); + assert.equal(Polymer.dom(container).children.length, 2); + assert.equal(Polymer.dom(echo).nextElementSibling, s2); + Polymer.dom(echo).appendChild(s2); + Polymer.dom.flush(); + assert.equal(Polymer.dom(container).children.length, 1); + assert.equal(Polymer.dom(echo).nextElementSibling, null); + Polymer.dom(container).appendChild(s1); + Polymer.dom.flush(); + assert.equal(Polymer.dom(container).children.length, 2); + assert.equal(Polymer.dom(echo).nextElementSibling, s1); + Polymer.dom(container).appendChild(s2); + Polymer.dom.flush(); + assert.equal(Polymer.dom(container).children.length, 3); + assert.equal(Polymer.dom(echo).nextElementSibling, s1); + assert.equal(Polymer.dom(s1).nextElementSibling, s2); + }); + test('distribute (forced)', function() { var rere = Polymer.dom(testElement.root).querySelector('x-rereproject'); var re = Polymer.dom(rere.root).querySelector('x-reproject'); diff --git a/test/unit/shady.html b/test/unit/shady.html index 9b3538f1f4..7bd9f45118 100644 --- a/test/unit/shady.html +++ b/test/unit/shady.html @@ -473,7 +473,7 @@ } function updateRootInsertionPoints(root) { - root._insertionPoints = root.querySelectorAll('content'); + Polymer.dom(root.host)._updateInsertionPoints(root.host); } function getComposedHTML(node) {