Improve `canRemoveAttributeQuotes`
authorMathias Bynens <mathias@qiwi.be>
Tue, 14 May 2013 16:33:08 +0000 (18:33 +0200)
committerMathias Bynens <mathias@qiwi.be>
Tue, 14 May 2013 16:40:24 +0000 (18:40 +0200)
See http://mathiasbynens.be/notes/unquoted-attribute-values for how browsers behave when it comes to unquoted attribute values, and what the HTML spec (based on pre-existing behavior) says about it.

Closes #60 and #61.

src/htmlminifier.js
tests/minifier.js

index 457e3a3..eafb599 100644 (file)
@@ -71,9 +71,8 @@
   }
 
   function canRemoveAttributeQuotes(value) {
-    // http://www.w3.org/TR/html4/intro/sgmltut.html#attributes
-    // avoid \w, which could match unicode in some implementations
-    return (/^[a-zA-Z0-9-._:]+$/).test(value);
+    // http://mathiasbynens.be/notes/unquoted-attribute-values
+    return /^[^\x20\t\n\f\r"'`=<>]+$/.test(value);
   }
 
   function attributesInclude(attributes, attribute) {
index c950245..5f65f35 100644 (file)
     equal(minify(input, { removeAttributeQuotes: true }), '<input value="hello world">');
 
     input = '<a href="#" title="foo#bar">x</a>';
-    equal(minify(input, { removeAttributeQuotes: true }), '<a href="#" title="foo#bar">x</a>');
+    equal(minify(input, { removeAttributeQuotes: true }), '<a href=# title=foo#bar>x</a>');
 
-    input = '<a href="http://example.com" title="blah">\nfoo\n\n</a>';
-    equal(minify(input, { removeAttributeQuotes: true }), '<a href="http://example.com" title=blah>\nfoo\n\n</a>');
+    input = '<a href="http://example.com/" title="blah">\nfoo\n\n</a>';
+    equal(minify(input, { removeAttributeQuotes: true }), '<a href=http://example.com/ title=blah>\nfoo\n\n</a>');
+
+    input = '<p class=foo|bar:baz></p>';
+    equal(minify(input, { removeAttributeQuotes: true }), '<p class=foo|bar:baz></p>');
   });
 
   test('collapsing whitespace', function() {