diff --git a/src/lib/dom-api.html b/src/lib/dom-api.html
index 66f7263a3b..285db365d9 100644
--- a/src/lib/dom-api.html
+++ b/src/lib/dom-api.html
@@ -57,48 +57,36 @@
}
},
- // cases in which we may not be able to just do standard appendChild
+ appendChild: function(node) {
+ return this._addNode(node);
+ },
+
+ insertBefore: function(node, ref_node) {
+ return this._addNode(node, ref_node);
+ },
+
+ // cases in which we may not be able to just do standard native call
// 1. container has a shadyRoot (needsDistribution IFF the shadyRoot
// has an insertion point)
// 2. container is a shadyRoot (don't distribute, instead set
// container to container.host.
// 3. node is (host of container needs distribution)
- appendChild: function(node) {
- this._removeNodeFromHost(node, true);
- // if a 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);
- // if not distributing and not adding to host, do a fast path addition
- 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);
- nativeAppendChild.call(container, node);
- }
- return node;
- },
-
- insertBefore: function(node, ref_node) {
- if (!ref_node) {
- return this.appendChild(node);
- }
+ _addNode: function(node, ref_node) {
this._removeNodeFromHost(node, true);
+ var addedInsertionPoint;
+ var root = this.getOwnerRoot();
// if a 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');
+ if (root) {
+ addedInsertionPoint = this._maybeAddInsertionPoint(node, this.node);
+ }
+ if (this._nodeHasLogicalChildren(this.node)) {
+ if (ref_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);
}
@@ -106,13 +94,22 @@
// if not distributing and not adding to host, do a fast path addition
if (!this._maybeDistribute(node, this.node) &&
!this._tryRemoveUndistributedNode(node)) {
- // if ref_node is replace with first distributed node
- ref_node = ref_node.localName === CONTENT ?
- this._firstComposedNode(ref_node) : ref_node;
+ if (ref_node) {
+ // if ref_node is replace with first distributed node
+ ref_node = ref_node.localName === CONTENT ?
+ this._firstComposedNode(ref_node) : ref_node;
+ }
// if adding to a shadyRoot, add to host instead
var container = this.node._isShadyRoot ? this.node.host : this.node;
addToComposedParent(container, node, ref_node);
- nativeInsertBefore.call(container, node, ref_node);
+ if (ref_node) {
+ nativeInsertBefore.call(container, node, ref_node);
+ } else {
+ nativeAppendChild.call(container, node);
+ }
+ }
+ if (addedInsertionPoint) {
+ this._updateInsertionPoints(root.host);
}
return node;
},
@@ -198,7 +195,8 @@
var root = this._ownerShadyRootForNode(parent);
if (root) {
var host = root.host;
- this._updateInsertionPoints(host);
+ // note, insertion point list update is handled after node
+ // mutations are complete
this._lazyDistribute(host);
}
}
@@ -214,6 +212,30 @@
return parentNeedsDist || (hasContent && !wrappedContent);
},
+ /* note: parent argument is required since node may have an out
+ of date parent at this point; returns true if a is being added */
+ _maybeAddInsertionPoint: function(node, parent) {
+ var added;
+ if (node.nodeType === Node.DOCUMENT_FRAGMENT_NODE &&
+ !node.__noContent) {
+ var c$ = factory(node).querySelectorAll(CONTENT);
+ for (var i=0, n, np, na; (i