|
65 | 65 | // 3. node is <content> (host of container needs distribution)
|
66 | 66 | appendChild: function(node) {
|
67 | 67 | var handled;
|
| 68 | + // if a <content> is added, make sure it's parent has logical info. |
| 69 | + this._ensureContentLogicalInfo(node); |
68 | 70 | this._removeNodeFromHost(node, true);
|
69 | 71 | if (this._nodeIsInLogicalTree(this.node)) {
|
70 | 72 | this._addLogicalInfo(node, this.node);
|
71 | 73 | this._addNodeToHost(node);
|
72 | 74 | handled = this._maybeDistribute(node, this.node);
|
| 75 | + } else { |
| 76 | + this._addNodeToHost(node); |
73 | 77 | }
|
74 | 78 | // if not distributing and not adding to host, do a fast path addition
|
75 | 79 | if (!handled && !this._tryRemoveUndistributedNode(node)) {
|
|
86 | 90 | return this.appendChild(node);
|
87 | 91 | }
|
88 | 92 | var handled;
|
| 93 | + // if a <content> is added, make sure it's parent has logical info. |
| 94 | + this._ensureContentLogicalInfo(node); |
89 | 95 | this._removeNodeFromHost(node, true);
|
90 | 96 | if (this._nodeIsInLogicalTree(this.node)) {
|
91 |
| - saveLightChildrenIfNeeded(this.node); |
92 | 97 | var children = this.childNodes;
|
93 | 98 | var index = children.indexOf(ref_node);
|
94 | 99 | if (index < 0) {
|
|
98 | 103 | this._addLogicalInfo(node, this.node, index);
|
99 | 104 | this._addNodeToHost(node);
|
100 | 105 | handled = this._maybeDistribute(node, this.node);
|
| 106 | + } else { |
| 107 | + this._addNodeToHost(node); |
101 | 108 | }
|
102 | 109 | // if not distributing and not adding to host, do a fast path addition
|
103 | 110 | if (!handled && !this._tryRemoveUndistributedNode(node)) {
|
|
125 | 132 | if (this._nodeIsInLogicalTree(this.node)) {
|
126 | 133 | this._removeNodeFromHost(node);
|
127 | 134 | handled = this._maybeDistribute(node, this.node);
|
| 135 | + } else { |
| 136 | + this._removeNodeFromHost(node); |
128 | 137 | }
|
129 | 138 | if (!handled) {
|
130 | 139 | // if removing from a shadyRoot, remove form host instead
|
|
223 | 232 | },
|
224 | 233 |
|
225 | 234 | _updateInsertionPoints: function(host) {
|
226 |
| - host.shadyRoot._insertionPoints = |
| 235 | + var i$ = host.shadyRoot._insertionPoints = |
227 | 236 | factory(host.shadyRoot).querySelectorAll(CONTENT);
|
| 237 | + // ensure <content>'s and their parents have logical dom info. |
| 238 | + for (var i=0, c; i < i$.length; i++) { |
| 239 | + c = i$[i]; |
| 240 | + saveLightChildrenIfNeeded(c); |
| 241 | + saveLightChildrenIfNeeded(factory(c).parentNode); |
| 242 | + } |
228 | 243 | },
|
229 | 244 |
|
230 | 245 | // a node is in a shadyRoot, is a shadyRoot,
|
231 | 246 | // or has a lightParent
|
232 | 247 | _nodeIsInLogicalTree: function(node) {
|
233 | 248 | return Boolean((node._lightParent !== undefined) || node._isShadyRoot ||
|
234 |
| - this._ownerShadyRootForNode(node) || |
235 | 249 | node.shadyRoot);
|
236 | 250 | },
|
237 | 251 |
|
| 252 | + _ensureContentLogicalInfo: function(node) { |
| 253 | + if (node.nodeType === Node.DOCUMENT_FRAGMENT_NODE) { |
| 254 | + saveLightChildrenIfNeeded(this.node); |
| 255 | + var c$ = Array.prototype.slice.call(node.childNodes); |
| 256 | + for (var i=0, n; (i<c$.length) && (n=c$[i]); i++) { |
| 257 | + this._ensureContentLogicalInfo(n); |
| 258 | + } |
| 259 | + } else if (node.localName === CONTENT) { |
| 260 | + // should be parent not this.node, but this is before parent is set. |
| 261 | + saveLightChildrenIfNeeded(this.node); |
| 262 | + saveLightChildrenIfNeeded(node); |
| 263 | + } |
| 264 | + }, |
| 265 | + |
238 | 266 | _parentNeedsDistribution: function(parent) {
|
239 | 267 | return parent && parent.shadyRoot && hasInsertionPoint(parent.shadyRoot);
|
240 | 268 | },
|
|
292 | 320 | }
|
293 | 321 | },
|
294 | 322 |
|
| 323 | + // a node being added is always in this same host as this.node. |
295 | 324 | _addNodeToHost: function(node) {
|
296 |
| - var checkNode = node.nodeType === Node.DOCUMENT_FRAGMENT_NODE ? |
297 |
| - node.firstChild : node; |
298 |
| - var root = this._ownerShadyRootForNode(checkNode); |
| 325 | + var root = this.getOwnerRoot(); |
299 | 326 | if (root) {
|
300 | 327 | root.host._elementAdd(node);
|
301 | 328 | }
|
302 | 329 | },
|
303 | 330 |
|
304 | 331 | _addLogicalInfo: function(node, container, index) {
|
305 |
| - saveLightChildrenIfNeeded(container); |
306 | 332 | var children = factory(container).childNodes;
|
307 | 333 | index = index === undefined ? children.length : index;
|
308 | 334 | // handle document fragments
|
|
0 commit comments