From ee616271e518905f88abe8323945277efe2cd072 Mon Sep 17 00:00:00 2001 From: Steven Orvell Date: Tue, 18 Aug 2015 12:10:42 -0700 Subject: [PATCH] Fixes #2276: avoid losing logical information and simplify logical tree handling --- src/lib/dom-api.html | 46 ++++++++++++----------------- test/unit/polymer-dom-content.html | 18 +++++++---- test/unit/polymer-dom-elements.html | 29 ++++++++++++++++++ test/unit/polymer-dom.js | 8 +++++ 4 files changed, 68 insertions(+), 33 deletions(-) diff --git a/src/lib/dom-api.html b/src/lib/dom-api.html index b2a4b53429..66f7263a3b 100644 --- a/src/lib/dom-api.html +++ b/src/lib/dom-api.html @@ -64,19 +64,18 @@ // container to container.host. // 3. node is (host of container needs distribution) appendChild: function(node) { - var handled; this._removeNodeFromHost(node, true); - if (this._nodeIsInLogicalTree(this.node)) { - // if a is added, make sure it's parent has logical info. + // if a is added, make sure it's parent has logical info. + if (this.getOwnerRoot()) { this._ensureContentLogicalInfo(node); + } + if (this._nodeIsInLogicalTree(this.node)) { this._addLogicalInfo(node, this.node); - this._addNodeToHost(node); - handled = this._maybeDistribute(node, this.node); - } else { - this._addNodeToHost(node); } + this._addNodeToHost(node); // if not distributing and not adding to host, do a fast path addition - if (!handled && !this._tryRemoveUndistributedNode(node)) { + if (!this._maybeDistribute(node, this.node) && + !this._tryRemoveUndistributedNode(node)) { // if adding to a shadyRoot, add to host instead var container = this.node._isShadyRoot ? this.node.host : this.node; addToComposedParent(container, node); @@ -89,11 +88,12 @@ if (!ref_node) { return this.appendChild(node); } - var handled; this._removeNodeFromHost(node, true); - if (this._nodeIsInLogicalTree(this.node)) { - // if a is added, make sure it's parent has logical info. + // if a is added, make sure it's parent has logical info. + if (this.getOwnerRoot()) { this._ensureContentLogicalInfo(node); + } + if (this._nodeIsInLogicalTree(this.node)) { var children = this.childNodes; var index = children.indexOf(ref_node); if (index < 0) { @@ -101,13 +101,11 @@ 'of this node'); } this._addLogicalInfo(node, this.node, index); - this._addNodeToHost(node); - handled = this._maybeDistribute(node, this.node); - } else { - this._addNodeToHost(node); } + this._addNodeToHost(node); // if not distributing and not adding to host, do a fast path addition - if (!handled && !this._tryRemoveUndistributedNode(node)) { + if (!this._maybeDistribute(node, this.node) && + !this._tryRemoveUndistributedNode(node)) { // if ref_node is replace with first distributed node ref_node = ref_node.localName === CONTENT ? this._firstComposedNode(ref_node) : ref_node; @@ -128,14 +126,8 @@ console.warn('The node to be removed is not a child of this node', node); } - var handled; - if (this._nodeIsInLogicalTree(this.node)) { - this._removeNodeFromHost(node); - handled = this._maybeDistribute(node, this.node); - } else { - this._removeNodeFromHost(node); - } - if (!handled) { + this._removeNodeFromHost(node); + if (!this._maybeDistribute(node, this.node)) { // if removing from a shadyRoot, remove form host instead var container = this.node._isShadyRoot ? this.node.host : this.node; // not guaranteed to physically be in container; e.g. @@ -249,9 +241,9 @@ }, _ensureContentLogicalInfo: function(node) { - if (node.nodeType === Node.DOCUMENT_FRAGMENT_NODE) { - saveLightChildrenIfNeeded(this.node); - var c$ = Array.prototype.slice.call(node.childNodes); + if (node.nodeType === Node.DOCUMENT_FRAGMENT_NODE && + !node.__noContent) { + var c$ = factory(node).querySelectorAll(CONTENT); for (var i=0, n; (i + + + + + + \ No newline at end of file diff --git a/test/unit/polymer-dom.js b/test/unit/polymer-dom.js index 79551aa04a..18873c8b70 100644 --- a/test/unit/polymer-dom.js +++ b/test/unit/polymer-dom.js @@ -53,6 +53,14 @@ suite('Polymer.dom', function() { assert(Polymer.dom(p).querySelectorAll('content').length, 1); }); + test('querySelectorAll with dom-repeat', function() { + var el = document.createElement('polymer-dom-repeat'); + document.body.appendChild(el); + Polymer.dom.flush(); + assert.equal(Polymer.dom(el.$.container).querySelectorAll('*').length, 6, 'querySelectorAll finds repeated elements'); + document.body.removeChild(el); + }) + test('querySelector document', function() { assert.ok(Polymer.dom().querySelector('body')); });