Skip to content

Commit

Permalink
Fixes #2235. Manages logical information in shady distribution more d…
Browse files Browse the repository at this point in the history
…irectly by capturing it explicitly when needed and not whenever distribution is run.
  • Loading branch information
Steven Orvell committed Aug 10, 2015
1 parent c4a9413 commit 21500fb
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 10 deletions.
8 changes: 7 additions & 1 deletion src/lib/dom-api.html
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,14 @@
},

_updateInsertionPoints: function(host) {
host.shadyRoot._insertionPoints =
var i$ = host.shadyRoot._insertionPoints =
factory(host.shadyRoot).querySelectorAll(CONTENT);
// ensure <content>'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,
Expand Down
18 changes: 10 additions & 8 deletions src/mini/shady.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,19 @@
this.shadyRoot._isShadyRoot = true;
this.shadyRoot._dirtyRoots = [];
// capture insertion point list
this.shadyRoot._insertionPoints = !this._notes ||
var i$ = this.shadyRoot._insertionPoints = !this._notes ||
this._notes._hasContent ?
this.shadyRoot.querySelectorAll('content') : [];
// save logical tree info for shadyRoot.
// save logical tree info
// a. for shadyRoot
// b. for insertion points (fallback)
// c. for parents of insertion points
saveLightChildrenIfNeeded(this.shadyRoot);
for (var i=0, c; i < i$.length; i++) {
c = i$[i];
saveLightChildrenIfNeeded(c);
saveLightChildrenIfNeeded(c.parentNode);
}
this.shadyRoot.host = this;
},

Expand Down Expand Up @@ -406,9 +414,6 @@
}
// remove child from its old parent first
remove(newChild);
// make sure we never lose logical DOM information:
// if the parentNode doesn't have lightChildren, save that information now.
saveLightChildrenIfNeeded(parentNode);
// insert it into the real DOM
nativeInsertBefore.call(parentNode, newChild, refChild || null);
newChild._composedParent = parentNode;
Expand All @@ -417,9 +422,6 @@
function remove(node) {
var parentNode = getComposedParent(node);
if (parentNode) {
// make sure we never lose logical DOM information:
// if the parentNode doesn't have lightChildren, save that information now.
saveLightChildrenIfNeeded(parentNode);
node._composedParent = null;
// remove it from the real DOM
nativeRemoveChild.call(parentNode, node);
Expand Down
6 changes: 6 additions & 0 deletions test/unit/polymer-dom-shadow.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@

<x-redistribute-a-b></x-redistribute-a-b>

<div id="container">
<x-echo></x-echo>
<span>1</span>
<span>2</span>
</div>

<script src="polymer-dom.js"></script>

</body>
Expand Down
6 changes: 6 additions & 0 deletions test/unit/polymer-dom.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@

<x-redistribute-a-b></x-redistribute-a-b>

<div id="container">
<x-echo></x-echo>
<span>1</span>
<span>2</span>
</div>

<script src="polymer-dom.js"></script>

</body>
Expand Down
28 changes: 28 additions & 0 deletions test/unit/polymer-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion test/unit/shady.html
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@
}

function updateRootInsertionPoints(root) {
root._insertionPoints = root.querySelectorAll('content');
Polymer.dom(root.host)._updateInsertionPoints(root.host);
}

function getComposedHTML(node) {
Expand Down

0 comments on commit 21500fb

Please sign in to comment.