# apply-html

> It's `.innerHTML = ''` for the 21st century.

Latest version **2.0.1** (published 2018-08-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install apply-html
pnpm add apply-html
yarn add apply-html
bun add apply-html
```

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.1 |
| Published | 2018-08-02 |
| First published | 2018-01-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Node | >= 6 |
| Dependencies | 0 |
| Unpacked size | 64.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 7 |
| Author | Shannon Moeller |
| Maintainers | shannonmoeller |
| Keywords | apply, bel, diff, dom, html, isomorphic, literal, morph, nano, patch, render, tag, template |

## Links

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

## 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.0.1 (latest) — 2018-08-02
- 2.0.0-1 (next) — 2018-08-02
- 2.0.0 — 2018-08-02
- 2.0.0-0 — 2018-08-02
- 1.0.0 — 2018-05-23
- 1.0.0-23 — 2018-02-14
- 1.0.0-22 — 2018-02-13
- 1.0.0-21 — 2018-02-09
- 1.0.0-20 — 2018-02-04
- 1.0.0-19 — 2018-02-04
- 1.0.0-18 — 2018-02-04
- 1.0.0-17 — 2018-02-03
- 1.0.0-16 — 2018-02-02
- 1.0.0-15 — 2018-02-01
- 1.0.0-14 — 2018-02-01
- … 12 more at https://npm.io/package/apply-html/versions

## README

# apply-html

[![NPM version][npm-img]][npm-url] [![Downloads][downloads-img]][npm-url] [![Build Status][travis-img]][travis-url] [![Coverage Status][coveralls-img]][coveralls-url]

It's `.innerHTML = ''` for the 21st century!

Yet another library to diff and patch an existing DOM tree by efficiently comparing it to a string. Why? This library is a little bit different than [others](#acknowledgements). It makes use of an [HTML `<template>`'s](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/template) unique ability to create an inert [document fragment](https://developer.mozilla.org/en-US/docs/Web/API/DocumentFragment), featuring:

- A real DOM tree
- Multiple root nodes
- Will not trigger resource loading prematurely
- Will not apply embedded stylesheets prematurely
- Will not trigger custom element constructors or lifecycle events prematurely

The live DOM is then patched with the inert fragment using a hyper-fast [diffing algorithm](http://npm.im/nanomorph) for real DOM nodes. This ensures that things only start happening if and when they're supposed to, organically.

Play with it on [CodePen](https://codepen.io/shannonmoeller/pen/XZXBpE?editors=1111).

## Install

```command
$ npm install --save apply-html
```

or

```html
<script src="https://wzrd.in/standalone/apply-html"></script>
```

or

```html
<script type="module">
    import { apply, html } from 'https://unpkg.com/apply-html?module';
</script>
```

## Usage

### Patching

```js
const { apply } = require('apply-html');

apply(document.body, '<h1 class="day">Hello World</h1>');

console.log(document.body.innerHTML);
// -> <h1 class="day">Hello World</h1>

apply(document.body, '<h1 class="night">Goodnight Moon</h1>');

console.log(document.body.innerHTML);
// -> <h1 class="night">Goodnight Moon</h1>
```

### Interpolation and Escaping

```js
const { apply, html, raw } = require('apply-html');

const foo = '<em>foo</em>';
const bar = raw('<em>bar</em>');
const baz = html`<strong>baz</strong>`;

apply(document.body, html`
    ${foo}
    ${bar}
    ${baz}
`);

console.log(document.body.innerHTML);
// -> &lt;em&gt;foo&lt;/em&gt;
// -> <em>bar</em>
// -> <strong>baz</strong>
```

### Server-side Rendering

The `html` and `raw` functions never touch the DOM so they're completely safe to use server-side.

```js
const http = require('http');
const { html } = require('apply-html');

const content = html`
    <h1>Hello <em>World</em></h1>
    <p>How are you today?</p>
`;

module.exports = http
    .createServer((req, res) => res.end(content.toString()))
    .listen(3000);
```

## API

### `apply(element, string): Element`

- `element` `{Element}` DOM element with children to be patched.
- `string` `{String|SafeString}` String or [SafeString](#safestring) containing safe HTML to render.

Updates the content of the given element, making the fewest possible changes required to match the given string of HTML. The string is converted into an HTML `<template>` and the resulting DOM trees are compared. Returns the updated element.

### `` html`string`: SafeString ``

A template tag that creates a new [SafeString](#safestring) containing a string of HTML. Interpolated values are serialized based on type:

- `Array` - Items are serialized then joined with an empty string (`''`).
- `Boolean|null|undefined` - Converted to an empty string (`''`).
- `Function` - Throws a `TypeError`.
- `Number` - Inserted as-is.
- `Object` - Converted to an HTML-encoded JSON blob.
- `SafeString` - Inserted as-is.
- `String` - HTML-encoded to safeguard against [XSS](https://www.owasp.org/index.php/Cross-site_Scripting_(XSS)). To opt out of escaping, use [`raw()`](#rawstring-safestring).

### `raw(string): SafeString`

- `string` `{String}` String of safe HTML.

Wraps a string in a [SafeString](#safestring) to indicate that it's safe to be inserted into the document. Only use on trusted strings to safeguard against [XSS](https://www.owasp.org/index.php/Cross-site_Scripting_(XSS)).

## SafeString

### `.raw` `{String}`

The wrapped string.

### `.length` `{Number}`

Length of the wrapped string. Read only.

### `.toJSON(): String`

Returns the raw string.

### `.toString(): String`

Returns the raw string.

## Acknowledgements

Standing on the shoulders of these giants:

- [bel](http://npm.im/bel)
- [diff-dom](http://npm.im/diff-dom)
- [domdiff](http://npm.im/domdiff)
- [hyperhtml](http://npm.im/hyperhtml)
- [lit-html](http://npm.im/lit-html)
- [morphdom](http://npm.im/morphdom)
- [nanomorph](http://npm.im/nanomorph)
- [preact](http://nipm.im/preact)
- [snabbdom](http://npm.im/snabbdom)
- [react](http://nipm.im/react)
- [vue.js](http://npm.im/vue)
- and [more...](https://rawgit.com/krausest/js-framework-benchmark/master/webdriver-ts-results/table.html)

----

MIT © [Shannon Moeller](http://shannonmoeller.com)

[coveralls-img]: http://img.shields.io/coveralls/shannonmoeller/apply-html/master.svg?style=flat-square
[coveralls-url]: https://coveralls.io/r/shannonmoeller/apply-html
[downloads-img]: http://img.shields.io/npm/dm/apply-html.svg?style=flat-square
[npm-img]:       http://img.shields.io/npm/v/apply-html.svg?style=flat-square
[npm-url]:       https://npmjs.org/package/apply-html
[travis-img]:    http://img.shields.io/travis/shannonmoeller/apply-html.svg?style=flat-square
[travis-url]:    https://travis-ci.org/shannonmoeller/apply-html

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