Repository navigation
feat(html): serialize trees as innerHTML and outerHTML through a TreeSource - #5
Open
natemoo-re wants to merge 2 commits into
Open
natemoo-re wants to merge 2 commits into
natemoo-re wants to merge 2 commits into
Conversation
…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.
commit: |
6 of 12 tasks
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
marked this pull request as ready for review
December 6, 2026 05:09
6 of 12 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
@clack/ui'sinnerHTMLsetter now parses with@bomb.sh/html(bombshell-dev/ui#159), but its getter still serializes through parse5. This PR adds the serializer, so@clack/uican drop parse5. A getter slice built on it is ready locally onnatemoo-re/ui-inner-html-getter, waiting for a release that includes this.API
@bomb.sh/htmlserialize(node, options?),serializeOuter(node, options?)@bomb.sh/html/sinkserialize(node, { source, scripting? }),serializeOuter(node, { source, scripting? })TreeSource<N>serializewrites a node's children, as theinnerHTMLgetter does.serializeOuterwrites the node itself, asouterHTMLdoes. Both names follow parse5's.TreeSource<N>is the read-side counterpart ofTreeSink<N>. Its six required methods aretype children name namespace attributes data, plus optionalcontentandprefix.typereturns DOMnodeTypevalues, so a DOM host passesnode.nodeTypestraight through.@clack/ui's parse5 adapter has 34 methods, 16 of them write-side stubs that throw. Its replacement has 7.Decisions
src/serialize.tsimports nothing from the engine.TreeSourcestays separate fromTreeSink. I measured one combined adapter, the way parse5'sTreeAdapterworks, against two separate interfaces. A combined adapter makesTreeBuilderone class with both halves, and a bundler can't drop unused class methods.parsealone 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 tomain.<and>. Processing instructions serialize as<?target data?>; this engine parses them, and parse5 8.1 skips them.Size and speed
@bomb.sh/htmlserialize+serializeOuter, bundled, brotliCPU time, medians of 21 rounds, load 7.7 (compare within a row):
serializeserializeis 1.06-1.29x faster than parse5 on fragments and 1.5-1.6x on documents. Escaping steps through a global regex's matches withtest(), copying the text between them. That's 12-18% faster than a regex test followed byreplace()with a callback, for +38 B brotli.replaceAllchains and acharCodeAtloop were both slower. A walker reading the plain tree directly, with noTreeSource, was only 7-10% faster, so the adapter stays.Tests
test/serialize.test.tsruns 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 leavingbasefontout of the void elements. Targeted cases cover raw-text parents,noscriptunder 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 hostTreeSourcewith prefixed elements.vitest run test/: 11,078 passed, 14 expected failures.Not covered
The spec's
isvalue andcustomelementregistryoutput need custom element state that aTreeSourcedoesn't carry. Shadow roots and XML serialization are out of scope. All of this is recorded under Serialization.Minor changeset included.