Hoist actions
This commit is contained in:
parent
b336faff25
commit
0e26a8cd9f
|
@ -57,11 +57,10 @@ function followPath(topNodes: ChildNode[], path: number[]): Node {
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
type PlaceholderAction = (variableParts: HtmlFragment[], topNodes: ChildNode[]) => void;
|
type PlaceholderAction = (variableParts: HtmlFragment[], node: Node) => void;
|
||||||
|
|
||||||
function nodeInserter(n: number, path: number[]): PlaceholderAction {
|
function nodeInserter(n: number): PlaceholderAction {
|
||||||
return (vs, topNodes) => {
|
return (vs, node) => {
|
||||||
const node = followPath(topNodes, path);
|
|
||||||
function walk(f: HtmlFragment) {
|
function walk(f: HtmlFragment) {
|
||||||
if (Array.isArray(f)) {
|
if (Array.isArray(f)) {
|
||||||
f.forEach(walk);
|
f.forEach(walk);
|
||||||
|
@ -80,9 +79,8 @@ function nodeInserter(n: number, path: number[]): PlaceholderAction {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function attributesInserter(n: number, path: number[]): PlaceholderAction {
|
function attributesInserter(n: number): PlaceholderAction {
|
||||||
return (vs, topNodes) => {
|
return (vs, node) => {
|
||||||
const node = followPath(topNodes, path);
|
|
||||||
const e = document.createElement('template');
|
const e = document.createElement('template');
|
||||||
e.innerHTML = `<x-dummy ${renderFragment(vs[n], true).join('')}></x-dummy>`;
|
e.innerHTML = `<x-dummy ${renderFragment(vs[n], true).join('')}></x-dummy>`;
|
||||||
Array.from(e.attributes).forEach(a =>
|
Array.from(e.attributes).forEach(a =>
|
||||||
|
@ -94,10 +92,8 @@ function attributeValueInserter(
|
||||||
attrName: string,
|
attrName: string,
|
||||||
constantParts: string[],
|
constantParts: string[],
|
||||||
placeholders: number[],
|
placeholders: number[],
|
||||||
path: number[],
|
|
||||||
): PlaceholderAction {
|
): PlaceholderAction {
|
||||||
return (vs, topNodes) => {
|
return (vs, node) => {
|
||||||
const node = followPath(topNodes, path);
|
|
||||||
const pieces = [constantParts[0]];
|
const pieces = [constantParts[0]];
|
||||||
placeholders.forEach((n, i) => {
|
placeholders.forEach((n, i) => {
|
||||||
pieces.push(...renderFragment(vs[n], false));
|
pieces.push(...renderFragment(vs[n], false));
|
||||||
|
@ -109,7 +105,7 @@ function attributeValueInserter(
|
||||||
|
|
||||||
export class HtmlFragmentBuilder {
|
export class HtmlFragmentBuilder {
|
||||||
template: HTMLTemplateElement = document.createElement('template');
|
template: HTMLTemplateElement = document.createElement('template');
|
||||||
placeholderActions: PlaceholderAction[] = [];
|
placeholderActions: { path: number[], action: PlaceholderAction }[] = [];
|
||||||
|
|
||||||
constructor(constantParts: TemplateStringsArray) {
|
constructor(constantParts: TemplateStringsArray) {
|
||||||
const pieces: string[] = [];
|
const pieces: string[] = [];
|
||||||
|
@ -145,15 +141,17 @@ export class HtmlFragmentBuilder {
|
||||||
placeholders.forEach((n, i) => {
|
placeholders.forEach((n, i) => {
|
||||||
const currentPath = path.slice();
|
const currentPath = path.slice();
|
||||||
currentPath[currentPath.length - 1] = i * 2 + 1;
|
currentPath[currentPath.length - 1] = i * 2 + 1;
|
||||||
this.placeholderActions.push(nodeInserter(n, currentPath));
|
this.placeholderActions.push({
|
||||||
|
path: currentPath,
|
||||||
|
action: nodeInserter(n),
|
||||||
|
});
|
||||||
});
|
});
|
||||||
bump(constantParts.length + placeholders.length);
|
bump(constantParts.length + placeholders.length);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Node.ELEMENT_NODE: {
|
case Node.ELEMENT_NODE: {
|
||||||
const currentPath = path.slice();
|
|
||||||
const e = n as Element;
|
const e = n as Element;
|
||||||
// TODO: hoist all actions for this node into a single action
|
const actions: PlaceholderAction[] = [];
|
||||||
for (let i = 0; i < e.attributes.length; i++) {
|
for (let i = 0; i < e.attributes.length; i++) {
|
||||||
const attr = e.attributes[i];
|
const attr = e.attributes[i];
|
||||||
const attrName = attr.name;
|
const attrName = attr.name;
|
||||||
|
@ -162,19 +160,24 @@ export class HtmlFragmentBuilder {
|
||||||
e.removeAttributeNode(attr);
|
e.removeAttributeNode(attr);
|
||||||
i--;
|
i--;
|
||||||
const n = parseInt(nameIsPlaceholder[1], 10);
|
const n = parseInt(nameIsPlaceholder[1], 10);
|
||||||
this.placeholderActions.push(attributesInserter(n, currentPath));
|
actions.push(attributesInserter(n));
|
||||||
} else {
|
} else {
|
||||||
const { constantParts, placeholders } =
|
const { constantParts, placeholders } =
|
||||||
splitByPlaceholders(attr.value);
|
splitByPlaceholders(attr.value);
|
||||||
if (constantParts.length !== 1) {
|
if (constantParts.length !== 1) {
|
||||||
this.placeholderActions.push(attributeValueInserter(
|
actions.push(attributeValueInserter(
|
||||||
attrName,
|
attrName,
|
||||||
constantParts,
|
constantParts,
|
||||||
placeholders,
|
placeholders));
|
||||||
currentPath));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (actions.length) {
|
||||||
|
this.placeholderActions.push({
|
||||||
|
path: path.slice(),
|
||||||
|
action: (vs, n) => actions.forEach(a => a(vs, n)),
|
||||||
|
});
|
||||||
|
}
|
||||||
walk(e);
|
walk(e);
|
||||||
nextN = e.nextSibling;
|
nextN = e.nextSibling;
|
||||||
bump(1);
|
bump(1);
|
||||||
|
@ -197,7 +200,9 @@ export class HtmlFragmentBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
update(template: ChildNode[], variableParts: Array<HtmlFragment>) {
|
update(template: ChildNode[], variableParts: Array<HtmlFragment>) {
|
||||||
this.placeholderActions.forEach(a => a(variableParts, template));
|
this.placeholderActions.forEach(({ path, action }) => {
|
||||||
|
action(variableParts, followPath(template, path));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue