diff --git a/patching-explainer.md b/patching-explainer.md index 68fb277..a6f22ab 100644 --- a/patching-explainer.md +++ b/patching-explainer.md @@ -32,21 +32,32 @@ It is similar to calling `document.write()`, scoped to an node. The most atomic form of patching is opening a container node for writing, creating a `WritableStream` for it. This can be done with an API as such: ```js -const writable = elementOrShadowRoot.streamHTML(); +const writable = elementOrShadowRoot.streamHTMLUnsafe({runScripts: true}); byteOrTextStream.pipeTo(writable); ``` A few details about one-off patching: -- Trying to patch an element that is currently being patched would abort the original stream. -- Replacing the contents of an existing script would only execute the script if the original contents were empty (equivalent to `innerHTML` behavior). +- Streams do not abort each other. It is the author's responsibility to manage conflicts between multiple streams. +- Unlike contextual fragments, when `runScripts` is true, classic scripts in the stream can block the parser until they are fetched. This makes the streaming parser behave more similarly to the main parser. +- Only the unsafe variant can run scripts. +- This describes `streamHTML`, but also `streamAppendHTML`, `streamPrependHTML`, `streamBeforeHTML`, `streamAfterHTML`, and `streamReplaceWithHTML` variants are proposed. To account for HTML sanitation, this API would have an "Unsafe" version and would accept a sanitizer in its option, like [`setHTML`](https://developer.mozilla.org/en-US/docs/Web/API/Element/setHTML): ```js byteOrTextStream.pipeTo(elementOrShadowRoot.streamHTML({sanitizer})); -byteOrTextStream.pipeTo(elementOrShadowRoot.streamHTMLUnsafe({sanitizer})); +byteOrTextStream.pipeTo(elementOrShadowRoot.streamHTMLUnsafe({sanitizer, runScripts})); ``` -Also see detailed discussion at https://github.com/whatwg/html/issues/11669, will amend this explainer once that's settled. +Since user-space sanitizers like DOMPurify are not well suited for streaming, TrustedTypes only allows streaming with either sanitation or by giving it a "free pass", by blessing parser options: +```js +// This would fail if there is a default policy with `createHTML` +element.streamHTMLUnsafe({sanitizer, runScripts}); + +// This would "bless" the parser options for streaming. +element.streamHTMLUnsafe(trustedSourcePolicy.createParserOptions({sanitizer, runScripts}); +``` + +Also see detailed discussion at https://github.com/whatwg/html/issues/11669. ## Interleaved patching @@ -57,50 +68,57 @@ parses its content as raw text, finds the target element using attributes, and r
Loading...
- + ``` A few details about interleaved patching: - Templates with a valid `contentmethod` are not attached to the DOM. - If the patching element is not a direct child of ``, the outlet has to have a common ancestor with the patching element's parent. - The patch template has to be in the same tree (shadow) scope as the outlet. -- `contentmethod` can be `replace-children`, `replace` (which replaces the entire element), `append`, or `prepend`. +- `contentmethod` can be `replace-children`, or `append`. `replace-children` is the basic one that allows replacing a placeholder with its contents, + while `append` allows for multiple patches that are interleaved in the same HTML stream to accumulate. +- Interleaved patching works together with one-off patching. When a `