Socket
Socket
Sign inDemoInstall

collapse-whitespace

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

collapse-whitespace

Remove unnecessary whitespace from an element.


Version published
Maintainers
1
Created
Source

collapse-whitespace

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

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:

<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; if you’re including this module as a global, it will be called collapse.

collapse(node [, isBlock])

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>

License

MIT

Keywords

FAQs

Package last updated on 13 Mar 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc