Skip to content

Commit 5b39ac5

Browse files
committed
Add Node sibling properties
1 parent baa93e3 commit 5b39ac5

4 files changed

Lines changed: 134 additions & 0 deletions

File tree

‎.changeset/dom-node-properties.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clack/ui': minor
3+
---
4+
5+
Add Node nextSibling and previousSibling properties that reflect the current tree after insertion, reordering, removal, and reparenting.

‎packages/ui/dom.test.ts‎

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/**
2+
* Local DOM coverage not provided by our enabled WPT cases.
3+
* These are independent tests, not adaptations of upstream WPT tests.
4+
* Sibling traversal lives here while Node-properties.html requires unsupported
5+
* HTML/XML document fixtures. Prefer WPT coverage when its dependencies are met.
6+
*/
7+
import { describe, expect, it } from 'vitest';
8+
import { Document } from './src/dom.ts';
9+
10+
describe('Node sibling properties outside WPT coverage', () => {
11+
it.each(['createElement', 'createTextNode', 'createComment'] as const)(
12+
'%s has no siblings when detached or an only child',
13+
(create) => {
14+
const document = new Document(() => {});
15+
const parent = document.documentElement;
16+
const node = document[create]('document');
17+
18+
expect(node.previousSibling).toBeNull();
19+
expect(node.nextSibling).toBeNull();
20+
21+
parent.appendChild(node);
22+
expect(node.previousSibling).toBeNull();
23+
expect(node.nextSibling).toBeNull();
24+
25+
parent.removeChild(node);
26+
expect(node.previousSibling).toBeNull();
27+
expect(node.nextSibling).toBeNull();
28+
},
29+
);
30+
31+
it('traverses immediate siblings including text and comments', () => {
32+
const document = new Document(() => {});
33+
const parent = document.documentElement;
34+
const first = parent.appendChild(document.createElement('document'));
35+
const text = parent.appendChild(document.createTextNode('text'));
36+
const comment = parent.appendChild(document.createComment('comment'));
37+
const last = parent.appendChild(document.createElement('document'));
38+
const nested = first.appendChild(document.createTextNode('nested'));
39+
40+
expect(first.previousSibling).toBeNull();
41+
expect(first.nextSibling).toBe(text);
42+
expect(text.previousSibling).toBe(first);
43+
expect(text.nextSibling).toBe(comment);
44+
expect(comment.previousSibling).toBe(text);
45+
expect(comment.nextSibling).toBe(last);
46+
expect(last.previousSibling).toBe(comment);
47+
expect(last.nextSibling).toBeNull();
48+
expect(nested.previousSibling).toBeNull();
49+
expect(nested.nextSibling).toBeNull();
50+
});
51+
52+
it('updates both directions when existing children are reordered', () => {
53+
const document = new Document(() => {});
54+
const parent = document.documentElement;
55+
const first = parent.appendChild(document.createElement('document'));
56+
const middle = parent.appendChild(document.createTextNode('middle'));
57+
const last = parent.appendChild(document.createComment('last'));
58+
59+
parent.insertBefore(last, middle);
60+
expect(first.previousSibling).toBeNull();
61+
expect(first.nextSibling).toBe(last);
62+
expect(last.previousSibling).toBe(first);
63+
expect(last.nextSibling).toBe(middle);
64+
expect(middle.previousSibling).toBe(last);
65+
expect(middle.nextSibling).toBeNull();
66+
67+
parent.appendChild(first);
68+
expect(last.previousSibling).toBeNull();
69+
expect(last.nextSibling).toBe(middle);
70+
expect(middle.previousSibling).toBe(last);
71+
expect(middle.nextSibling).toBe(first);
72+
expect(first.previousSibling).toBe(middle);
73+
expect(first.nextSibling).toBeNull();
74+
});
75+
76+
it('updates both trees when a child is moved to a different parent', () => {
77+
const document = new Document(() => {});
78+
const source = document.createElement('document');
79+
const destination = document.createElement('document');
80+
const first = source.appendChild(document.createTextNode('first'));
81+
const moved = source.appendChild(document.createElement('document'));
82+
const last = source.appendChild(document.createComment('last'));
83+
const anchor = destination.appendChild(document.createTextNode('anchor'));
84+
85+
destination.insertBefore(moved, anchor);
86+
expect(first.previousSibling).toBeNull();
87+
expect(first.nextSibling).toBe(last);
88+
expect(last.previousSibling).toBe(first);
89+
expect(last.nextSibling).toBeNull();
90+
expect(moved.previousSibling).toBeNull();
91+
expect(moved.nextSibling).toBe(anchor);
92+
expect(anchor.previousSibling).toBe(moved);
93+
expect(anchor.nextSibling).toBeNull();
94+
});
95+
96+
it('closes the sibling gap on removal and clears detached relationships', () => {
97+
const document = new Document(() => {});
98+
const parent = document.documentElement;
99+
const first = parent.appendChild(document.createTextNode('first'));
100+
const middle = parent.appendChild(document.createComment('middle'));
101+
const last = parent.appendChild(document.createElement('document'));
102+
103+
parent.removeChild(middle);
104+
expect(first.nextSibling).toBe(last);
105+
expect(last.previousSibling).toBe(first);
106+
expect(middle.previousSibling).toBeNull();
107+
expect(middle.nextSibling).toBeNull();
108+
109+
parent.removeChild(first);
110+
expect(first.previousSibling).toBeNull();
111+
expect(first.nextSibling).toBeNull();
112+
expect(last.previousSibling).toBeNull();
113+
expect(last.nextSibling).toBeNull();
114+
});
115+
});

‎packages/ui/src/dom.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,18 @@ export abstract class Node {
8585
return this.#childNodes[this.childNodes.length - 1] ?? null;
8686
}
8787

88+
get previousSibling(): Node | null {
89+
if (!this.#parentNode) return null;
90+
const siblings = this.#parentNode.#childNodes;
91+
return siblings[siblings.indexOf(this) - 1] ?? null;
92+
}
93+
94+
get nextSibling(): Node | null {
95+
if (!this.#parentNode) return null;
96+
const siblings = this.#parentNode.#childNodes;
97+
return siblings[siblings.indexOf(this) + 1] ?? null;
98+
}
99+
88100
get nodeValue(): string | null {
89101
return null;
90102
}

‎packages/ui/test/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ Rendering, scheduling, notifications, and lifecycle behavior belong in the UI te
2020

2121
[`dom.compatibility.test.ts`](../src/dom.compatibility.test.ts) registers the shared compatibility suite with Vitest. Its synchronous factory creates a fresh instance of the real [`Document`](../src/dom.ts) for each case, with a no-op render notification callback. This fixture owns no external resources, so it needs no disposal callback.
2222

23+
[`dom.test.ts`](../dom.test.ts) explicitly holds local DOM coverage missing from the enabled WPT cases. This is usually because getting the WPT test prequisites set up is too onerous a task. For example, testing node properties like nextSibling and previousSibling require things like `document.implementation.createDocumentType()`
24+
2325
[`wpt.config.ts`](wpt.config.ts) supplies the implementation's `globals` and selects the upstream files and cases enforced by this integration. Add new global bindings there. Omitting `config` runs all adapted cases; `exclude` removes matches and explicit `include` rules take precedence. Source keys support `*` and `**`; values are `true` for a whole source or arrays of exact case names and regular expressions. Unmatched rules are configuration errors. No other test groups must be enabled to run a case.
2426

2527
The configuration explicitly excludes unsupported directories. Add includes or narrow exclusions as cases pass. Selecting a source runs all of our adapted cases from that file; it does not imply complete coverage of the upstream file or interface.

0 commit comments

Comments
 (0)