From cecf458dd72af7f0506d12af76c09e08995f0f1a Mon Sep 17 00:00:00 2001 From: Charles Lowell Date: Sat, 14 Nov 2026 17:43:23 -0600 Subject: [PATCH] Add document event bubbling and inert focus handling --- .changeset/dom-inert.md | 5 + packages/ui/src/dom.ts | 365 ++++++++++++++++++--------------- packages/ui/test/wpt.config.ts | 2 + 3 files changed, 210 insertions(+), 162 deletions(-) create mode 100644 .changeset/dom-inert.md diff --git a/.changeset/dom-inert.md b/.changeset/dom-inert.md new file mode 100644 index 0000000..3d6eec6 --- /dev/null +++ b/.changeset/dom-inert.md @@ -0,0 +1,5 @@ +--- +'@clack/ui': minor +--- + +Make Document a DOM node so events bubble to it, and prevent programmatic focus inside inert subtrees. diff --git a/packages/ui/src/dom.ts b/packages/ui/src/dom.ts index 007c769..ccdb547 100644 --- a/packages/ui/src/dom.ts +++ b/packages/ui/src/dom.ts @@ -12,10 +12,174 @@ import { const HTML_NS = 'http://www.w3.org/1999/xhtml'; -export class Document { +export interface EventListener { + (this: EventTarget, event: Event): void; +} + +export abstract class Node extends EventTarget { + static readonly ELEMENT_NODE = 1; + static readonly ATTRIBUTE_NODE = 2; + static readonly TEXT_NODE = 3; + static readonly PROCESSING_INSTRUCTION_NODE = 7; + static readonly COMMENT_NODE = 8; + static readonly DOCUMENT_NODE = 9; + + readonly ELEMENT_NODE = Node.ELEMENT_NODE; + readonly ATTRIBUTE_NODE = Node.ATTRIBUTE_NODE; + readonly TEXT_NODE = Node.TEXT_NODE; + readonly PROCESSING_INSTRUCTION_NODE = Node.PROCESSING_INSTRUCTION_NODE; + readonly COMMENT_NODE = Node.COMMENT_NODE; + readonly DOCUMENT_NODE = Node.DOCUMENT_NODE; + + readonly #childNodes: Node[] = []; + readonly ownerDocument: Owner; + + abstract readonly nodeType: number; + abstract readonly nodeName: string; + + protected readonly notify: () => void; + + #parentNode: Node | null = null; + + constructor(owner: Owner, notify: Node['notify']) { + super(); + this.notify = notify; + this.ownerDocument = owner; + } + + override dispatchEvent(event: Event): boolean { + return dispatchEvent(this, { event, isTrusted: false }); + } + + hasChildNodes(): boolean { + return this.#childNodes.length > 0; + } + + get parentNode(): Node | null { + return this.#parentNode; + } + + get parentElement(): Element | null { + return this.#parentNode instanceof Element ? this.#parentNode : null; + } + + get childNodes(): ReadonlyArray { + return this.#childNodes; + } + + get firstChild(): Node | null { + return this.#childNodes[0] ?? null; + } + + get lastChild(): Node | null { + return this.#childNodes[this.childNodes.length - 1] ?? null; + } + + get previousSibling(): Node | null { + if (!this.#parentNode) { + return null; + } + const siblings = this.#parentNode.#childNodes; + return siblings[siblings.indexOf(this) - 1] ?? null; + } + + get nextSibling(): Node | null { + if (!this.#parentNode) { + return null; + } + const siblings = this.#parentNode.#childNodes; + return siblings[siblings.indexOf(this) + 1] ?? null; + } + + get nodeValue(): string | null { + return null; + } + + get isConnected(): boolean { + const owner = this.ownerDocument; + if (!owner) { + return true; + } + + for ( + let current: Node | null = this.parentNode; + current; + current = current.parentNode + ) { + if (current === owner) { + return true; + } + } + return false; + } + + appendChild(node: Node): Node { + return this.insertBefore(node, null); + } + + removeChild(node: Node): Node { + const currentIndex = this.childNodes.indexOf(node); + if (currentIndex < 0) { + throw new DOMException( + `cannot remove ${node.nodeName} from ${this.nodeName}. It is not a child`, + 'NotFoundError', + ); + } + + this.#childNodes.splice(currentIndex, 1); + node.#parentNode = null; + + this.notify(); + return node; + } + + remove(): void { + this.parentNode?.removeChild(this); + } + + insertBefore(node: Node, anchor: Node | null): Node { + // oxlint-disable-next-line typescript/no-this-alias -- This cursor walks the insertion parent's ancestry. + for (let current: Node | null = this; current; current = current.parentNode) { + if (current === node) { + throw new DOMException( + 'cannot add an ancestor Node to its descendant', + 'HierarchyRequestError', + ); + } + } + + if (anchor && anchor.parentNode !== this) { + throw new DOMException('Anchor is not a child of this node', 'NotFoundError'); + } + + if (node === this) { + throw new DOMException('cannot make a Node a child of itself', 'HierarchyRequestError'); + } + + if (node === anchor) { + return node; + } + if (node.parentNode) { + if (node.parentNode === this) { + const currentIndex = this.childNodes.indexOf(node); + this.#childNodes.splice(currentIndex, 1); + } else { + node.parentNode.removeChild(node); + } + } + const anchorIndex = anchor ? this.childNodes.indexOf(anchor) : this.childNodes.length; + this.#childNodes.splice(anchorIndex, 0, node); + + node.#parentNode = this; + this.notify(); + + return node; + } +} + +export class Document extends Node { readonly documentElement: Element; readonly body: HTMLBodyElement; - readonly notify: () => void; #registry: Map = new Map([ ['html', HTMLHtmlElement], @@ -27,10 +191,19 @@ export class Document { #activeElement: Element | null = null; constructor(notify: () => void) { - this.notify = notify; + super(null, notify); this.documentElement = this.createElement('html'); this.body = this.createElement('body') as HTMLBodyElement; this.documentElement.appendChild(this.body); + this.appendChild(this.documentElement); + } + + get nodeType(): number { + return Node.DOCUMENT_NODE; + } + + get nodeName(): string { + return '#document'; } get activeElement(): Element { @@ -187,163 +360,7 @@ export class Document { } } -export interface EventListener { - (this: Target, event: Event): void; -} - -export abstract class Node extends EventTarget { - static readonly ELEMENT_NODE = 1; - static readonly ATTRIBUTE_NODE = 2; - static readonly TEXT_NODE = 3; - static readonly PROCESSING_INSTRUCTION_NODE = 7; - static readonly COMMENT_NODE = 8; - - readonly ELEMENT_NODE = Node.ELEMENT_NODE; - readonly ATTRIBUTE_NODE = Node.ATTRIBUTE_NODE; - readonly TEXT_NODE = Node.TEXT_NODE; - readonly PROCESSING_INSTRUCTION_NODE = Node.PROCESSING_INSTRUCTION_NODE; - readonly COMMENT_NODE = Node.COMMENT_NODE; - - readonly #childNodes: Node[] = []; - readonly ownerDocument: Document; - - abstract readonly nodeType: number; - abstract readonly nodeName: string; - - protected readonly notify: () => void; - - #parentNode: Node | null = null; - - constructor(owner: Document, notify: Node['notify']) { - super(); - this.notify = notify; - this.ownerDocument = owner; - } - - override dispatchEvent(event: Event): boolean { - return dispatchEvent(this, { event, isTrusted: false }); - } - - hasChildNodes(): boolean { - return this.#childNodes.length > 0; - } - - get parentNode(): Node | null { - return this.#parentNode; - } - - get parentElement(): Element | null { - return this.#parentNode instanceof Element ? this.#parentNode : null; - } - - get childNodes(): ReadonlyArray { - return this.#childNodes; - } - - get firstChild(): Node | null { - return this.#childNodes[0] ?? null; - } - - get lastChild(): Node | null { - return this.#childNodes[this.childNodes.length - 1] ?? null; - } - - get previousSibling(): Node | null { - if (!this.#parentNode) { - return null; - } - const siblings = this.#parentNode.#childNodes; - return siblings[siblings.indexOf(this) - 1] ?? null; - } - - get nextSibling(): Node | null { - if (!this.#parentNode) { - return null; - } - const siblings = this.#parentNode.#childNodes; - return siblings[siblings.indexOf(this) + 1] ?? null; - } - - get nodeValue(): string | null { - return null; - } - - get isConnected(): boolean { - const { documentElement } = this.ownerDocument; - // oxlint-disable-next-line typescript/no-this-alias -- This cursor walks the node's ancestry. - for (let current: Node | null = this; current; current = current.parentNode) { - if (current === documentElement) { - return true; - } - } - return false; - } - - appendChild(node: Node): Node { - return this.insertBefore(node, null); - } - - removeChild(node: Node): Node { - const currentIndex = this.childNodes.indexOf(node); - if (currentIndex < 0) { - throw new DOMException( - `cannot remove ${node.nodeName} from ${this.nodeName}. It is not a child`, - 'NotFoundError', - ); - } - - this.#childNodes.splice(currentIndex, 1); - node.#parentNode = null; - - this.notify(); - return node; - } - - remove(): void { - this.parentNode?.removeChild(this); - } - - insertBefore(node: Node, anchor: Node | null): Node { - // oxlint-disable-next-line typescript/no-this-alias -- This cursor walks the insertion parent's ancestry. - for (let current: Node | null = this; current; current = current.parentNode) { - if (current === node) { - throw new DOMException( - 'cannot add an ancestor Node to its descendant', - 'HierarchyRequestError', - ); - } - } - - if (anchor && anchor.parentNode !== this) { - throw new DOMException('Anchor is not a child of this node', 'NotFoundError'); - } - - if (node === this) { - throw new DOMException('cannot make a Node a child of itself', 'HierarchyRequestError'); - } - - if (node === anchor) { - return node; - } - if (node.parentNode) { - if (node.parentNode === this) { - const currentIndex = this.childNodes.indexOf(node); - this.#childNodes.splice(currentIndex, 1); - } else { - node.parentNode.removeChild(node); - } - } - const anchorIndex = anchor ? this.childNodes.indexOf(anchor) : this.childNodes.length; - this.#childNodes.splice(anchorIndex, 0, node); - - node.#parentNode = this; - this.notify(); - - return node; - } -} - -export class Element extends Node { +export class Element extends Node { readonly localName: string; readonly nodeName: string; readonly namespaceURI: string | null; @@ -468,10 +485,28 @@ export class HTMLElement extends Element { #onfocus: EventListener | undefined = undefined; #onblur: EventListener | undefined = undefined; + get inert(): boolean { + return this.hasAttributeNS(null, 'inert'); + } + + set inert(value: boolean) { + if (value) { + this.setAttributeNS(null, 'inert', ''); + } else { + this.removeAttributeNS(null, 'inert'); + } + } + focus(): void { if (!this.isConnected) { return; } + // inert nodes don't get focus + for (const node of lineage(this)) { + if (node instanceof HTMLElement && node.inert) { + return; + } + } this.ownerDocument._setActiveElement(this); } @@ -518,7 +553,7 @@ export class HTMLBodyElement extends HTMLElement {} export class HTMLUnknownElement extends HTMLElement {} -export class Attr extends Node { +export class Attr extends Node { readonly localName: string; readonly namespaceURI: string | null; readonly prefix: string | null; @@ -566,7 +601,7 @@ export class Attr extends Node { const normal = `${value}`; if (this.#value !== normal) { this.#value = normal; - this.ownerDocument?.notify(); + this.notify(); } } @@ -745,3 +780,9 @@ function dispatchEvent( } } } + +function* lineage(node: Node): Iterable { + for (let current: Node | null = node; current; current = current.parentNode) { + yield current; + } +} diff --git a/packages/ui/test/wpt.config.ts b/packages/ui/test/wpt.config.ts index bf46abd..7649661 100644 --- a/packages/ui/test/wpt.config.ts +++ b/packages/ui/test/wpt.config.ts @@ -87,6 +87,8 @@ const config: WPTConfig = { 'html/editing/focus/focus-management/focusing-disconnected-noop.html': true, 'html/editing/focus/focus-management/focusing-focused-noop.html': true, 'html/interaction/focus/focus-management/focus-events.html': true, + 'inert/inert-node-is-unfocusable.html': true, + 'inert/nested-inert-unfocusable.html': true, 'dom/nodes/Document-getElementsByTagName.html': [ 'Caching is allowed', "Shouldn't be able to set unsigned properties on a HTMLCollection (non-strict mode)",