Skip to content

Commit 18ca180

Browse files
committed
Support namespace-aware element creation
1 parent 10ab864 commit 18ca180

3 files changed

Lines changed: 97 additions & 22 deletions

File tree

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 provisional namespace-aware element creation with qualified-name validation and span element types, and enable the upstream createElementNS tests.

‎packages/ui/src/dom.ts‎

Lines changed: 77 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,54 @@
11
import { asciiUppercase, asciiLowercase } from './ascii.ts';
22

3-
/**
4-
* Container type for valid names of createElement()
5-
*/
6-
export interface Elements {
7-
document: Element;
8-
}
3+
const HTML_NS = 'http://www.w3.org/1999/xhtml';
4+
const XML_NS = 'http://www.w3.org/XML/1998/namespace';
5+
const XMLNS_NS = 'http://www.w3.org/2000/xmlns/';
96

107
export class Document {
118
readonly documentElement: Element;
129
readonly notify: () => void;
1310

11+
#registry: Map<string, typeof Element> = new Map([['span', HTMLSpanElement]]);
12+
1413
constructor(notify: () => void) {
1514
this.notify = notify;
1615
this.documentElement = new Element(this, 'document', notify);
1716
}
1817

19-
createElement(tagName: keyof Elements): Element {
18+
createElement(tagName: string): Element {
2019
if (arguments.length === 0) {
2120
// oxlint-disable-next-line bombshell-dev/no-generic-error -- Web IDL requires TypeError for missing arguments.
2221
throw new TypeError('createElement requires a tag name');
2322
}
23+
const localName = `${tagName}`;
24+
validateElementName(localName);
25+
return this.#createElement(asciiLowercase(localName), { namespaceURI: HTML_NS, prefix: null });
26+
}
27+
28+
createElementNS(namespace: string | null, qualifiedName: string): Element {
29+
if (arguments.length < 2) {
30+
// oxlint-disable-next-line bombshell-dev/no-generic-error -- Web IDL requires TypeError for missing arguments.
31+
throw new TypeError('createElementNS requires a namespace and qualified name');
32+
}
33+
const namespaceURI = namespace == null ? null : `${namespace}` || null;
34+
const name = `${qualifiedName}`;
35+
const colonIndex = name.indexOf(':');
36+
const prefix = colonIndex < 0 ? null : name.slice(0, colonIndex);
37+
const localName = colonIndex < 0 ? name : name.slice(colonIndex + 1);
38+
39+
if (prefix !== null && !/^[^\0\t\n\f\r\u0020/>]+$/u.test(prefix)) {
40+
throw new DOMException('Invalid namespace prefix', 'InvalidCharacterError');
41+
}
42+
validateElementName(localName);
2443
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-
)
44+
(prefix !== null && namespaceURI === null) ||
45+
(prefix === 'xml' && namespaceURI !== XML_NS) ||
46+
((name === 'xmlns' || prefix === 'xmlns') && namespaceURI !== XMLNS_NS) ||
47+
(namespaceURI === XMLNS_NS && name !== 'xmlns' && prefix !== 'xmlns')
2848
) {
29-
throw new DOMException('Invalid element name', 'InvalidCharacterError');
49+
throw new DOMException('Invalid namespace for qualified name', 'NamespaceError');
3050
}
31-
return new Element(this, asciiLowercase(String(tagName)), this.notify);
51+
return this.#createElement(localName, { namespaceURI, prefix });
3252
}
3353

3454
createTextNode(data: string): Text {
@@ -42,6 +62,15 @@ export class Document {
4262
createProcessingInstruction(target: string, data: string): ProcessingInstruction {
4363
return new ProcessingInstruction(this, target, data, this.notify);
4464
}
65+
66+
#createElement(localName: string, namespace: ElementNamespace): Element {
67+
if (namespace.namespaceURI !== HTML_NS) {
68+
return new Element(this, localName, this.notify, namespace);
69+
} else {
70+
const ElementConstructor = this.#registry.get(localName) ?? HTMLUnknownElement;
71+
return new ElementConstructor(this, localName, this.notify, namespace);
72+
}
73+
}
4574
}
4675

4776
export abstract class Node {
@@ -51,6 +80,8 @@ export abstract class Node {
5180
static readonly PROCESSING_INSTRUCTION_NODE = 7;
5281
static readonly COMMENT_NODE = 8;
5382

83+
readonly ELEMENT_NODE = Node.ELEMENT_NODE;
84+
5485
readonly #childNodes: Node[] = [];
5586
readonly ownerDocument: Document;
5687

@@ -161,11 +192,21 @@ export abstract class Node {
161192
export class Element extends Node {
162193
readonly localName: string;
163194
readonly nodeName: string;
164-
165-
constructor(owner: Document, localName: string, notify: Node['notify']) {
195+
readonly namespaceURI: string | null;
196+
readonly prefix: string | null;
197+
198+
constructor(
199+
owner: Document,
200+
localName: string,
201+
notify: Node['notify'],
202+
{ namespaceURI = HTML_NS, prefix = null }: Partial<ElementNamespace> = {},
203+
) {
166204
super(owner, notify);
167205
this.localName = localName;
168-
this.nodeName = asciiUppercase(String(localName));
206+
this.namespaceURI = namespaceURI;
207+
this.prefix = prefix;
208+
const qualifiedName = prefix === null ? localName : `${prefix}:${localName}`;
209+
this.nodeName = namespaceURI === HTML_NS ? asciiUppercase(qualifiedName) : qualifiedName;
169210
}
170211

171212
get nodeType(): number {
@@ -179,14 +220,14 @@ export class Element extends Node {
179220
get children(): Element[] {
180221
return this.childNodes.filter((node) => node instanceof Element);
181222
}
182-
get prefix(): null {
183-
return null;
184-
}
185-
get namespaceURI(): string {
186-
return 'http://www.w3.org/1999/xhtml';
187-
}
188223
}
189224

225+
export class HTMLElement extends Element {}
226+
227+
export class HTMLSpanElement extends HTMLElement {}
228+
229+
export class HTMLUnknownElement extends HTMLElement {}
230+
190231
export abstract class CharacterData extends Node {
191232
#data: string;
192233
constructor(owner: Document, data: string, notify: Node['notify']) {
@@ -263,3 +304,18 @@ export class ProcessingInstruction extends CharacterData {
263304
return this.target;
264305
}
265306
}
307+
308+
interface ElementNamespace {
309+
namespaceURI: string | null;
310+
prefix: string | null;
311+
}
312+
313+
function validateElementName(localName: string): void {
314+
if (
315+
!/^(?:[A-Za-z][^\0\t\n\f\r\u0020/>]*|[:_\u0080-\u{10FFFF}][A-Za-z0-9_.:\-\u0080-\u{10FFFF}]*)$/u.test(
316+
localName,
317+
)
318+
) {
319+
throw new DOMException('Invalid element name', 'InvalidCharacterError');
320+
}
321+
}

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
import type { WPTConfig } from '@bomb.sh/web-platform-tests';
2-
import { CharacterData, Comment, Document, Element, Node, Text } from '../src/dom.ts';
2+
import {
3+
CharacterData,
4+
Comment,
5+
Document,
6+
Element,
7+
HTMLElement,
8+
HTMLSpanElement,
9+
HTMLUnknownElement,
10+
Node,
11+
Text,
12+
} from '../src/dom.ts';
313

414
export default {
515
globals: {
@@ -8,12 +18,16 @@ export default {
818
Document,
919
DOMException: globalThis.DOMException,
1020
Element,
21+
HTMLElement,
22+
HTMLSpanElement,
23+
HTMLUnknownElement,
1124
Node,
1225
Text,
1326
},
1427
include: {
1528
'dom/nodes/Document-createTextNode.html': true,
1629
'dom/nodes/Document-createElement.html': true,
30+
'dom/nodes/Document-createElementNS.html': true,
1731
'dom/nodes/Document-createComment.html': true,
1832
'dom/nodes/CharacterData-data.html': true,
1933
'dom/nodes/CharacterData-remove.html': true,

0 commit comments

Comments
 (0)