From d3123ed29636448b3299c7f20de485fa1c55badc Mon Sep 17 00:00:00 2001 From: Charles Lowell Date: Tue, 17 Nov 2026 11:56:07 -0600 Subject: [PATCH] Add element queries and static querySelectorAll results --- packages/ui/src/dom.ts | 23 +++++++++++- packages/ui/src/node-list.ts | 59 +++++++++++++++++++++++++++++ packages/ui/wpt.config.ts | 73 ++++++++++++++++++++++++++++++++---- 3 files changed, 147 insertions(+), 8 deletions(-) create mode 100644 packages/ui/src/node-list.ts diff --git a/packages/ui/src/dom.ts b/packages/ui/src/dom.ts index 570b652..af08dd9 100644 --- a/packages/ui/src/dom.ts +++ b/packages/ui/src/dom.ts @@ -1,6 +1,7 @@ /* oxlint-disable bombshell-dev/no-generic-error -- Web IDL requires TypeError for missing arguments. */ -import { compile, querySelector } from './css-select.ts'; +import { compile, querySelector, querySelectorAll } from './css-select.ts'; +import { NodeList } from './node-list.ts'; import { asciiUppercase, asciiLowercase } from './ascii.ts'; import { NamedNodeMap } from './named-node-map.ts'; import { HTMLCollection } from './html-collection.ts'; @@ -385,6 +386,10 @@ export class Document extends Node { return null; } + querySelectorAll(selector: string): NodeList { + return new NodeList(querySelectorAll(Array.from(this.childNodes), selector)); + } + createElement(tagName: string): Element { if (arguments.length === 0) { throw new TypeError('createElement requires a tag name'); @@ -484,6 +489,14 @@ export class DocumentFragment extends Node { this.appendChild(this.ownerDocument.createTextNode(data)); } } + + querySelector(selector: string): Element | null { + return querySelector(Array.from(this.childNodes), selector); + } + + querySelectorAll(selector: string): NodeList { + return new NodeList(querySelectorAll(Array.from(this.childNodes), selector)); + } } export class DocumentType extends Node { @@ -637,6 +650,14 @@ export class Element extends Node { return compile(selector)(this); } + querySelector(selector: string): Element | null { + return querySelector(Array.from(this.childNodes), selector); + } + + querySelectorAll(selector: string): NodeList { + return new NodeList(querySelectorAll(Array.from(this.childNodes), selector)); + } + get id(): string { return this.#attributes.getNamedItemNS(null, 'id')?.value ?? ''; } diff --git a/packages/ui/src/node-list.ts b/packages/ui/src/node-list.ts new file mode 100644 index 0000000..2faa053 --- /dev/null +++ b/packages/ui/src/node-list.ts @@ -0,0 +1,59 @@ +/* oxlint-disable bombshell-dev/no-generic-error -- Web IDL requires TypeError for missing arguments. */ +import type { Element } from './dom.ts'; + +/** + * A static snapshot of nodes, as returned by querySelectorAll. + * Full Web IDL indexed-property deletion and redefinition are not supported. + */ +export class NodeList implements Iterable { + readonly [index: number]: Element; + #elements: Element[]; + + constructor(nodes: Element[]) { + this.#elements = nodes; + for (const [index, node] of nodes.entries()) { + Object.defineProperty(this, index, { + value: node, + writable: false, + enumerable: true, + configurable: true, + }); + } + } + + get length(): number { + return this.#elements.length; + } + + item(index: number): Element | null { + if (arguments.length === 0) { + throw new TypeError('item requires an index'); + } + return this.#elements[index >>> 0] ?? null; + } + + keys(): ArrayIterator { + return this.#elements.keys(); + } + + values(): ArrayIterator { + return this.#elements.values(); + } + + entries(): ArrayIterator<[number, Element]> { + return this.#elements.entries(); + } + + forEach( + callback: (value: Element, index: number, list: NodeList) => void, + thisArg?: unknown, + ): void { + this.#elements.forEach((element, index) => { + callback.call(thisArg, element, index, this); + }); + } + + [Symbol.iterator](): ArrayIterator { + return this.#elements[Symbol.iterator](); + } +} diff --git a/packages/ui/wpt.config.ts b/packages/ui/wpt.config.ts index bf21128..79a328a 100644 --- a/packages/ui/wpt.config.ts +++ b/packages/ui/wpt.config.ts @@ -146,17 +146,76 @@ const config: WPTConfig = { 'empty string namespace, matches', 'has a namespace, matches', ], - 'dom/nodes/Element-matches-active-pseudo-class.html': [ - 'Regular div should not match :active by default', - 'Button should not match :active by default', - 'Link should not match :active by default', + 'dom/nodes/Element-matches-active-pseudo-class.html': true, + 'dom/nodes/Element-querySelectorAll-context-element.html': true, + 'dom/nodes/DocumentFragment-querySelectorAll-after-modification.html': true, + 'dom/nodes/ParentNode-querySelector-All-comma-selector.html': true, + 'dom/nodes/ParentNode-querySelector-escapes.html': [ + '"nonescaped" should match with "#nonescaped"', + '"0nextIsWhiteSpace" should match with "#\\\\30 nextIsWhiteSpace"', + '"0nextIsNotHexLetters" should match with "#\\\\30nextIsNotHexLetters"', + '"0connectHexMoreThan6Hex" should match with "#\\\\000030connectHexMoreThan6Hex"', + '"0spaceMoreThan6Hex" should match with "#\\\\000030 spaceMoreThan6Hex"', + '"outOfRange0" should never match with "#outOfRange\\\\110030"', + '".comma" should match with "#\\\\.comma"', + '"-minus" should match with "#\\\\-minus"', + '"g" should match with "#\\\\g"', + '"aBMPRegular" should match with "#\\\\61 BMPRegular"', + '"\ud83d\udd11nonBMP" should match with "#\\\\1f511 nonBMP"', + '"00continueEscapes" should match with "#\\\\30\\\\30 continueEscapes"', + '"00continueEscapes" should match with "#\\\\30 \\\\30 continueEscapes"', + '"continueEscapes00" should match with "#continueEscapes\\\\30 \\\\30 "', + '"continueEscapes00" should match with "#continueEscapes\\\\30 \\\\30"', + '"continueEscapes00" should match with "#continueEscapes\\\\30\\\\30 "', + '"continueEscapes00" should match with "#continueEscapes\\\\30\\\\30"', + '"hello" should match with "#hel\\\\6Co"', + '"&B" should match with "#\\\\26 B"', + '"spaces" should match with "#sp\\\\61\\tc\\\\65\\fs"', + '"test\ud799" should match with "#test\\\\D799"', + '"\ue000" should match with "#\\\\E000"', + '"test" should match with "#te\\\\s\\\\t"', + '"spaces in\\tident" should match with "#spaces\\\\ in\\\\\\tident"', + '".,:!" should match with "#\\\\.\\\\,\\\\:\\\\!"', + '"\udbff\udfff" should match with "#\\\\10fFfF"', + '"\udbff\udfff0" should match with "#\\\\10fFfF0"', + '"\udbc0\udc0000" should match with "#\\\\10000000"', + '"simple-ident" should match with "#simple-ident"', + '"testing123" should match with "#testing123"', + '"_underscore" should match with "#_underscore"', + '"-text" should match with "#-text"', + '"-m" should match with "#-\\\\6d"', + '"--abc" should match with "#--abc"', + '"--" should match with "#--"', + '"--11" should match with "#--11"', + '"---" should match with "#---"', + '"\u2003" should match with "#\u2003"', + '"\u1234" should match with "#\u1234"', + '"\ud808\udf45" should match with "#\ud808\udf45"', + // Remaining cases expose escape parsing and invalid code point handling gaps. ], - 'dom/nodes/ParentNode-querySelector-invalid-comment-like.html': [ - 'matches throws SyntaxError for comment-like selector', + 'dom/nodes/ParentNode-querySelector-invalid-comment-like.html': true, + 'dom/nodes/ParentNode-querySelector-nested.html': true, + 'dom/nodes/ParentNode-querySelector-orphan-scope.html': true, + 'dom/nodes/ParentNode-querySelector-tag-case.html': [ + 'Tag name case-insensitivity with child combinator (>) dynamically created', + ], + 'dom/nodes/ParentNode-querySelectorAll-not-descendant.html': true, + 'dom/nodes/NodeList-Iterable.html': [ + 'NodeList has length method.', + 'NodeList has values method.', + 'NodeList has entries method.', + 'NodeList has forEach method.', + 'NodeList has Symbol.iterator.', + 'NodeList is iterable via for-of loop.', + 'NodeList responds to Object.keys correctly', + // The remaining case exercises live childNodes rather than querySelectorAll results. ], 'css/selectors/child-indexed-pseudo-class.html': [ - // Defer the four detached :nth-*(n) cases until https://github.com/fb55/css-select/issues/1945 is fixed. + // Defer :nth-*(n) cases for elements without an Element parent until + // https://github.com/fb55/css-select/issues/1945 is fixed. /^Expected DIV element to (?!.*\(n\)).* with matches$/, + /^Expected HTML element to (?!.*\(n\)).* with matches, querySelector\(\), and querySelectorAll\(\)$/, + /^Expected DIV element to (?!.*\(n\)).* with matches, querySelector\(\), and querySelectorAll\(\)$/, ], 'dom/nodes/Element-setAttribute.html': true, 'dom/nodes/Element-setAttributeNodeNS.html': true,