|
15 | 15 |
|
16 | 16 | var Settings = Polymer.Settings;
|
17 | 17 | var DomApi = Polymer.DomApi;
|
| 18 | + var dom = DomApi.factory; |
18 | 19 | var TreeApi = Polymer.TreeApi;
|
19 | 20 | var getInnerHTML = Polymer.domInnerHTML.getInnerHTML;
|
20 | 21 | var CONTENT = DomApi.CONTENT;
|
|
69 | 70 | throw Error('The ref_node to be inserted before is not a child ' +
|
70 | 71 | 'of this node');
|
71 | 72 | }
|
72 |
| - this._addLogicalInfo(node, this.node, ref_node); |
| 73 | + TreeApi.Logical.recordInsertBefore(node, this.node, ref_node); |
73 | 74 | }
|
74 | 75 | this._addNodeToHost(node);
|
75 | 76 | // if not distributing and not adding to host, do a fast path addition
|
|
101 | 102 | This method also performs dom composition.
|
102 | 103 | */
|
103 | 104 | removeChild: function(node) {
|
104 |
| - if (DomApi.factory(node).parentNode !== this.node) { |
| 105 | + if (dom(node).parentNode !== this.node) { |
105 | 106 | console.warn('The node to be removed is not a child of this node',
|
106 | 107 | node);
|
107 | 108 | }
|
|
201 | 202 | var added;
|
202 | 203 | if (node.nodeType === Node.DOCUMENT_FRAGMENT_NODE &&
|
203 | 204 | !node.__noContent) {
|
204 |
| - var c$ = DomApi.factory(node).querySelectorAll(CONTENT); |
| 205 | + var c$ = dom(node).querySelectorAll(CONTENT); |
205 | 206 | for (var i=0, n, np, na; (i<c$.length) && (n=c$[i]); i++) {
|
206 |
| - np = DomApi.factory(n).parentNode; |
| 207 | + np = dom(n).parentNode; |
207 | 208 | // don't allow node's parent to be fragment itself
|
208 | 209 | if (np === node) {
|
209 | 210 | np = parent;
|
|
231 | 232 |
|
232 | 233 | _updateInsertionPoints: function(host) {
|
233 | 234 | var i$ = host.shadyRoot._insertionPoints =
|
234 |
| - DomApi.factory(host.shadyRoot).querySelectorAll(CONTENT); |
| 235 | + dom(host.shadyRoot).querySelectorAll(CONTENT); |
235 | 236 | // ensure <content>'s and their parents have logical dom info.
|
236 | 237 | for (var i=0, c; i < i$.length; i++) {
|
237 | 238 | c = i$[i];
|
238 | 239 | TreeApi.Logical.saveChildNodes(c);
|
239 |
| - TreeApi.Logical.saveChildNodes(DomApi.factory(c).parentNode); |
| 240 | + TreeApi.Logical.saveChildNodes(dom(c).parentNode); |
240 | 241 | }
|
241 | 242 | },
|
242 | 243 |
|
|
248 | 249 | _removeNodeFromParent: function(node) {
|
249 | 250 | // note: we may need to notify and not have logical info so fallback
|
250 | 251 | // to composed parentNode.
|
| 252 | + |
251 | 253 | var parent = node.__parentNode || node.parentNode;
|
| 254 | + //var parent = dom(node).parentNode; |
| 255 | + // TODO(sorvell): hasDomApi doesn't make sense now |
252 | 256 | if (parent && DomApi.hasApi(parent)) {
|
253 |
| - DomApi.factory(parent).notifyObserver(); |
| 257 | + dom(parent).notifyObserver(); |
254 | 258 | }
|
255 | 259 | this._removeNodeFromHost(node, true);
|
256 | 260 | },
|
|
262 | 266 | // to require distribution... both cases are handled here.
|
263 | 267 | var hostNeedsDist;
|
264 | 268 | var root;
|
| 269 | + // important that we want to do this only if the node has a logical parent |
265 | 270 | var parent = node.__parentNode;
|
266 | 271 | if (parent) {
|
267 | 272 | // distribute node's parent iff needed
|
268 |
| - DomApi.factory(node)._distributeParent(); |
| 273 | + dom(node)._distributeParent(); |
269 | 274 | root = this._ownerShadyRootForNode(node);
|
270 | 275 | // remove node from root and distribute it iff needed
|
271 | 276 | if (root) {
|
272 | 277 | root.host._elementRemove(node);
|
273 | 278 | hostNeedsDist = this._removeDistributedChildren(root, node);
|
274 | 279 | }
|
275 |
| - this._removeLogicalInfo(node, parent); |
| 280 | + TreeApi.Logical.recordRemoveChild(node, parent); |
276 | 281 | }
|
277 | 282 | this._removeOwnerShadyRoot(node);
|
278 | 283 | if (root && hostNeedsDist) {
|
|
290 | 295 | for (var i=0; i<ip$.length; i++) {
|
291 | 296 | var content = ip$[i];
|
292 | 297 | if (this._contains(container, content)) {
|
293 |
| - var dc$ = DomApi.factory(content).getDistributedNodes(); |
| 298 | + var dc$ = dom(content).getDistributedNodes(); |
294 | 299 | for (var j=0; j<dc$.length; j++) {
|
295 | 300 | hostNeedsDist = true;
|
296 | 301 | var node = dc$[j];
|
|
310 | 315 | if (node == container) {
|
311 | 316 | return true;
|
312 | 317 | }
|
313 |
| - node = DomApi.factory(node).parentNode; |
| 318 | + node = dom(node).parentNode; |
314 | 319 | }
|
315 | 320 | },
|
316 | 321 |
|
|
322 | 327 | }
|
323 | 328 | },
|
324 | 329 |
|
325 |
| - _addLogicalInfo: function(node, container, ref_node) { |
326 |
| - container.__childNodes = null; |
327 |
| - // handle document fragments |
328 |
| - if (node.nodeType === Node.DOCUMENT_FRAGMENT_NODE) { |
329 |
| - // NOTE: the act of setting this info can affect patched nodes |
330 |
| - // getters; therefore capture childNodes before patching. |
331 |
| - var c$ = TreeApi.arrayCopyChildNodes(node); |
332 |
| - for (var i=0, n; (i<c$.length) && (n=c$[i]); i++) { |
333 |
| - this._linkNode(n, container, ref_node); |
334 |
| - } |
335 |
| - } else { |
336 |
| - this._linkNode(node, container, ref_node); |
337 |
| - } |
338 |
| - }, |
339 |
| - |
340 |
| - _linkNode: function(node, container, ref_node) { |
341 |
| - // update node <-> ref_node |
342 |
| - node.__previousSibling = ref_node ? ref_node.__previousSibling : |
343 |
| - container.__lastChild; |
344 |
| - if (node.__previousSibling) { |
345 |
| - node.__previousSibling.__nextSibling = node; |
346 |
| - } |
347 |
| - node.__nextSibling = ref_node; |
348 |
| - if (ref_node) { |
349 |
| - ref_node.__previousSibling = node; |
350 |
| - } |
351 |
| - // update node <-> container |
352 |
| - node.__parentNode = container; |
353 |
| - if (ref_node && ref_node === container.__firstChild) { |
354 |
| - container.__firstChild = node; |
355 |
| - } else { |
356 |
| - container.__lastChild = node; |
357 |
| - if (!container.__firstChild) { |
358 |
| - container.__firstChild = node; |
359 |
| - } |
360 |
| - } |
361 |
| - // remove caching of childNodes |
362 |
| - container.__childNodes = null; |
363 |
| - }, |
364 |
| - |
365 |
| - // NOTE: in general, we expect contents of the lists here to be small-ish |
366 |
| - // and therefore indexOf to be nbd. Other optimizations can be made |
367 |
| - // for larger lists (linked list) |
368 |
| - _removeLogicalInfo: function(node, container) { |
369 |
| - if (node === container.__firstChild) { |
370 |
| - container.__firstChild = node.__nextSibling; |
371 |
| - } |
372 |
| - if (node === container.__lastChild) { |
373 |
| - container.__lastChild = node.__previousSibling; |
374 |
| - } |
375 |
| - var p = node.__previousSibling; |
376 |
| - var n = node.__nextSibling; |
377 |
| - if (p) { |
378 |
| - p.__nextSibling = n; |
379 |
| - } |
380 |
| - if (n) { |
381 |
| - n.__previousSibling = p; |
382 |
| - } |
383 |
| - node.__parentNode = node.__previousSibling = node.__nextSibling = null; |
384 |
| - // remove caching of childNodes |
385 |
| - container.__childNodes = null; |
386 |
| - }, |
387 |
| - |
388 | 330 | _removeOwnerShadyRoot: function(node) {
|
389 | 331 | // optimization: only reset the tree if node is actually in a root
|
390 | 332 | if (this._hasCachedOwnerRoot(node)) {
|
391 |
| - var c$ = DomApi.factory(node).childNodes; |
| 333 | + var c$ = dom(node).childNodes; |
392 | 334 | for (var i=0, l=c$.length, n; (i<l) && (n=c$[i]); i++) {
|
393 | 335 | this._removeOwnerShadyRoot(n);
|
394 | 336 | }
|
|
400 | 342 | // question is pending; this is expected to be exceedingly rare, but if
|
401 | 343 | // the issue comes up, we can force a flush in this case.
|
402 | 344 | _firstComposedNode: function(content) {
|
403 |
| - var n$ = DomApi.factory(content).getDistributedNodes(); |
| 345 | + var n$ = dom(content).getDistributedNodes(); |
404 | 346 | for (var i=0, l=n$.length, n, p$; (i<l) && (n=n$[i]); i++) {
|
405 |
| - p$ = DomApi.factory(n).getDestinationInsertionPoints(); |
| 347 | + p$ = dom(n).getDestinationInsertionPoints(); |
406 | 348 | // means that we're composed to this spot.
|
407 | 349 | if (p$[p$.length-1] === content) {
|
408 | 350 | return n;
|
|
424 | 366 | _query: function(matcher, node) {
|
425 | 367 | node = node || this.node;
|
426 | 368 | var list = [];
|
427 |
| - this._queryElements(DomApi.factory(node).childNodes, matcher, list); |
| 369 | + this._queryElements(dom(node).childNodes, matcher, list); |
428 | 370 | return list;
|
429 | 371 | },
|
430 | 372 |
|
|
440 | 382 | if (matcher(node)) {
|
441 | 383 | list.push(node);
|
442 | 384 | }
|
443 |
| - this._queryElements(DomApi.factory(node).childNodes, matcher, list); |
| 385 | + this._queryElements(dom(node).childNodes, matcher, list); |
444 | 386 | },
|
445 | 387 |
|
446 | 388 | getDestinationInsertionPoints: function() {
|
|
477 | 419 | var n = nativeCloneNode.call(this.node, false);
|
478 | 420 | if (deep) {
|
479 | 421 | var c$ = this.childNodes;
|
480 |
| - var d = DomApi.factory(n); |
| 422 | + var d = dom(n); |
481 | 423 | for (var i=0, nc; i < c$.length; i++) {
|
482 |
| - nc = DomApi.factory(c$[i]).cloneNode(true); |
| 424 | + nc = dom(c$[i]).cloneNode(true); |
483 | 425 | d.appendChild(nc);
|
484 | 426 | }
|
485 | 427 | }
|
|
492 | 434 | this.node.ownerDocument;
|
493 | 435 | var n = nativeImportNode.call(doc, externalNode, false);
|
494 | 436 | if (deep) {
|
495 |
| - var c$ = DomApi.factory(externalNode).childNodes; |
496 |
| - var d = DomApi.factory(n); |
| 437 | + var c$ = dom(externalNode).childNodes; |
| 438 | + var d = dom(n); |
497 | 439 | for (var i=0, nc; i < c$.length; i++) {
|
498 |
| - nc = DomApi.factory(doc).importNode(c$[i], true); |
| 440 | + nc = dom(doc).importNode(c$[i], true); |
499 | 441 | d.appendChild(nc);
|
500 | 442 | }
|
501 | 443 | }
|
|
0 commit comments