# HTML & sanitization

> Type formatted HTML markup safely with allowlist sanitization and balanced tag trees.

Canonical documentation: https://staticcanvas.github.io/typeflow/guides/html-sanitization/


## Safe HTML Typing

TypeFlow constructs an internal sanitized DOM source tree and uses a recursive fragment cloner.

```javascript
import { TypeFlow } from &#39;@staticcanvas/typeflow&#39;;

TypeFlow.type(&#39;#notice&#39;, &#39;Deploy to &lt;strong&gt;production&lt;/strong&gt; with &lt;span class=&#34;highlight&#34;&gt;zero downtime&lt;/span&gt;.&#39;, {
  html: true,
  speed: 35
});
```

During each character tick, TypeFlow clones only the visible character slice while keeping all parent tags closed and well-formed.

## Allowlist &amp; Attribute Filtering

TypeFlow enforces safe HTML defaults:

- **Allowed Tags**: `b`, `strong`, `i`, `em`, `mark`, `span`, `small`, `sub`, `sup`, `u`, `br`, `a`, `code`, `ul`, `ol`, `li`.
- **Attribute Filtering**: Strips all inline event handlers (`onclick`, `onload`, etc.) and javascript URI schemes (`javascript:`, `data:`).
- **Target `_blank` Protection**: Automatically adds `rel=&#34;noopener noreferrer&#34;` to external anchor links.

### Extending Allowed Tags &amp; Attributes

```javascript
TypeFlow.type(&#39;#custom&#39;, &#39;&lt;custom-tag class=&#34;chip&#34; data-id=&#34;42&#34;&gt;Custom content&lt;/custom-tag&gt;&#39;, {
  html: true,
  allowedTags: [&#39;custom-tag&#39;],
  allowedAttributes: {
    &#39;custom-tag&#39;: [&#39;class&#39;, &#39;data-id&#39;]
  }
});
```

### Custom Sanitizer Hook

Supply a custom `sanitizer` function for specialized requirements (e.g. DOMPurify):

```javascript
import DOMPurify from &#39;dompurify&#39;;

TypeFlow.type(&#39;#output&#39;, rawUserInput, {
  html: true,
  sanitizer: (dirty) =&gt; DOMPurify.sanitize(dirty)
});
```

