Skip to content

Commit ee61627

Browse files
author
Steven Orvell
committed
Fixes #2276: avoid losing logical information and simplify logical tree handling
1 parent 6619f6c commit ee61627

File tree

4 files changed

+68
-33
lines changed

4 files changed

+68
-33
lines changed

src/lib/dom-api.html

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,18 @@
6464
// container to container.host.
6565
// 3. node is <content> (host of container needs distribution)
6666
appendChild: function(node) {
67-
var handled;
6867
this._removeNodeFromHost(node, true);
69-
if (this._nodeIsInLogicalTree(this.node)) {
70-
// if a <content> is added, make sure it's parent has logical info.
68+
// if a <content> is added, make sure it's parent has logical info.
69+
if (this.getOwnerRoot()) {
7170
this._ensureContentLogicalInfo(node);
71+
}
72+
if (this._nodeIsInLogicalTree(this.node)) {
7273
this._addLogicalInfo(node, this.node);
73-
this._addNodeToHost(node);
74-
handled = this._maybeDistribute(node, this.node);
75-
} else {
76-
this._addNodeToHost(node);
7774
}
75+
this._addNodeToHost(node);
7876
// if not distributing and not adding to host, do a fast path addition
79-
if (!handled && !this._tryRemoveUndistributedNode(node)) {
77+
if (!this._maybeDistribute(node, this.node) &&
78+
!this._tryRemoveUndistributedNode(node)) {
8079
// if adding to a shadyRoot, add to host instead
8180
var container = this.node._isShadyRoot ? this.node.host : this.node;
8281
addToComposedParent(container, node);
@@ -89,25 +88,24 @@
8988
if (!ref_node) {
9089
return this.appendChild(node);
9190
}
92-
var handled;
9391
this._removeNodeFromHost(node, true);
94-
if (this._nodeIsInLogicalTree(this.node)) {
95-
// if a <content> is added, make sure it's parent has logical info.
92+
// if a <content> is added, make sure it's parent has logical info.
93+
if (this.getOwnerRoot()) {
9694
this._ensureContentLogicalInfo(node);
95+
}
96+
if (this._nodeIsInLogicalTree(this.node)) {
9797
var children = this.childNodes;
9898
var index = children.indexOf(ref_node);
9999
if (index < 0) {
100100
throw Error('The ref_node to be inserted before is not a child ' +
101101
'of this node');
102102
}
103103
this._addLogicalInfo(node, this.node, index);
104-
this._addNodeToHost(node);
105-
handled = this._maybeDistribute(node, this.node);
106-
} else {
107-
this._addNodeToHost(node);
108104
}
105+
this._addNodeToHost(node);
109106
// if not distributing and not adding to host, do a fast path addition
110-
if (!handled && !this._tryRemoveUndistributedNode(node)) {
107+
if (!this._maybeDistribute(node, this.node) &&
108+
!this._tryRemoveUndistributedNode(node)) {
111109
// if ref_node is <content> replace with first distributed node
112110
ref_node = ref_node.localName === CONTENT ?
113111
this._firstComposedNode(ref_node) : ref_node;
@@ -128,14 +126,8 @@
128126
console.warn('The node to be removed is not a child of this node',
129127
node);
130128
}
131-
var handled;
132-
if (this._nodeIsInLogicalTree(this.node)) {
133-
this._removeNodeFromHost(node);
134-
handled = this._maybeDistribute(node, this.node);
135-
} else {
136-
this._removeNodeFromHost(node);
137-
}
138-
if (!handled) {
129+
this._removeNodeFromHost(node);
130+
if (!this._maybeDistribute(node, this.node)) {
139131
// if removing from a shadyRoot, remove form host instead
140132
var container = this.node._isShadyRoot ? this.node.host : this.node;
141133
// not guaranteed to physically be in container; e.g.
@@ -249,9 +241,9 @@
249241
},
250242

251243
_ensureContentLogicalInfo: function(node) {
252-
if (node.nodeType === Node.DOCUMENT_FRAGMENT_NODE) {
253-
saveLightChildrenIfNeeded(this.node);
254-
var c$ = Array.prototype.slice.call(node.childNodes);
244+
if (node.nodeType === Node.DOCUMENT_FRAGMENT_NODE &&
245+
!node.__noContent) {
246+
var c$ = factory(node).querySelectorAll(CONTENT);
255247
for (var i=0, n; (i<c$.length) && (n=c$[i]); i++) {
256248
this._ensureContentLogicalInfo(n);
257249
}

test/unit/polymer-dom-content.html

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,16 @@
6262

6363
<dom-module id="x-dynamic-content">
6464
<template>
65-
<div id="container">
66-
<template is="dom-if" id="domif">
67-
<content select=".insert" id="dynamicContent"></content>
68-
</template>
65+
<div>
66+
<div>
67+
<div>
68+
<div id="container">
69+
<template is="dom-if" id="domif">
70+
<content select=".insert" id="dynamicContent"></content>
71+
</template>
72+
</div>
73+
</div>
74+
</div>
6975
</div>
7076
</template>
7177
<script>
@@ -950,7 +956,7 @@
950956
var div = document.createElement('div');
951957
div.classList.add('insert');
952958
Polymer.dom(el).appendChild(div);
953-
assert(!el.querySelector('#container .insert'));
959+
assert.ok(!el.querySelector('#container .insert'));
954960
el.$.domif.if = true;
955961
el.$.domif.render();
956962
Polymer.dom.flush();
@@ -978,7 +984,7 @@
978984
var div = document.createElement('div');
979985
div.classList.add('insert');
980986
Polymer.dom(el).appendChild(div);
981-
assert(!el.querySelector('#redistContainer .insert'));
987+
assert.ok(!el.querySelector('#redistContainer .insert'));
982988
el.$.redistDomif.if = true;
983989
el.$.redistContainer.$.domif.if = true;
984990
el.$.redistDomif.render();

test/unit/polymer-dom-elements.html

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,4 +299,33 @@
299299
<dom-module id="x-commented">
300300
<template><span>[</span><!--comment--><content></content></span><span>]</span></content></template>
301301
<script>Polymer({is: 'x-commented'});</script>
302+
</dom-module>
303+
304+
305+
<dom-module id="polymer-dom-repeat">
306+
<template>
307+
<div>
308+
<div>
309+
<div>
310+
<div id="container">
311+
<template is="dom-repeat" items="{{items}}">
312+
<div>stuff</div>
313+
</template>
314+
</div>
315+
</div>
316+
</div>
317+
</div>
318+
</template>
319+
<script>
320+
Polymer({
321+
is: 'polymer-dom-repeat',
322+
properties: {
323+
items: {
324+
value: function() {
325+
return ['a', 'b', 'c', 'd', 'e'];
326+
}
327+
}
328+
}
329+
});
330+
</script>
302331
</dom-module>

test/unit/polymer-dom.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ suite('Polymer.dom', function() {
5353
assert(Polymer.dom(p).querySelectorAll('content').length, 1);
5454
});
5555

56+
test('querySelectorAll with dom-repeat', function() {
57+
var el = document.createElement('polymer-dom-repeat');
58+
document.body.appendChild(el);
59+
Polymer.dom.flush();
60+
assert.equal(Polymer.dom(el.$.container).querySelectorAll('*').length, 6, 'querySelectorAll finds repeated elements');
61+
document.body.removeChild(el);
62+
})
63+
5664
test('querySelector document', function() {
5765
assert.ok(Polymer.dom().querySelector('body'));
5866
});

0 commit comments

Comments
 (0)