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 - npm Package Compare versions

Comparing version 1.0.5 to 1.1.0

LICENSE

10

package.json
{
"name": "collapse-whitespace",
"version": "1.0.5",
"version": "1.1.0",
"description": "Remove unnecessary whitespace from an element.",
"main": "./whitespace.js",
"scripts": {
"test": "./node_modules/.bin/gulp test",
"js": "./node_modules/.bin/gulp",
"dev": "./node_modules/.bin/gulp watch"
"test": "make test"
},

@@ -30,5 +28,3 @@ "keywords": [

"browserify": "~6.1.0",
"gulp": "~3.8.8",
"gulp-jshint": "~1.5.3",
"jshint-stylish": "~0.1.5",
"jshint": "~2.6.3",
"uglify-js": "~2.4.15"

@@ -35,0 +31,0 @@ },

@@ -7,3 +7,3 @@ # collapse-whitespace

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

@@ -14,3 +14,3 @@ ```

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

@@ -21,3 +21,3 @@ ```js

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

@@ -30,50 +30,45 @@ ```html

`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](https://github.com/lucthev/collapse-whitespace/blob/master/test.html) for some examples.
```js
var collapse = require('collapse-whitespace')
var div = document.createElement('div')
### Important things to keep in mind:
div.innerHTML = ' <p>Foo bar</p> <p>Words</p> '
collapse(div)
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/`.
console.log(div.innerHTML)
// '<p>Foo bar</p><p>Words</p>'
```
2. `collapse-whitespace` does not take into account the parent(s) of the given node. For example,
For more examples of what `collapse-whitespace` does, check out the [test page](https://github.com/lucthev/collapse-whitespace/blob/master/test.html).
```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>
```
## API
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:
`collapse-whitespace` exposes a single function; if you’re including this module as a global, it will be called `collapse`.
3. `collapse-whitespace` does not pay attention to CSS styles, even `style` attributes. Instead, it relies on a theoretical [list](https://github.com/lucthev/collapse-whitespace/blob/master/whitespace.js#L3-L24) of block elements, and assumes that only `pre` elements have `whitespace: pre`. So, if you’re doing something like:
### collapse(node [, isBlock])
```html
<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>
```
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.
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](https://github.com/lucthev/collapse-whitespace/issues/new?title=Hi) is probably the easiest way to get in touch).
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>
```
## License
MIT.
MIT
[regexp]: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/RegExp
[blocks]: https://github.com/webmodules/block-elements
'use strict';
var blocks = require('block-elements').map(function (block) {
return block.toUpperCase()
var blocks = require('block-elements').map(function (name) {
return name.toUpperCase()
})
if (blocks.indexOf('LI') < 0)
blocks.push('LI')
function isBlock (node) {
function defaultBlockTest (node) {
return isElem(node) && blocks.indexOf(node.nodeName) >= 0

@@ -23,8 +20,12 @@ }

/**
* whitespace(elem) removes extraneous whitespace from an element
* and its children.
* whitespace(elem [, isBlock]) removes extraneous whitespace from an
* the given element. The function isBlock may optionally be passed in
* to determine whether or not an element is a block element; if none
* is provided, defaults to using the list of block elements provided
* by the `block-elements` module.
*
* @param {Element} root
* @param {Function} isBlock
*/
function whitespace (root) {
function whitespace (root, isBlock) {
var startSpace = /^ /,

@@ -38,2 +39,5 @@ endSpace = / $/,

if (typeof isBlock !== 'function')
isBlock = defaultBlockTest
function next (node) {

@@ -40,0 +44,0 @@ while (node && node !== root) {

@@ -1,1 +0,1 @@

!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var n;"undefined"!=typeof window?n=window:"undefined"!=typeof global?n=global:"undefined"!=typeof self&&(n=self),n.collapse=e()}}(function(){return function e(n,t,r){function o(f,u){if(!t[f]){if(!n[f]){var a="function"==typeof require&&require;if(!u&&a)return a(f,!0);if(i)return i(f,!0);var d=new Error("Cannot find module '"+f+"'");throw d.code="MODULE_NOT_FOUND",d}var l=t[f]={exports:{}};n[f][0].call(l.exports,function(e){var t=n[f][1][e];return o(t?t:e)},l,l.exports,e,n,t,r)}return t[f].exports}for(var i="function"==typeof require&&require,f=0;f<r.length;f++)o(r[f]);return o}({1:[function(e,n){"use strict";function t(e){return o(e)&&f.indexOf(e.nodeName)>=0}function r(e){return e&&e.nodeType===Node.TEXT_NODE}function o(e){return e&&e.nodeType===Node.ELEMENT_NODE}function i(e){function n(n){for(;n&&n!==e;){if(n.nextSibling)return n.nextSibling;n=n.parentNode,d&&t(n)&&(d.data=d.data.replace(/[ \r\n\t]$/,""),d=null)}return null}function i(e){return e.firstChild?e.firstChild:n(e)}function f(e){var t=n(e);return e.parentNode.removeChild(e),t}var u,a,d,l,c,p=/^ /,s=/ $/;if("PRE"!==e.nodeName){for(e.normalize(),l=i(e);l;)if(a=l.previousSibling,u=l.nextSibling,r(l))c=l.data.replace(/[ \r\n\t]+/g," "),(!d||a&&t(a))&&(c=c.replace(p,"")),u&&t(u)&&(c=c.replace(s,"")),d&&s.test(d.data)&&p.test(c)&&(c=c.substr(1)),c?(l.data=c,d=l,l=n(l)):l=f(l);else if(o(l)){if("PRE"===l.nodeName){l=n(l);continue}d&&t(l)&&(d.data=d.data.replace(s,""),d=null),l=i(l)}else l=f(l);d&&(d.data=d.data.replace(s,""))}}var f=e("block-elements").map(function(e){return e.toUpperCase()});f.indexOf("LI")<0&&f.push("LI"),n.exports=i},{"block-elements":2}],2:[function(e,n){n.exports=["address","article","aside","audio","blockquote","canvas","dd","div","dl","fieldset","figcaption","figure","footer","form","h1","h2","h3","h4","h5","h6","header","hgroup","hr","noscript","ol","output","p","pre","section","table","tfoot","ul","video"]},{}]},{},[1])(1)});
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var n;"undefined"!=typeof window?n=window:"undefined"!=typeof global?n=global:"undefined"!=typeof self&&(n=self),n.collapse=e()}}(function(){var e,n,t;return function r(e,n,t){function i(f,u){if(!n[f]){if(!e[f]){var a=typeof require=="function"&&require;if(!u&&a)return a(f,!0);if(o)return o(f,!0);var d=new Error("Cannot find module '"+f+"'");throw d.code="MODULE_NOT_FOUND",d}var l=n[f]={exports:{}};e[f][0].call(l.exports,function(n){var t=e[f][1][n];return i(t?t:n)},l,l.exports,r,e,n,t)}return n[f].exports}var o=typeof require=="function"&&require;for(var f=0;f<t.length;f++)i(t[f]);return i}({1:[function(e,n,t){n.exports=["address","article","aside","audio","blockquote","canvas","dd","div","dl","fieldset","figcaption","figure","footer","form","h1","h2","h3","h4","h5","h6","header","hgroup","hr","noscript","ol","output","p","pre","section","table","tfoot","ul","video"]},{}],2:[function(e,n,t){"use strict";var r=e("block-elements").map(function(e){return e.toUpperCase()});function i(e){return f(e)&&r.indexOf(e.nodeName)>=0}function o(e){return e&&e.nodeType===Node.TEXT_NODE}function f(e){return e&&e.nodeType===Node.ELEMENT_NODE}function u(e,n){var t=/^ /,r=/ $/,u,a,d,l,c;if(typeof n!=="function")n=i;function p(t){while(t&&t!==e){if(t.nextSibling)return t.nextSibling;t=t.parentNode;if(d&&n(t)){d.data=d.data.replace(/[ \r\n\t]$/,"");d=null}}return null}function s(e){return e.firstChild?e.firstChild:p(e)}function h(e){var n=p(e);e.parentNode.removeChild(e);return n}if(e.nodeName==="PRE")return;e.normalize();l=s(e);while(l){a=l.previousSibling;u=l.nextSibling;if(o(l)){c=l.data.replace(/[ \r\n\t]+/g," ");if(!d||a&&n(a))c=c.replace(t,"");if(u&&n(u))c=c.replace(r,"");if(d&&r.test(d.data)&&t.test(c))c=c.substr(1);if(c){l.data=c;d=l;l=p(l)}else{l=h(l)}}else if(f(l)){if(l.nodeName==="PRE"){l=p(l);continue}if(d&&n(l)){d.data=d.data.replace(r,"");d=null}l=s(l)}else{l=h(l)}}if(d)d.data=d.data.replace(r,"")}n.exports=u},{"block-elements":1}]},{},[2])(2)});

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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