Skip to content

Commit

Permalink
Add logical info iff an element being added is an insertion point; do…
Browse files Browse the repository at this point in the history
… not add logical info for any element in a shady root.
  • Loading branch information
Steven Orvell committed Aug 11, 2015
1 parent 21500fb commit 45cb150
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions src/lib/dom-api.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,15 @@
// 3. node is <content> (host of container needs distribution)
appendChild: function(node) {
var handled;
// if a <content> is added, make sure it's parent has logical info.
this._ensureContentLogicalInfo(node);
this._removeNodeFromHost(node, true);
if (this._nodeIsInLogicalTree(this.node)) {
this._addLogicalInfo(node, this.node);
this._addNodeToHost(node);
handled = this._maybeDistribute(node, this.node);
} else {
this._addNodeToHost(node);
}
// if not distributing and not adding to host, do a fast path addition
if (!handled && !this._tryRemoveUndistributedNode(node)) {
Expand All @@ -86,9 +90,10 @@
return this.appendChild(node);
}
var handled;
// if a <content> is added, make sure it's parent has logical info.
this._ensureContentLogicalInfo(node);
this._removeNodeFromHost(node, true);
if (this._nodeIsInLogicalTree(this.node)) {
saveLightChildrenIfNeeded(this.node);
var children = this.childNodes;
var index = children.indexOf(ref_node);
if (index < 0) {
Expand All @@ -98,6 +103,8 @@
this._addLogicalInfo(node, this.node, index);
this._addNodeToHost(node);
handled = this._maybeDistribute(node, this.node);
} else {
this._addNodeToHost(node);
}
// if not distributing and not adding to host, do a fast path addition
if (!handled && !this._tryRemoveUndistributedNode(node)) {
Expand Down Expand Up @@ -125,6 +132,8 @@
if (this._nodeIsInLogicalTree(this.node)) {
this._removeNodeFromHost(node);
handled = this._maybeDistribute(node, this.node);
} else {
this._removeNodeFromHost(node);
}
if (!handled) {
// if removing from a shadyRoot, remove form host instead
Expand Down Expand Up @@ -237,10 +246,23 @@
// or has a lightParent
_nodeIsInLogicalTree: function(node) {
return Boolean((node._lightParent !== undefined) || node._isShadyRoot ||
this._ownerShadyRootForNode(node) ||
node.shadyRoot);
},

_ensureContentLogicalInfo: function(node) {
if (node.nodeType === Node.DOCUMENT_FRAGMENT_NODE) {
saveLightChildrenIfNeeded(this.node);
var c$ = Array.prototype.slice.call(node.childNodes);
for (var i=0, n; (i<c$.length) && (n=c$[i]); i++) {
this._ensureContentLogicalInfo(n);
}
} else if (node.localName === CONTENT) {
// should be parent not this.node, but this is before parent is set.
saveLightChildrenIfNeeded(this.node);
saveLightChildrenIfNeeded(node);
}
},

_parentNeedsDistribution: function(parent) {
return parent && parent.shadyRoot && hasInsertionPoint(parent.shadyRoot);
},
Expand Down Expand Up @@ -298,17 +320,15 @@
}
},

// a node being added is always in this same host as this.node.
_addNodeToHost: function(node) {
var checkNode = node.nodeType === Node.DOCUMENT_FRAGMENT_NODE ?
node.firstChild : node;
var root = this._ownerShadyRootForNode(checkNode);
var root = this.getOwnerRoot();
if (root) {
root.host._elementAdd(node);
}
},

_addLogicalInfo: function(node, container, index) {
saveLightChildrenIfNeeded(container);
var children = factory(container).childNodes;
index = index === undefined ? children.length : index;
// handle document fragments
Expand Down

0 comments on commit 45cb150

Please sign in to comment.