Skip to content

Commit 21500fb

Browse files
author
Steven Orvell
committed
Fixes #2235. Manages logical information in shady distribution more directly by capturing it explicitly when needed and not whenever distribution is run.
1 parent c4a9413 commit 21500fb

6 files changed

Lines changed: 58 additions & 10 deletions

File tree

‎src/lib/dom-api.html‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,14 @@
223223
},
224224

225225
_updateInsertionPoints: function(host) {
226-
host.shadyRoot._insertionPoints =
226+
var i$ = host.shadyRoot._insertionPoints =
227227
factory(host.shadyRoot).querySelectorAll(CONTENT);
228+
// ensure <content>'s and their parents have logical dom info.
229+
for (var i=0, c; i < i$.length; i++) {
230+
c = i$[i];
231+
saveLightChildrenIfNeeded(c);
232+
saveLightChildrenIfNeeded(factory(c).parentNode);
233+
}
228234
},
229235

230236
// a node is in a shadyRoot, is a shadyRoot,

‎src/mini/shady.html‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,19 @@
5353
this.shadyRoot._isShadyRoot = true;
5454
this.shadyRoot._dirtyRoots = [];
5555
// capture insertion point list
56-
this.shadyRoot._insertionPoints = !this._notes ||
56+
var i$ = this.shadyRoot._insertionPoints = !this._notes ||
5757
this._notes._hasContent ?
5858
this.shadyRoot.querySelectorAll('content') : [];
59-
// save logical tree info for shadyRoot.
59+
// save logical tree info
60+
// a. for shadyRoot
61+
// b. for insertion points (fallback)
62+
// c. for parents of insertion points
6063
saveLightChildrenIfNeeded(this.shadyRoot);
64+
for (var i=0, c; i < i$.length; i++) {
65+
c = i$[i];
66+
saveLightChildrenIfNeeded(c);
67+
saveLightChildrenIfNeeded(c.parentNode);
68+
}
6169
this.shadyRoot.host = this;
6270
},
6371

@@ -406,9 +414,6 @@
406414
}
407415
// remove child from its old parent first
408416
remove(newChild);
409-
// make sure we never lose logical DOM information:
410-
// if the parentNode doesn't have lightChildren, save that information now.
411-
saveLightChildrenIfNeeded(parentNode);
412417
// insert it into the real DOM
413418
nativeInsertBefore.call(parentNode, newChild, refChild || null);
414419
newChild._composedParent = parentNode;
@@ -417,9 +422,6 @@
417422
function remove(node) {
418423
var parentNode = getComposedParent(node);
419424
if (parentNode) {
420-
// make sure we never lose logical DOM information:
421-
// if the parentNode doesn't have lightChildren, save that information now.
422-
saveLightChildrenIfNeeded(parentNode);
423425
node._composedParent = null;
424426
// remove it from the real DOM
425427
nativeRemoveChild.call(parentNode, node);

‎test/unit/polymer-dom-shadow.html‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@
4747

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

50+
<div id="container">
51+
<x-echo></x-echo>
52+
<span>1</span>
53+
<span>2</span>
54+
</div>
55+
5056
<script src="polymer-dom.js"></script>
5157

5258
</body>

‎test/unit/polymer-dom.html‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@
4747

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

50+
<div id="container">
51+
<x-echo></x-echo>
52+
<span>1</span>
53+
<span>2</span>
54+
</div>
55+
5056
<script src="polymer-dom.js"></script>
5157

5258
</body>

‎test/unit/polymer-dom.js‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,34 @@ suite('Polymer.dom', function() {
424424
assert.equal(Polymer.dom(rere.root).querySelectorAll('span').length, 0);
425425
});
426426

427+
test('appendChild interacts with unmanaged parent tree', function() {
428+
var container = document.querySelector('#container');
429+
var echo = Polymer.dom(container).firstElementChild;
430+
assert.equal(echo.localName, 'x-echo');
431+
var s1 = Polymer.dom(echo).nextElementSibling;
432+
assert.equal(s1.textContent, '1');
433+
var s2 = Polymer.dom(s1).nextElementSibling;
434+
assert.equal(s2.textContent, '2');
435+
assert.equal(Polymer.dom(container).children.length, 3);
436+
Polymer.dom(echo).appendChild(s1);
437+
Polymer.dom.flush();
438+
assert.equal(Polymer.dom(container).children.length, 2);
439+
assert.equal(Polymer.dom(echo).nextElementSibling, s2);
440+
Polymer.dom(echo).appendChild(s2);
441+
Polymer.dom.flush();
442+
assert.equal(Polymer.dom(container).children.length, 1);
443+
assert.equal(Polymer.dom(echo).nextElementSibling, null);
444+
Polymer.dom(container).appendChild(s1);
445+
Polymer.dom.flush();
446+
assert.equal(Polymer.dom(container).children.length, 2);
447+
assert.equal(Polymer.dom(echo).nextElementSibling, s1);
448+
Polymer.dom(container).appendChild(s2);
449+
Polymer.dom.flush();
450+
assert.equal(Polymer.dom(container).children.length, 3);
451+
assert.equal(Polymer.dom(echo).nextElementSibling, s1);
452+
assert.equal(Polymer.dom(s1).nextElementSibling, s2);
453+
});
454+
427455
test('distribute (forced)', function() {
428456
var rere = Polymer.dom(testElement.root).querySelector('x-rereproject');
429457
var re = Polymer.dom(rere.root).querySelector('x-reproject');

‎test/unit/shady.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@
473473
}
474474

475475
function updateRootInsertionPoints(root) {
476-
root._insertionPoints = root.querySelectorAll('content');
476+
Polymer.dom(root.host)._updateInsertionPoints(root.host);
477477
}
478478

479479
function getComposedHTML(node) {

0 commit comments

Comments
 (0)