Add option to remove type="text/css" attributes from <style> and <link> elements...
authorkangax <kangax@gmail.com>
Sat, 27 Nov 2010 07:39:15 +0000 (02:39 -0500)
committerkangax <kangax@gmail.com>
Sat, 27 Nov 2010 07:39:15 +0000 (02:39 -0500)
index.html
master.js
src/htmlminifier.js
tests/index.html

index f6e9dea..b51d2fb 100644 (file)
@@ -10,7 +10,7 @@
     <div>
       <div id="outer-wrapper">
         <div id="wrapper">
-          <h1>HTML Minifier <span style="font-size:0.6em">(ver. 0.42)</span></h1>
+          <h1>HTML Minifier <span style="font-size:0.6em">(ver. 0.43)</span></h1>
           <p id="warning">
             Minifier is <strong>very draft</strong> and is <strong>not yet thoroughly tested</strong>. Use at your own risk. 
           </p>
                 </span>
               </label>
             </li>
+            <li>
+              <input type="checkbox" id="remove-style-link-type-attributes">
+              <label for="remove-style-link-type-attributes" class="unsafe">
+                Remove <code>type="text/css"</code> from <code>style</code>'s and <code>link</code>'s
+                <br>
+                <span class="quiet short">
+                  Values other than "text/css" (e.g. "text/plain" or "application/atom+xml") are left intact.
+                </span>
+              </label>
+            </li>
             <li>
               <input type="checkbox" id="use-htmllint" checked>
               <label for="use-htmllint">
index 45c39f1..2697b32 100644 (file)
--- a/master.js
+++ b/master.js
   
   function getOptions() {
     return {
-      removeComments:               byId('remove-comments').checked,
-      removeCommentsFromCDATA:      byId('remove-comments-from-cdata').checked,
-      removeCDATASectionsFromCDATA: byId('remove-cdata-sections-from-cdata').checked,
-      collapseWhitespace:           byId('collapse-whitespace').checked,
-      collapseBooleanAttributes:    byId('collapse-boolean-attributes').checked,
-      removeAttributeQuotes:        byId('remove-attribute-quotes').checked,  
-      removeRedundantAttributes:    byId('remove-redundant-attributes').checked,
-      useShortDoctype:              byId('use-short-doctype').checked,
-      removeEmptyAttributes:        byId('remove-empty-attributes').checked,
-      removeEmptyElements:          byId('remove-empty-elements').checked,
-      removeOptionalTags:           byId('remove-optional-tags').checked,
-      removeScriptTypeAttributes:   byId('remove-script-type-attributes').checked,
-      lint:                         byId('use-htmllint').checked ? new HTMLLint() : null
+      removeComments:                 byId('remove-comments').checked,
+      removeCommentsFromCDATA:        byId('remove-comments-from-cdata').checked,
+      removeCDATASectionsFromCDATA:   byId('remove-cdata-sections-from-cdata').checked,
+      collapseWhitespace:             byId('collapse-whitespace').checked,
+      collapseBooleanAttributes:      byId('collapse-boolean-attributes').checked,
+      removeAttributeQuotes:          byId('remove-attribute-quotes').checked,  
+      removeRedundantAttributes:      byId('remove-redundant-attributes').checked,
+      useShortDoctype:                byId('use-short-doctype').checked,
+      removeEmptyAttributes:          byId('remove-empty-attributes').checked,
+      removeEmptyElements:            byId('remove-empty-elements').checked,
+      removeOptionalTags:             byId('remove-optional-tags').checked,
+      removeScriptTypeAttributes:     byId('remove-script-type-attributes').checked,
+      removeStyleLinkTypeAttributes:  byId('remove-style-link-type-attributes').checked,
+      lint:                           byId('use-htmllint').checked ? new HTMLLint() : null
     };
   }
   
index 22308dc..a4b7428 100644 (file)
@@ -1,5 +1,5 @@
 /*!
- * HTMLMinifier v0.42
+ * HTMLMinifier v0.43
  * http://kangax.github.com/html-minifier/
  *
  * Copyright (c) 2010 Juriy "kangax" Zaytsev
     );
   }
   
+  function isStyleLinkTypeAttribute(tag, attrName, attrValue) {
+    return (
+      (tag === 'style' || tag === 'link') &&
+      attrName === 'type' &&
+      trimWhitespace(attrValue.toLowerCase()) === 'text/css'
+    );
+  }
+  
   function isBooleanAttribute(attrName) {
     return (/^(?:checked|disabled|selected|readonly)$/).test(attrName);
   }
       isAttributeRedundant(tag, attrName, attrValue, attrs)) 
       ||
       (options.removeScriptTypeAttributes && 
-      isScriptTypeAttribute(tag, attrName, attrValue))) {
+      isScriptTypeAttribute(tag, attrName, attrValue))
+      ||
+      (options.removeStyleLinkTypeAttributes &&
+      isStyleLinkTypeAttribute(tag, attrName, attrValue))) {
       return '';
     }
     
index 4df5676..c8cbf25 100644 (file)
           equals(minify(input, { removeScriptTypeAttributes: true }), output);
         });
         
+        test('removing type="text/css" attributes', function(){
+          input = '<style type="text/css">.foo { color: red }<\/style>';
+          output = '<style>.foo { color: red }<\/style>';
+          
+          equals(minify(input, { removeStyleLinkTypeAttributes: true }), output);
+          equals(minify(input, { removeStyleLinkTypeAttributes: false }), input);
+          
+          input = '<STYLE TYPE = "  text/CSS ">body { font-size: 1.75em }<\/style>';
+          output = '<style>body { font-size: 1.75em }<\/style>';
+          
+          equals(minify(input, { removeStyleLinkTypeAttributes: true }), output);
+          
+          input = '<style type="text/plain">.foo { background: green }<\/style>';
+          output = '<style type="text/plain">.foo { background: green }<\/style>';
+          
+          equals(minify(input, { removeStyleLinkTypeAttributes: true }), output);
+          
+          input = '<link rel="stylesheet" type="text/css" href="http://example.com">';
+          output = '<link rel="stylesheet" href="http://example.com">';
+          
+          equals(minify(input, { removeStyleLinkTypeAttributes: true }), output);
+          
+          input = '<link rel="alternate" type="application/atom+xml" href="data.xml">';
+          
+          equals(minify(input, { removeStyleLinkTypeAttributes: true }), input);
+        });
+        
         test('removing attribute quotes', function(){
           input = '<p title="blah" class="a23B-foo.bar_baz:qux" id="moo">foo</p>';
           equals(minify(input, { removeAttributeQuotes: true }), '<p title=blah class=a23B-foo.bar_baz:qux id=moo>foo</p>');