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 @@ -7,6 +7,7 @@ import { exposeAttributes, NamedNodeMap } from './named-node-map.ts';
import { HTMLCollection } from './html-collection.ts';
import { StyleDeclaration } from './style.ts';
import type { Window } from './window.ts';
import { serialize, serializeOuter } from './serialize.ts';
import {
addEventListener,
removeEventListener,
Expand Down Expand Up @@ -609,6 +610,14 @@ export class Element extends Node {
this.appendChild(fragment);
}

get innerHTML(): string {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with only a getter, el.innerHTML = ... from strict code now throws a generic TypeError, where before it silently set an own prop. vue, solid and preact's dangerouslySetInnerHTML all assign it. is the setter coming soon, or worth a setter that throws a clearer NotSupportedError until then?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we will need to have them eventually. We are actually already using parse5 in the WPT to parse the html files and build our DOM, so it isn't even that far off.

However, I know that @natemoo-re is also cooking something with html parsing as well, but we could wire it in pretty easily.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reference #137

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 @@ -102,6 +102,15 @@ const config: WPTConfig<Document> = {
'webidl/ecmascript-binding/legacy-platform-object/OwnPropertyKeys.html': [
'must enumerate property names in list order',
],
'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