HtmlStreamWriter Class

The writer of the HTML streamed into an element or a shadow root (https://whatpr.org/html/12758/dynamic-markup-insertion.html#stream-html), returned by Element.StreamAppendHtmlUnsafe and the other streaming methods: the chunks are parsed as they are written, by a fragment parser that inserts the nodes directly into the target (before the reference child), so the template for elements patch the content as it arrives. In JavaScript, the methods return a WritableStream whose sink is this writer.

public sealed class HtmlStreamWriter

Remarks

With runScripts, the streamed scripts are parser-inserted: an inline script runs when its end tag is parsed, an async script runs as soon as it is fetched, a parser-blocking script (an external classic script without async or defer) pauses the parser until it is fetched and run from a task (the chunks written meanwhile are parsed after it), and the defer and module scripts run from a task once the stream is closed.

Properties

NameDescription
IsCompletedGets whether the stream is closed or aborted: the writer cannot be written to anymore.

Gets whether the stream is closed or aborted: the writer cannot be written to anymore.

public bool IsCompleted { get; }

Property value

bool

Methods

NameDescription
Abort()Aborts the parser: the rest of the input is not parsed, and the script elements that have not run yet never run.
Close()Parses the end of the input (the close algorithm of https://whatpr.org/html/12758/dynamic-markup-insertion.html#stream-html). When the parser waits for a parsing-blocking script, the rest of the input is parsed once the script has run.
Write(string)Parses a chunk of HTML, after the previous chunks (the write algorithm of https://whatpr.org/html/12758/dynamic-markup-insertion.html#stream-html).

Aborts the parser: the rest of the input is not parsed, and the script elements that have not run yet never run.

public void Abort()

Parses the end of the input (the close algorithm of https://whatpr.org/html/12758/dynamic-markup-insertion.html#stream-html). When the parser waits for a parsing-blocking script, the rest of the input is parsed once the script has run.

public void Close()

Exceptions

InvalidOperationException
The writer is closed or aborted.

Write(string)

HtmlStreamWriter.cs

Parses a chunk of HTML, after the previous chunks (the write algorithm of https://whatpr.org/html/12758/dynamic-markup-insertion.html#stream-html).

public void Write(string chunk)

Parameters

chunk string

Exceptions

InvalidOperationException
The writer is closed or aborted.
DomException
The reference child is no longer a child of the target: the parser is aborted.

Remarks

The method returns without waiting for a parsing-blocking script: the chunk is parsed once the script has run. A chunk written by a script that the parser runs is parsed after the rest of the current chunk.