diff --git a/.changeset/dom-events.md b/.changeset/dom-events.md new file mode 100644 index 0000000..276c763 --- /dev/null +++ b/.changeset/dom-events.md @@ -0,0 +1,6 @@ +--- +'@clack/ui': minor +'@bomb.sh/web-platform-tests': patch +--- + +Support native event listeners and bubbling through the UI's node tree. diff --git a/packages/ui/src/dom.ts b/packages/ui/src/dom.ts index 785590a..3930318 100644 --- a/packages/ui/src/dom.ts +++ b/packages/ui/src/dom.ts @@ -79,7 +79,11 @@ export class Document { } } -export abstract class Node { +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; @@ -103,10 +107,60 @@ export abstract class Node { #parentNode: Node | null = null; constructor(owner: Document, notify: Node['notify']) { + super(); this.notify = notify; this.ownerDocument = owner; } + // Adds bubbling to the host EventTarget. Capture traversal and resetting the + // host's propagation flags after dispatch are not implemented here. + override dispatchEvent(event: Event): boolean { + if (event.eventPhase !== Event.NONE) { + throw new DOMException('Cannot redispatch an active event', 'InvalidStateError'); + } + + const path: Node[] = [this]; + for (let current = this.parentNode; current; current = current.parentNode) { + path.push(current); + } + + // Native dispatch retargets on each node. Keep the tree's original target + // and expose its current node and phase while each native dispatch runs. + Object.defineProperty(event, 'target', { configurable: true, value: this }); + const originals = Object.getOwnPropertyDescriptors(event); + + try { + Object.defineProperty(event, 'composedPath', { + value: () => [...path], + configurable: true, + }); + const end = event.bubbles ? path.length : 1; + for (let i = 0; i < end; i++) { + Object.defineProperties(event, { + currentTarget: { value: path[i], configurable: true }, + eventPhase: { + value: i === 0 ? Event.AT_TARGET : Event.BUBBLING_PHASE, + configurable: true, + }, + }); + super.dispatchEvent.call(path[i], event); + if (event.cancelBubble) { + break; + } + } + return !event.defaultPrevented; + } finally { + for (const name of ['currentTarget', 'eventPhase', 'composedPath']) { + const original = originals[name]; + if (original) { + Object.defineProperty(event, name, original); + } else { + Reflect.deleteProperty(event, name); + } + } + } + } + hasChildNodes(): boolean { return this.#childNodes.length > 0; } @@ -468,3 +522,17 @@ interface ElementNamespace { namespaceURI: string | null; prefix: string | null; } + +export interface EventOptions { + bubbles?: boolean; + cancelable?: boolean; + composed?: boolean; +} + +// Node exposes these constants at runtime, but its type declarations omit them. +const Event = globalThis.Event as typeof globalThis.Event & { + readonly NONE: 0; + readonly CAPTURING_PHASE: 1; + readonly AT_TARGET: 2; + readonly BUBBLING_PHASE: 3; +}; diff --git a/packages/ui/test/wpt.config.ts b/packages/ui/test/wpt.config.ts index 1e38750..a50a1e8 100644 --- a/packages/ui/test/wpt.config.ts +++ b/packages/ui/test/wpt.config.ts @@ -13,22 +13,40 @@ import { } from '../src/dom.ts'; import { NamedNodeMap } from '../src/named-node-map.ts'; -export default { +const config: WPTConfig = { globals: { + AbortController: globalThis.AbortController, Attr, CharacterData, Comment, + CustomEvent: globalThis.CustomEvent, Document, DOMException: globalThis.DOMException, Element, + Event: globalThis.Event, + EventTarget: globalThis.EventTarget, HTMLElement, HTMLSpanElement, HTMLUnknownElement, NamedNodeMap, Node, Text, + TypeError: globalThis.TypeError, }, include: { + 'dom/events/Event-constructors.any.js': true, + 'dom/events/EventTarget-addEventListener.any.js': true, + 'dom/events/EventTarget-add-remove-listener.any.js': true, + 'dom/events/AddEventListenerOptions-once.any.js': true, + 'dom/events/AddEventListenerOptions-signal.any.js': true, + 'dom/events/AddEventListenerOptions-passive.any.js': true, + 'dom/events/remove-all-listeners.html': true, + 'dom/events/EventListener-handleEvent.html': [ + 'calls `handleEvent` method of `EventListener`', + "doesn't call `handleEvent` method on callable `EventListener`", + // Native EventTarget reads handleEvent at registration; the getter-timing + // case remains excluded until that behavior is implemented. + ], 'dom/nodes/Document-createTextNode.html': true, 'dom/nodes/Document-createElement.html': true, 'dom/nodes/Document-createElementNS.html': true, @@ -142,4 +160,6 @@ export default { 'uievents/**': true, 'xhr/**': true, }, -} satisfies WPTConfig; +}; + +export default config; diff --git a/packages/web-platform-tests/docs/omissions.md b/packages/web-platform-tests/docs/omissions.md index 77c63bc..770039e 100644 --- a/packages/web-platform-tests/docs/omissions.md +++ b/packages/web-platform-tests/docs/omissions.md @@ -11,6 +11,7 @@ the upstream WPT root. Consumer exclusions in `wpt.config.ts` are separate. | `dom/nodes/Element-matches-namespaced-elements.html` | The `webkitMatchesSelector` rows. | Vendor-prefixed alias. | | `dom/nodes/ParentNode-querySelector-All.html`, `dom/nodes/ParentNode-querySelector-All-xht.xht`, `dom/nodes/Element-matches.html` | Unnamed iframe-loader cases. | The consumer's document factory replaces upstream fixture loading; behavioral subtests remain. | | `dom/nodes/attributes.html` | `setAttributeNode, if it fires mutation events, should fire one with the new node when resetting an existing attribute`, and its ` (outer shell)` companion. | Legacy `DOMAttrModified` coverage is outside the current event scope; both the listener assertions and their outer-shell setup remain omitted. | +| `dom/events/EventListener-handleEvent.html` | `` rethrows errors when getting `handleEvent` ``, ``throws if `handleEvent` is falsy and not callable``, and ``throws if `handleEvent` is thruthy and not callable``. | Require asynchronous window error reporting and the upstream `EventWatcher` harness. | | `domparsing/createContextualFragment.html` | `