diff --git a/.changeset/dom-create-element-ns.md b/.changeset/dom-create-element-ns.md new file mode 100644 index 0000000..34571e6 --- /dev/null +++ b/.changeset/dom-create-element-ns.md @@ -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. diff --git a/packages/ui/src/dom.ts b/packages/ui/src/dom.ts index 4614325..8b4d75b 100644 --- a/packages/ui/src/dom.ts +++ b/packages/ui/src/dom.ts @@ -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 = 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}`; + 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 { @@ -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 { @@ -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; @@ -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 = {}, + ) { 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 { @@ -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']) { @@ -263,3 +292,8 @@ export class ProcessingInstruction extends CharacterData { return this.target; } } + +interface ElementNamespace { + namespaceURI: string | null; + prefix: string | null; +} diff --git a/packages/ui/src/validation.ts b/packages/ui/src/validation.ts new file mode 100644 index 0000000..f4d6c4f --- /dev/null +++ b/packages/ui/src/validation.ts @@ -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 }; +} diff --git a/packages/ui/test/wpt.config.ts b/packages/ui/test/wpt.config.ts index 0a9959e..ab047bc 100644 --- a/packages/ui/test/wpt.config.ts +++ b/packages/ui/test/wpt.config.ts @@ -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: { @@ -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,