Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/dom-create-element-ns.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clack/ui': minor
---

Add provisional namespace-aware element creation with qualified-name validation and span element types, and enable the upstream createElementNS tests.
80 changes: 57 additions & 23 deletions packages/ui/src/dom.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
import { asciiUppercase, asciiLowercase } from './ascii.ts';
import { validateElementName, validateElementNamespace } from './validation.ts';

/**
* Container type for valid names of createElement()
*/
export interface Elements {
document: Element;
}
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: keyof Elements): Element {
createElement(tagName: string): Element {
if (arguments.length === 0) {
// oxlint-disable-next-line bombshell-dev/no-generic-error -- Web IDL requires TypeError for missing arguments.
throw new TypeError('createElement requires a tag name');
}
if (
!/^(?:[A-Za-z][^\0\t\n\f\r\u0020/>]*|[:_\u0080-\u{10FFFF}][A-Za-z0-9_.:\-\u0080-\u{10FFFF}]*)$/u.test(
tagName,
)
) {
throw new DOMException('Invalid element name', 'InvalidCharacterError');
const localName = `${tagName}`;
Comment thread
cowboyd marked this conversation as resolved.
Resolved
validateElementName(localName);
return this.#createElement(asciiLowercase(localName), { namespaceURI: HTML_NS, prefix: null });
}

createElementNS(namespace: string | null, qualifiedName: string): Element {
if (arguments.length < 2) {
// oxlint-disable-next-line bombshell-dev/no-generic-error -- Web IDL requires TypeError for missing arguments.
throw new TypeError('createElementNS requires a namespace and qualified name');
}
return new Element(this, asciiLowercase(String(tagName)), this.notify);
const namespaceURI = namespace == null ? null : `${namespace}` || null;
const name = `${qualifiedName}`;
const { prefix, localName } = validateElementNamespace(namespaceURI, name);
return this.#createElement(localName, { namespaceURI, prefix });
}

createTextNode(data: string): Text {
Expand All @@ -42,6 +46,15 @@ export class Document {
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 abstract class Node {
Expand All @@ -51,6 +64,12 @@ export abstract class Node {
static readonly PROCESSING_INSTRUCTION_NODE = 7;
static readonly COMMENT_NODE = 8;

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 #childNodes: Node[] = [];
readonly ownerDocument: Document;

Expand Down Expand Up @@ -161,11 +180,21 @@ export abstract class Node {
export class Element extends Node {
readonly localName: string;
readonly nodeName: string;

constructor(owner: Document, localName: string, notify: Node['notify']) {
readonly namespaceURI: string | null;
readonly prefix: string | null;

constructor(
owner: Document,
localName: string,
notify: Node['notify'],
{ namespaceURI = HTML_NS, prefix = null }: Partial<ElementNamespace> = {},
) {
super(owner, notify);
this.localName = localName;
this.nodeName = asciiUppercase(String(localName));
this.namespaceURI = namespaceURI;
this.prefix = prefix;
const qualifiedName = prefix === null ? localName : `${prefix}:${localName}`;
this.nodeName = namespaceURI === HTML_NS ? asciiUppercase(qualifiedName) : qualifiedName;
}

get nodeType(): number {
Expand All @@ -179,14 +208,14 @@ export class Element extends Node {
get children(): Element[] {
return this.childNodes.filter((node) => node instanceof Element);
}
get prefix(): null {
return null;
}
get namespaceURI(): string {
return 'http://www.w3.org/1999/xhtml';
}
}

export class HTMLElement extends Element {}

export class HTMLSpanElement extends HTMLElement {}

export class HTMLUnknownElement extends HTMLElement {}

export abstract class CharacterData extends Node {
#data: string;
constructor(owner: Document, data: string, notify: Node['notify']) {
Expand Down Expand Up @@ -263,3 +292,8 @@ export class ProcessingInstruction extends CharacterData {
return this.target;
}
}

interface ElementNamespace {
namespaceURI: string | null;
prefix: string | null;
}
35 changes: 35 additions & 0 deletions packages/ui/src/validation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const XML_NS = 'http://www.w3.org/XML/1998/namespace';
const XMLNS_NS = 'http://www.w3.org/2000/xmlns/';

export function validateElementName(localName: string): void {
if (
!/^(?:[A-Za-z][^\0\t\n\f\r\u0020/>]*|[:_\u0080-\u{10FFFF}][A-Za-z0-9_.:\-\u0080-\u{10FFFF}]*)$/u.test(
localName,
)
) {
throw new DOMException('Invalid element name', 'InvalidCharacterError');
}
}

export function validateElementNamespace(
namespaceURI: string | null,
qualifiedName: string,
): { prefix: string | null; localName: string } {
const colonIndex = qualifiedName.indexOf(':');
const prefix = colonIndex < 0 ? null : qualifiedName.slice(0, colonIndex);
const localName = colonIndex < 0 ? qualifiedName : qualifiedName.slice(colonIndex + 1);

if (prefix !== null && !/^[^\0\t\n\f\r\u0020/>]+$/u.test(prefix)) {
throw new DOMException('Invalid namespace prefix', 'InvalidCharacterError');
}
validateElementName(localName);
if (
(prefix !== null && namespaceURI === null) ||
(prefix === 'xml' && namespaceURI !== XML_NS) ||
((qualifiedName === 'xmlns' || prefix === 'xmlns') && namespaceURI !== XMLNS_NS) ||
(namespaceURI === XMLNS_NS && qualifiedName !== 'xmlns' && prefix !== 'xmlns')
) {
throw new DOMException('Invalid namespace for qualified name', 'NamespaceError');
}
return { prefix, localName };
}
16 changes: 15 additions & 1 deletion packages/ui/test/wpt.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import type { WPTConfig } from '@bomb.sh/web-platform-tests';
import { CharacterData, Comment, Document, Element, Node, Text } from '../src/dom.ts';
import {
CharacterData,
Comment,
Document,
Element,
HTMLElement,
HTMLSpanElement,
HTMLUnknownElement,
Node,
Text,
} from '../src/dom.ts';

export default {
globals: {
Expand All @@ -8,12 +18,16 @@ export default {
Document,
DOMException: globalThis.DOMException,
Element,
HTMLElement,
HTMLSpanElement,
HTMLUnknownElement,
Node,
Text,
},
include: {
'dom/nodes/Document-createTextNode.html': true,
'dom/nodes/Document-createElement.html': true,
'dom/nodes/Document-createElementNS.html': true,
'dom/nodes/Document-createComment.html': true,
'dom/nodes/CharacterData-data.html': true,
'dom/nodes/CharacterData-remove.html': true,
Expand Down
Loading