diff --git a/packages/ui/src/dom.ts b/packages/ui/src/dom.ts index b3d1c3a..154acc9 100644 --- a/packages/ui/src/dom.ts +++ b/packages/ui/src/dom.ts @@ -222,8 +222,30 @@ export class DOMImplementation { this.#notify = notify; } - createDocument(): Document { - return new Document(this.#notify); + // oxlint-disable-next-line bombshell-dev/max-params -- DOMImplementation.createDocument uses three positional arguments. + createDocument( + namespace: string | null, + qualifiedName: string | null, + doctype: DocumentType | null = null, + ): XMLDocument { + if (arguments.length < 2) { + throw new TypeError('createDocument requires a namespace and qualified name'); + } + const namespaceURI = namespace == null ? null : `${namespace}` || null; + const name = qualifiedName === null ? '' : `${qualifiedName}`; + if (doctype !== null && !(doctype instanceof DocumentType)) { + throw new TypeError('createDocument requires a DocumentType or null for its doctype'); + } + + const document = new XMLDocument(this.#notify); + const element = name === '' ? null : document.createElementNS(namespaceURI, name); + if (doctype !== null) { + document.appendChild(doctype); + } + if (element !== null) { + document.appendChild(element); + } + return document; } createHTMLDocument(title?: string): HTMLDocument { @@ -730,3 +752,7 @@ export class HTMLBodyElement extends HTMLElement {} export class HTMLSpanElement extends HTMLElement {} export class HTMLUnknownElement extends HTMLElement {} + +/** XML */ + +export class XMLDocument extends Document {} diff --git a/packages/ui/test/wpt.config.ts b/packages/ui/test/wpt.config.ts index 893b16f..51fe144 100644 --- a/packages/ui/test/wpt.config.ts +++ b/packages/ui/test/wpt.config.ts @@ -15,6 +15,7 @@ import { HTMLUnknownElement, Node, Text, + XMLDocument, } from '../src/dom.ts'; import { NamedNodeMap } from '../src/named-node-map.ts'; @@ -42,6 +43,7 @@ const config: WPTConfig = { Node, Text, TypeError: globalThis.TypeError, + XMLDocument, }, include: { 'dom/events/Event-constructors.any.js': true, @@ -61,6 +63,18 @@ const config: WPTConfig = { 'dom/nodes/Document-createElement.html': true, 'dom/nodes/Document-createElementNS.html': true, 'dom/nodes/Document-createComment.html': true, + 'dom/nodes/DOMImplementation-createDocument.html': [ + 'createDocument row 0', // null qualified name: no root. + 'createDocument row 1', // undefined qualified name: an "undefined" root. + 'createDocument row 2', // A root without a namespace. + 'createDocument row 3', // Invalid qualified name. + 'createDocument row 23', // A prefix requires a namespace. + 'createDocument row 63', // A root with a namespace and prefix. + 'createDocument row 182', // A non-DocumentType doctype argument. + 'createDocument row 183', // Empty qualified name: no root. + 'createDocument row 185', // Explicit undefined arguments. + 'createDocument with missing arguments', + ], 'dom/nodes/DOMImplementation-createHTMLDocument.html': [ 'createHTMLDocument()', 'createHTMLDocument(null)', @@ -72,7 +86,7 @@ const config: WPTConfig = { 'createHTMLDocument(foo\r\rbar baz)', 'Missing title argument', ], - // XML factory cases require DOMImplementation.createDocument. + // XML cases also require createAttribute() to preserve name casing. 'dom/nodes/Document-createAttribute.html': [/^HTML document\.createAttribute\(/], 'dom/nodes/CharacterData-data.html': true, 'dom/nodes/CharacterData-remove.html': true,