diff --git a/.changeset/dom-id-lookup.md b/.changeset/dom-id-lookup.md new file mode 100644 index 0000000..96b16af --- /dev/null +++ b/.changeset/dom-id-lookup.md @@ -0,0 +1,5 @@ +--- +'@clack/ui': patch +--- + +Add Document.getElementById() to find the first matching element in current tree order. diff --git a/packages/ui/src/css-select.ts b/packages/ui/src/css-select.ts index cc4b451..a1c867f 100644 --- a/packages/ui/src/css-select.ts +++ b/packages/ui/src/css-select.ts @@ -72,6 +72,14 @@ export function compile(selector: string): (node: Node) => boolean { } } +export function getElementById(nodes: Node[], id: string): Element | null { + return selectOne((element) => element.id === id, nodes, { + adapter, + // Traverse template descendants like other elements for literal ID lookup. + xmlMode: true, + }); +} + export function querySelector(nodes: Node[], selector: string): Element | null { const query = compile(selector); return selectOne(query, nodes, { adapter }); diff --git a/packages/ui/src/dom.ts b/packages/ui/src/dom.ts index 4833873..506f9a1 100644 --- a/packages/ui/src/dom.ts +++ b/packages/ui/src/dom.ts @@ -1,6 +1,6 @@ /* oxlint-disable bombshell-dev/no-generic-error -- Web IDL requires TypeError for missing arguments. */ -import { querySelector } from './css-select.ts'; +import { getElementById, querySelector } from './css-select.ts'; import { asciiUppercase, asciiLowercase } from './ascii.ts'; import { NamedNodeMap } from './named-node-map.ts'; import { @@ -316,6 +316,18 @@ export class Document extends Node { return querySelector(Array.from(this.childNodes), selector); } + getElementById(id: string): Element | null { + if (arguments.length === 0) { + throw new TypeError('getElementById requires an ID'); + } + const name = `${id}`; + if (name === '') { + return null; + } + + return getElementById(Array.from(this.childNodes), name); + } + createElement(tagName: string): Element { if (arguments.length === 0) { throw new TypeError('createElement requires a tag name'); diff --git a/packages/ui/wpt.config.ts b/packages/ui/wpt.config.ts index 90cec07..6d0d9fc 100644 --- a/packages/ui/wpt.config.ts +++ b/packages/ui/wpt.config.ts @@ -47,6 +47,14 @@ const config: WPTConfig = { XMLDocument, }, include: { + 'html/dom/elements/global-attributes/id-name.html': true, + 'html/dom/elements/global-attributes/id-attribute.html': [ + 'After setting id via id attribute, getElementById find the element by the new id.', + "After setting id via id attribute, getElementById doesn't find the element by the old id.", + 'After setting id via setAttribute attribute, getElementById find the element by the new id.', + "After setting id via setAttribute attribute, getElementById doesn't find the element by the old id.", + // Other lookup and CSS cases require getElementsByTagName() or getComputedStyle(). + ], 'dom/events/Event-constructors.any.js': true, 'dom/events/EventTarget-addEventListener.any.js': true, 'dom/events/EventTarget-add-remove-listener.any.js': true,