From: alexlamsl Date: Tue, 19 Jan 2016 15:58:02 +0000 (+0800) Subject: fix `isLast` corner case X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=a2af1bac558a38ff725293acb3526156e1fd6810;p=html-minifier.git fix `isLast` corner case trailing attributes can be removed --- diff --git a/src/htmlminifier.js b/src/htmlminifier.js index e529632..f43bf6f 100644 --- a/src/htmlminifier.js +++ b/src/htmlminifier.js @@ -653,10 +653,6 @@ var openTag = '<' + tag; var hasUnarySlash = unarySlash && options.keepClosingSlash; - var closeTag = (hasUnarySlash ? '/' : '') + '>'; - if (attrs.length === 0) { - openTag += closeTag; - } buffer.push(openTag); @@ -664,18 +660,20 @@ 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) { diff --git a/tests/minifier.js b/tests/minifier.js index b9ce4a2..732c48b 100644 --- a/tests/minifier.js +++ b/tests/minifier.js @@ -510,6 +510,9 @@ input = '\nfoo\n\n'; equal(minify(input, { removeAttributeQuotes: true }), '\nfoo\n\n'); + input = '\nfoo\n\n'; + equal(minify(input, { removeAttributeQuotes: true, removeEmptyAttributes: true }), '\nfoo\n\n'); + input = '

'; equal(minify(input, { removeAttributeQuotes: true }), '

'); }); @@ -740,6 +743,7 @@ equal(minify('', { keepClosingSlash: true }), ''); // https://github.com/kangax/html-minifier/issues/233 equal(minify('', { keepClosingSlash: true, removeAttributeQuotes: true }), ''); + equal(minify('', { keepClosingSlash: true, removeAttributeQuotes: true, removeEmptyAttributes: true }), ''); equal(minify('', { keepClosingSlash: true, removeAttributeQuotes: true }), ''); });