Use TreeWalker for template-stamp.

This commit is contained in:
Kevin Schaaf
2018-10-23 15:06:51 -07:00
parent 527f519c9a
commit c61df6f422
+13 -6
View File
@@ -16,6 +16,8 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
'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 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
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 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
// 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 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
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 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
// 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 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
childInfo.infoIndex = templateInfo.nodeInfoList.push(/** @type {!NodeInfo} */(childInfo)) - 1;
}
// Increment if not removed
if (node.parentNode) {
walker.currentNode = node;
if (walker.parentNode()) {
parentIndex++;
}
}