collapse-whitespace
Remove unnecessary whitespace from an element.
Last updated 6 years ago by lucthev .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ gnpm install collapse-whitespace 
SYNC missed versions from official npm registry.

collapse-whitespace

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

Installation

Use npm:

$ npm install collapse-whitespace

Then, if you’re using browserify or something similar:

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:

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

Usage

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.

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

<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

Current Tags

  • 1.1.7                                ...           latest (6 years ago)

14 Versions

  • 1.1.7                                ...           6 years ago
  • 1.1.6                                ...           8 years ago
  • 1.1.5                                ...           8 years ago
  • 1.1.4                                ...           8 years ago
  • 1.1.3                                ...           8 years ago
  • 1.1.2                                ...           9 years ago
  • 1.1.1                                ...           10 years ago
  • 1.1.0                                ...           10 years ago
  • 1.0.5                                ...           10 years ago
  • 1.0.4                                ...           10 years ago
  • 1.0.3                                ...           10 years ago
  • 1.0.2                                ...           10 years ago
  • 1.0.1                                ...           10 years ago
  • 1.0.0                                ...           10 years ago
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (2)
Dev Dependencies (4)
Dependents (1)

Copyright 2013 - present © cnpmjs.org | Home |