# ting

> Opinionated HTML Sanitizer for Node.js

Latest version **2.5.1** (published 2020-01-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install ting
pnpm add ting
yarn add ting
bun add ting
```

## 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 | 2.5.1 |
| Published | 2020-01-06 |
| First published | 2016-05-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=8.0.0 |
| Dependencies | 1 |
| Unpacked size | 10.8 KB |
| Known vulnerabilities | 0 (+10 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Mgen |
| Maintainers | mgenware |
| Keywords | html sanitizer, sanitize html |

## Links

- npm: https://www.npmjs.com/package/ting
- Repository: https://github.com/mgenware/ting
- Issues: https://github.com/mgenware/ting/issues
- npm.io page: https://npm.io/package/ting

## Dependencies (1)

- [sanitize-html](https://npm.io/package/sanitize-html.md) ^1.20.1

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

- 2.5.1 (latest) — 2020-01-06
- 2.5.0 — 2019-11-13
- 2.4.4 — 2019-07-05
- 2.4.3 — 2019-06-10
- 2.4.2 — 2019-03-23
- 2.4.1 — 2019-03-15
- 2.4.0 — 2019-02-08
- 2.3.0 — 2018-11-20
- 2.2.0 — 2018-05-11
- 2.1.0 — 2018-05-10
- 2.0.0 — 2018-05-09
- 1.1.0 — 2018-05-09
- 1.0.1 — 2018-05-09
- 0.0.3 — 2016-05-14
- 0.0.2 — 2016-05-06
- … 1 more at https://npm.io/package/ting/versions

## README

# ting

[![MEAN Module](https://img.shields.io/badge/MEAN%20Module-TypeScript-blue.svg?style=flat-square)](https://github.com/mgenware/MEAN-Module)
[![Build Status](https://img.shields.io/travis/mgenware/ting.svg?style=flat-square&label=Build+Status)](https://travis-ci.org/mgenware/ting)
[![npm version](https://img.shields.io/npm/v/ting.svg?style=flat-square)](https://npmjs.com/package/ting)
[![Node.js Version](http://img.shields.io/node/v/ting.svg?style=flat-square)](https://nodejs.org/en/)

Opinionated HTML Sanitizer for Node.js. Built upon [sanitize-html](https://www.npmjs.com/package/sanitize-html).

* Keep up with the latest standards (new tags are allowed, e.g. `<aside>`, `<progress>`, `<time>`...).
* `<iframe>` is not allowed.
* `style` attribute is not allowed.
* `id` attribute is not allowed unless `idFilter` returns true (see [Options](#options)).
* Inline SVG is not allowed (use `<img>` with an external SVG source).
* Customizable via [sanitize-html](https://www.npmjs.com/package/sanitize-html) options.
* TypeScript friendly.

## Installation
```sh
yarn add ting
```

## Usage
```js
const ting = require('ting');

ting.sanitize(
  html,             // the HTML string which need to be sanitized
  options,          // [Optional] ting options
  overrideOptions,  // [Optional] a function to override the internal sanitize-html options
);
```

Example:
```js
const ting = require('ting');

const dirty = `
<script>alert(1)</script>
<img src="x.jpg" onclick="alert(1)"/>
<img src="cool.jpg"/>
<figcaption>caption</figcaption>
`;

const safe = ting.sanitize(dirty);
console.log(safe);
/** Prints
  <img src="x.jpg" />
  <img src="cool.jpg" />
  <figcaption>caption</figcaption>
 */
```

### Options
```typescript
{
  // `id` attribute is not allowed unless `idFilter` returns true
  idFilter: (id: string) => boolean;
}
```

* Example: allow all `id`s starting with `"user-content-"`:
```js
ting.sanitize(`
<a id="id-attack">bad</a>
<a id="user-content-link">fine</a>
<a>no id</a>`, {
    idFilter: (id) => {
      return id.startsWith('user-content-');
    },
  });
/** Prints
  <a id="user-content-link">fine</a>
  <a>no id</a>
 */
```

### Overriding sanitize-html Options
ting is built upon [sanitize-html](https://www.npmjs.com/package/sanitize-html), you can override the internal sanitize-html options, or pass a new one (which would make ting no different than sanitize-html). e.g. to allow `<iframe>` tags, override the `allowedTags` and `allowedAttributes` of sanitize-html options.

```js
ting.sanitize('<iframe src="https://coldfunction.com"></iframe>', 
  undefined,    // no options for ting
  (opts) => {   // override sanitize-html options
    opts.allowedTags.push('iframe');
    opts.allowedAttributes.iframe = ['src'];
    return opts;
  });
// Prints: <iframe src="https://coldfunction.com"></iframe>
```

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