Skip to content
Open
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
3 changes: 2 additions & 1 deletion packages/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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:*"
Expand Down
9 changes: 9 additions & 0 deletions packages/ui/src/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -568,6 +569,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);
}
Expand Down
103 changes: 103 additions & 0 deletions packages/ui/src/serialize.ts
Original file line number Diff line number Diff line change
@@ -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<DOMTree> = {
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,
};
9 changes: 9 additions & 0 deletions packages/ui/wpt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ const config: WPTConfig<Document> = {
},
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.',
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading