From 32404a4c3bec8ffd5fbb55ad7789f45ef9246d78 Mon Sep 17 00:00:00 2001 From: Charles Lowell Date: Mon, 16 Nov 2026 13:10:43 -0600 Subject: [PATCH] Make Document part of the node tree --- packages/ui/src/dom.ts | 171 ++++++++++++++++++--------------- packages/ui/src/layout.test.ts | 9 +- packages/ui/src/ui.ts | 2 +- packages/ui/test/dom.test.ts | 8 +- packages/ui/test/wpt.config.ts | 4 + 5 files changed, 107 insertions(+), 87 deletions(-) diff --git a/packages/ui/src/dom.ts b/packages/ui/src/dom.ts index 8a5bfd4..ef0bf3f 100644 --- a/packages/ui/src/dom.ts +++ b/packages/ui/src/dom.ts @@ -11,93 +11,23 @@ import { const HTML_NS = 'http://www.w3.org/1999/xhtml'; -export class Document { - readonly documentElement: Element; - readonly notify: () => void; - - #registry: Map = new Map([['span', HTMLSpanElement]]); - - constructor(notify: () => void) { - this.notify = notify; - this.documentElement = new Element(this, 'document', notify); - } - - createElement(tagName: string): Element { - if (arguments.length === 0) { - throw new TypeError('createElement requires a tag name'); - } - const localName = `${tagName}`; - validateElementName(localName); - return this.#createElement(asciiLowercase(localName), { namespaceURI: HTML_NS, prefix: null }); - } - - createElementNS(namespace: string | null, qualifiedName: string): Element { - if (arguments.length < 2) { - throw new TypeError('createElementNS requires a namespace and qualified name'); - } - const namespaceURI = namespace == null ? null : `${namespace}` || null; - const name = `${qualifiedName}`; - const { prefix, localName } = validateElementNamespace(namespaceURI, name); - return this.#createElement(localName, { namespaceURI, prefix }); - } - - createAttribute(localName: string): Attr { - const name = `${localName}`; - validateAttributeName(name); - return new Attr(this, asciiLowercase(name), this.notify); - } - - createAttributeNS(namespace: string | null, qualifiedName: string): Attr { - if (arguments.length < 2) { - throw new TypeError('createAttributeNS requires a namespace and qualified name'); - } - const namespaceURI = namespace == null ? null : `${namespace}` || null; - const name = `${qualifiedName}`; - const { prefix, localName } = validateAttributeNamespace(namespaceURI, name); - return new Attr(this, localName, this.notify, { namespaceURI, prefix }); - } - - createTextNode(data: string): Text { - return new Text(this, String(data), this.notify); - } - - createComment(data: string): Text { - return new Comment(this, String(data), this.notify); - } - - createProcessingInstruction(target: string, data: string): ProcessingInstruction { - return new ProcessingInstruction(this, target, data, this.notify); - } - - #createElement(localName: string, namespace: ElementNamespace): Element { - if (namespace.namespaceURI !== HTML_NS) { - return new Element(this, localName, this.notify, namespace); - } else { - const ElementConstructor = this.#registry.get(localName) ?? HTMLUnknownElement; - return new ElementConstructor(this, localName, this.notify, namespace); - } - } -} - -export interface EventListener { - (this: Target, event: Event): void; -} - -export abstract class Node extends EventTarget { +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: Document; + readonly ownerDocument: Owner; abstract readonly nodeType: number; abstract readonly nodeName: string; @@ -106,7 +36,7 @@ export abstract class Node extends EventTarget { #parentNode: Node | null = null; - constructor(owner: Document, notify: Node['notify']) { + constructor(owner: Owner, notify: Node['notify']) { super(); this.notify = notify; this.ownerDocument = owner; @@ -269,7 +199,90 @@ export abstract class Node extends EventTarget { } } -export class Element extends Node { +export class Document extends Node { + readonly documentElement: Element; + readonly body: Element; + + #registry: Map = new Map([['span', HTMLSpanElement]]); + + constructor(notify: () => void) { + super(null, notify); + this.documentElement = new Element(this, 'html', notify); + this.body = new Element(this, 'body', notify); + this.documentElement.appendChild(this.body); + this.appendChild(this.documentElement); + } + + get nodeType(): number { + return Node.DOCUMENT_NODE; + } + + get nodeName(): string { + return '#document'; + } + + createElement(tagName: string): Element { + if (arguments.length === 0) { + throw new TypeError('createElement requires a tag name'); + } + const localName = `${tagName}`; + validateElementName(localName); + return this.#createElement(asciiLowercase(localName), { namespaceURI: HTML_NS, prefix: null }); + } + + createElementNS(namespace: string | null, qualifiedName: string): Element { + if (arguments.length < 2) { + throw new TypeError('createElementNS requires a namespace and qualified name'); + } + const namespaceURI = namespace == null ? null : `${namespace}` || null; + const name = `${qualifiedName}`; + const { prefix, localName } = validateElementNamespace(namespaceURI, name); + return this.#createElement(localName, { namespaceURI, prefix }); + } + + createAttribute(localName: string): Attr { + const name = `${localName}`; + validateAttributeName(name); + return new Attr(this, asciiLowercase(name), this.notify); + } + + createAttributeNS(namespace: string | null, qualifiedName: string): Attr { + if (arguments.length < 2) { + throw new TypeError('createAttributeNS requires a namespace and qualified name'); + } + const namespaceURI = namespace == null ? null : `${namespace}` || null; + const name = `${qualifiedName}`; + const { prefix, localName } = validateAttributeNamespace(namespaceURI, name); + return new Attr(this, localName, this.notify, { namespaceURI, prefix }); + } + + createTextNode(data: string): Text { + return new Text(this, String(data), this.notify); + } + + createComment(data: string): Text { + return new Comment(this, String(data), this.notify); + } + + createProcessingInstruction(target: string, data: string): ProcessingInstruction { + return new ProcessingInstruction(this, target, data, this.notify); + } + + #createElement(localName: string, namespace: ElementNamespace): Element { + if (namespace.namespaceURI !== HTML_NS) { + return new Element(this, localName, this.notify, namespace); + } else { + const ElementConstructor = this.#registry.get(localName) ?? HTMLUnknownElement; + return new ElementConstructor(this, localName, this.notify, namespace); + } + } +} + +export interface EventListener { + (this: EventTarget, event: Event): void; +} + +export class Element extends Node { readonly localName: string; readonly nodeName: string; readonly namespaceURI: string | null; @@ -396,7 +409,7 @@ export class HTMLSpanElement 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; @@ -444,7 +457,7 @@ export class Attr extends Node { const normal = `${value}`; if (this.#value !== normal) { this.#value = normal; - this.ownerDocument?.notify(); + this.notify(); } } @@ -457,7 +470,7 @@ export class Attr extends Node { } } -export abstract class CharacterData extends Node { +export abstract class CharacterData extends Node { #data: string; constructor(owner: Document, data: string, notify: Node['notify']) { super(owner, notify); diff --git a/packages/ui/src/layout.test.ts b/packages/ui/src/layout.test.ts index 632e650..93f28d3 100644 --- a/packages/ui/src/layout.test.ts +++ b/packages/ui/src/layout.test.ts @@ -6,7 +6,8 @@ import { Layout } from './layout.ts'; describe('Layout', () => { it('groups adjacent text around nested and empty elements and keeps IDs stable', () => { const document = new Document(() => {}); - const root = document.documentElement; + const root = document.createElement('document'); + document.body.appendChild(root); const child = document.createElement('document'); const empty = document.createElement('document'); root.appendChild(document.createTextNode('before')); @@ -35,7 +36,8 @@ describe('Layout', () => { it('lays out a deeply nested tree without recursive generator delegation', () => { const document = new Document(() => {}); - const root = document.documentElement; + const root = document.createElement('document'); + document.body.appendChild(root); const depth = 20_000; let current = root; for (let i = 1; i < depth; i++) { @@ -57,7 +59,8 @@ describe('Layout', () => { it('closes active elements when iteration is returned early', () => { const document = new Document(() => {}); - const root = document.documentElement; + const root = document.createElement('document'); + document.body.appendChild(root); const child = document.createElement('document'); root.appendChild(child); child.appendChild(document.createTextNode('inside')); diff --git a/packages/ui/src/ui.ts b/packages/ui/src/ui.ts index 8889407..bb7f217 100644 --- a/packages/ui/src/ui.ts +++ b/packages/ui/src/ui.ts @@ -31,7 +31,7 @@ export async function createUI(options: UIOptions): Promise { let running: Promise | undefined = undefined; - const run = async () => { + const run = async (): Promise => { await write(output, setup.apply); try { diff --git a/packages/ui/test/dom.test.ts b/packages/ui/test/dom.test.ts index 7cddf6a..943759d 100644 --- a/packages/ui/test/dom.test.ts +++ b/packages/ui/test/dom.test.ts @@ -12,7 +12,7 @@ describe('Node sibling properties outside WPT coverage', () => { '%s has no siblings when detached or an only child', (create) => { const document = new Document(() => {}); - const parent = document.documentElement; + const parent = document.body; const node = document[create]('document'); expect(node.previousSibling).toBeNull(); @@ -30,7 +30,7 @@ describe('Node sibling properties outside WPT coverage', () => { it('traverses immediate siblings including text and comments', () => { const document = new Document(() => {}); - const parent = document.documentElement; + const parent = document.body; const first = parent.appendChild(document.createElement('document')); const text = parent.appendChild(document.createTextNode('text')); const comment = parent.appendChild(document.createComment('comment')); @@ -51,7 +51,7 @@ describe('Node sibling properties outside WPT coverage', () => { it('updates both directions when existing children are reordered', () => { const document = new Document(() => {}); - const parent = document.documentElement; + const parent = document.body; const first = parent.appendChild(document.createElement('document')); const middle = parent.appendChild(document.createTextNode('middle')); const last = parent.appendChild(document.createComment('last')); @@ -95,7 +95,7 @@ describe('Node sibling properties outside WPT coverage', () => { it('closes the sibling gap on removal and clears detached relationships', () => { const document = new Document(() => {}); - const parent = document.documentElement; + const parent = document.body; const first = parent.appendChild(document.createTextNode('first')); const middle = parent.appendChild(document.createComment('middle')); const last = parent.appendChild(document.createElement('document')); diff --git a/packages/ui/test/wpt.config.ts b/packages/ui/test/wpt.config.ts index a50a1e8..da746f9 100644 --- a/packages/ui/test/wpt.config.ts +++ b/packages/ui/test/wpt.config.ts @@ -138,7 +138,11 @@ const config: WPTConfig = { 'For a Text, textContent should set the data', 'For a Comment, textContent should set the data', ], + 'dom/nodes/Node-parentNode.html': ['Document', 'Root element'], 'dom/nodes/Node-parentElement.html': [ + 'When the parent is null, parentElement should be null', + 'When the parent is a document, parentElement should be null (element)', + 'When the parent is a document, parentElement should be null (comment)', 'parentElement should work correctly in disconnected subtrees (element)', 'parentElement should work correctly in disconnected subtrees (text)', ],