Quasi-cosmetic

This commit is contained in:
Tony Garnock-Jones 2023-12-03 23:40:36 +01:00
parent 0e26a8cd9f
commit be6dbd2782
1 changed files with 59 additions and 52 deletions

View File

@ -117,16 +117,7 @@ export class HtmlFragmentBuilder {
this.indexPlaceholders();
}
private indexPlaceholders() {
const path: number[] = [];
const bump = (n: number) => path[path.length - 1] += n;
const walk = (parentNode: ParentNode) => {
path.push(0);
let nextN = parentNode.firstChild;
while (nextN !== null) {
const n = nextN;
switch (n.nodeType) {
case Node.TEXT_NODE: {
private indexTextNode(n: Text, path: number[]): ChildNode | null {
const { constantParts, placeholders } =
splitByPlaceholders(n.textContent ?? '');
constantParts.forEach((c, i) => {
@ -136,7 +127,7 @@ export class HtmlFragmentBuilder {
}
n.parentNode?.insertBefore(document.createTextNode(c), n);
});
nextN = n.nextSibling;
const nextN = n.nextSibling;
n.parentNode?.removeChild(n);
placeholders.forEach((n, i) => {
const currentPath = path.slice();
@ -146,11 +137,11 @@ export class HtmlFragmentBuilder {
action: nodeInserter(n),
});
});
bump(constantParts.length + placeholders.length);
break;
path[path.length - 1] += constantParts.length + placeholders.length;
return nextN;
}
case Node.ELEMENT_NODE: {
const e = n as Element;
private indexElement(e: Element, path: number[]) {
const actions: PlaceholderAction[] = [];
for (let i = 0; i < e.attributes.length; i++) {
const attr = e.attributes[i];
@ -178,14 +169,30 @@ export class HtmlFragmentBuilder {
action: (vs, n) => actions.forEach(a => a(vs, n)),
});
}
}
private indexPlaceholders() {
const path: number[] = [];
const walk = (parentNode: ParentNode) => {
path.push(0);
let nextN = parentNode.firstChild;
while (nextN !== null) {
const n = nextN;
switch (n.nodeType) {
case Node.TEXT_NODE:
nextN = this.indexTextNode(n as Text, path);
break;
case Node.ELEMENT_NODE: {
const e = n as Element;
this.indexElement(e, path)
walk(e);
nextN = e.nextSibling;
bump(1);
path[path.length - 1]++;
break;
}
default:
nextN = n.nextSibling;
bump(1);
path[path.length - 1]++;
break;
}
}