diff --git a/.changeset/dom-focus-selectors.md b/.changeset/dom-focus-selectors.md new file mode 100644 index 0000000..2395b8f --- /dev/null +++ b/.changeset/dom-focus-selectors.md @@ -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. diff --git a/packages/ui/src/css-select.ts b/packages/ui/src/css-select.ts index 0d791eb..abad3c4 100644 --- a/packages/ui/src/css-select.ts +++ b/packages/ui/src/css-select.ts @@ -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['adapter']>; @@ -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', diff --git a/packages/ui/src/dom.ts b/packages/ui/src/dom.ts index ada5c88..b06e3b9 100644 --- a/packages/ui/src/dom.ts +++ b/packages/ui/src/dom.ts @@ -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'; @@ -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); } @@ -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; } @@ -1161,6 +1174,8 @@ function transitionActiveElement(document: Document, to: Element | null): void { isTrusted: true, }); } + + document.notify([current, to].filter((element) => element !== null)); } /** internal helpers **/ diff --git a/packages/ui/wpt.config.ts b/packages/ui/wpt.config.ts index e3e4ba6..9911855 100644 --- a/packages/ui/wpt.config.ts +++ b/packages/ui/wpt.config.ts @@ -71,6 +71,15 @@ const config: WPTConfig = { 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', @@ -316,6 +325,7 @@ const config: WPTConfig = { '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, @@ -352,6 +362,7 @@ const config: WPTConfig = { // 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,