Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including collapse-whitespace with all npm packages installed. Try it out:

var collapseWhitespace = require("collapse-whitespace")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

collapse-whitespace v1.1.7

Remove unnecessary whitespace from an element.


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


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>


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

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

// '<p>Foo bar</p><p>Words</p>'

For more examples of what collapse-whitespace does, check out the test page.


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:

  <span class="test">
    Lots of whitespace around this text.


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

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.



RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free