Skip to content
Draft
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-id-lookup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clack/ui': patch
---

Add Document.getElementById() to find the first matching element in current tree order.
8 changes: 8 additions & 0 deletions packages/ui/src/css-select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
14 changes: 13 additions & 1 deletion packages/ui/src/dom.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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');
Expand Down
8 changes: 8 additions & 0 deletions packages/ui/wpt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading