Skip to content

Commit 2a98c55

Browse files
committed
Implement basic DOM creation
1 parent c24229f commit 2a98c55

5 files changed

Lines changed: 125 additions & 20 deletions

File tree

‎.changeset/dom-basic-creation.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clack/ui': minor
3+
---
4+
5+
Add DOM element, text, and comment creation with string conversion, element name validation, ASCII-only HTML name casing, and node ownership and metadata. Add the CharacterData base class and enable the existing WPT creation tests.

‎packages/ui/src/ascii.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export function asciiLowercase(s: string): string {
2+
return s.replace(/[A-Z]/g, (c) => c.toLowerCase());
3+
}
4+
5+
export function asciiUppercase(s: string): string {
6+
return s.replace(/[a-z]/g, (c) => c.toUpperCase());
7+
}
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import { describe, it, expect } from 'vitest';
22
import { testDOMCompatibility } from '@bomb.sh/web-platform-tests';
3-
import { Document, Element, Node, Text } from './dom.ts';
3+
import { CharacterData, Comment, Document, Element, Node, Text } from './dom.ts';
44
import config from '../test/wpt.config.ts';
55

6-
// Keep this file runnable while all compatibility cases are excluded.
7-
it.todo('enable DOM compatibility coverage as the implementation catches up');
6+
const DOMException = globalThis.DOMException;
87

98
testDOMCompatibility({
109
runner: { describe, it, expect },
1110
config,
1211
create: () => new Document(() => {}),
13-
globals: { Document, Element, Node, Text },
12+
globals: { CharacterData, Comment, Document, DOMException, Element, Node, Text },
1413
});

‎packages/ui/src/dom.ts‎

Lines changed: 105 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { asciiUppercase, asciiLowercase } from './ascii.ts';
2+
13
/**
24
* Container type for valid names of createElement()
35
*/
@@ -11,58 +13,113 @@ export class Document {
1113

1214
constructor(notify: () => void) {
1315
this.notify = notify;
14-
this.documentElement = new Element('document', notify);
16+
this.documentElement = new Element(this, 'document', notify);
1517
}
1618

17-
createElement(tagName: keyof Elements) {
18-
return new Element(tagName, this.notify);
19+
createElement(tagName: keyof Elements): Element {
20+
if (arguments.length === 0) {
21+
// oxlint-disable-next-line bombshell-dev/no-generic-error -- Web IDL requires TypeError for missing arguments.
22+
throw new TypeError('createElement requires a tag name');
23+
}
24+
if (
25+
!/^(?:[A-Za-z][^\0\t\n\f\r\u0020/>]*|[:_\u0080-\u{10FFFF}][A-Za-z0-9_.:\-\u0080-\u{10FFFF}]*)$/u.test(
26+
tagName,
27+
)
28+
) {
29+
throw new DOMException('Invalid element name', 'InvalidCharacterError');
30+
}
31+
return new Element(this, asciiLowercase(String(tagName)), this.notify);
1932
}
2033

2134
createTextNode(data: string): Text {
22-
return new Text(data, this.notify);
35+
return new Text(this, String(data), this.notify);
36+
}
37+
38+
createComment(data: string): Text {
39+
return new Comment(this, String(data), this.notify);
2340
}
2441
}
2542

26-
export class Node {
27-
readonly nodeName: string;
43+
export abstract class Node {
44+
static readonly ELEMENT_NODE = 1;
45+
static readonly ATTRIBUTE_NODE = 2;
46+
static readonly TEXT_NODE = 3;
47+
static readonly COMMENT_NODE = 8;
48+
2849
readonly childNodes: Node[];
50+
readonly ownerDocument: Document;
51+
52+
abstract readonly nodeType: number;
53+
abstract readonly nodeName: string;
2954

3055
protected readonly notify: () => void;
3156

32-
constructor(nodeName: string, notify: Node['notify']) {
57+
constructor(owner: Document, notify: Node['notify']) {
3358
this.notify = notify;
34-
this.nodeName = nodeName;
3559
this.childNodes = [];
60+
this.ownerDocument = owner;
61+
}
62+
63+
hasChildNodes(): boolean {
64+
return this.childNodes.length > 0;
65+
}
66+
67+
get firstChild(): Node | null {
68+
return this.childNodes[0] ?? null;
69+
}
70+
71+
get lastChild(): Node | null {
72+
return this.childNodes[this.childNodes.length - 1] ?? null;
3673
}
3774

3875
appendChild(node: Node): Node {
3976
this.childNodes.push(node);
4077
this.notify();
4178
return node;
4279
}
80+
81+
get nodeValue(): string | null {
82+
return null;
83+
}
4384
}
4485

4586
export class Element extends Node {
46-
readonly tagName: string;
87+
readonly localName: string;
88+
readonly nodeName: string;
4789

48-
constructor(tagName: keyof Elements, notify: Node['notify']) {
49-
super(tagName.toUpperCase(), notify);
50-
this.tagName = tagName;
90+
constructor(owner: Document, localName: string, notify: Node['notify']) {
91+
super(owner, notify);
92+
this.localName = localName;
93+
this.nodeName = asciiUppercase(String(localName));
94+
}
95+
96+
get nodeType(): number {
97+
return Node.ELEMENT_NODE;
98+
}
99+
100+
get tagName(): string {
101+
return this.nodeName;
51102
}
52103

53104
get children(): Element[] {
54105
return this.childNodes.filter((node) => node instanceof Element);
55106
}
107+
get prefix(): null {
108+
return null;
109+
}
110+
get namespaceURI(): string {
111+
return 'http://www.w3.org/1999/xhtml';
112+
}
56113
}
57114

58-
export class Text extends Node {
115+
export abstract class CharacterData extends Node {
59116
#data: string;
60-
constructor(data: string, notify: Node['notify']) {
61-
super('#text', notify);
117+
constructor(owner: Document, data: string, notify: Node['notify']) {
118+
super(owner, notify);
62119
this.#data = data;
63120
}
64121

65-
get data() {
122+
get data(): string {
66123
return this.#data;
67124
}
68125

@@ -72,4 +129,36 @@ export class Text extends Node {
72129
this.notify();
73130
}
74131
}
132+
133+
override get nodeValue(): string {
134+
return this.data;
135+
}
136+
137+
get textContent(): string {
138+
return this.data;
139+
}
140+
141+
get length(): number {
142+
return this.data.length;
143+
}
144+
}
145+
146+
export class Text extends CharacterData {
147+
get nodeType(): number {
148+
return Node.TEXT_NODE;
149+
}
150+
151+
get nodeName(): string {
152+
return '#text';
153+
}
154+
}
155+
156+
export class Comment extends CharacterData {
157+
get nodeType(): number {
158+
return Node.COMMENT_NODE;
159+
}
160+
161+
get nodeName(): string {
162+
return '#comment';
163+
}
75164
}

‎packages/ui/test/wpt.config.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import type { WPTConfig } from '@bomb.sh/web-platform-tests';
22

33
export default {
4+
include: {
5+
'dom/nodes/Document-createTextNode.html': true,
6+
'dom/nodes/Document-createElement.html': true,
7+
'dom/nodes/Document-createComment.html': true,
8+
},
49
exclude: {
510
'css/**': true,
611
'custom-elements/**': true,

0 commit comments

Comments
 (0)