Skip to content

Commit

Permalink
Implement normalize function
Browse files Browse the repository at this point in the history
  • Loading branch information
esmith164 committed Jun 21, 2024
1 parent e9ba6c1 commit bfe0c43
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/dom/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,32 @@ export class Node extends EventTarget {
oldChild._replaceWith(newChild);
return oldChild;
}
normalize() {
let i = 0;
while (i < this.childNodes.length) {
const currentNode = this.childNodes[i];
if (currentNode.nodeType === NodeType.TEXT_NODE) {
let nextNode = this.childNodes[i + 1];

// Merge adjacent text nodes
while (nextNode && nextNode.nodeType === NodeType.TEXT_NODE) {
if (currentNode.nodeValue) {
currentNode.nodeValue += nextNode.nodeValue;
}
this.removeChild(nextNode);
nextNode = this.childNodes[i + 1];
}
if (currentNode.nodeValue === "") {
this.removeChild(currentNode);
} else {
i++;
}
} else {
currentNode.normalize();
i++;
}
}
}

insertBefore(newNode: Node, refNode: Node | null): Node {
this._assertNotAncestor(newNode);
Expand Down

0 comments on commit bfe0c43

Please sign in to comment.