Skip to content
This repository was archived by the owner on Mar 13, 2018. It is now read-only.

Commit 36fe41a

Browse files
committed
Make sure we call nodeWasAdded_ on all added nodes
1 parent 941a863 commit 36fe41a

2 files changed

Lines changed: 46 additions & 8 deletions

File tree

src/wrappers/Node.js

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* This updates the internal pointers for node, previousNode and nextNode.
2727
*/
2828
function collectNodes(node, parentNode, previousNode, nextNode) {
29-
if (node.nodeType !== Node.DOCUMENT_FRAGMENT_NODE) {
29+
if (!(node instanceof DocumentFragment)) {
3030
if (node.parentNode)
3131
node.parentNode.removeChild(node);
3232
node.parentNode_ = parentNode;
@@ -60,6 +60,24 @@
6060
return nodes;
6161
}
6262

63+
function collectNodesNoNeedToUpdatePointers(node) {
64+
if (node instanceof DocumentFragment) {
65+
var nodes = [];
66+
var i = 0;
67+
for (var child = node.firstChild; child; child = child.nextSibling) {
68+
nodes[i++] = child;
69+
}
70+
return nodes;
71+
}
72+
return [node];
73+
}
74+
75+
function nodesWereAdded(nodes) {
76+
for (var i = 0; i < nodes.length; i++) {
77+
nodes[i].nodeWasAdded_();
78+
}
79+
}
80+
6381
function ensureSameOwnerDocument(parent, child) {
6482
var ownerDoc = parent.nodeType === Node.DOCUMENT_NODE ?
6583
parent : parent.ownerDocument;
@@ -188,22 +206,25 @@
188206
appendChild: function(childWrapper) {
189207
assertIsNodeWrapper(childWrapper);
190208

209+
var nodes;
210+
191211
if (this.invalidateShadowRenderer() || invalidateParent(childWrapper)) {
192212
var previousNode = this.lastChild;
193213
var nextNode = null;
194-
var nodes = collectNodes(childWrapper, this, previousNode, nextNode);
214+
nodes = collectNodes(childWrapper, this, previousNode, nextNode);
195215

196216
this.lastChild_ = nodes[nodes.length - 1];
197217
if (!previousNode)
198218
this.firstChild_ = nodes[0];
199219

200220
originalAppendChild.call(this.impl, unwrapNodesForInsertion(this, nodes));
201221
} else {
222+
nodes = collectNodesNoNeedToUpdatePointers(childWrapper)
202223
ensureSameOwnerDocument(this, childWrapper);
203224
originalAppendChild.call(this.impl, unwrap(childWrapper));
204225
}
205226

206-
childWrapper.nodeWasAdded_();
227+
nodesWereAdded(nodes);
207228

208229
return childWrapper;
209230
},
@@ -217,10 +238,12 @@
217238
assertIsNodeWrapper(refWrapper);
218239
assert(refWrapper.parentNode === this);
219240

241+
var nodes;
242+
220243
if (this.invalidateShadowRenderer() || invalidateParent(childWrapper)) {
221244
var previousNode = refWrapper.previousSibling;
222245
var nextNode = refWrapper;
223-
var nodes = collectNodes(childWrapper, this, previousNode, nextNode);
246+
nodes = collectNodes(childWrapper, this, previousNode, nextNode);
224247

225248
if (this.firstChild === refWrapper)
226249
this.firstChild_ = nodes[0];
@@ -238,12 +261,13 @@
238261
adoptNodesIfNeeded(this, nodes);
239262
}
240263
} else {
264+
nodes = collectNodesNoNeedToUpdatePointers(childWrapper);
241265
ensureSameOwnerDocument(this, childWrapper);
242266
originalInsertBefore.call(this.impl, unwrap(childWrapper),
243267
unwrap(refWrapper));
244268
}
245269

246-
childWrapper.nodeWasAdded_();
270+
nodesWereAdded(nodes);
247271

248272
return childWrapper;
249273
},
@@ -300,15 +324,15 @@
300324
}
301325

302326
var oldChildNode = unwrap(oldChildWrapper);
327+
var nodes;
303328

304329
if (this.invalidateShadowRenderer() ||
305330
invalidateParent(newChildWrapper)) {
306331
var previousNode = oldChildWrapper.previousSibling;
307332
var nextNode = oldChildWrapper.nextSibling;
308333
if (nextNode === newChildWrapper)
309334
nextNode = newChildWrapper.nextSibling;
310-
var nodes = collectNodes(newChildWrapper, this,
311-
previousNode, nextNode);
335+
nodes = collectNodes(newChildWrapper, this, previousNode, nextNode);
312336

313337
if (this.firstChild === oldChildWrapper)
314338
this.firstChild_ = nodes[0];
@@ -326,12 +350,13 @@
326350
oldChildNode);
327351
}
328352
} else {
353+
nodes = collectNodesNoNeedToUpdatePointers(newChildWrapper);
329354
ensureSameOwnerDocument(this, newChildWrapper);
330355
originalReplaceChild.call(this.impl, unwrap(newChildWrapper),
331356
oldChildNode);
332357
}
333358

334-
newChildWrapper.nodeWasAdded_();
359+
nodesWereAdded(nodes);
335360

336361
return oldChildWrapper;
337362
},

test/js/HTMLContentElement.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@ suite('HTMLContentElement', function() {
4242
assertArrayEqual(content.getDistributedNodes(), [b]);
4343
});
4444

45+
test('getDistributedNodes add document fragment with content', function() {
46+
var host = document.createElement('div');
47+
host.innerHTML = ' <a></a> <a></a> <a></a> ';
48+
var root = host.createShadowRoot();
49+
var df = document.createDocumentFragment();
50+
df.appendChild(document.createTextNode(' '));
51+
var content = df.appendChild(document.createElement('content'));
52+
df.appendChild(document.createTextNode(' '));
53+
root.appendChild(df);
54+
55+
assertArrayEqual(content.getDistributedNodes().length, 3);
56+
});
57+
4558
test('adding a new content element to a shadow tree', function() {
4659
var host = document.createElement('div');
4760
host.innerHTML = '<a></a><b></b>';

0 commit comments

Comments
 (0)