File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ ---
2+ ' @clack/ui ' : patch
3+ ---
4+
5+ Add Document.getElementById() to find the first matching element in current tree order.
Original file line number Diff line number Diff 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+
7583export function querySelector ( nodes : Node [ ] , selector : string ) : Element | null {
7684 const query = compile ( selector ) ;
7785 return selectOne ( query , nodes , { adapter } ) ;
Original file line number Diff line number Diff line change 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' ;
44import { asciiUppercase , asciiLowercase } from './ascii.ts' ;
55import { NamedNodeMap } from './named-node-map.ts' ;
66import {
@@ -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' ) ;
Original file line number Diff line number Diff 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 ,
You can’t perform that action at this time.
0 commit comments