Skip to content

Commit 5fc9851

Browse files
committed
Add document ID lookup before focus support
1 parent 0e018ce commit 5fc9851

4 files changed

Lines changed: 34 additions & 1 deletion

File tree

‎.changeset/dom-id-lookup.md‎

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.getElementById() to find the first matching element in current tree order.

‎packages/ui/src/css-select.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ export function compile(selector: string): (node: Node) => boolean {
7272
}
7373
}
7474

75+
export function getElementById(nodes: Node[], id: string): Element | null {
76+
return selectOne((element) => element.id === id, nodes, {
77+
adapter,
78+
// Traverse template descendants like other elements for literal ID lookup.
79+
xmlMode: true,
80+
});
81+
}
82+
7583
export function querySelector(nodes: Node[], selector: string): Element | null {
7684
const query = compile(selector);
7785
return selectOne(query, nodes, { adapter });

‎packages/ui/src/dom.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* oxlint-disable bombshell-dev/no-generic-error -- Web IDL requires TypeError for missing arguments. */
22

3-
import { querySelector } from './css-select.ts';
3+
import { getElementById, querySelector } from './css-select.ts';
44
import { asciiUppercase, asciiLowercase } from './ascii.ts';
55
import { NamedNodeMap } from './named-node-map.ts';
66
import {
@@ -316,6 +316,18 @@ export class Document extends Node {
316316
return querySelector(Array.from(this.childNodes), selector);
317317
}
318318

319+
getElementById(id: string): Element | null {
320+
if (arguments.length === 0) {
321+
throw new TypeError('getElementById requires an ID');
322+
}
323+
const name = `${id}`;
324+
if (name === '') {
325+
return null;
326+
}
327+
328+
return getElementById(Array.from(this.childNodes), name);
329+
}
330+
319331
createElement(tagName: string): Element {
320332
if (arguments.length === 0) {
321333
throw new TypeError('createElement requires a tag name');

‎packages/ui/wpt.config.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ const config: WPTConfig = {
4747
XMLDocument,
4848
},
4949
include: {
50+
'html/dom/elements/global-attributes/id-name.html': true,
51+
'html/dom/elements/global-attributes/id-attribute.html': [
52+
'After setting id via id attribute, getElementById find the element by the new id.',
53+
"After setting id via id attribute, getElementById doesn't find the element by the old id.",
54+
'After setting id via setAttribute attribute, getElementById find the element by the new id.',
55+
"After setting id via setAttribute attribute, getElementById doesn't find the element by the old id.",
56+
// Other lookup and CSS cases require getElementsByTagName() or getComputedStyle().
57+
],
5058
'dom/events/Event-constructors.any.js': true,
5159
'dom/events/EventTarget-addEventListener.any.js': true,
5260
'dom/events/EventTarget-add-remove-listener.any.js': true,

0 commit comments

Comments
 (0)