# ts-html-sanitizer

> Extendable HTML sanitizer for typescript and JS projects

Latest version **1.0.5** (published 2016-03-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install ts-html-sanitizer
pnpm add ts-html-sanitizer
yarn add ts-html-sanitizer
bun add ts-html-sanitizer
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.5 |
| Published | 2016-03-09 |
| First published | 2016-03-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | nyrkovalex@gmail.com |
| Maintainers | nyrkovalex |
| Keywords | sanitize, html, sanitizer, cleanup |

## Links

- npm: https://www.npmjs.com/package/ts-html-sanitizer
- Repository: https://github.com/nyrkovalex/ts-html-sanitizer
- Homepage: https://github.com/nyrkovalex/ts-html-sanitizer#readme
- Issues: https://github.com/nyrkovalex/ts-html-sanitizer/issues
- npm.io page: https://npm.io/package/ts-html-sanitizer

## Dependencies (1)

- [htmlparser2](https://npm.io/package/htmlparser2.md) ^3.9.0

## 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

- 1.0.5 (latest) — 2016-03-09
- 1.0.4 — 2016-03-06
- 1.0.3 — 2016-03-06
- 1.0.2 — 2016-03-06
- 1.0.1 — 2016-03-06
- 1.0.0 — 2016-03-06
- 0.1.1 — 2016-03-05
- 0.1.0 — 2016-03-05

## README

# ts-html-sanitizer

Extendable HTML sanitizer written in typescript on top of [htmlparser2](https://www.npmjs.com/package/htmlparser2)
suitable for both JS and TS projects

## Usage
```typescript
import * as s from 'ts-html-sanitizer'; // for TS users
// let s = require('ts-html-sanitizer'); // for JS users

const INPUT = `
  <html>
    <head>
      <title>Something</title>
    </head>
    <body class="content" style="color: red">
      <form action="/message" method="post">
        <input name="text" />
        <button type="submit">Send</button>
      </form>
      <main>
        <h1 class="heading">Awesome page</h1>
        <section style="width: 600px">
          <img src="/1.jpg" alt="some image">
          <P>
            Some <big>BIG</big> text <a href="/boom">here</a>
            With custom <font>font</font>
          </P>
        </section>
      </main>
      <script>
        alert('malicious script');
      </script>
    </body>
  </html>
`;

let sanitizer = s.sanitizer();
console.log(sanitizer.sanitize(INPUT));

// outputs
// <section><h1>Awesome page</h1><section><img src="/1.jpg" alt="some image" /><p> Some <strong>BIG</strong> text <a href="/boom" target="_blank" rel="nofollow">here</a> With custom font</p></section></section>
```


## `sanitizer()`
`sanitizer` function creates an instance of `Sanitize.Sanitizer` and takes an optional parameter:
* `tagMap: Object` — transforms mapped to tag names


## `Sanitize.Sanitizer`
Interface declares a single `sanitize()` function which takes following arguments:
* `input: string` — html input to sanitize
* `options: Sanitize.Options` — an object with following keys:
  * `sourceHost: string` — hostname of a document source used by link transformer, it will be appended to links starting with `"/"`
  so `src="/1.png"` will become `src="${sourceHost}/1.png"`. Defaults to empty string
  * `sourcePath: string` — path of a source document, it will be append to realtive links so `href="next.html"`
  will become `href="${sourceHost}/${sourcePath}/next.html"`. Defaults to empty string


## Customization

Parsing logic can be greatly altered by creating a new or modifying the default `Sanitize.TagMap`.

All you need to do is register your own function implementing `Transforms.Transform` interface within the tag map.
Transfomations can be used as middleware functions by utilizing `Transfomrs.chain()` which takes an array of tranformations and runs is top down.

One may also consider writing custom `Tags.Tag` implmentation.


_More documentation to come, consult the source code for any details it's really simple_

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