fix corner cases in `collapseWhitespace` (#883)
authorAlex Lam S.L <alexlamsl@gmail.com>
Wed, 7 Feb 2018 13:12:51 +0000 (21:12 +0800)
committerGitHub <noreply@github.com>
Wed, 7 Feb 2018 13:12:51 +0000 (21:12 +0800)
fixes #878

src/htmlminifier.js
tests/minifier.js

index 9e5a9a9..df37d5c 100644 (file)
@@ -67,7 +67,7 @@ function collapseWhitespace(str, options, trimLeft, trimRight, collapseAll) {
 
 var createMapFromString = utils.createMapFromString;
 // non-empty tags that will maintain whitespace around them
-var inlineTags = createMapFromString('a,abbr,acronym,b,bdi,bdo,big,button,cite,code,del,dfn,em,font,i,ins,kbd,mark,math,nobr,q,rt,rp,s,samp,small,span,strike,strong,sub,sup,svg,time,tt,u,var');
+var inlineTags = createMapFromString('a,abbr,acronym,b,bdi,bdo,big,button,cite,code,del,dfn,em,font,i,ins,kbd,label,mark,math,nobr,object,q,rt,rp,s,samp,select,small,span,strike,strong,sub,sup,svg,textarea,time,tt,u,var');
 // non-empty tags that will maintain whitespace within them
 var inlineTextTags = createMapFromString('a,abbr,acronym,b,big,del,em,font,i,ins,kbd,mark,nobr,s,samp,small,span,strike,strong,sub,sup,time,tt,u,var');
 // self-closing tags that will maintain whitespace around them
@@ -996,11 +996,13 @@ function minify(value, options, partialMarkup) {
         if (!stackNoTrimWhitespace.length) {
           squashTrailingWhitespace(tag);
         }
-        if (!_canTrimWhitespace(tag, attrs) || stackNoTrimWhitespace.length) {
-          stackNoTrimWhitespace.push(tag);
-        }
-        if (!_canCollapseWhitespace(tag, attrs) || stackNoCollapseWhitespace.length) {
-          stackNoCollapseWhitespace.push(tag);
+        if (!unary) {
+          if (!_canTrimWhitespace(tag, attrs) || stackNoTrimWhitespace.length) {
+            stackNoTrimWhitespace.push(tag);
+          }
+          if (!_canCollapseWhitespace(tag, attrs) || stackNoCollapseWhitespace.length) {
+            stackNoCollapseWhitespace.push(tag);
+          }
         }
       }
 
index 979f666..c2bb274 100644 (file)
@@ -345,6 +345,21 @@ QUnit.test('space normalization around text', function(assert) {
   input = '<p> foo\u00A0bar\nbaz  \u00A0\nmoo\t</p>';
   output = '<p>foo\u00A0bar baz \u00A0 moo</p>';
   assert.equal(minify(input, { collapseWhitespace: true }), output);
+  input = '<label> foo </label>\n' +
+          '<input>\n' +
+          '<object> bar </object>\n' +
+          '<select> baz </select>\n' +
+          '<textarea> moo </textarea>\n';
+  output = '<label>foo</label> <input> <object>bar</object> <select>baz</select> <textarea> moo </textarea>';
+  assert.equal(minify(input, { collapseWhitespace: true }), output);
+  input = '<pre>\n' +
+          'foo\n' +
+          '<br>\n' +
+          'bar\n' +
+          '</pre>\n' +
+          'baz\n';
+  output = '<pre>\nfoo\n<br>\nbar\n</pre>baz';
+  assert.equal(minify(input, { collapseWhitespace: true }), output);
 });
 
 QUnit.test('types of whitespace that should always be preserved', function(assert) {
@@ -1250,7 +1265,7 @@ QUnit.test('collapsing whitespace', function(assert) {
            '<textarea disabled="disabled">     this is a textarea </textarea>' +
            '</div></div></div></div></div></div>' +
            '<pre>       \r\nxxxx</pre><span>x</span> <span>Hello</span> <b>billy</b> ' +
-           '<input type="text"><textarea></textarea><pre></pre>';
+           '<input type="text"> <textarea></textarea><pre></pre>';
   assert.equal(minify(input, { collapseWhitespace: true }), output);
 
   input = '<pre title="some title...">   hello     world </pre>';
@@ -2937,7 +2952,7 @@ QUnit.test('markups from Angular 2', function(assert) {
            '<form (ngSubmit)=onSubmit(theForm) #theForm=ngForm>' +
            '<div class=form-group>' +
            '<label for=name>Name</label>' +
-           '<input class=form-control required ngControl=firstName [(ngModel)]=currentHero.firstName>' +
+           ' <input class=form-control required ngControl=firstName [(ngModel)]=currentHero.firstName>' +
            '</div>' +
            '<button type=submit [disabled]=!theForm.form.valid>Submit</button>' +
            '</form>';