#722: introduce `collapseCustomFragments` option to collapse white space around ...
authorAdam Chalemian <achalemian@resolute.com>
Fri, 23 Sep 2016 18:29:33 +0000 (14:29 -0400)
committerAdam Chalemian <achalemian@resolute.com>
Fri, 23 Sep 2016 18:29:33 +0000 (14:29 -0400)
README.md
cli.js
index.html
src/htmlminifier.js
tests/minifier.js

index de74c98..7328c30 100644 (file)
--- a/README.md
+++ b/README.md
@@ -44,6 +44,7 @@ How does HTMLMinifier compare to other solutions — [HTML Minifier from Will Pe
 | `collapseBooleanAttributes`    | [Omit attribute values from boolean attributes](http://perfectionkills.com/experimenting-with-html-minifier/#collapse_boolean_attributes) | `false` |
 | `collapseInlineTagWhitespace`  | Don't leave any spaces between `display:inline;` elements when collapsing. Must be used in conjunction with `collapseWhitespace=true` | `false` |
 | `collapseWhitespace`           | [Collapse white space that contributes to text nodes in a document tree](http://perfectionkills.com/experimenting-with-html-minifier/#collapse_whitespace) | `false` |
+| `collapseCustomFragments`      | Collapse white space around `ignoreCustomFragments`. | `false` |
 | `conservativeCollapse`         | Always collapse to 1 space (never remove it entirely). Must be used in conjunction with `collapseWhitespace=true` | `false` |
 | `customAttrAssign`             | Arrays of regex'es that allow to support custom attribute assign expressions (e.g. `'<div flex?="{{mode != cover}}"></div>'`) | `[ ]` |
 | `customAttrCollapse`           | Regex that specifies custom attribute to strip newlines from (e.g. `/ng-class/`) | |
diff --git a/cli.js b/cli.js
index ab68d96..b30a5ac 100755 (executable)
--- a/cli.js
+++ b/cli.js
@@ -101,6 +101,7 @@ var mainOptions = {
   collapseBooleanAttributes: 'Omit attribute values from boolean attributes',
   collapseInlineTagWhitespace: 'Collapse white space around inline tag',
   collapseWhitespace: 'Collapse white space that contributes to text nodes in a document tree.',
+  collapseCustomFragments: 'Collapse white space around ignoreCustomFragments.',
   conservativeCollapse: 'Always collapse to 1 space (never remove it entirely)',
   customAttrAssign: ['Arrays of regex\'es that allow to support custom attribute assign expressions (e.g. \'<div flex?="{{mode != cover}}"></div>\')', parseJSONRegExpArray],
   customAttrCollapse: ['Regex that specifies custom attribute to strip newlines from (e.g. /ng-class/)', parseRegExp],
index 97f169e..e6c0722 100644 (file)
               Collapse white space that contributes to text nodes in a document tree
             </span>
           </li>
+          <li>
+            <input type="checkbox" id="collapseCustomFragments" checked>
+            <label for="collapseCustomFragments">
+              Collapse custom fragment white space
+            </label>
+            <span class="quiet short">
+              Collapse white space around <code>`ignoreCustomFragments</code>.
+            </span>
+          </li>
           <li>
             <input type="checkbox" id="conservativeCollapse">
             <label for="conservativeCollapse">
index 8f4f497..2a658ba 100644 (file)
@@ -1260,7 +1260,7 @@ function minify(value, options, partialMarkup) {
         }
         return collapseWhitespace(chunk, {
           preserveLineBreaks: options.preserveLineBreaks,
-          conservativeCollapse: true
+          conservativeCollapse: !options.collapseCustomFragments
         }, /^\s/.test(chunk), /\s$/.test(chunk));
       }
       return chunk;
index b6b6a3f..b5de492 100644 (file)
@@ -1750,6 +1750,17 @@ QUnit.test('Ignore custom fragments', function(assert) {
       /\{\{.*?\}\}/g
     ]
   }), output);
+
+  input = '<?php echo "foo"; ?> <span>bar</span>';
+  assert.equal(minify(input), input);
+  assert.equal(minify(input, {
+    collapseWhitespace: true
+  }), input);
+  output = '<?php echo "foo"; ?><span>bar</span>';
+  assert.equal(minify(input, {
+    collapseWhitespace: true,
+    collapseCustomFragments: true
+  }), output);
 });
 
 QUnit.test('bootstrap\'s span > button > span', function(assert) {