Remove removable attrs with unspecified values
authorDuncan Beevers <duncan@dweebd.com>
Sun, 24 Aug 2014 08:32:14 +0000 (03:32 -0500)
committerDuncan Beevers <duncan@dweebd.com>
Sun, 24 Aug 2014 10:15:50 +0000 (05:15 -0500)
src/htmlminifier.js
tests/minifier.js

index 0ec98b5..f98b455 100644 (file)
       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);
       }
       '?: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') ||
index 93fa1f6..4079e95 100644 (file)
 
     input = '<img src="" alt="">';
     equal(minify(input, { removeEmptyAttributes: true }), '<img src="" alt="">');
+
+    // preserve unrecognized attribute
+    // remove recognized attrs with unspecified values
+    input = '<div data-foo class id style title lang dir onfocus onblur onchange onclick ondblclick onmousedown onmouseup onmouseover onmousemove onmouseout onkeypress onkeydown onkeyup></div>';
+    equal(minify(input, { removeEmptyAttributes: true }), '<div data-foo></div>');
   });
 
   test('cleaning class/style attributes', function() {