Skip to content

Stream HTML into element or ShadowRoot - #12758

Open
noamr wants to merge 7 commits into
noamr/streaming-sanitizerfrom
noamr/stream-html
Open

Stream HTML into element or ShadowRoot#12758
noamr wants to merge 7 commits into
noamr/streaming-sanitizerfrom
noamr/stream-html

Conversation

@noamr

@noamr noamr commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Add streaming HTML insertion methods to Element, ShadowRoot, and ChildNode

Introduce safe and unsafe streaming HTML parsing and insertion methods:

  • Element & ShadowRoot: streamHTML(), streamHTMLUnsafe(), streamAppendHTML(), streamAppendHTMLUnsafe(), streamPrependHTML(), and streamPrependHTMLUnsafe()
  • ChildNode: streamBeforeHTML(), streamBeforeHTMLUnsafe(), streamAfterHTML(), streamAfterHTMLUnsafe(), streamReplaceWithHTML(), and streamReplaceWithHTMLUnsafe()
    This allows developers to parse and stream HTML directly into specific positions of the DOM tree using WritableStream.

The HTML fragment parser is divided into two:

  • Preparing the fragment parser (create an inert document with a proper element stack)
  • Using that parser to parse into a DocumentFragment

The preparation algorithm is used when streaming, by creating a new fragment parser, and then the stream is routed to feed that parser.

When parsing, the "insertion target redirection map" is configured to reroute insertions intended for the root to go directly into the stream's target element, instead of going into the DocumentFragment.

A special sanitizer mitigation is in place to avoid these removed elements from incurring side effects, by creating them in an inert document if they are about to be removed.

"Scripting mode" has been refactored so that the streaming fragment parser can execute scripts. Instead of relying on the fragment/inert/normal scripting mode, the parser receives a "target document" which is assigned to the script as an "intended document" and checked at preparation time. It is set to the dummy inert document for inert fragment parsing, and to the context document when runScripts is true.

Closes #2142

(https://github.com/whatwg/meta/blob/main/COMMITTING.md) to use.

(See WHATWG Working Mode: Changes for more details.)


💥 Error: Wattsi server error 💥

PR Preview failed to build. (Last tried on Aug 12, 2026, 4:09 PM UTC).

More

PR Preview relies on a number of web services to run. There seems to be an issue with the following one:

🚨 Wattsi Server - Wattsi Server is the web service used to build the WHATWG HTML spec.

🔗 Related URL

Error output:

      <!DOCTYPE html>
      <html>
      <head>
          <meta name="viewport" content="width=device-width, initial-scale=1">
          <meta name="robots" content="noindex">
          <style>body,html{height:100%;margin:0}body{display:flex;align-items:center;justify-content:center;flex-direction:column;-webkit-font-smoothing:antialiased;text-rendering:optimizeLegibility}p{text-align:center;font-family:-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen,Ubuntu,Cantarell,Fira Sans,Droid Sans,Helvetica Neue,sans-serif;color:#000;font-size:14px;margin-top:-50px}p.code{font-size:24px;font-weight:500;border-bottom:1px solid #e0e1e2;padding:0 20px 15px}p.text{margin:0}a,a:visited{color:#aaa}</style>
      </head>
      <body>
      <p class="code">
        Error code: 503      </p>
      <p class="text">
        Well, This is unexpected. An Error has occurred, and we are working to fix the problem! We will be up and running shortly. Try refreshing the page or try again in a few minutes.
      </p>
        <div style="display:none;">
          <h1>
    upstream_reset_before_response_started{connection_termination} (503 UC)      </h1>
          <p data-translate="connection_timed_out">App Platform failed to forward this request to the application.</p>
      </div>
      </body>
      </html>
    

This seems to be an issue with the Wattsi Server service. PR Preview doesn't manage this service and so has no control over it. If you've identified an issue with it, you can report the issue to the maintainers of Wattsi Server directly. Please be courteous. Thank you!

If you don't have enough information above to solve the error by yourself or if the issue doesn't seem related to Wattsi Server, you can file an issue with PR Preview.

@noamr noamr changed the title Add streaming HTML insertion methods Streaming HTML insertion methods Aug 5, 2026
@noamr noamr changed the title Streaming HTML insertion methods Stream HTML into element or ShadowRoot Aug 5, 2026
@noamr
noamr force-pushed the noamr/stream-html branch from df24ea2 to e416732 Compare August 5, 2026 09:32
@noamr noamr closed this Aug 5, 2026
@noamr noamr reopened this Aug 5, 2026
@noamr
noamr force-pushed the noamr/stream-html branch from e416732 to 5106e9d Compare August 6, 2026 16:23
@noamr
noamr force-pushed the noamr/stream-html branch from deb55bb to e97c374 Compare August 6, 2026 19:46
@noamr
noamr force-pushed the noamr/stream-html branch from d5a18a7 to b19b385 Compare August 7, 2026 20:17
Comment thread source
Comment thread source Outdated
Comment thread source Outdated
Comment thread source Outdated
@noamr
noamr force-pushed the noamr/stream-html branch from d87ab8d to 4880d38 Compare August 11, 2026 15:49
@noamr
noamr force-pushed the noamr/stream-html branch from 2de75ec to ce9f49b Compare August 11, 2026 20:18
noamr added a commit to web-platform-tests/wpt that referenced this pull request Aug 12, 2026
See whatwg/html#12758

Note that tests that assert how this works with <template for>
are still tentative as that PR is still not landed.

@zcorpan zcorpan left a comment

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.

LGTM % spaces

Comment thread source Outdated
Comment thread source Outdated
@noamr
noamr force-pushed the noamr/stream-html branch from 421aa3f to 043dcdf Compare August 12, 2026 13:02
@noamr
noamr force-pushed the noamr/stream-html branch from 043dcdf to 286c499 Compare August 12, 2026 13:25
@noamr
noamr force-pushed the noamr/stream-html branch from 286c499 to 146266b Compare August 12, 2026 16:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

A way to stream content into an element

2 participants