# dom-serialize

> Serializes any DOM node into a String

Latest version **2.2.1** (published 2015-11-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install dom-serialize
pnpm add dom-serialize
yarn add dom-serialize
bun add dom-serialize
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.2.1 |
| Published | 2015-11-05 |
| First published | 2015-01-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 38 |
| Author | Nathan Rajlich |
| Maintainers | tootallnate |
| Keywords | browser, node, dom, serialize, string |

## Links

- npm: https://www.npmjs.com/package/dom-serialize
- Repository: https://github.com/webmodules/dom-serialize
- Issues: https://github.com/webmodules/dom-serialize/issues
- npm.io page: https://npm.io/package/dom-serialize

## Dependencies (4)

- [ent](https://npm.io/package/ent.md) ~2.2.0
- [extend](https://npm.io/package/extend.md) ^3.0.0
- [custom-event](https://npm.io/package/custom-event.md) ~1.0.0
- [void-elements](https://npm.io/package/void-elements.md) ^2.0.0

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 2.2.1 (latest) — 2015-11-05
- 2.2.0 — 2015-02-13
- 2.1.0 — 2015-02-10
- 2.0.1 — 2015-02-04
- 2.0.0 — 2015-02-04
- 1.2.1 — 2015-02-04
- 1.2.0 — 2015-02-03
- 1.1.0 — 2015-01-17

## README

dom-serialize
=============
### Serializes any DOM node into a String

[![Sauce Test Status](https://saucelabs.com/browser-matrix/dom-serialize.svg)](https://saucelabs.com/u/dom-serialize)

[![Build Status](https://travis-ci.org/webmodules/dom-serialize.svg?branch=master)](https://travis-ci.org/webmodules/dom-serialize)

It's like `outerHTML`, but it works with:

 * DOM elements
 * Text nodes
 * Attributes
 * Comment nodes
 * Documents
 * DocumentFragments
 * Doctypes
 * NodeLists / Arrays

For custom serialization logic, a "serialize" event is dispatched on
every `Node` which event listeners can override the default behavior on by
setting the `event.detail.serialize` property to a String or other Node.

The "serialize" event bubbles, so it could be a good idea to utilize
event delegation on a known root node that will be serialized.
Check the `event.serializeTarget` property to check which `Node` is
currently being serialized.


Installation
------------

``` bash
$ npm install dom-serialize
```


Example
-------

``` js
var serialize = require('dom-serialize');
var node;

// works with Text nodes
node = document.createTextNode('foo & <bar>');
console.log(serialize(node));


// works with DOM elements
node = document.createElement('body');
node.appendChild(document.createElement('strong'));
node.firstChild.appendChild(document.createTextNode('hello'));
console.log(serialize(node));


// custom "serialize" event
node.firstChild.addEventListener('serialize', function (event) {
  event.detail.serialize = 'pwn';
}, false);
console.log(serialize(node));


// you can also just pass a function in for a one-time serializer
console.log(serialize(node, function (event) {
  if (event.serializeTarget === node.firstChild) {
    // for the first child, output an ellipsis to summarize "content"
    event.detail.serialze = '…';
  } else if (event.serializeTarget !== node) {
    // any other child
    event.preventDefault();
  }
}));
```

```
foo &amp; &lt;bar&gt;
<body><strong>hello</strong></body>
<body>pwn</body>
<body>…</body>
```

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