Skip to content

feat(html): serialize trees as innerHTML and outerHTML through a TreeSource - #5

Open
natemoo-re wants to merge 2 commits into
mainfrom
natemoo-re/serialize
Open

natemoo-re wants to merge 2 commits into
mainfrom
natemoo-re/serialize

Conversation

@natemoo-re

@natemoo-re natemoo-re commented Dec 6, 2026 •

Copy link
Copy Markdown
Member

@clack/ui's innerHTML setter now parses with @bomb.sh/html (bombshell-dev/ui#159), but its getter still serializes through parse5. This PR adds the serializer, so @clack/ui can drop parse5. A getter slice built on it is ready locally on natemoo-re/ui-inner-html-getter, waiting for a release that includes this.

API

Import Function Reads
@bomb.sh/html serialize(node, options?), serializeOuter(node, options?) the plain tree
@bomb.sh/html/sink serialize(node, { source, scripting? }), serializeOuter(node, { source, scripting? }) a host DOM, through a TreeSource<N>

serialize writes a node's children, as the innerHTML getter does. serializeOuter writes the node itself, as outerHTML does. Both names follow parse5's.

TreeSource<N> is the read-side counterpart of TreeSink<N>. Its six required methods are type children name namespace attributes data, plus optional content and prefix. type returns DOM nodeType values, so a DOM host passes node.nodeType straight through. @clack/ui's parse5 adapter has 34 methods, 16 of them write-side stubs that throw. Its replacement has 7.

Decisions

  • TypeScript, not the engine. The engine can't read a host's objects, so serializing in wasm would first copy the tree into linear memory. src/serialize.ts imports nothing from the engine.
  • TreeSource stays separate from TreeSink. I measured one combined adapter, the way parse5's TreeAdapter works, against two separate interfaces. A combined adapter makes TreeBuilder one class with both halves, and a bundler can't drop unused class methods. parse alone grew from 8,874 to 9,382 B minified. With both halves used, combined was still 53 B larger. Kept separate, bundles that only parse are byte-identical to main.
  • An explicit stack, because the parser builds trees of any depth from untrusted input.
  • Current spec, checked against the spec text. Attribute values escape < and >. Processing instructions serialize as <?target data?>; this engine parses them, and parse5 8.1 skips them.

Size and speed

@bomb.sh/html parse5
serialize + serializeOuter, bundled, brotli 1,034 B (no wasm) 8,475 B

CPU time, medians of 21 rounds, load 7.7 (compare within a row):

Input serialize parse5 over the same tree parse5 over its own tree
400 B 1.93 µs 2.05 µs 2.62 µs
1.4 KB 7.00 µs 9.02 µs 9.03 µs
4.2 KB 21.01 µs 26.41 µs 26.15 µs
Corpus, 6 documents 2.22 ms 3.56 ms 3.33 ms

serialize is 1.06-1.29x faster than parse5 on fragments and 1.5-1.6x on documents. Escaping steps through a global regex's matches with test(), copying the text between them. That's 12-18% faster than a regex test followed by replace() with a callback, for +38 B brotli. replaceAll chains and a charCodeAt loop were both slower. A walker reading the plain tree directly, with no TreeSource, was only 7-10% faster, so the adapter stays.

Tests

test/serialize.test.ts runs parse5's serializer on the same plain trees, through a read-only parse5 adapter, so any difference comes from the serializer, not the parser. It covers 3,492 WPT tree-construction documents, the WPT fragment cases, the six corpus documents and 2,000 generated fragments. Two deliberate breaks each failed it: dropping </> from attribute escaping, and leaving basefont out of the void elements. Targeted cases cover raw-text parents, noscript under both scripting flags, void elements in and out of the HTML namespace, template contents, namespaced attributes, processing-instruction round trips, 200,000 levels of nesting, and a host TreeSource with prefixed elements.

vitest run test/: 11,078 passed, 14 expected failures.

Not covered

The spec's is value and customelementregistry output need custom element state that a TreeSource doesn't carry. Shadow roots and XML serialization are out of scope. All of this is recorded under Serialization.

Minor changeset included.

…Source

Adds serialize() and serializeOuter(): for the plain tree from @bomb.sh/html, and for any host DOM from @bomb.sh/html/sink, read through a TreeSource, the read-side counterpart of TreeSink. Follows the current spec, including processing instructions and < and > escaped in attribute values. Loads no wasm, adds nothing to bundles that only parse, and walks with an explicit stack.
@pkg-pr-new

pkg-pr-new Bot commented Dec 6, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@bomb.sh/html@5

commit: 294fcb9

Replaces a regex test plus replace() with a callback by a loop over the global regex's matches that copies the text between them. 12-18% faster on fragments and documents, +38 B brotli.
@natemoo-re
natemoo-re marked this pull request as ready for review December 6, 2026 05:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant