From 85b041503639773db3b444793b9a0b6b9de88ffb Mon Sep 17 00:00:00 2001 From: Charles Lowell Date: Wed, 18 Nov 2026 14:19:11 -0600 Subject: [PATCH] Add HTML focusability and disabled reflection --- .changeset/dom-focusability.md | 5 ++ packages/ui/src/dom.ts | 101 +++++++++++++++++++++++++++++++-- packages/ui/wpt.config.ts | 36 +++++++++++- 3 files changed, 137 insertions(+), 5 deletions(-) create mode 100644 .changeset/dom-focusability.md diff --git a/.changeset/dom-focusability.md b/.changeset/dom-focusability.md new file mode 100644 index 0000000..d595eda --- /dev/null +++ b/.changeset/dom-focusability.md @@ -0,0 +1,5 @@ +--- +'@clack/ui': minor +--- + +Add HTML focusability checks for [HTMLElement.focus()](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/focus) and reflected [disabled attributes](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Attributes/disabled) on form controls. diff --git a/packages/ui/src/dom.ts b/packages/ui/src/dom.ts index 19750e0..a90b1b4 100644 --- a/packages/ui/src/dom.ts +++ b/packages/ui/src/dom.ts @@ -823,9 +823,19 @@ export class HTMLDocument extends Document { ['head', HTMLHeadElement], ['title', HTMLTitleElement], ['body', HTMLBodyElement], + ['a', HTMLAnchorElement], ['button', HTMLButtonElement], + ['input', HTMLInputElement], + ['select', HTMLSelectElement], + ['textarea', HTMLTextAreaElement], + ['option', HTMLOptionElement], + ['optgroup', HTMLOptGroupElement], + ['fieldset', HTMLFieldSetElement], ['span', HTMLSpanElement], ['div', HTMLDivElement], + ['iframe', HTMLIFrameElement], + ['details', HTMLDetailsElement], + ['summary', HTMLSummaryElement], ]); constructor(notify: Node['notify']) { super(notify, (owner, localName, notifyElement, namespace) => { @@ -883,8 +893,16 @@ export class HTMLElement extends Element { } get tabIndex(): number { - const attr = this.getAttributeNS(null, 'tabindex'); - return attr === null ? -1 : Number.parseInt(attr, 10) || 0; + const attr = this.getAttributeNodeNS(null, 'tabindex'); + if (!attr) { + return -1; + } else { + const value = Number.parseInt(attr.value, 10); + if (Number.isNaN(value)) { + return -1; + } + return value === 0 ? 0 : value; + } } set tabIndex(value: number) { @@ -892,7 +910,7 @@ export class HTMLElement extends Element { } focus(): void { - if (!this.isConnected) { + if (!isFocusable(this)) { return; } // inert nodes don't get focus @@ -937,6 +955,20 @@ export class HTMLElement extends Element { } } +export class DisableableElement extends HTMLElement { + get disabled(): boolean { + return this.hasAttributeNS(null, 'disabled'); + } + + set disabled(value: boolean) { + if (value) { + this.setAttributeNS(null, 'disabled', ''); + } else { + this.removeAttributeNS(null, 'disabled'); + } + } +} + export class HTMLHtmlElement extends HTMLElement {} export class HTMLHeadElement extends HTMLElement {} @@ -949,7 +981,27 @@ export class HTMLSpanElement extends HTMLElement {} export class HTMLDivElement extends HTMLElement {} -export class HTMLButtonElement extends Focusable(HTMLElement) {} +export class HTMLAnchorElement extends Focusable(HTMLElement) {} + +export class HTMLButtonElement extends Focusable(DisableableElement) {} + +export class HTMLInputElement extends Focusable(DisableableElement) {} + +export class HTMLSelectElement extends Focusable(DisableableElement) {} + +export class HTMLTextAreaElement extends Focusable(DisableableElement) {} + +export class HTMLFieldSetElement extends DisableableElement {} + +export class HTMLOptionElement extends DisableableElement {} + +export class HTMLOptGroupElement extends DisableableElement {} + +export class HTMLIFrameElement extends Focusable(HTMLElement) {} + +export class HTMLDetailsElement extends HTMLElement {} + +export class HTMLSummaryElement extends HTMLElement {} export class HTMLUnknownElement extends HTMLElement {} @@ -987,6 +1039,47 @@ export class FocusEvent extends Event { } } +function isFocusable(element: HTMLElement): boolean { + if (!element.isConnected) { + return false; + } + + for (const node of lineage(element)) { + if (node instanceof HTMLElement && node.inert) { + return false; + } + if (node instanceof DisableableElement && node.disabled) { + return false; + } + } + + const hasTabIndex = (target: HTMLElement): boolean => { + const attr = target.getAttributeNode('tabindex'); + return !!attr && !isNaN(Number(attr.value)); + }; + + switch (element.localName) { + case 'a': + return !!element.getAttributeNodeNS(null, 'href') || hasTabIndex(element); + case 'input': + return element.getAttributeNS(null, 'type') !== 'hidden'; + case 'summary': + return ( + hasTabIndex(element) || + (element.parentElement instanceof HTMLDetailsElement && + element.parentElement.children[0] === element) + ); + default: { + const editable = element.getAttribute('contenteditable'); + return ( + (editable && ['true', '', 'plaintext-only'].includes(editable)) || + hasTabIndex(element) || + element.tabIndex === 0 + ); + } + } +} + function dispatchEvent( node: Node, { event, isTrusted }: { event: Event; isTrusted: boolean }, diff --git a/packages/ui/wpt.config.ts b/packages/ui/wpt.config.ts index 7816cb6..2d24b0b 100644 --- a/packages/ui/wpt.config.ts +++ b/packages/ui/wpt.config.ts @@ -147,7 +147,41 @@ const config: WPTConfig = { // real layout, hit testing, and an input backend are not available yet. 'inert/nested-inert-unfocusable.html': true, 'html/dom/reflection-misc.html': [/^html\.inert:/, /^html\.tabIndex:/], - 'html/dom/reflection-forms.html': [/^button\.tabIndex:/], + 'html/dom/reflection-forms.html': [ + /^button\.tabIndex:/, + /^(button|input|select|textarea|fieldset|optgroup|option)\.disabled:/, + ], + 'html/interaction/focus/tabindex-focus-flag.html': [ + 'a should not be focusable by default.', + 'a[href] should be focusable by default.', + 'button should be focusable by default.', + 'input[type="hidden"] should not be focusable by default.', + 'input[type="button"] should be focusable by default.', + 'select should be focusable by default.', + 'textarea should be focusable by default.', + '#summary-out should not be focusable by default.', + '#summary-first should be focusable by default.', + '#summary-second should not be focusable by default.', + '[contenteditable] should be focusable by default.', + 'iframe should be focusable by default.', + 'img should not be focusable by default.', + 'A with tabindex=0 should be focusable.', + 'SUMMARY#summary-out-tabindex0 with tabindex=0 should be focusable.', + 'SUMMARY#summary-second-tabindex0 with tabindex=0 should be focusable.', + 'IMG with tabindex=0 should be focusable.', + 'A with tabindex=-1 should be focusable.', + 'SUMMARY#summary-out-tabindex-negative with tabindex=-1 should be focusable.', + 'SUMMARY#summary-second-tabindex-negative with tabindex=-1 should be focusable.', + 'IMG with tabindex=-1 should be focusable.', + 'A with tabindex=invalid should not be focusable.', + 'A#with-href with tabindex=invalid should be focusable.', + 'DIV with tabindex=invalid should not be focusable.', + 'SUMMARY#summary-out-tabindex-invalid with tabindex=invalid should not be focusable.', + 'IMG with tabindex=invalid should not be focusable.', + // SVG focus coverage awaits its element interfaces. + ], + 'html/semantics/forms/the-input-element/number-disabled.html': true, + 'html/semantics/disabled-elements/disabledElement.html': true, 'html/interaction/focus/sequential-focus-navigation-and-the-tabindex-attribute/focus-tabindex-default-value.html': true, 'dom/nodes/NodeList-Iterable.html': [/^NodeList /], 'dom/nodes/ParentNode-querySelector-All-comma-selector.html': [