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
3 changes: 2 additions & 1 deletion packages/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"test": "pnpm --filter @bomb.sh/web-platform-tests build && bsh test"
},
"dependencies": {
"@bomb.sh/tty": "catalog:"
"@bomb.sh/tty": "catalog:",
"css-select": "^7.0.0"
Comment thread
cowboyd marked this conversation as resolved.
Resolved
},
"devEngines": {
"packageManager": {
Expand Down
83 changes: 83 additions & 0 deletions packages/ui/src/css-select.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import type { Options } from 'css-select';
import { compile as compileSelector, selectAll, selectOne } from 'css-select';
import { Element, Node } from './dom.ts';

type Adapter = NonNullable<Options<Node, Element>['adapter']>;

const adapter: Adapter = {
isTag(node): node is Element {
return node instanceof Element;
},

getAttributeValue(element, name): string | undefined {
return element.getAttributeNS(null, name) ?? undefined;
},

getChildren(node): Node[] {
return [...node.childNodes];
},

getName(element): string {
return element.localName;
},

getParent(node): Node | null {
return node.parentNode;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be node.parentElement? I didn't check the adapter interface but this surprised me

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out that css-select is expecting any and all nodes and does explicit checking if it is an element. 🤷🏻‍♂️

},

getSiblings(node): Node[] {
return node.parentNode ? [...node.parentNode.childNodes] : [node];
},

getText(node): string {
const parts: string[] = [];
const stack = [node];
while (stack.length > 0) {
const current = stack.pop()!;
if (current.nodeType === Node.TEXT_NODE) {
parts.push(current.nodeValue ?? '');
}
for (let index = current.childNodes.length - 1; index >= 0; index--) {
stack.push(current.childNodes[index]!);
}
}
return parts.join('');
},

hasAttrib(element, name): boolean {
return element.hasAttributeNS(null, name);
},

removeSubsets(nodes): Node[] {
const candidates = new Set(nodes);
return [...candidates].filter((node) => {
for (let parent = node.parentNode; parent; parent = parent.parentNode) {
if (candidates.has(parent)) {
return false;
}
}
return true;
});
},
};

export function compile(selector: string): (node: Node) => boolean {
try {
return compileSelector(selector, { adapter });
} catch (error) {
throw new DOMException(
error instanceof Error ? error.message : 'Invalid selector',
'SyntaxError',
);
}
}

export function querySelector(nodes: Node[], selector: string): Element | null {
const query = compile(selector);
return selectOne(query, nodes, { adapter });
}

export function querySelectorAll(nodes: Node[], selector: string): Element[] {
const query = compile(selector);
return selectAll(query, nodes, { adapter });
}
5 changes: 5 additions & 0 deletions packages/ui/src/dom.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* oxlint-disable bombshell-dev/no-generic-error -- Web IDL requires TypeError for missing arguments. */

import { querySelector } from './css-select.ts';
import { asciiUppercase, asciiLowercase } from './ascii.ts';
import { NamedNodeMap } from './named-node-map.ts';
import {
Expand Down Expand Up @@ -311,6 +312,10 @@ export class Document extends Node {
return null;
}

querySelector(selector: string): Element | null {
return querySelector(Array.from(this.childNodes), selector);
}

createElement(tagName: string): Element {
if (arguments.length === 0) {
throw new TypeError('createElement requires a tag name');
Expand Down
75 changes: 75 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading