Skip to content

Commit 337f184

Browse files
committed
Support namespace-aware element creation
1 parent 10ab864 commit 337f184

3 files changed

Lines changed: 101 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: 81 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,12 @@ 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+
readonly ATTRIBUTE_NODE = Node.ATTRIBUTE_NODE;
85+
readonly TEXT_NODE = Node.TEXT_NODE;
86+
readonly PROCESSING_INSTRUCTION_NODE = Node.PROCESSING_INSTRUCTION_NODE;
87+
readonly COMMENT_NODE = Node.COMMENT_NODE;
88+
5489
readonly #childNodes: Node[] = [];
5590
readonly ownerDocument: Document;
5691

@@ -161,11 +196,21 @@ export abstract class Node {
161196
export class Element extends Node {
162197
readonly localName: string;
163198
readonly nodeName: string;
164-
165-
constructor(owner: Document, localName: string, notify: Node['notify']) {
199+
readonly namespaceURI: string | null;
200+
readonly prefix: string | null;
201+
202+
constructor(
203+
owner: Document,
204+
localName: string,
205+
notify: Node['notify'],
206+
{ namespaceURI = HTML_NS, prefix = null }: Partial<ElementNamespace> = {},
207+
) {
166208
super(owner, notify);
167209
this.localName = localName;
168-
this.nodeName = asciiUppercase(String(localName));
210+
this.namespaceURI = namespaceURI;
211+
this.prefix = prefix;
212+
const qualifiedName = prefix === null ? localName : `${prefix}:${localName}`;
213+
this.nodeName = namespaceURI === HTML_NS ? asciiUppercase(qualifiedName) : qualifiedName;
169214
}
170215

171216
get nodeType(): number {
@@ -179,14 +224,14 @@ export class Element extends Node {
179224
get children(): Element[] {
180225
return this.childNodes.filter((node) => node instanceof Element);
181226
}
182-
get prefix(): null {
183-
return null;
184-
}
185-
get namespaceURI(): string {
186-
return 'http://www.w3.org/1999/xhtml';
187-
}
188227
}
189228

229+
export class HTMLElement extends Element {}
230+
231+
export class HTMLSpanElement extends HTMLElement {}
232+
233+
export class HTMLUnknownElement extends HTMLElement {}
234+
190235
export abstract class CharacterData extends Node {
191236
#data: string;
192237
constructor(owner: Document, data: string, notify: Node['notify']) {
@@ -263,3 +308,18 @@ export class ProcessingInstruction extends CharacterData {
263308
return this.target;
264309
}
265310
}
311+
312+
interface ElementNamespace {
313+
namespaceURI: string | null;
314+
prefix: string | null;
315+
}
316+
317+
function validateElementName(localName: string): void {
318+
if (
319+
!/^(?:[A-Za-z][^\0\t\n\f\r\u0020/>]*|[:_\u0080-\u{10FFFF}][A-Za-z0-9_.:\-\u0080-\u{10FFFF}]*)$/u.test(
320+
localName,
321+
)
322+
) {
323+
throw new DOMException('Invalid element name', 'InvalidCharacterError');
324+
}
325+
}

‎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)