Quote terminal attribute value by unary slash
authorDuncan Beevers <duncan@dweebd.com>
Wed, 20 Aug 2014 17:06:23 +0000 (12:06 -0500)
committerDuncan Beevers <duncan@dweebd.com>
Wed, 20 Aug 2014 17:06:23 +0000 (12:06 -0500)
src/htmlminifier.js
tests/minifier.js

index 6d07307..ff05b04 100644 (file)
     return markup;
   }
 
-  function normalizeAttribute(attr, attrs, tag, options) {
+  function normalizeAttribute(attr, attrs, tag, unarySlash, index, options) {
 
     var attrName = options.caseSensitive ? attr.name : attr.name.toLowerCase(),
         attrValue = attr.escaped,
-        attrFragment;
+        attrFragment,
+        isTerminalOfUnarySlash = unarySlash && index === attrs.length - 1;
 
     if ((options.removeRedundantAttributes &&
       isAttributeRedundant(tag, attrName, attrValue, attrs))
     attrValue = cleanAttributeValue(tag, attrName, attrValue, options, attrs);
 
     if (attrValue !== undefined && !options.removeAttributeQuotes ||
-        !canRemoveAttributeQuotes(attrValue)) {
+        !canRemoveAttributeQuotes(attrValue) || isTerminalOfUnarySlash) {
       attrValue = '"' + attrValue + '"';
     }
 
         var token;
         for ( var i = 0, len = attrs.length; i < len; i++ ) {
           lint && lint.testAttribute(tag, attrs[i].name.toLowerCase(), attrs[i].escaped);
-          token = normalizeAttribute(attrs[i], attrs, tag, options);
+          token = normalizeAttribute(attrs[i], attrs, tag, unarySlash, i, options);
           if ( i === len - 1 ) {
             token += closeTag;
           }
index 7fba30c..2e9753c 100644 (file)
 
   test('keeping trailing slashes in tags', function() {
     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 title="foo" src="test"/>', { keepClosingSlash: true, removeAttributeQuotes: true }), '<img title=foo src="test"/>');
   });
 
   test('removing optional tags', function() {