Skip to content
Open
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
7 changes: 7 additions & 0 deletions .changeset/dom-focus-selectors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@clack/ui': minor
---

Add Element.matches() and :focus, :focus-within, and :focus-visible matching.
Treat focused elements as focus-visible in the terminal.
Notify the renderer of the previous and next focused elements when focus changes.
18 changes: 16 additions & 2 deletions packages/ui/src/css-select.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Options } from 'css-select';
import { compile as compileSelector, selectAll, selectOne } from 'css-select';
import { Element, Node } from './dom.ts';
import { Element, getActiveElement, Node } from './dom.ts';

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

Expand Down Expand Up @@ -62,8 +62,22 @@ const adapter: Adapter = {
};

export function compile(selector: string, context?: Node): (node: Node) => boolean {
if (selector.trim() === '') {
throw new DOMException('A selector must not be empty', 'SyntaxError');
}
try {
return compileSelector(selector, { adapter, context, relativeSelector: false });
return compileSelector(selector, {
adapter,
context,
relativeSelector: false,
cacheResults: false,
pseudos: {
focus: (element) => getActiveElement(element.ownerDocument) === element,
// Focused elements always indicate focus in the terminal.
'focus-visible': ':focus',
'focus-within': (element) => element.contains(getActiveElement(element.ownerDocument)),
},
});
} catch (error) {
throw new DOMException(
error instanceof Error ? error.message : 'Invalid selector',
Expand Down
19 changes: 17 additions & 2 deletions packages/ui/src/dom.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
/* 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, querySelectorAll } from './css-select.ts';
import {
compile,
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 @@ -662,6 +668,13 @@ export class Element extends Node {
return querySelectorAll(this, { selector: `${selector}` });
}

matches(selector: string): boolean {
if (arguments.length === 0) {
throw new TypeError('matches requires a selector');
}
return compile(`${selector}`, this)(this);
}

override get textContent(): string {
return getTextContent(this);
}
Expand Down Expand Up @@ -1117,7 +1130,7 @@ function setActiveElement(document: Document, element: Element | null): void {
}
}

function getActiveElement(document: Document): Element | null {
export function getActiveElement(document: Document): Element | null {
return activeElements.get(document) ?? null;
}

Expand Down Expand Up @@ -1161,6 +1174,8 @@ function transitionActiveElement(document: Document, to: Element | null): void {
isTrusted: true,
});
}

document.notify([current, to].filter((element) => element !== null));
}

/** internal helpers **/
Expand Down
11 changes: 11 additions & 0 deletions packages/ui/wpt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ const config: WPTConfig<Document> = {
XMLDocument,
},
include: {
'css/selectors/focus-in-focus-event-001.html': true,
'css/selectors/focus-in-focusin-event-001.html': true,
'css/selectors/focus-within.html': true,
'dom/nodes/Element-matches-namespaced-elements.html': [
'empty string namespace, matches',
'has a namespace, matches',
// Wildcard namespace selectors and webkitMatchesSelector remain unsupported.
],
'dom/nodes/ParentNode-querySelector-matches.html': true,
'html/syntax/parsing/the-end.html': ['DOMContentLoaded', 'load'],
'html/browsers/the-window-object/Document-defaultView.html': [
'Document in a browsing context',
Expand Down Expand Up @@ -316,6 +325,7 @@ const config: WPTConfig<Document> = {
'dom/nodes/ParentNode-querySelector-empty-attribute-value.html': true,
'dom/nodes/ParentNode-querySelector-invalid-comment-like.html': [
'querySelectorAll throws SyntaxError for comment-like selector',
'matches throws SyntaxError for comment-like selector',
],
'dom/nodes/ParentNode-querySelector-is-nested-pseudo-class.html': true,
'dom/nodes/ParentNode-querySelector-orphan-scope.html': true,
Expand Down Expand Up @@ -352,6 +362,7 @@ const config: WPTConfig<Document> = {
// The focused-descendant removal case requires innerHTML mutation.
],
'dom/nodes/insertion-removing-steps/blur-event.window.js': true,
'html/editing/focus/focus-management/focus-blur.html': true,
'html/editing/focus/focus-management/focusing-disconnected-noop.html': true,
// focusing-focused-noop.html requires the innerHTML setter for setup and cleanup.
'html/interaction/focus/focus-management/focus-events.html': true,
Expand Down
Loading