Minifier now removes insignificant whitespace in conditional comments.
authorJuriy Zaytsev <kangax@gmail.com>
Thu, 11 Mar 2010 17:06:41 +0000 (12:06 -0500)
committerJuriy Zaytsev <kangax@gmail.com>
Thu, 11 Mar 2010 17:06:41 +0000 (12:06 -0500)
index.html
src/htmlminifier.js
tests/index.html

index 76f8fe8..f6c7c86 100644 (file)
@@ -26,7 +26,7 @@
               <input type="checkbox" id="remove-comments" checked>
               <label for="remove-comments">Remove comments</label>
               <span class="quiet short" style="margin-left:1.5em;float:left;clear:both;">
-                Conditional comments are left intact.
+                Conditional comments are left intact, but their inner (insignificant) whitespace is removed.
               </span>
             </li>
             <li>
@@ -98,7 +98,8 @@
                   Currently, only: 
                   <code>&lt;/html></code>, 
                   <code>&lt;/head></code>, 
-                  <code>&lt;/body></code>, 
+                  <code>&lt;/body></code>,
+                  <code>&lt;/option></code>
                   <code>&lt;/thead></code>,
                   <code>&lt;/tbody></code>,
                   <code>&lt;/tfoot></code>,
index 2092add..dcf730d 100644 (file)
     return attrValue;
   }
   
+  function cleanConditionalComment(comment) {
+    return comment
+      .replace(/^(\[[^\]]+\]>)\s*/, '$1')
+      .replace(/\s*(<!\[endif\])$/, '$1');
+  }
+  
   function removeCDATASections(text) {
     return text
       // "/* <![CDATA[ */" or "// <![CDATA["
         buffer.push(text);
       },
       comment: function( text ) {
-        if (options.removeComments && !isConditionalComment(text)) {
-          text = '';
+        if (options.removeComments) {
+          if (isConditionalComment(text)) {
+            text = '<!--' + cleanConditionalComment(text) + '-->';
+          }
+          else {
+            text = '';
+          }
         }
         else {
           text = '<!--' + text + '-->';
index b329e2f..1a195f7 100644 (file)
         });
         
         test('conditional comments', function(){
-          input = '<!--[if IE 6]> test <![endif]-->';
+          input = '<!--[if IE 6]>test<![endif]-->';
           equals(minify(input, { removeComments: true }), input);
           
-          input = '<!--[if lt IE 5.5]> test <![endif]-->';
+          input = '<!--[if lt IE 5.5]>test<![endif]-->';
           equals(minify(input, { removeComments: true }), input);
           
-          input = '<!--[if (gt IE 5)&(lt IE 7)]> test <![endif]-->';
+          input = '<!--[if (gt IE 5)&(lt IE 7)]>test<![endif]-->';
           equals(minify(input, { removeComments: true }), input);
         });
         
+        test('collapsing space in conditional comments', function(){
+          input = '<!--[if IE 7]>\n\n   \t\n   \t\t ' +
+                       '<link rel="stylesheet" href="/css/ie7-fixes.css" type="text/css" />\n\t' +
+                  '<![endif]-->';
+          output = '<!--[if IE 7]>'+
+                      '<link rel="stylesheet" href="/css/ie7-fixes.css" type="text/css" />'+
+                   '<![endif]-->'
+          
+          equals(minify(input, { removeComments: true }), output);
+          
+          
+          input = '<!--[if lte IE 6]>\n    \n   \n\n\n\t' +
+                       '<p title=" sigificant     whitespace   ">blah blah</p>' +
+                  '<![endif]-->';
+          output = '<!--[if lte IE 6]>' +
+                       '<p title=" sigificant     whitespace   ">blah blah</p>' +
+                  '<![endif]-->';
+          
+          equals(minify(input, { removeComments: true }), output);
+        });
+        
         test('remove comments from scripts', function(){
           input = '<script><!--alert(1)--><\/script>';
           output = '<script><\/script>';