From 230f116c748abe9d497555ab44c8109f814240d2 Mon Sep 17 00:00:00 2001 From: Charles Lowell Date: Tue, 17 Nov 2026 11:54:29 -0600 Subject: [PATCH] Add Element.matches using the shared selector adapter --- packages/ui/src/dom.ts | 6 +++++- packages/ui/wpt.config.ts | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/ui/src/dom.ts b/packages/ui/src/dom.ts index 971806d..570b652 100644 --- a/packages/ui/src/dom.ts +++ b/packages/ui/src/dom.ts @@ -1,6 +1,6 @@ /* oxlint-disable bombshell-dev/no-generic-error -- Web IDL requires TypeError for missing arguments. */ -import { querySelector } from './css-select.ts'; +import { compile, querySelector } from './css-select.ts'; import { asciiUppercase, asciiLowercase } from './ascii.ts'; import { NamedNodeMap } from './named-node-map.ts'; import { HTMLCollection } from './html-collection.ts'; @@ -633,6 +633,10 @@ export class Element extends Node { return this.#attributes.toggleAttribute(name, force); } + matches(selector: string): boolean { + return compile(selector)(this); + } + get id(): string { return this.#attributes.getNamedItemNS(null, 'id')?.value ?? ''; } diff --git a/packages/ui/wpt.config.ts b/packages/ui/wpt.config.ts index da32fd3..bf21128 100644 --- a/packages/ui/wpt.config.ts +++ b/packages/ui/wpt.config.ts @@ -142,6 +142,22 @@ const config: WPTConfig = { 'dom/nodes/CharacterData-data.html': true, 'dom/nodes/CharacterData-remove.html': true, 'dom/nodes/Element-remove.html': true, + 'dom/nodes/Element-matches-namespaced-elements.html': [ + '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/ParentNode-querySelector-invalid-comment-like.html': [ + 'matches throws SyntaxError for comment-like selector', + ], + '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. + /^Expected DIV element to (?!.*\(n\)).* with matches$/, + ], 'dom/nodes/Element-setAttribute.html': true, 'dom/nodes/Element-setAttributeNodeNS.html': true, 'dom/nodes/Element-removeAttribute.html': true,