Skip to content

Commit

Permalink
Add dynamic content tests, fix logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinpschaaf committed Jun 12, 2015
1 parent 17ad90f commit 05b8579
Show file tree
Hide file tree
Showing 6 changed files with 857 additions and 93 deletions.
59 changes: 50 additions & 9 deletions src/lib/dom-api.html
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@
}
var handled;
if (this._nodeIsInLogicalTree(this.node)) {
handled = this._maybeDistribute(node, this.node);
this._removeNodeFromHost(node);
handled = this._maybeDistribute(node, this.node);
}
if (!handled) {
// if removing from a shadyRoot, remove form host instead
Expand Down Expand Up @@ -163,15 +163,16 @@
node._ownerShadyRoot = root;
}
return node._ownerShadyRoot;

},

_maybeDistribute: function(node, parent) {
// TODO(sorvell): technically we should check non-fragment nodes for
// <content> children but since this case is assumed to be exceedingly
// rare, we avoid the cost and will address with some specific api
// when the need arises.
var fragContent = (node.nodeType === Node.DOCUMENT_FRAGMENT_NODE) &&
// when the need arises. For now, the user must call
// distributeContent(true), which updates insertion points manually
// and forces distribution.
var fragContent = (node.nodeType === Node.DOCUMENT_FRAGMENT_NODE) &&
node.querySelector(CONTENT);
var wrappedContent = fragContent &&
(fragContent.parentNode.nodeType !== Node.DOCUMENT_FRAGMENT_NODE);
Expand All @@ -182,9 +183,12 @@
// 2. children being inserted into parent with a shady root (parent
// needs distribution)
if (hasContent) {
var host = this._ownerShadyRootForNode(parent).host;
this._updateInsertionPoints(host);
this._lazyDistribute(host);
var root = this._ownerShadyRootForNode(parent);
if (root) {
var host = root.host;
this._updateInsertionPoints(host);
this._lazyDistribute(host);
}
}
var parentNeedsDist = this._parentNeedsDistribution(parent);
if (parentNeedsDist) {
Expand All @@ -195,7 +199,7 @@
// and the parent does not need distribution, return false to allow
// the nodes to be added directly, after which children may be
// distributed and composed into the wrapping node(s)
return false;// (parentNeedsDist && (!hasContent || !wrappedContent));
return parentNeedsDist || (hasContent && !wrappedContent);
},

_tryRemoveUndistributedNode: function(node) {
Expand Down Expand Up @@ -225,14 +229,51 @@
},

_removeNodeFromHost: function(node) {
var hostNeedsDist;
var root;
if (node._lightParent) {
var root = this._ownerShadyRootForNode(node);
root = this._ownerShadyRootForNode(node);
if (root) {
root.host._elementRemove(node);
hostNeedsDist = this._removeDistributedChildren(root, node);
}
this._removeLogicalInfo(node, node._lightParent);
}
this._removeOwnerShadyRoot(node);
if (root && hostNeedsDist) {
this._updateInsertionPoints(root.host);
this._lazyDistribute(root.host);
}
},

_removeDistributedChildren: function(root, container) {
var hostNeedsDist;
var ip$ = root._insertionPoints;
for (var i=0; i<ip$.length; i++) {
var content = ip$[i];
if (this._contains(container, content)) {
var dc$ = factory(content).getDistributedNodes();
for (var j=0; j<dc$.length; j++) {
hostNeedsDist = true;
var node = dc$[i];
var parent = node.parentNode;
if (parent) {
nativeRemoveChild.call(parent, node);
removeFromComposedParent(parent, node);
}
}
}
}
return hostNeedsDist;
},

_contains: function(container, node) {
while (node) {
if (node == container) {
return true;
}
node = factory(node).parentNode;
}
},

_addNodeToHost: function(node) {
Expand Down
12 changes: 10 additions & 2 deletions src/mini/shady.html
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,21 @@
* `<content select=".foo">` and a `foo` class is added to a child,
* then `distributeContent` must be called to update
* local dom distribution.
* @method distributeContent
* @param {boolean} updateInsertionPoints Shady DOM does not detect
* <content> insertion that is nested in a sub-tree being appended.
* Set to true to distribute to newly added nested <content>'s.
*/
distributeContent: function() {
distributeContent: function(updateInsertionPoints) {
if (this.shadyRoot) {
var dom = Polymer.dom(this);
if (updateInsertionPoints) {
dom._updateInsertionPoints(this);
}
// Distribute the host that's the top of this element's distribution
// tree. Distributing that host will *always* distibute this element.
var host = getTopDistributingHost(this);
Polymer.dom(this)._lazyDistribute(host);
dom._lazyDistribute(host);
}
},

Expand Down
1 change: 1 addition & 0 deletions test/runner.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
'unit/shady.html',
'unit/polymer-dom.html',
'unit/polymer-dom-shadow.html',
'unit/polymer-dom-content.html',
'unit/bind.html',
'unit/bind.html?dom=shadow',
'unit/notify-path.html',
Expand Down
Loading

0 comments on commit 05b8579

Please sign in to comment.