From 09ab8e89e22b12fdc99abc2ed76d2fccf4c0e8be Mon Sep 17 00:00:00 2001 From: Charles Lowell Date: Wed, 11 Nov 2026 10:00:56 -0600 Subject: [PATCH] Add node parent relationships and basic insertion --- .changeset/dom-tree-insertion.md | 5 ++ packages/ui/src/dom.ts | 82 ++++++++++++++++++++++++++++---- packages/ui/test/wpt.config.ts | 8 ++++ 3 files changed, 87 insertions(+), 8 deletions(-) create mode 100644 .changeset/dom-tree-insertion.md diff --git a/.changeset/dom-tree-insertion.md b/.changeset/dom-tree-insertion.md new file mode 100644 index 0000000..84773cb --- /dev/null +++ b/.changeset/dom-tree-insertion.md @@ -0,0 +1,5 @@ +--- +'@clack/ui': minor +--- + +Add node parent relationships and basic insertion and removal operations, with appendChild delegating to insertBefore. diff --git a/packages/ui/src/dom.ts b/packages/ui/src/dom.ts index fe2f1e8..efd6984 100644 --- a/packages/ui/src/dom.ts +++ b/packages/ui/src/dom.ts @@ -46,7 +46,7 @@ export abstract class Node { static readonly TEXT_NODE = 3; static readonly COMMENT_NODE = 8; - readonly childNodes: Node[]; + readonly #childNodes: Node[] = []; readonly ownerDocument: Document; abstract readonly nodeType: number; @@ -54,32 +54,98 @@ export abstract class Node { protected readonly notify: () => void; + #parentNode: Node | null = null; + constructor(owner: Document, notify: Node['notify']) { this.notify = notify; - this.childNodes = []; this.ownerDocument = owner; } hasChildNodes(): boolean { - return this.childNodes.length > 0; + return this.#childNodes.length > 0; + } + + get parentNode(): Node | null { + return this.#parentNode; + } + + get parentElement(): Element | null { + return this.#parentNode instanceof Element ? this.#parentNode : null; + } + + get childNodes(): ReadonlyArray { + return this.#childNodes; } get firstChild(): Node | null { - return this.childNodes[0] ?? null; + return this.#childNodes[0] ?? null; } get lastChild(): Node | null { - return this.childNodes[this.childNodes.length - 1] ?? null; + return this.#childNodes[this.childNodes.length - 1] ?? null; + } + + get nodeValue(): string | null { + return null; } appendChild(node: Node): Node { - this.childNodes.push(node); + return this.insertBefore(node, null); + } + + removeChild(node: Node): Node { + const currentIndex = this.childNodes.indexOf(node); + if (currentIndex < 0) { + throw new DOMException( + `cannot remove ${node.nodeName} from ${this.nodeName}. It is not a child`, + 'NotFoundError', + ); + } + + this.#childNodes.splice(currentIndex, 1); + node.#parentNode = null; + this.notify(); return node; } - get nodeValue(): string | null { - return null; + insertBefore(node: Node, anchor: Node | null): Node { + // oxlint-disable-next-line typescript/no-this-alias -- This cursor walks the insertion parent's ancestry. + for (let current: Node | null = this; current; current = current.parentNode) { + if (current === node) { + throw new DOMException( + 'cannot add an ancestor Node to its descendant', + 'HierarchyRequestError', + ); + } + } + + if (anchor && anchor.parentNode !== this) { + throw new DOMException('Anchor is not a child of this node', 'NotFoundError'); + } + + if (node === this) { + throw new DOMException('cannot make a Node a child of itself', 'HierarchyRequestError'); + } + + if (node === anchor) { + return node; + } + if (node.parentNode) { + if (node.parentNode === this) { + const currentIndex = this.childNodes.indexOf(node); + this.#childNodes.splice(currentIndex, 1); + } else { + node.parentNode.removeChild(node); + } + } + const anchorIndex = anchor ? this.childNodes.indexOf(anchor) : this.childNodes.length; + this.#childNodes.splice(anchorIndex, 0, node); + + node.#parentNode = this; + this.notify(); + + return node; } } diff --git a/packages/ui/test/wpt.config.ts b/packages/ui/test/wpt.config.ts index 7a4ed5f..292b7ab 100644 --- a/packages/ui/test/wpt.config.ts +++ b/packages/ui/test/wpt.config.ts @@ -25,6 +25,14 @@ export default { 'For a Text, textContent should set the data', 'For a Comment, textContent should set the data', ], + 'dom/nodes/Node-parentElement.html': [ + 'parentElement should work correctly in disconnected subtrees (element)', + 'parentElement should work correctly in disconnected subtrees (text)', + ], + 'dom/nodes/Node-insertBefore.html': [ + 'Calling insertBefore with a reference child whose parent is not the context node must throw a NotFoundError.', + 'Inserting a node before itself should not move the node', + ], }, exclude: { 'css/**': true,