From 6ddaf784cf8348c2ba4721ecd094b91fd50b3b0e Mon Sep 17 00:00:00 2001 From: Charles Lowell Date: Wed, 18 Nov 2026 07:50:19 -0600 Subject: [PATCH] Add ParentNode.querySelectorAll() --- .changeset/dom-query-selector-all.md | 5 ++++ packages/ui/src/css-select.ts | 13 +++++---- packages/ui/src/dom.ts | 23 ++++++++++++++- packages/ui/wpt.config.ts | 42 ++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 6 deletions(-) create mode 100644 .changeset/dom-query-selector-all.md diff --git a/.changeset/dom-query-selector-all.md b/.changeset/dom-query-selector-all.md new file mode 100644 index 00000000..7544c444 --- /dev/null +++ b/.changeset/dom-query-selector-all.md @@ -0,0 +1,5 @@ +--- +'@clack/ui': minor +--- + +Add `querySelectorAll()` methods to `Document`, `Element`, and `DocumentFragment`. diff --git a/packages/ui/src/css-select.ts b/packages/ui/src/css-select.ts index d393a9d5..0d791eb0 100644 --- a/packages/ui/src/css-select.ts +++ b/packages/ui/src/css-select.ts @@ -61,9 +61,9 @@ const adapter: Adapter = { }, }; -export function compile(selector: string): (node: Node) => boolean { +export function compile(selector: string, context?: Node): (node: Node) => boolean { try { - return compileSelector(selector, { adapter }); + return compileSelector(selector, { adapter, context, relativeSelector: false }); } catch (error) { throw new DOMException( error instanceof Error ? error.message : 'Invalid selector', @@ -88,7 +88,10 @@ export function querySelector(nodes: Node[], selector: string): Element | null { return selectOne(query, nodes, { adapter }); } -export function querySelectorAll(nodes: Node[], selector: string): Element[] { - const query = compile(selector); - return selectAll(query, nodes, { adapter }); +export function querySelectorAll( + root: Node, + { selector, scope }: { selector: string; scope?: Node | null }, +): Element[] { + const query = compile(selector, scope ?? root); + return selectAll(query, [...root.childNodes], { adapter }); } diff --git a/packages/ui/src/dom.ts b/packages/ui/src/dom.ts index 1e5d89c5..cbd56aae 100644 --- a/packages/ui/src/dom.ts +++ b/packages/ui/src/dom.ts @@ -1,7 +1,7 @@ /* oxlint-disable bombshell-dev/no-generic-error -- Web IDL requires TypeError for missing arguments. */ /* oxlint-disable bombshell-dev/exported-function-async -- DOM apis are synchronous. */ -import { findElement, findElements, querySelector } from './css-select.ts'; +import { findElement, findElements, querySelector, querySelectorAll } from './css-select.ts'; import { asciiUppercase, asciiLowercase } from './ascii.ts'; import { exposeAttributes, NamedNodeMap } from './named-node-map.ts'; import { HTMLCollection } from './html-collection.ts'; @@ -378,6 +378,13 @@ export class Document extends Node { return querySelector(Array.from(this.childNodes), selector); } + querySelectorAll(selector: string): Element[] { + if (arguments.length === 0) { + throw new TypeError('querySelectorAll requires a selector'); + } + return querySelectorAll(this, { selector: `${selector}`, scope: this.documentElement }); + } + getElementsByTagName(qualifiedName: string): HTMLCollection { if (arguments.length === 0) { throw new TypeError('getElementsByTagName requires a qualified name'); @@ -508,6 +515,13 @@ export class DocumentFragment extends Node { Element.prototype.append.call(this, ...nodes); } + querySelectorAll(selector: string): Element[] { + if (arguments.length === 0) { + throw new TypeError('querySelectorAll requires a selector'); + } + return querySelectorAll(this, { selector: `${selector}` }); + } + override get textContent(): string { return getTextContent(this); } @@ -618,6 +632,13 @@ export class Element extends Node { return serializeOuter(this); } + querySelectorAll(selector: string): Element[] { + if (arguments.length === 0) { + throw new TypeError('querySelectorAll requires a selector'); + } + return querySelectorAll(this, { selector: `${selector}` }); + } + override get textContent(): string { return getTextContent(this); } diff --git a/packages/ui/wpt.config.ts b/packages/ui/wpt.config.ts index 67f59a8d..eee44e65 100644 --- a/packages/ui/wpt.config.ts +++ b/packages/ui/wpt.config.ts @@ -288,6 +288,48 @@ const config: WPTConfig = { 'getElementsByTagName() should be a live collection', // Preserve the existing selection; interface and named own-property coverage remains deferred. ], + 'dom/nodes/NodeList-Iterable.html': [/^NodeList /], + 'dom/nodes/ParentNode-querySelector-All-comma-selector.html': true, + 'dom/nodes/ParentNode-querySelector-All-multiple-not.html': true, + 'dom/nodes/ParentNode-querySelector-All-scope.html': [ + 'querySelectorAll', + 'querySelectorAll comma separated', + 'querySelectorAll with :scope > child should only match direct children', + ], + 'dom/nodes/ParentNode-querySelector-argument-stringification.html': [ + 'querySelectorAll stringifies Array argument via toString()', + ], + 'dom/nodes/ParentNode-querySelector-case-insensitive.html': ['querySelectorAll'], + 'dom/nodes/ParentNode-querySelector-empty-attribute-value.html': true, + 'dom/nodes/ParentNode-querySelector-invalid-comment-like.html': [ + 'querySelectorAll throws SyntaxError for comment-like selector', + ], + 'dom/nodes/ParentNode-querySelector-is-nested-pseudo-class.html': true, + 'dom/nodes/ParentNode-querySelector-orphan-scope.html': true, + 'dom/nodes/ParentNode-querySelector-scope.html': [ + 'querySelectorAll with :scope', + 'querySelectorAll with id and sibling', + ], + 'dom/nodes/ParentNode-querySelector-scope-in-not.html': true, + 'dom/nodes/ParentNode-querySelector-tag-case.html': [ + 'The same NodeList should be returned for querySelectorAll with lowercase and uppercase p selectors', + 'Mixed case selectors should work correctly', + 'Tag name case-insensitivity with child combinator (>) dynamically created', + ], + 'dom/nodes/ParentNode-querySelectorAll-case-insensitive-attribute-flag.html': true, + 'dom/nodes/ParentNode-querySelectorAll-nested-not-has.html': true, + 'dom/nodes/ParentNode-querySelectorAll-not-descendant.html': true, + 'dom/nodes/ParentNode-querySelectorAll-scope-colon-id-class.html': [ + ':scope child selector on element with colon-prefixed class', + ], + 'dom/nodes/ParentNode-querySelectorAll-svg-class.html': true, + 'dom/nodes/ParentNode-querySelectors-space-and-dash-attribute-value.html': ['querySelectorAll'], + 'dom/nodes/Element-querySelectorAll-context-element.html': true, + 'dom/nodes/DocumentFragment-querySelectorAll-after-modification.html': true, + 'dom/nodes/querySelector-empty-id.html': true, + 'dom/nodes/querySelector-id-nth-child.html': [ + 'querySelectorAll matching #id + constant nth-child()', + ], 'dom/nodes/Document-createEvent.https.html': [ /^(?:Event|Events|HTMLEvents|SVGEvents) should be an alias for Event\.$/i, /^createEvent\('(?:Event|Events|HTMLEvents|SVGEvents)'\) should be initialized correctly\.$/i,