Skip to content

Commit

Permalink
Fixes #2276: avoid losing logical information and simplify logical tr…
Browse files Browse the repository at this point in the history
…ee handling
  • Loading branch information
Steven Orvell committed Aug 18, 2015
1 parent 6619f6c commit ee61627
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 33 deletions.
46 changes: 19 additions & 27 deletions src/lib/dom-api.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,18 @@
// container to container.host.
// 3. node is <content> (host of container needs distribution)
appendChild: function(node) {
var handled;
this._removeNodeFromHost(node, true);
if (this._nodeIsInLogicalTree(this.node)) {
// if a <content> is added, make sure it's parent has logical info.
// if a <content> 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);
Expand All @@ -89,25 +88,24 @@
if (!ref_node) {
return this.appendChild(node);
}
var handled;
this._removeNodeFromHost(node, true);
if (this._nodeIsInLogicalTree(this.node)) {
// if a <content> is added, make sure it's parent has logical info.
// if a <content> 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) {
throw Error('The ref_node to be inserted before is not a child ' +
'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 <content> replace with first distributed node
ref_node = ref_node.localName === CONTENT ?
this._firstComposedNode(ref_node) : ref_node;
Expand All @@ -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.
Expand Down Expand Up @@ -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<c$.length) && (n=c$[i]); i++) {
this._ensureContentLogicalInfo(n);
}
Expand Down
18 changes: 12 additions & 6 deletions test/unit/polymer-dom-content.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,16 @@

<dom-module id="x-dynamic-content">
<template>
<div id="container">
<template is="dom-if" id="domif">
<content select=".insert" id="dynamicContent"></content>
</template>
<div>
<div>
<div>
<div id="container">
<template is="dom-if" id="domif">
<content select=".insert" id="dynamicContent"></content>
</template>
</div>
</div>
</div>
</div>
</template>
<script>
Expand Down Expand Up @@ -950,7 +956,7 @@
var div = document.createElement('div');
div.classList.add('insert');
Polymer.dom(el).appendChild(div);
assert(!el.querySelector('#container .insert'));
assert.ok(!el.querySelector('#container .insert'));
el.$.domif.if = true;
el.$.domif.render();
Polymer.dom.flush();
Expand Down Expand Up @@ -978,7 +984,7 @@
var div = document.createElement('div');
div.classList.add('insert');
Polymer.dom(el).appendChild(div);
assert(!el.querySelector('#redistContainer .insert'));
assert.ok(!el.querySelector('#redistContainer .insert'));
el.$.redistDomif.if = true;
el.$.redistContainer.$.domif.if = true;
el.$.redistDomif.render();
Expand Down
29 changes: 29 additions & 0 deletions test/unit/polymer-dom-elements.html
Original file line number Diff line number Diff line change
Expand Up @@ -299,4 +299,33 @@
<dom-module id="x-commented">
<template><span>[</span><!--comment--><content></content></span><span>]</span></content></template>
<script>Polymer({is: 'x-commented'});</script>
</dom-module>


<dom-module id="polymer-dom-repeat">
<template>
<div>
<div>
<div>
<div id="container">
<template is="dom-repeat" items="{{items}}">
<div>stuff</div>
</template>
</div>
</div>
</div>
</div>
</template>
<script>
Polymer({
is: 'polymer-dom-repeat',
properties: {
items: {
value: function() {
return ['a', 'b', 'c', 'd', 'e'];
}
}
}
});
</script>
</dom-module>
8 changes: 8 additions & 0 deletions test/unit/polymer-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
});
Expand Down

0 comments on commit ee61627

Please sign in to comment.