fix `isLast` corner case
authoralexlamsl <alexlamsl@gmail.com>
Tue, 19 Jan 2016 15:58:02 +0000 (23:58 +0800)
committeralexlamsl <alexlamsl@gmail.com>
Tue, 19 Jan 2016 15:58:02 +0000 (23:58 +0800)
trailing attributes can be removed

src/htmlminifier.js
tests/minifier.js

index e529632..f43bf6f 100644 (file)
 
         var openTag = '<' + tag;
         var hasUnarySlash = unarySlash && options.keepClosingSlash;
-        var closeTag = (hasUnarySlash ? '/' : '') + '>';
-        if (attrs.length === 0) {
-          openTag += closeTag;
-        }
 
         buffer.push(openTag);
 
           lint.testElement(tag);
         }
 
-        var token, isLast;
-        for (var i = 0, len = attrs.length; i < len; i++) {
-          isLast = i === len - 1;
+        var token, isLast = true;
+        var insert = buffer.length;
+        for (var i = attrs.length; --i >= 0; ) {
           if (lint) {
             lint.testAttribute(tag, attrs[i].name.toLowerCase(), attrs[i].escaped);
           }
           token = normalizeAttribute(attrs[i], attrs, tag, hasUnarySlash, i, options, isLast);
-          if (isLast) {
-            token += closeTag;
+          if (token) {
+            isLast = false;
+            buffer.splice(insert, 0, token);
           }
-          buffer.push(token);
         }
+
+        buffer.push(buffer.pop() + (hasUnarySlash ? '/' : '') + '>');
       },
       end: function(tag, attrs) {
 
index b9ce4a2..732c48b 100644 (file)
     input = '<a title="blah" href="http://example.com/">\nfoo\n\n</a>';
     equal(minify(input, { removeAttributeQuotes: true }), '<a title=blah href=http://example.com/ >\nfoo\n\n</a>');
 
+    input = '<a href="http://example.com/" title="">\nfoo\n\n</a>';
+    equal(minify(input, { removeAttributeQuotes: true, removeEmptyAttributes: true }), '<a href=http://example.com/ >\nfoo\n\n</a>');
+
     input = '<p class=foo|bar:baz></p>';
     equal(minify(input, { removeAttributeQuotes: true }), '<p class=foo|bar:baz></p>');
   });
     equal(minify('<img src="test"/>', { keepClosingSlash: true }), '<img src="test"/>');
     // https://github.com/kangax/html-minifier/issues/233
     equal(minify('<img src="test"/>', { keepClosingSlash: true, removeAttributeQuotes: true }), '<img src=test />');
+    equal(minify('<img src="test" id=""/>', { keepClosingSlash: true, removeAttributeQuotes: true, removeEmptyAttributes: true }), '<img src=test />');
     equal(minify('<img title="foo" src="test"/>', { keepClosingSlash: true, removeAttributeQuotes: true }), '<img title=foo src=test />');
   });