Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

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.

  • 1.0.5
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
19K
decreased by-13.48%
Maintainers
1
Weekly downloads
 
Created
Source

collapse-whitespace

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

Installation

Using npm:

$ npm install collapse-whitespace

If you’re using browserify or something similar:

var collapse = require('collapse-whitespace')

Otherwise, just include the bundled file, whitespace.min.js, somewhere on your page:

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

Usage

collapse-whitespace exposes a single function (called collapse, if you’re including this module as a global), which takes a DOM node as its only parameter and returns nothing of significance. collapse-whitespace will reduce all whitespace in that node to a minimum; see here for some examples.

Important things to keep in mind:

  1. collapse-whitespace will almost certainly make modifications to the given node; these may include joining adjacent text nodes, removing whitespace from text nodes, and even removing text nodes entirely if they contain only whitespace. Whitespace, if you’re curious, is those characters that match the RegExp / \t\r\n/.

  2. collapse-whitespace does not take into account the parent(s) of the given node. For example,

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

    This will almost certainly result in a visually different representation of that text — unless you’ve applied CSS whitespace: pre to the parent pre, which bring us to the next point:

  3. collapse-whitespace does not pay attention to CSS styles, even style attributes. Instead, it relies on a theoretical list of block elements, and assumes that only pre elements have whitespace: pre. So, if you’re doing something like:

    <style>
        .test {
            white-space: pre;
        }
    </style>
    <div class="test">
        I am indented
        text.
    </div>
    <script>
        collapse(document.querySelector('.test'))
    </script>
    <!-- Results in: -->
    <div class="test">I am indented text.</div>
    

    That text will no longer be indented. I have no plans to support checking the computed style, although, if that’s important to you, I would encourage you to fork this project; there should only be a few changes to make, which I’ll be happy to guide you through if need be (opening an issue is probably the easiest way to get in touch).

License

MIT.

Keywords

FAQs

Package last updated on 01 Feb 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