From: Duncan Beevers Date: Sun, 24 Aug 2014 08:32:14 +0000 (-0500) Subject: Remove removable attrs with unspecified values X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=2eefd9bc857150f6c13d1a10ba12d7b2b84a180b;p=html-minifier.git Remove removable attrs with unspecified values --- diff --git a/src/htmlminifier.js b/src/htmlminifier.js index 0ec98b5..f98b455 100644 --- a/src/htmlminifier.js +++ b/src/htmlminifier.js @@ -209,7 +209,10 @@ return trimWhitespace(attrValue); } else if (attrName === 'style') { - attrValue = trimWhitespace(attrValue).replace(/\s*;\s*$/, ''); + attrValue = trimWhitespace(attrValue); + if (attrValue) { + attrValue = attrValue.replace(/\s*;\s*$/, ''); + } if (options.minifyCSS) { return minifyCSS(attrValue, options.minifyCSS); } @@ -278,7 +281,7 @@ '?:down|up|over|move|out)|key(?:press|down|up)))$'); function canDeleteEmptyAttribute(tag, attrName, attrValue) { - var isValueEmpty = /^(["'])?\s*\1$/.test(attrValue); + var isValueEmpty = !attrValue || /^(["'])?\s*\1$/.test(attrValue); if (isValueEmpty) { return ( (tag === 'input' && attrName === 'value') || diff --git a/tests/minifier.js b/tests/minifier.js index 93fa1f6..4079e95 100644 --- a/tests/minifier.js +++ b/tests/minifier.js @@ -260,6 +260,11 @@ input = ''; equal(minify(input, { removeEmptyAttributes: true }), ''); + + // preserve unrecognized attribute + // remove recognized attrs with unspecified values + input = '
'; + equal(minify(input, { removeEmptyAttributes: true }), '
'); }); test('cleaning class/style attributes', function() {