Support ignored tags as preprocessing pass
authorDuncan Beevers <duncan@dweebd.com>
Tue, 16 Sep 2014 18:14:37 +0000 (13:14 -0500)
committerDuncan Beevers <duncan@dweebd.com>
Fri, 19 Sep 2014 14:27:05 +0000 (09:27 -0500)
src/htmlminifier.js
src/htmlparser.js
tests/minifier.js

index d5f4b00..833c1f1 100644 (file)
         isIgnoring = false,
         t = new Date();
 
+    if (options.removeIgnored) {
+      value = value
+        .replace(/<\?[^\?]+\?>/g, '')
+        .replace(/<%[^%]+%>/g, '');
+    }
+
     function _canCollapseWhitespace(tag, attrs) {
       return canCollapseWhitespace(tag) || options.canCollapseWhitespace(tag, attrs);
     }
         }
         buffer.push(text);
       },
-      ignore: function(text) {
-        // `text` === strings that start with `<?` or `<%` and end with `?>` or `%>`.
-        buffer.push(options.removeIgnored ? '' : text); // `text` allowed by default.
-      },
       doctype: function(doctype) {
         buffer.push(options.useShortDoctype ? '<!DOCTYPE html>' : collapseWhitespace(doctype));
       },
index c6e29eb..e11d8f2 100644 (file)
@@ -44,9 +44,7 @@
       startTagClose = /\s*(\/?)>/,
       endTag = /^<\/([\w:-]+)[^>]*>/,
       endingSlash = /\/>$/,
-      doctype = /^<!DOCTYPE [^>]+>/i,
-      startIgnore = /<(%|\?)/,
-      endIgnore = /(%|\?)>/;
+      doctype = /^<!DOCTYPE [^>]+>/i;
 
   var IS_REGEX_CAPTURING_BROKEN = false;
   'x'.replace(/x(.)?/g, function(m, g) {
         }
 
         // Ignored elements?
-        else if (html.search(startIgnore) === 0) {
-          index = html.search(endIgnore); // Find closing tag.
-          if (index >= 0) { // Found?
-            // @TODO: Pass matched open/close tags back to handler.
-            handler.ignore && handler.ignore(html.substring(0, index + 2)); // Return ignored string if callback exists.
-            html = html.substring(index + 2); // Next starting point for parser.
-            chars = false; // Chars flag.
+        else if ( /^<\?/.test( html ) ) {
+          index = html.indexOf( '?>', 2 );
+          if ( index >= 0 ) {
+            if ( handler.chars ) {
+              handler.chars( html.substring( 0, index + 2 ) );
+            }
+            html = html.substring( index + 2 );
+          }
+        }
+
+        else if ( /^<%/.test( html ) ) {
+          index = html.indexOf( '%>', 2 );
+          if ( index >= 0 ) {
+            if ( handler.chars ) {
+              handler.chars(html.substring( 0, index + 2) );
+            }
+            html = html.substring( index + 2 );
           }
         }
 
index ea6b0d7..87ab682 100644 (file)
     output = 'This is the start.<% ... %><%= ... %><? ... ?>No comment, but middle.<?= ... ?><?php ... ?><?xml ... ?>Hello, this is the end!';
     equal(minify(input, {}), input);
     equal(minify(input, { removeComments: true, collapseWhitespace: true }), output);
-    output = 'This is the start.No comment, but middle.Hello, this is the end!';
+    output = 'This is the start.No comment, but middle. Hello, this is the end!';
     equal(minify(input, { removeComments: true, collapseWhitespace: true, removeIgnored: true }), output);
 
     input = '<% if foo? %>\r\n  <div class="bar">\r\n    ...\r\n  </div>\r\n<% end %>';
     output = '<div class="bar">...</div>';
     equal(minify(input, { collapseWhitespace: true, removeIgnored: true }), output);
 
+    input = '<a class="<% if foo? %>bar<% end %>"></a>';
+    equal(minify(input, {}), input);
+    output = '<a class="bar"></a>';
+    equal(minify(input, { removeIgnored: true }), output);
   });
 
   test('bootstrap\'s span > button > span', function() {