1.0.3 • Published 3 years ago
@saasquatch/stencil-html-parser v1.0.3
stencil-html-parser
HTML to Stencil Parser. Takes HTML as a string, uses a template tag to turn it into DOM, then converts those nodes into Stencil VNode elements.
Usage
Preferred use of this library is via an import from NPM and bundled into your stencil components.
npm i @saasquatch/stencil-html-parserThe parse method will provide VNode elements that can be rendered directly.
import { Component, h, Host, State, Method } from "@stencil/core";
import { parse } from "@saasquatch/stencil-html-parser";
@Component({
tag: "html-renderer",
})
export class TestComponent {
@Prop() html: string = `<div>Hello world</div>`;
render() {
try{
const el = this.html?parse(this.html):"";
return <Host>{el}</Host>;
}catch(e){
console.error("Parsing error", e.message);
return <Host></Host>
}
}
}What is supported?
Security
You can sanitize the HTML before rendering, e.g. with DOMPurify
Why use this library?
- Unlike setting
innerHTMLon a component, the parsedVNodeelements will maintain state, and can be modified before being inserted.