Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/dom-tree-insertion.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clack/ui': minor
---

Add node parent relationships and basic insertion and removal operations, with appendChild delegating to insertBefore.
82 changes: 74 additions & 8 deletions packages/ui/src/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,40 +46,106 @@ 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;
abstract readonly nodeName: string;

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<Node> {
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;
}
}

Expand Down
8 changes: 8 additions & 0 deletions packages/ui/test/wpt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading