Skip to content

Commit eee4cc2

Browse files
committed
Support document ID lookup
1 parent aff2e8f commit eee4cc2

3 files changed

Lines changed: 58 additions & 1 deletion

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clack/ui': patch
3+
---
4+
5+
Add document ID lookup in current tree order, ignoring detached subtrees.

‎packages/ui/src/dom.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,30 @@ export class Document {
2222
this.documentElement = new Element(this, 'document', notify);
2323
}
2424

25+
getElementById(id: string): Element | null {
26+
if (arguments.length === 0) {
27+
throw new TypeError('getElementById requires an ID');
28+
}
29+
const name = `${id}`;
30+
if (name === '') {
31+
return null;
32+
}
33+
const stack = [this.documentElement];
34+
while (stack.length > 0) {
35+
const element = stack.pop()!;
36+
if (element.id === name) {
37+
return element;
38+
}
39+
for (let i = element.childNodes.length - 1; i >= 0; i--) {
40+
const child = element.childNodes[i];
41+
if (child instanceof Element) {
42+
stack.push(child);
43+
}
44+
}
45+
}
46+
return null;
47+
}
48+
2549
createElement(tagName: string): Element {
2650
if (arguments.length === 0) {
2751
throw new TypeError('createElement requires a tag name');

‎packages/ui/test/dom.test.ts‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,39 @@
22
* Local DOM coverage not provided by our enabled WPT cases.
33
* These are independent tests, not adaptations of upstream WPT tests.
44
* Sibling traversal lives here while Node-properties.html requires unsupported
5-
* HTML/XML document fixtures. Prefer WPT coverage when its dependencies are met.
5+
* HTML/XML document fixtures. Document-getElementById.html also needs parsed HTML.
6+
* Prefer WPT coverage when its dependencies are met.
67
*/
78
import { describe, expect, it } from 'vitest';
89
import { Document } from '../src/dom.ts';
910

11+
it('getElementById follows current tree order and ignores detached subtrees', () => {
12+
const document = new Document(() => {});
13+
const first = document.createElement('div');
14+
const nested = document.createElement('span');
15+
const later = document.createElement('div');
16+
nested.id = 'match';
17+
later.id = 'match';
18+
first.appendChild(nested);
19+
expect(document.getElementById('match')).toBeNull();
20+
expect(document.getElementById('')).toBeNull();
21+
22+
document.documentElement.appendChild(first);
23+
document.documentElement.appendChild(later);
24+
expect(document.getElementById('match')).toBe(nested);
25+
26+
document.documentElement.insertBefore(later, first);
27+
expect(document.getElementById('match')).toBe(later);
28+
later.remove();
29+
expect(document.getElementById('match')).toBe(nested);
30+
31+
nested.id = 'changed';
32+
expect(document.getElementById('match')).toBeNull();
33+
expect(document.getElementById('changed')).toBe(nested);
34+
first.remove();
35+
expect(document.getElementById('changed')).toBeNull();
36+
});
37+
1038
describe('Node sibling properties outside WPT coverage', () => {
1139
it.each(['createElement', 'createTextNode', 'createComment'] as const)(
1240
'%s has no siblings when detached or an only child',

0 commit comments

Comments
 (0)