# vue-dompurify-html

> Safe replacement for the v-html directive

Latest version **5.3.0** (published 2025-05-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install vue-dompurify-html
pnpm add vue-dompurify-html
yarn add vue-dompurify-html
bun add vue-dompurify-html
```

## Health

**Score 60/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; has provenance; high maintenance score; high quality score.

Warnings: low downloads.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 5.3.0 |
| Published | 2025-05-05 |
| First published | 2019-04-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 13.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 334 |
| Author | Thomas Gerbet |
| Maintainers | tgerbet |
| Keywords | vue, vue-directive, xss, dom-xss, dompurify, security, html |

## Links

- npm: https://www.npmjs.com/package/vue-dompurify-html
- Repository: https://github.com/LeSuisse/vue-dompurify-html
- Homepage: https://github.com/LeSuisse/vue-dompurify-html/tree/main/packages/vue-dompurify-html#readme
- Issues: https://github.com/LeSuisse/vue-dompurify-html/issues
- npm.io page: https://npm.io/package/vue-dompurify-html

## Dependencies (1)

- [dompurify](https://npm.io/package/dompurify.md) ^3.2.5

## Alternatives

- [@tsparticles/shape-image](https://npm.io/package/@tsparticles/shape-image.md) — 303.7K weekly downloads
- [@tsparticles/shape-line](https://npm.io/package/@tsparticles/shape-line.md) — 233.7K weekly downloads
- [stringify-attributes](https://npm.io/package/stringify-attributes.md) — 58.6K weekly downloads
- [mobile-drag-drop](https://npm.io/package/mobile-drag-drop.md) — 46.3K weekly downloads
- [@comunica/actor-rdf-parse-html](https://npm.io/package/@comunica/actor-rdf-parse-html.md) — 29.2K weekly downloads

## Recent versions

- 5.3.0 (latest) — 2025-05-05
- 2.6.0 (vue-legacy) — 2022-06-05
- 5.2.0 — 2024-11-24
- 5.1.0 — 2024-06-16
- 5.0.1 — 2023-12-15
- 5.0.0 — 2023-12-13
- 4.1.4 — 2023-05-22
- 4.1.3 — 2023-05-22
- 4.1.2 — 2023-05-22
- 4.0.0 — 2023-02-16
- 3.1.2 — 2022-12-15
- 3.1.1 — 2022-09-05
- 3.1.0 — 2022-08-28
- 3.0.0 — 2022-05-13
- 2.5.2 — 2022-04-24
- … 22 more at https://npm.io/package/vue-dompurify-html/versions

## README

# vue-dompurify-html

[![npm](https://img.shields.io/npm/v/vue-dompurify-html)](https://www.npmjs.com/package/vue-dompurify-html)
[![Build Status](https://github.com/LeSuisse/vue-dompurify-html/actions/workflows/CI.yml/badge.svg?branch=main)](https://github.com/LeSuisse/vue-dompurify-html/actions/workflows/CI.yml?query=branch%3Amain)
[![Code Coverage](https://codecov.io/gh/LeSuisse/vue-dompurify-html/branch/main/graph/badge.svg)](https://codecov.io/gh/LeSuisse/vue-dompurify-html)

A "safe" replacement for the `v-html` directive. The HTML code is
sanitized with [DOMPurify](https://github.com/cure53/DOMPurify) before being interpreted.

This is only a small wrapper around DOMPurify to ease its usage in a Vue app.
You should take a look at the
[DOMPurify Security Goals & Threat Model](https://github.com/cure53/DOMPurify/wiki/Security-Goals-&-Threat-Model)
to understand what are the limitations and possibilities.

## Installation

```
npm install vue-dompurify-html
```

The current version is only compatible with Vue 3. If you need Vue 2 support use a 4.1.x version.

## Usage

You can see setup examples in the [examples/](../../examples) folder.

```js
import { createApp } from 'vue';
import App from './App.vue';
import VueDOMPurifyHTML from 'vue-dompurify-html';

const app = createApp(App);
app.use(VueDOMPurifyHTML);
app.mount('#app');
```

In your <abbr title="Single File Component">SFC</abbr>:

```vue
<template>
    <div v-dompurify-html="rawHtml"></div>
</template>
<script setup>
import { ref } from 'vue';

const rawHtml = ref('<span style="color: red">This should be red.</span>');
</script>
```

You can also define your [DOMPurify configurations](https://github.com/cure53/DOMPurify#can-i-configure-dompurify):

```js
import { createApp } from 'vue';
import App from './App.vue';
import VueDOMPurifyHTML from 'vue-dompurify-html';

const app = createApp(App);
app.use(VueDOMPurifyHTML, {
    namedConfigurations: {
        svg: {
            USE_PROFILES: { svg: true },
        },
        mathml: {
            USE_PROFILES: { mathMl: true },
        },
    },
});
app.mount('#app');
```

Your configuration keys can then be used as an argument of the directive:

```vue
<template>
    <div v-dompurify-html="rawHtml"></div>
    <div v-dompurify-html:svg="svgContent"></div>
</template>
<script setup>
import { ref } from 'vue';

const rawHtml = ref('<span style="color: red">This should be red.</span>');
const svgContent = ref('<svg><rect height="50"></rect></svg>');
</script>
```

Alternatively, you can define a default [DOMPurify configuration](https://github.com/cure53/DOMPurify#can-i-configure-dompurify):

```js
import { createApp } from 'vue';
import App from './App.vue';
import VueDOMPurifyHTML from 'vue-dompurify-html';

const app = createApp(App);
app.use(VueDOMPurifyHTML, {
    default: {
        USE_PROFILES: { html: false },
    },
});
app.mount('#app');
```

The `default` [DOMPurify configuration](https://github.com/cure53/DOMPurify#can-i-configure-dompurify) will be used:

```vue
<template>
    <div v-dompurify-html="rawHtml"></div>
</template>
<script setup>
import { ref } from 'vue';

const rawHtml = ref('<span style="color: red">This should be red.</span>');
</script>
```

There is also the possibility to set-up [DOMPurify hooks](https://github.com/cure53/DOMPurify#hooks):

```js
import { createApp } from 'vue';
import App from './App.vue';
import VueDOMPurifyHTML from 'vue-dompurify-html';

const app = createApp(App);
app.use(VueDOMPurifyHTML, {
    hooks: {
        uponSanitizeElement: (currentNode) => {
            // Do something with the node
        },
    },
});
app.mount('#app');
```

If needed you can use the directive without installing it globally:

```vue
<template>
    <div v-dompurify-html="rawHtml"></div>
</template>

<script setup lang="ts">
import { buildVueDompurifyHTMLDirective } from '../src/';

const vdompurifyHtml = buildVueDompurifyHTMLDirective(<config...>);
const rawHtml = '<span style="color: red">Hello!</span>';
</script>
```

## Usage with [Nuxt 3](https://nuxtjs.org/)

In your Nuxt folder, add the [`isomorphic-dompurify` dependency](https://github.com/kkomelin/isomorphic-dompurify)
and create a new plugin `plugins/dompurify-html.ts` with the following content:

```js
import VueDOMPurifyHTML from 'vue-dompurify-html';
import { JSDOM } from 'jsdom';
import DOMPurify from 'isomorphic-dompurify';

export default defineNuxtPlugin((nuxtApp) => {
    nuxtApp.vueApp.use(
        VueDOMPurifyHTML,
        { enableSSRPropsSupport: true },
        () => DOMPurify,
    );
});
```

You can use the same configuration options than the Vue setup. You can see a complete example
with some advanced configuration in the [Nuxt 3 example](../../examples/nuxt3/).

**Note:** due to [current limitations](https://github.com/vuejs/core/issues/8112), the content processed by the
directive are only processed client side.

---
_Source: https://npm.io/package/vue-dompurify-html · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
