From e131258b939e31d5f05352589087da6ab37c6c47 Mon Sep 17 00:00:00 2001 From: Charles Lowell Date: Mon, 23 Nov 2026 15:34:46 -0600 Subject: [PATCH] Add label control association and focus forwarding --- .changeset/dom-label-focus.md | 5 ++++ packages/ui/src/dom.ts | 53 +++++++++++++++++++++++++++++++++++ packages/ui/wpt.config.ts | 29 +++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 .changeset/dom-label-focus.md diff --git a/.changeset/dom-label-focus.md b/.changeset/dom-label-focus.md new file mode 100644 index 0000000..71bb02f --- /dev/null +++ b/.changeset/dom-label-focus.md @@ -0,0 +1,5 @@ +--- +'@clack/ui': minor +--- + +Add label control association and focus forwarding. diff --git a/packages/ui/src/dom.ts b/packages/ui/src/dom.ts index ada5c88..3f15380 100644 --- a/packages/ui/src/dom.ts +++ b/packages/ui/src/dom.ts @@ -747,6 +747,16 @@ export class Element extends Node { return this.#attributes.toggleAttribute(name, force); } + getRootNode(): Node { + // oxlint-disable-next-line typescript/no-this-alias -- This cursor walks the node's ancestry. + for (let node: Node | null = this; node; node = node.parentNode) { + if (!node.parentNode) { + return node; + } + } + return this; + } + get id(): string { return this.#attributes.getNamedItemNS(null, 'id')?.value ?? ''; } @@ -977,6 +987,7 @@ export class HTMLDocument extends Document { ['title', HTMLTitleElement], ['body', HTMLBodyElement], ['button', HTMLButtonElement], + ['label', HTMLLabelElement], ['span', HTMLSpanElement], ['div', HTMLDivElement], ]); @@ -1099,6 +1110,33 @@ export class HTMLDivElement extends HTMLElement {} export class HTMLButtonElement extends mixin(HTMLElement, [Focusable]) {} +export class HTMLLabelElement extends HTMLElement { + get control(): HTMLElement | null { + const id = this.getAttributeNodeNS(null, 'for'); + if (id) { + if (id.value === '') { + return null; + } + + const root = this.getRootNode(); + const nodes = root instanceof Element ? [root] : [...root.childNodes]; + const target = findElement(nodes, (element) => element.id === id.value); + + return target instanceof HTMLElement && isLabelable(target) ? target : null; + } else { + return findElement([this], isLabelable) as HTMLElement; + } + } + + override focus(): void { + if (parseTabIndex(this.getAttributeNS(null, 'tabindex')) !== null) { + super.focus(); + } else { + this.control?.focus(); + } + } +} + export class HTMLUnknownElement extends HTMLElement {} /** XML */ @@ -1163,6 +1201,21 @@ function transitionActiveElement(document: Document, to: Element | null): void { } } +function isLabelable(element: Element): element is HTMLElement { + if (element instanceof HTMLElement) { + if ( + element.localName === 'input' && + asciiLowercase(element.getAttributeNS(null, 'type') ?? '') === 'hidden' + ) { + return false; + } + return ['button', 'input', 'meter', 'output', 'progress', 'select', 'textarea'].includes( + element.localName, + ); + } + return false; +} + /** internal helpers **/ function getTextContent(node: Element | DocumentFragment): string { diff --git a/packages/ui/wpt.config.ts b/packages/ui/wpt.config.ts index e3e4ba6..b063114 100644 --- a/packages/ui/wpt.config.ts +++ b/packages/ui/wpt.config.ts @@ -355,6 +355,35 @@ const config: WPTConfig = { '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, + 'inert/inert-label-focus.html': [ + 'Calling focus() on an inert label should still send focus to its target.', + // Forwarding to a control in an inert subtree requires inert focus handling (#119). + // The click case requires an input backend; its catch also swallows unsupported clicks. + ], + 'html/semantics/forms/the-label-element/labelable-elements.html': [ + 'Check if the output element is a labelable element', + 'Check if the progress element is a labelable element', + 'Check if the select element is a labelable element', + 'Check if the textarea element is a labelable form-element', + 'Check if the button element is a labelable element', + 'Check if the hidden input element is not a labelable element.', + 'Check if the input element in radio state is a labelable element', + 'Check if the keygen element is not a labelable element', + 'Check if the meter element is a labelable element', + 'Check if the fieldset element is not a labelable element', + 'Check if the label element is not a labelable element', + 'Check if the object element is not a labelable element', + 'Check if the img element is not a labelable element', + // The remaining cases cover the controls' labels collections. + ], + 'html/semantics/forms/the-label-element/forward-focus-to-associated-element.html': [ + 'focusing a label with for attribute should forward focus to the associated element', + 'focusing a label without for attribute should fowrad focus to the first labelable child', + 'focusing a label with tabindex should not forward focus to the labelable element', + 'focusing a label with negative tabindex should not forward focus to the labelable element', + 'focusing a label with empty tabindex should forward focus to the labelable element', + // The hidden-label case requires focusability to account for rendered visibility. + ], 'dom/nodes/Document-createEvent.https.html': [ /^(?:Event|Events|HTMLEvents|SVGEvents) should be an alias for Event\.$/i, /^createEvent\('(?:Event|Events|HTMLEvents|SVGEvents)'\) should be initialized correctly\.$/i,