Skip to content
Merged
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-query-selector-all.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clack/ui': minor
---

Add `querySelectorAll()` methods to `Document`, `Element`, and `DocumentFragment`.
13 changes: 8 additions & 5 deletions packages/ui/src/css-select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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 });
}
23 changes: 22 additions & 1 deletion packages/ui/src/dom.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down
42 changes: 42 additions & 0 deletions packages/ui/wpt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,48 @@ const config: WPTConfig<Document> = {
'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,
Expand Down
Loading