Skip to content

Commit

Permalink
Use TreeWalker for template-stamp.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinpschaaf committed Oct 23, 2018
1 parent 527f519 commit c61df6f
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions lib/mixins/template-stamp.html
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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()) {
Expand All @@ -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);
Expand All @@ -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;
}
Expand All @@ -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++;
}
}
Expand Down

0 comments on commit c61df6f

Please sign in to comment.