diff --git a/lib/mixins/template-stamp.html b/lib/mixins/template-stamp.html index 622356b80a..808a9820f2 100644 --- a/lib/mixins/template-stamp.html +++ b/lib/mixins/template-stamp.html @@ -16,6 +16,8 @@ 'use strict'; + const walker = document.createTreeWalker(document); + // 1.x backwards-compatible auto-wrapper for template type extensions // This is a clear layering violation and gives favored-nation status to // dom-if and dom-repeat templates. This is a conceit we're choosing to keep @@ -50,7 +52,8 @@ if (parent) { // note: marginally faster than indexing via childNodes // (http://jsperf.com/childnodes-lookup) - for (let n=parent.firstChild, i=0; n; n=n.nextSibling) { + walker.currentNode = parent; + for (let n=walker.firstChild(), i=0; n; n=walker.nextSibling()) { if (nodeInfo.parentIndex === i++) { return n; } @@ -234,7 +237,8 @@ // For ShadyDom optimization, indicating there is an insertion point templateInfo.hasInsertionPoint = true; } - if (element.firstChild) { + walker.currentNode = element; + if (walker.firstChild()) { noted = this._parseTemplateChildNodes(element, templateInfo, nodeInfo) || noted; } if (element.hasAttributes && element.hasAttributes()) { @@ -260,7 +264,8 @@ if (root.localName === 'script' || root.localName === 'style') { return; } - for (let node=root.firstChild, parentIndex=0, next; node; node=next) { + walker.currentNode = root; + for (let node=walker.firstChild(), parentIndex=0, next; node; node=next) { // Wrap templates if (node.localName == 'template') { node = wrapTemplateExtension(node); @@ -269,12 +274,13 @@ // text nodes to be inexplicably split =( // note that root.normalize() should work but does not so we do this // manually. - next = node.nextSibling; + walker.currentNode = node; + next = walker.nextSibling(); if (node.nodeType === Node.TEXT_NODE) { let /** Node */ n = next; while (n && (n.nodeType === Node.TEXT_NODE)) { node.textContent += n.textContent; - next = n.nextSibling; + next = walker.nextSibling(); root.removeChild(n); n = next; } @@ -289,7 +295,8 @@ childInfo.infoIndex = templateInfo.nodeInfoList.push(/** @type {!NodeInfo} */(childInfo)) - 1; } // Increment if not removed - if (node.parentNode) { + walker.currentNode = node; + if (walker.parentNode()) { parentIndex++; } }