Skip to content

Commit

Permalink
Fixes #1565, $1584
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Orvell committed May 22, 2015
1 parent 99f23e6 commit 6b03111
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
23 changes: 19 additions & 4 deletions src/lib/dom-api.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@
distributed = this._maybeDistribute(node, this.node, host);
}
}
if (!distributed) {
// if not distributing and not adding to host, do a fast path addition
if (!distributed && !this._tryRemoveUndistributedNode(node)) {
// if adding to a shadyRoot, add to host instead
var container = this.node._isShadyRoot ? this.node.host : this.node;
nativeAppendChild.call(container, node);
Expand Down Expand Up @@ -98,7 +99,8 @@
distributed = this._maybeDistribute(node, this.node, host);
}
}
if (!distributed) {
// if not distributing and not adding to host, do a fast path addition
if (!distributed && !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 @@ -124,8 +126,12 @@
if (!distributed) {
// if removing from a shadyRoot, remove form host instead
var container = this.node._isShadyRoot ? this.node.host : this.node;
nativeRemoveChild.call(container, node);
removeFromComposedParent(container, node);
// not guaranteed to physically be in container; e.g.
// undistributed nodes.
if (container === node.parentNode) {
nativeRemoveChild.call(container, node);
removeFromComposedParent(container, node);
}
}
return node;
},
Expand Down Expand Up @@ -176,6 +182,15 @@
return distribute;
},

_tryRemoveUndistributedNode: function(node) {
if (this.node.shadyRoot) {
if (node.parentNode) {
nativeRemoveChild.call(node.parentNode, node);
}
return true;
}
},

_updateInsertionPoints: function(host) {
host.shadyRoot._insertionPoints =
factory(host.shadyRoot).querySelectorAll(CONTENT);
Expand Down
8 changes: 8 additions & 0 deletions src/lib/template/dom-if.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@
// TODO(kschaaf): add logic to re-stamp in attached?
this._teardownInstance();
},

attached: function() {
if (this.if && this.ctor) {
// TODO(sorvell): should not be async, but node can be attached
// when shady dom is in the act of distributing/composing so push it out
this.async(this._ensureInstance);
}
},

render: function() {
this._flushTemplates();
Expand Down
2 changes: 1 addition & 1 deletion test/unit/polymer-dom-elements.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

<dom-module id="x-test-no-distribute">
<template>
<content select=".foo"></content>
<span>Local dom without insertion point.</span>
</template>
</dom-module>
<script>
Expand Down
8 changes: 8 additions & 0 deletions test/unit/polymer-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,14 @@ suite('Polymer.dom non-distributed elements', function() {
Polymer.dom(nd).appendChild(b);
Polymer.dom.flush();
assert.equal(Polymer.dom(nd).children.length, 2, 'children length not incremented due to element addition');
var d = document.createElement('div');
d.innerHTML = 'added';
Polymer.dom(nd).insertBefore(d, b);
Polymer.dom.flush();
assert.equal(Polymer.dom(nd).children.length, 3, 'children length not incremented due to element addition');
Polymer.dom(nd).removeChild(d);
Polymer.dom.flush();
assert.equal(Polymer.dom(nd).children.length, 2, 'children length not decremented due to element removal');
});

test('Polymer.dom removes/adds between light and local dom', function() {
Expand Down

0 comments on commit 6b03111

Please sign in to comment.