From: alexlamsl Date: Mon, 2 May 2016 11:51:27 +0000 (+0800) Subject: fix corner cases in conservativeCollapse & preserveLineBreaks X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=c7673f6fa1e1cbf90c03150727485d28dc0dc958;p=html-minifier.git fix corner cases in conservativeCollapse & preserveLineBreaks fixes #652 --- diff --git a/src/htmlminifier.js b/src/htmlminifier.js index 447b0a5..598906a 100644 --- a/src/htmlminifier.js +++ b/src/htmlminifier.js @@ -804,7 +804,9 @@ function minify(value, options, partialMarkup) { options = options || {}; var optionsStack = []; processOptions(options); - value = options.collapseWhitespace ? trimWhitespace(value) : value; + if (options.collapseWhitespace) { + value = collapseWhitespace(value, options, true, true); + } var buffer = [], charsPrevTag, @@ -1085,7 +1087,7 @@ function minify(value, options, partialMarkup) { if (!prevComment) { prevTag = charsPrevTag; } - if (buffer.length > 1 && (!prevComment || / $/.test(currentChars))) { + if (buffer.length > 1 && (!prevComment || !options.conservativeCollapse && / $/.test(currentChars))) { var charsIndex = buffer.length - 2; buffer[charsIndex] = buffer[charsIndex].replace(/\s+$/, function(trailingSpaces) { text = trailingSpaces + text; @@ -1104,13 +1106,18 @@ function minify(value, options, partialMarkup) { text = collapseWhitespace(text, options, /(?:^|\s)$/.test(currentChars)); } } - text = prevTag || nextTag ? collapseWhitespaceSmart(text, prevTag, nextTag, options) : trimWhitespace(text); + if (prevTag || nextTag) { + text = collapseWhitespaceSmart(text, prevTag, nextTag, options); + } + else { + text = collapseWhitespace(text, options, true, true); + } if (!text && /\s$/.test(currentChars) && prevTag && prevTag.charAt(0) === '/') { trimTrailingWhitespace(buffer.length - 1, nextTag); } } - if (!stackNoCollapseWhitespace.length) { - text = prevTag && nextTag || nextTag === 'html' ? text : collapseWhitespaceAll(text); + if (!stackNoCollapseWhitespace.length && nextTag !== 'html' && !(prevTag && nextTag)) { + text = collapseWhitespace(text, options, false, false, true); } } if (options.processScripts && specialContentTags(currentTag)) { @@ -1248,8 +1255,7 @@ function joinResultSegments(results, options) { else { str = results.join(''); } - - return options.collapseWhitespace ? trimWhitespace(str) : str; + return options.collapseWhitespace ? collapseWhitespace(str, options, true, true) : str; } exports.minify = function(value, options) { diff --git a/tests/minifier.js b/tests/minifier.js index bc45d12..5b08c6c 100644 --- a/tests/minifier.js +++ b/tests/minifier.js @@ -147,6 +147,22 @@ test('space normalization around text', function() { equal(minify(input), input); output = '

blah

'; equal(minify(input, { collapseWhitespace: true }), output); + output = '

blah

'; + equal(minify(input, { + collapseWhitespace: true, + conservativeCollapse: true + }), output); + output = '

blah

\n'; + equal(minify(input, { + collapseWhitespace: true, + preserveLineBreaks: true + }), output); + output = '

blah

\n'; + equal(minify(input, { + collapseWhitespace: true, + conservativeCollapse: true, + preserveLineBreaks: true + }), output); [ 'a', 'abbr', 'acronym', 'b', 'big', 'del', 'em', 'font', 'i', 'ins', 'kbd', 'mark', 's', 'samp', 'small', 'span', 'strike', 'strong', 'sub', 'sup', @@ -213,8 +229,16 @@ test('space normalization around text', function() { ['a b c', 'a b c'], ['a b c', 'a b c'] ].forEach(function(inputs) { + equal(minify(inputs[0], { + collapseWhitespace: true, + conservativeCollapse: true + }), inputs[0], inputs[0]); equal(minify(inputs[0], { collapseWhitespace: true }), inputs[1], inputs[0]); var input = '
' + inputs[0] + '
'; + equal(minify(input, { + collapseWhitespace: true, + conservativeCollapse: true + }), input, input); var output = '
' + inputs[1] + '
'; equal(minify(input, { collapseWhitespace: true }), output, input); }); @@ -226,21 +250,48 @@ test('space normalization around text', function() { equal(minify('

foo bar

', { collapseWhitespace: true }), '

foo bar

'); equal(minify('

foo bar

', { collapseWhitespace: true }), '

foo bar

'); equal(minify('
Empty not
', { collapseWhitespace: true }), '
Empty not
'); - equal(minify('
a c
', { removeComments: true, collapseWhitespace: true }), '
a c
'); + equal(minify('
a c
', { + collapseWhitespace: true, + removeComments: true + }), '
a c
'); [ - ' a c ', - ' a c ', - ' a c ', - ' a c ', - ' a c ', - ' a c ', - ' a c ', - ' a c ', - ' a c ' - ].forEach(function(input, index) { - input = input.replace(/b/, 'b' + index); - equal(minify(input, { removeComments: true, collapseWhitespace: true }), 'a c'); - equal(minify('

' + input + '

', { removeComments: true, collapseWhitespace: true }), '

a c

'); + ' a c ', + ' a c ', + ' a c ', + ' a c ', + ' a c ', + ' a c ', + ' a c ', + ' a c ', + ' a c ' + ].forEach(function(input) { + equal(minify(input, { + collapseWhitespace: true, + conservativeCollapse: true + }), input, input); + equal(minify(input, { + collapseWhitespace: true, + removeComments: true + }), 'a c', input); + equal(minify(input, { + collapseWhitespace: true, + conservativeCollapse: true, + removeComments: true + }), ' a c ', input); + input = '

' + input + '

'; + equal(minify(input, { + collapseWhitespace: true, + conservativeCollapse: true + }), input, input); + equal(minify(input, { + collapseWhitespace: true, + removeComments: true + }), '

a c

', input); + equal(minify(input, { + collapseWhitespace: true, + conservativeCollapse: true, + removeComments: true + }), '

a c

', input); }); input = '
  • foo
  • '; output = '
  • foo
  • '; @@ -1949,7 +2000,7 @@ test('collapse preseving a line break', function() { ' \n' + ' \n' + ' \n \n

    \n test test\n\ttest\n\n

    \n'; - output = '\n\n' + + output = '\n\n\n' + '\n\n\n' + '\n' + 'Carbon\n\n' + @@ -1970,7 +2021,7 @@ test('collapse preseving a line break', function() { conservativeCollapse: true, preserveLineBreaks: true }), output); - output = '\n\n' + + output = '\n\n\n' + '\n\n\n' + 'Carbon\n\n' + '\n' +