From 3d98c7549a106f583b0a4d51e125d1b095611a97 Mon Sep 17 00:00:00 2001 From: alexlamsl Date: Wed, 8 Feb 2017 09:09:29 +0800 Subject: [PATCH] preserve non-breaking spaces during collapseWhitespace fixes #777 replaces #184 --- src/htmlminifier.js | 8 ++++---- tests/minifier.js | 9 +++++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/htmlminifier.js b/src/htmlminifier.js index 431281b..cbfa953 100644 --- a/src/htmlminifier.js +++ b/src/htmlminifier.js @@ -21,21 +21,21 @@ var trimWhitespace = String.prototype.trim ? function(str) { }; function compressWhitespace(spaces) { - return spaces === '\t' ? spaces : ' '; + return spaces === '\t' ? '\t' : ~spaces.indexOf('\xA0') ? '\xA0' : ' '; } function collapseWhitespaceAll(str) { - return str ? str.replace(/[\t\n\r ]+/g, compressWhitespace) : str; + return str ? str.replace(/\s+/g, compressWhitespace) : str; } function collapseWhitespace(str, options, trimLeft, trimRight, collapseAll) { var lineBreakBefore = '', lineBreakAfter = ''; if (options.preserveLineBreaks) { - str = str.replace(/^[\t ]*[\n\r][\t\n\r ]*/, function() { + str = str.replace(/^\s*?[\n\r]\s*/, function() { lineBreakBefore = '\n'; return ''; - }).replace(/[\t ]*[\n\r][\t\n\r ]*$/, function() { + }).replace(/\s*?[\n\r]\s*$/, function() { lineBreakAfter = '\n'; return ''; }); diff --git a/tests/minifier.js b/tests/minifier.js index dd7ec27..85aa4b0 100644 --- a/tests/minifier.js +++ b/tests/minifier.js @@ -323,6 +323,9 @@ QUnit.test('space normalization around text', function(assert) { input = ' '; output = ''; assert.equal(minify(input, { collapseWhitespace: true }), output); + input = '

foo\u00A0bar\nbaz \u00A0\nmoo\t

'; + output = '

foo\u00A0bar baz\u00A0moo

'; + assert.equal(minify(input, { collapseWhitespace: true }), output); }); QUnit.test('doctype normalization', function(assert) { @@ -2315,6 +2318,12 @@ QUnit.test('conservative collapse', function(assert) { collapseWhitespace: true, conservativeCollapse: true }), output); + + input = '

\u00A0

'; + assert.equal(minify(input, { + collapseWhitespace: true, + conservativeCollapse: true + }), input); }); QUnit.test('collapse preseving a line break', function(assert) { -- 2.34.1