# collapse-whitespace

> Remove unnecessary whitespace from an element.

Latest version **1.1.7** (published 2018-11-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install collapse-whitespace
pnpm add collapse-whitespace
yarn add collapse-whitespace
bun add collapse-whitespace
```

## 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 | 1.1.7 |
| Published | 2018-11-09 |
| First published | 2014-10-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 19 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 9 |
| Author | Luc Thevenard |
| Maintainers | bradley.ayers, lucthev |
| Keywords | browser, collapse, collapse-whitespace, dom, whitespace |

## Links

- npm: https://www.npmjs.com/package/collapse-whitespace
- Repository: https://github.com/lucthev/collapse-whitespace
- Homepage: https://github.com/lucthev/serialize
- Issues: https://github.com/lucthev/serialize/issues
- npm.io page: https://npm.io/package/collapse-whitespace

## Dependencies (2)

- [void-elements](https://npm.io/package/void-elements.md) ^2.0.1
- [block-elements](https://npm.io/package/block-elements.md) ^1.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

- 1.1.7 (latest) — 2018-11-09
- 1.1.6 — 2017-04-03
- 1.1.5 — 2017-03-24
- 1.1.4 — 2016-11-27
- 1.1.3 — 2016-11-27
- 1.1.2 — 2015-07-11
- 1.1.1 — 2015-04-27
- 1.1.0 — 2015-03-13
- 1.0.5 — 2015-02-01
- 1.0.4 — 2014-11-09
- 1.0.3 — 2014-11-03
- 1.0.2 — 2014-11-02
- 1.0.1 — 2014-10-23
- 1.0.0 — 2014-10-21

## README

# collapse-whitespace

`collapse-whitespace` is a module for removing unnecessary whitespace from a DOM node.

## Installation

Use [`npm`](https://www.npmjs.org/):

```
$ npm install collapse-whitespace
```

Then, if you’re using [`browserify`](https://github.com/substack/node-browserify) or something similar:

```js
var collapse = require('collapse-whitespace')
```

Otherwise, just include the minified file, `whitespace.min.js`, somewhere on your page, and this module will make itself available as `collapse`:

```html
<script src="./node_modules/collapse-whitespace/whitespace.min.js"></script>
```

## Usage

```js
var collapse = require('collapse-whitespace')
var div = document.createElement('div')

div.innerHTML = '   <p>Foo   bar</p>  <p>Words</p> '
collapse(div)

console.log(div.innerHTML)
// '<p>Foo bar</p><p>Words</p>'
```

For more examples of what `collapse-whitespace` does, check out the [test page](https://github.com/lucthev/collapse-whitespace/blob/master/test.html).

## API

`collapse-whitespace` exposes a single function (called `collapse` if you're including this module via a `script` tag).

### collapse(node [, isBlock [, isPre]])

Removes all extraneous whitespace from the given node. By default, `collapse-whitespace` relies on a theoretical [list][blocks] of block elements to determine which elements are block and which ones are inline. This list may be unsuitable for your needs; the optional parameter `isBlock` can be used to tweak this behaviour. `isBlock` should be a function that accepts a DOM node and returns a Boolean.

Note that `collapse-whitespace` also does not take into account the parent(s) of the given node:

```html
<pre>
  <span class="test">
    Lots of whitespace around this text.
  </span>
</pre>

<script>
  collapse(document.querySelector('.test'))
</script>

<!-- Results in: -->
<pre>
  <span class="test">Lots of whitespace around this text.</span>
</pre>
```

By default only `PRE` nodes have whitespace preserved, but this can be customized via the optional `isPre` parameter. `isPre` should be a function that accepts a DOM node and returns a Boolean.

## License

MIT

[regexp]: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/RegExp
[blocks]: https://github.com/webmodules/block-elements

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