Repository navigation
Add Document.querySelector for native WPT fixture lookup #86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Turns out that |
||
| }, | ||
|
|
||
| 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 }); | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.