html-setters-polyfill
v0.1.1
Published
Polyfill for new HTML setters (appendHTML, streamHTML..etc. and unsafe variants)
Maintainers
Keywords
Readme
HTML Setters Polyfill
A polyfill for new HTML setters (streamHTML, appendHTML... etc. and unsafe variants) as proposed in WHATWG HTML issue #11669.
Features
- Insertion variants:
setHTML,beforeHTML,afterHTMLonElementandShadowRoot. - Child insertion variants:
replaceWithHTML,prependHTML,appendHTMLon nodes with children. - Insertion Unsafe variants:
setHTMLUnsafe,appendHTMLUnsafe,prependHTMLUnsafe,beforeHTMLUnsafe,afterHTMLUnsafe,replaceWithHTMLUnsafe. - Streaming variants:
steamHTML,streamHTMLUnsafe,streamBeforeHTML...etc. return aWritableStreamthat buffers and applies the HTML on close. - Supports
runScripts: trueusingcreateContextualFragment. - JSDoc types for TypeScript compatibility in JS.
- No dependencies.
- No Trusted Types support on streaming.
Usage
// Set HTML unsafe
element.setHTMLUnsafe('<div><script>console.log("hi")</script></div>', { runScripts: true });
// Stream HTML
const stream = element.streamHTMLUnsafe({ runScripts: true });
const writer = stream.getWriter();
writer.write('<div>Hello ');
writer.write('World!</div>');
writer.close();Limitations
- Streaming is not natively "streamed" to the parser (it buffers first).
- Safe streaming is only supported if
setHTMLis supported in the target browser (i.e. not Safari). - Declarative Shadow DOM (DSD) is supported if
createContextualFragmentsupports it in the target browser.
License
Contributing
We'd love to accept your patches and contributions to this project. See the enclosed CONTRIBUTING.md for details.
Disclaimer
This is not an officially supported Google product. This project is not eligible for the Google Open Source Software Vulnerability Rewards Program.
