From 2eefd9bc857150f6c13d1a10ba12d7b2b84a180b Mon Sep 17 00:00:00 2001 From: Duncan Beevers Date: Sun, 24 Aug 2014 03:32:14 -0500 Subject: [PATCH] Remove removable attrs with unspecified values --- src/htmlminifier.js | 7 +++++-- tests/minifier.js | 5 +++++ 2 files changed, 10 insertions(+), 2 deletions(-) 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() { -- 2.34.1