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
4 changes: 2 additions & 2 deletions packages/ui/src/dom.compatibility.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { describe, it, expect } from 'vitest';
import { testDOMCompatibility } from '@bomb.sh/web-platform-tests';
import { Document } from './dom.ts';
import { createHTMLDocument } from './dom.ts';
import config from '../test/wpt.config.ts';

testDOMCompatibility({
runner: { describe, it, expect },
config,
create: () => new Document(() => {}),
create: () => createHTMLDocument(() => {}),
});
203 changes: 184 additions & 19 deletions packages/ui/src/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
validateElementNamespace,
validateAttributeName,
validateAttributeNamespace,
validateDocumentTypeName,
} from './validation.ts';

const HTML_NS = 'http://www.w3.org/1999/xhtml';
Expand All @@ -18,13 +19,15 @@ export abstract class Node<Owner extends Document | null = Document | null> exte
static readonly PROCESSING_INSTRUCTION_NODE = 7;
static readonly COMMENT_NODE = 8;
static readonly DOCUMENT_NODE = 9;
static readonly DOCUMENT_TYPE_NODE = 10;

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 DOCUMENT_TYPE_NODE = Node.DOCUMENT_TYPE_NODE;

readonly #childNodes: Node[] = [];
readonly ownerDocument: Owner;
Expand Down Expand Up @@ -135,6 +138,18 @@ export abstract class Node<Owner extends Document | null = Document | null> exte
return null;
}

set nodeValue(_: string | null) {
//ignore
}

get textContent(): string | null {
return null;
}

set textContent(_: string | null) {
//ignore
}

appendChild(node: Node): Node {
return this.insertBefore(node, null);
}
Expand Down Expand Up @@ -199,18 +214,51 @@ export abstract class Node<Owner extends Document | null = Document | null> exte
}
}

export class DOMImplementation {
#document: Document;
#notify: Node['notify'];
constructor(document: Document, notify: Node['notify']) {
this.#document = document;
this.#notify = notify;
}

createDocument(): Document {
return new Document(this.#notify);
}

createHTMLDocument(title?: string): HTMLDocument {
return createHTMLDocument(this.#notify, title);
}

// oxlint-disable-next-line bombshell-dev/max-params -- DOMImplementation.createDocumentType requires three positional arguments.
createDocumentType(name: string, publicId: string, systemId: string): DocumentType {
if (arguments.length < 3) {
throw new TypeError('createDocumentType requires a name, public ID, and system ID');
}
const options = {
owner: this.#document,
name: `${name}`,
publicId: `${publicId}`,
systemId: `${systemId}`,
notify: this.#notify,
};
validateDocumentTypeName(options.name);
return new DocumentType(options);
}
}

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

#registry: Map<string, typeof Element> = new Map([['span', HTMLSpanElement]]);
#factory: (...params: ConstructorParameters<typeof Element>) => Element;

constructor(notify: () => void) {
constructor(
notify: () => void,
factory?: (...params: ConstructorParameters<typeof Element>) => Element,
) {
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);
this.#factory = factory ?? ((...params) => new Element(...params));
this.implementation = new DOMImplementation(this, notify);
}

get nodeType(): number {
Expand All @@ -221,6 +269,24 @@ export class Document extends Node<null> {
return '#document';
}

get doctype(): DocumentType | null {
for (const child of this.childNodes) {
if (child instanceof DocumentType) {
return child;
}
}
return null;
}

get documentElement(): Element | null {
for (const child of this.childNodes) {
if (child instanceof Element) {
return child;
}
}
return null;
}

createElement(tagName: string): Element {
if (arguments.length === 0) {
throw new TypeError('createElement requires a tag name');
Expand Down Expand Up @@ -272,12 +338,38 @@ export class Document extends Node<null> {
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);
return this.#factory(this, localName, this.notify, namespace);
}
}
}

export class DocumentType extends Node<Document> {
readonly name: string;
readonly publicId: string;
readonly systemId: string;

constructor(options: {
owner: Document;
name: string;
publicId: string;
systemId: string;
notify: Node['notify'];
}) {
super(options.owner, options.notify);
this.name = options.name;
this.publicId = options.publicId;
this.systemId = options.systemId;
}

get nodeType(): number {
return Node.DOCUMENT_TYPE_NODE;
}

get nodeName(): string {
return this.name;
}
}

export interface EventListener {
(this: EventTarget, event: Event): void;
}
Expand Down Expand Up @@ -403,12 +495,6 @@ export class Element extends Node<Document> {
}
}

export class HTMLElement extends Element {}

export class HTMLSpanElement extends HTMLElement {}

export class HTMLUnknownElement extends HTMLElement {}

export class Attr extends Node<Document> {
readonly localName: string;
readonly namespaceURI: string | null;
Expand Down Expand Up @@ -465,7 +551,7 @@ export class Attr extends Node<Document> {
return this.value;
}

get textContent(): string {
override get textContent(): string {
return this.value;
}
}
Expand Down Expand Up @@ -497,11 +583,11 @@ export abstract class CharacterData extends Node<Document> {
this.data = value ?? '';
}

get textContent(): string {
override get textContent(): string {
return this.data;
}

set textContent(value: string | null) {
override set textContent(value: string | null) {
this.data = value ?? '';
}

Expand Down Expand Up @@ -565,3 +651,82 @@ const Event = globalThis.Event as typeof globalThis.Event & {
readonly AT_TARGET: 2;
readonly BUBBLING_PHASE: 3;
};

/* HTML */

export function createHTMLDocument(notify: Node['notify'], title?: string): HTMLDocument {
const titleText = title === undefined ? undefined : `${title}`;
const document = new HTMLDocument(notify);
const doctype = document.implementation.createDocumentType('html', '', '');
document.appendChild(doctype);

const root = document.createElement('html');
const head = document.createElement('head');
const body = document.createElement('body');

root.appendChild(head);
if (titleText !== undefined) {
const titleElement = document.createElement('title');
head.appendChild(titleElement);
titleElement.appendChild(document.createTextNode(titleText));
}
root.appendChild(body);
document.appendChild(root);

return document;
}

export class HTMLDocument extends Document {
#elements = new Map<string, typeof HTMLElement>([
['html', HTMLHtmlElement],
['head', HTMLHeadElement],
['title', HTMLTitleElement],
['body', HTMLBodyElement],
['span', HTMLSpanElement],
]);
constructor(notify: Node['notify']) {
super(notify, (owner, localName, notifyElement, namespace) => {
const ElementConstructor = this.#elements.get(localName) ?? HTMLUnknownElement;

return new ElementConstructor(owner, localName, notifyElement, namespace);
});
}

get head(): HTMLHeadElement | null {
const root = this.documentElement;
if (root instanceof HTMLHtmlElement) {
for (const child of root.childNodes) {
if (child instanceof HTMLHeadElement) {
return child;
}
}
}
return null;
}

get body(): HTMLBodyElement | null {
const root = this.documentElement;
if (root && root instanceof HTMLHtmlElement) {
for (const child of root.childNodes) {
if (child instanceof HTMLBodyElement) {
return child;
}
}
}
return null;
}
}

export class HTMLElement extends Element {}

export class HTMLHtmlElement extends HTMLElement {}

export class HTMLHeadElement extends HTMLElement {}

export class HTMLTitleElement extends HTMLElement {}

export class HTMLBodyElement extends HTMLElement {}

export class HTMLSpanElement extends HTMLElement {}

export class HTMLUnknownElement extends HTMLElement {}
11 changes: 11 additions & 0 deletions packages/ui/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
export { createUI } from './ui.ts';
export {
createHTMLDocument,
HTMLDocument,
HTMLElement,
HTMLHtmlElement,
HTMLHeadElement,
HTMLBodyElement,
HTMLSpanElement,
HTMLTitleElement,
HTMLUnknownElement,
} from './dom.ts';
14 changes: 7 additions & 7 deletions packages/ui/src/layout.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { close, open, text } from '@bomb.sh/tty';
import { describe, expect, it } from 'vitest';
import { Document } from './dom.ts';
import { createHTMLDocument } from './dom.ts';
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 document = createHTMLDocument(() => {});
const root = document.createElement('document');
document.body.appendChild(root);
document.body!.appendChild(root);
const child = document.createElement('document');
const empty = document.createElement('document');
root.appendChild(document.createTextNode('before'));
Expand Down Expand Up @@ -35,9 +35,9 @@ describe('Layout', () => {
});

it('lays out a deeply nested tree without recursive generator delegation', () => {
const document = new Document(() => {});
const document = createHTMLDocument(() => {});
const root = document.createElement('document');
document.body.appendChild(root);
document.body!.appendChild(root);
const depth = 20_000;
let current = root;
for (let i = 1; i < depth; i++) {
Expand All @@ -58,9 +58,9 @@ describe('Layout', () => {
});

it('closes active elements when iteration is returned early', () => {
const document = new Document(() => {});
const document = createHTMLDocument(() => {});
const root = document.createElement('document');
document.body.appendChild(root);
document.body!.appendChild(root);
const child = document.createElement('document');
root.appendChild(child);
child.appendChild(document.createTextNode('inside'));
Expand Down
8 changes: 4 additions & 4 deletions packages/ui/src/ui.lifecycle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('UI lifecycle', () => {
const controller = new AbortController();
controller.abort();
const ui = await createUI({ ...io, signal: controller.signal });
ui.document.documentElement.appendChild(ui.document.createTextNode('unused'));
ui.document.documentElement!.appendChild(ui.document.createTextNode('unused'));

await ui.main();
await ui.main();
Expand Down Expand Up @@ -82,11 +82,11 @@ describe('UI lifecycle', () => {

try {
const { document } = ui;
document.documentElement.appendChild(document.createTextNode('first'));
document.documentElement!.appendChild(document.createTextNode('first'));
await nextTurn();
expect(frames).toBe(1);

document.documentElement.appendChild(document.createTextNode(' second'));
document.documentElement!.appendChild(document.createTextNode(' second'));
await nextTurn();
expect(frames).toBe(2);

Expand Down Expand Up @@ -129,7 +129,7 @@ describe('UI lifecycle', () => {

try {
const failed = expect(running).rejects.toBe(failure);
ui.document.documentElement.appendChild(text);
ui.document.documentElement!.appendChild(text);
await failed;

controller.abort();
Expand Down
Loading
Loading