A tiny, friendly sanitizer for Svelte that keeps your HTML shiny and safe using DOMPurify. SSR-ready by default.
options
npm i -S @humanspeak/svelte-purify
# or
pnpm add @humanspeak/svelte-purify
# or
yarn add @humanspeak/svelte-purify
<script lang="ts">
import { SveltePurify } from '@humanspeak/svelte-purify'
const html = `<p>Hello <strong>world</strong><script>alert(1)</script></p>`
</script>
<SveltePurify {html} />
Pass any DOMPurify.sanitize
options. We don’t hide anything—use the full power of DOMPurify.
<script lang="ts">
import { SveltePurify } from '@humanspeak/svelte-purify'
const html = `<a href="javascript:alert(1)" title="nope">click me</a>`
const options = {
ALLOWED_TAGS: ['a'],
ALLOWED_ATTR: ['href', 'title']
}
</script>
<SveltePurify {html} {options} />
Note: The component returns sanitized HTML as a string (not DOM nodes).
Component | Prop | Type | Description |
---|---|---|---|
SveltePurify |
html |
string |
Raw HTML to sanitize and render |
options |
Parameters<typeof DOMPurify.sanitize>[1] |
DOMPurify options (all supported) |
import { SveltePurify } from '@humanspeak/svelte-purify'
This library delegates sanitization to DOMPurify, a battle-tested sanitizer. It removes script tags, event handler attributes (like onerror
), and unsafe URLs (javascript:
), among many other protections.
Strip a specific tag with DOMPurify options:
<SveltePurify html="<p>Hello <strong>world</strong></p>" options={{ FORBID_TAGS: ['strong'] }} />
Allow an extra tag:
<SveltePurify
html="<iframe src=\"about:blank\"></iframe>"
options={{ ADD_TAGS: ['iframe'] }}
/>
MIT © Humanspeak, Inc.
Made with ❤️ by Humanspeak
Special thanks to @jill64 — her years of Svelte contributions taught me so much and inspired this work.