# undom

> Minimally viable DOM Document implementation.

Latest version **0.4.0** (published 2018-06-13) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.4.0 |
| Published | 2018-06-13 |
| First published | 2016-07-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 60.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 689 |
| Author | Jason Miller |
| Maintainers | developit |
| Keywords | dom, document, shim |

## Links

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

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

- 0.4.0 (latest) — 2018-06-13
- 0.3.3 — 2017-03-03
- 0.3.2 — 2017-03-03
- 0.3.1 — 2017-02-08
- 0.3.0 — 2016-07-11
- 0.2.1 — 2016-07-10
- 0.2.0 — 2016-07-10
- 0.1.0 — 2016-07-09

## README

# undom

[![NPM](https://img.shields.io/npm/v/undom.svg?style=flat)](https://www.npmjs.org/package/undom)
[![travis-ci](https://travis-ci.org/developit/undom.svg?branch=master)](https://travis-ci.org/developit/undom)

### **Minimally viable DOM Document implementation**

> A bare-bones HTML DOM in a box. If you want the DOM but not a parser, this might be for you.
>
> `1kB`, works in Node and browsers, plugins coming soon!


[**JSFiddle Demo:**](https://jsfiddle.net/developit/4qv3v6r3/) Rendering [preact](https://github.com/developit/preact/) components into an undom Document.

[![preview](https://i.gyazo.com/7fcca9dd3e562b076293ef2cf3979d23.gif)](https://jsfiddle.net/developit/4qv3v6r3/)

---


## Project Goals

Undom aims to find a sweet spot between size/performance and utility. The goal is to provide the simplest possible implementation of a DOM Document, such that libraries relying on the DOM can run in places where there isn't one available.

The intent to keep things as simple as possible means undom lacks some DOM features like HTML parsing & serialization, Web Components, etc. These features can be added through additional libraries.


---


## Installation

Via npm:

`npm install --save undom`


---


## Require Hook

In CommonJS environments, simply import `undom/register` to patch the global object with a singleton Document.

```js
require('undom/register');

// now you have a DOM.
document.createElement('div');
```


---


## Usage

```js
// import the library:
import undom from 'undom';

let document = undom();

let foo = document.createElement('foo');
foo.appendChild(document.createTextNode('Hello, World!'));
document.body.appendChild(foo);
```


---


## Recipe: Serialize to HTML

One task `undom` doesn't handle for you by default is HTML serialization.  A proper implementation of this would be cumbersome to maintain and would rely heavily on getters and setters, which limits browser support.  Below is a simple recipe for serializing an `undom` Element (Document, etc) to HTML.


#### Small & in ES2015:

```js
Element.prototype.toString = el => this.nodeType===3 ? enc(this.textContent) : (
	'<'+this.nodeName.toLowerCase() + this.attributes.map(attr).join('') + '>' +
	this.childNodes.map(serialize).join('') + '</'+this.nodeName.toLowerCase()+'>'
);
let attr = a => ` ${a.name}="${enc(a.value)}"`;
let enc = s => s.replace(/[&'"<>]/g, a => `&#${a};`);
```


#### ES3 Version

This also does pretty-printing.

```js
function serialize(el) {
	if (el.nodeType===3) return el.textContent;
	var name = String(el.nodeName).toLowerCase(),
		str = '<'+name,
		c, i;
	for (i=0; i<el.attributes.length; i++) {
		str += ' '+el.attributes[i].name+'="'+el.attributes[i].value+'"';
	}
	str += '>';
	for (i=0; i<el.childNodes.length; i++) {
		c = serialize(el.childNodes[i]);
		if (c) str += '\n\t'+c.replace(/\n/g,'\n\t');
	}
	return str + (c?'\n':'') + '</'+name+'>';
}

function enc(s) {
	return s.replace(/[&'"<>]/g, function(a){ return `&#${a};` });
}
```

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