Skip to content
Closed
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
23 changes: 22 additions & 1 deletion packages/ui/src/dom.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 ?? '';
}
Expand Down
59 changes: 59 additions & 0 deletions packages/ui/src/node-list.ts
Original file line number Diff line number Diff line change
@@ -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<Element> {
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<number> {
return this.#elements.keys();
}

values(): ArrayIterator<Element> {
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<Element> {
return this.#elements[Symbol.iterator]();
}
}
73 changes: 66 additions & 7 deletions packages/ui/wpt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading