Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 92 additions & 79 deletions packages/ui/src/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, typeof Element> = 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<Target extends EventTarget = EventTarget> {
(this: Target, event: Event): void;
}

export abstract class Node extends EventTarget {
export abstract class Node<Owner extends Document | null = Document | null> 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;
Expand All @@ -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;
Expand Down Expand Up @@ -269,7 +199,90 @@ export abstract class Node extends EventTarget {
}
}

export class Element extends Node {
export class Document extends Node<null> {
readonly documentElement: Element;
readonly body: Element;

#registry: Map<string, typeof Element> = 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<Document> {
readonly localName: string;
readonly nodeName: string;
readonly namespaceURI: string | null;
Expand Down Expand Up @@ -396,7 +409,7 @@ export class HTMLSpanElement extends HTMLElement {}

export class HTMLUnknownElement extends HTMLElement {}

export class Attr extends Node {
export class Attr extends Node<Document> {
readonly localName: string;
readonly namespaceURI: string | null;
readonly prefix: string | null;
Expand Down Expand Up @@ -444,7 +457,7 @@ export class Attr extends Node {
const normal = `${value}`;
if (this.#value !== normal) {
this.#value = normal;
this.ownerDocument?.notify();
this.notify();
}
}

Expand All @@ -457,7 +470,7 @@ export class Attr extends Node {
}
}

export abstract class CharacterData extends Node {
export abstract class CharacterData extends Node<Document> {
#data: string;
constructor(owner: Document, data: string, notify: Node['notify']) {
super(owner, notify);
Expand Down
9 changes: 6 additions & 3 deletions packages/ui/src/layout.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down Expand Up @@ -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++) {
Expand All @@ -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'));
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export async function createUI(options: UIOptions): Promise<UI> {

let running: Promise<void> | undefined = undefined;

const run = async () => {
const run = async (): Promise<void> => {
await write(output, setup.apply);

try {
Expand Down
8 changes: 4 additions & 4 deletions packages/ui/test/dom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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'));
Expand All @@ -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'));
Expand Down Expand Up @@ -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'));
Expand Down
4 changes: 4 additions & 0 deletions packages/ui/test/wpt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)',
],
Expand Down
Loading