From b111e1c37cc45d2f560c372f38ca5c6d64c1f236 Mon Sep 17 00:00:00 2001 From: Charles Lowell Date: Mon, 30 Nov 2026 07:57:08 -0600 Subject: [PATCH] Add HTML serialization getters with parse5 --- packages/ui/package.json | 3 +- packages/ui/src/dom.ts | 9 +++ packages/ui/src/serialize.ts | 103 +++++++++++++++++++++++++++++++++++ packages/ui/wpt.config.ts | 9 +++ pnpm-lock.yaml | 3 + 5 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 packages/ui/src/serialize.ts diff --git a/packages/ui/package.json b/packages/ui/package.json index c608e9b..384f3ac 100644 --- a/packages/ui/package.json +++ b/packages/ui/package.json @@ -42,7 +42,8 @@ "@bomb.sh/tty": "catalog:", "css-select": "^7.0.0", "@standard-schema/spec": "^1.1.0", - "hueful": "^0.8.1" + "hueful": "^0.8.1", + "parse5": "^8.1.1" }, "devDependencies": { "@bomb.sh/web-platform-tests": "workspace:*" diff --git a/packages/ui/src/dom.ts b/packages/ui/src/dom.ts index 862bcf0..874ba3d 100644 --- a/packages/ui/src/dom.ts +++ b/packages/ui/src/dom.ts @@ -6,6 +6,7 @@ import { asciiUppercase, asciiLowercase } from './ascii.ts'; import { NamedNodeMap } from './named-node-map.ts'; import { HTMLCollection } from './html-collection.ts'; import { StyleDeclaration } from './style.ts'; +import { serialize, serializeOuter } from './serialize.ts'; import { validateElementName, validateElementNamespace, @@ -582,6 +583,14 @@ export class Element extends Node { return this.nodeName; } + get innerHTML(): string { + return serialize(this); + } + + get outerHTML(): string { + return serializeOuter(this); + } + override get textContent(): string { return getTextContent(this); } diff --git a/packages/ui/src/serialize.ts b/packages/ui/src/serialize.ts new file mode 100644 index 0000000..9735541 --- /dev/null +++ b/packages/ui/src/serialize.ts @@ -0,0 +1,103 @@ +/* oxlint-disable bombshell-dev/exported-function-async -- DOM serialization is synchronous. */ + +import { html, serialize as serializeHTML, serializeOuter as serializeOuterHTML } from 'parse5'; +import type { TreeAdapter, TreeAdapterTypeMap } from 'parse5'; +import { Comment, type Document, DocumentType, Element, Node, Text, XMLDocument } from './dom.ts'; + +/** Serialize an element's children as HTML. */ +export function serialize(element: Element): string { + assertHTMLDocument(element); + return serializeHTML(element, { treeAdapter }); +} + +/** Serialize an element and its children as HTML. */ +export function serializeOuter(element: Element): string { + assertHTMLDocument(element); + return serializeOuterHTML(element, { treeAdapter }); +} + +function assertHTMLDocument(element: Element): void { + if (element.ownerDocument instanceof XMLDocument) { + throw new DOMException('XML serialization is not supported', 'NotSupportedError'); + } +} + +function unsupported(): never { + throw new DOMException('This tree adapter only supports serialization', 'NotSupportedError'); +} + +type DOMTree = TreeAdapterTypeMap< + Node, + Node, + Node, + Document, + Node, + Element, + Comment, + Text, + Element, + DocumentType +>; + +const treeAdapter: TreeAdapter = { + getAttrList(element) { + return Array.from(element.attributes, (attribute) => ({ + name: attribute.localName, + value: attribute.value, + namespace: attribute.namespaceURI ?? undefined, + prefix: attribute.prefix ?? undefined, + })); + }, + getChildNodes(node) { + if (node.childNodes.some((child) => child.nodeType === Node.PROCESSING_INSTRUCTION_NODE)) { + throw new DOMException( + 'Processing instruction serialization is not supported', + 'NotSupportedError', + ); + } + return Array.from(node.childNodes); + }, + getCommentNodeContent: (node) => node.data, + getDocumentMode: unsupported, + getDocumentTypeNodeName: (node) => node.name, + getDocumentTypeNodePublicId: (node) => node.publicId, + getDocumentTypeNodeSystemId: (node) => node.systemId, + getFirstChild: (node) => node.firstChild, + getNamespaceURI: (element) => element.namespaceURI as html.NS, + getNodeSourceCodeLocation: () => undefined, + getParentNode: (node) => node.parentNode, + getTagName(element) { + if ( + element.namespaceURI === html.NS.HTML || + element.namespaceURI === html.NS.SVG || + element.namespaceURI === html.NS.MATHML + ) { + return element.localName; + } + return element.prefix === null ? element.localName : `${element.prefix}:${element.localName}`; + }, + getTextNodeContent: (node) => node.data, + getTemplateContent() { + throw new DOMException('Template contents are not supported', 'NotSupportedError'); + }, + isCommentNode: (node): node is Comment => node instanceof Comment, + isDocumentTypeNode: (node): node is DocumentType => node instanceof DocumentType, + isElementNode: (node): node is Element => node instanceof Element, + isTextNode: (node): node is Text => node instanceof Text, + adoptAttributes: unsupported, + appendChild: unsupported, + createCommentNode: unsupported, + createTextNode: unsupported, + createDocument: unsupported, + createDocumentFragment: unsupported, + createElement: unsupported, + detachNode: unsupported, + insertBefore: unsupported, + insertText: unsupported, + insertTextBefore: unsupported, + setDocumentMode: unsupported, + setDocumentType: unsupported, + setNodeSourceCodeLocation: unsupported, + updateNodeSourceCodeLocation: unsupported, + setTemplateContent: unsupported, +}; diff --git a/packages/ui/wpt.config.ts b/packages/ui/wpt.config.ts index e419c73..293b52c 100644 --- a/packages/ui/wpt.config.ts +++ b/packages/ui/wpt.config.ts @@ -60,6 +60,15 @@ const config: WPTConfig = { }, include: { 'html/syntax/parsing/the-end.html': ['DOMContentLoaded', 'load'], + 'domparsing/Comment-serialization-double-hyphen.html': true, + 'domparsing/innerhtml-06.html': true, + 'html/syntax/serializing-html-fragments/initial-linefeed-pre.html': true, + 'html/syntax/serializing-html-fragments/outerHTML.html': [ + /^(?!Node for template$)/, + // Template contents are not implemented yet. + ], + 'html/syntax/serializing-html-fragments/serializing-lt-gt.html': true, + 'html/syntax/serializing-html-fragments/serializing.html': true, 'html/dom/elements/global-attributes/id-name.html': true, 'html/dom/elements/global-attributes/id-attribute.html': [ 'After setting id via id attribute, getElementById find the element by the new id.', diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 288fa8b..a591839 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -248,6 +248,9 @@ importers: hueful: specifier: ^0.8.1 version: 0.8.1 + parse5: + specifier: ^8.1.1 + version: 8.1.1 devDependencies: '@bomb.sh/web-platform-tests': specifier: workspace:*