From: Duncan Beevers Date: Tue, 16 Sep 2014 18:14:37 +0000 (-0500) Subject: Support ignored tags as preprocessing pass X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=049b5a78bd355eb5116f54f1608ce861c1be8751;p=html-minifier.git Support ignored tags as preprocessing pass --- diff --git a/src/htmlminifier.js b/src/htmlminifier.js index d5f4b00..833c1f1 100644 --- a/src/htmlminifier.js +++ b/src/htmlminifier.js @@ -534,6 +534,12 @@ 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); } @@ -698,10 +704,6 @@ } buffer.push(text); }, - ignore: function(text) { - // `text` === strings that start with `` or `%>`. - buffer.push(options.removeIgnored ? '' : text); // `text` allowed by default. - }, doctype: function(doctype) { buffer.push(options.useShortDoctype ? '' : collapseWhitespace(doctype)); }, diff --git a/src/htmlparser.js b/src/htmlparser.js index c6e29eb..e11d8f2 100644 --- a/src/htmlparser.js +++ b/src/htmlparser.js @@ -44,9 +44,7 @@ startTagClose = /\s*(\/?)>/, endTag = /^<\/([\w:-]+)[^>]*>/, endingSlash = /\/>$/, - doctype = /^]+>/i, - startIgnore = /<(%|\?)/, - endIgnore = /(%|\?)>/; + doctype = /^]+>/i; var IS_REGEX_CAPTURING_BROKEN = false; 'x'.replace(/x(.)?/g, function(m, g) { @@ -192,13 +190,23 @@ } // 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 ); } } diff --git a/tests/minifier.js b/tests/minifier.js index ea6b0d7..87ab682 100644 --- a/tests/minifier.js +++ b/tests/minifier.js @@ -827,7 +827,7 @@ output = 'This is the start.<% ... %><%= ... %>No comment, but middle.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
\r\n ...\r\n
\r\n<% end %>'; @@ -837,6 +837,10 @@ output = '
...
'; equal(minify(input, { collapseWhitespace: true, removeIgnored: true }), output); + input = ''; + equal(minify(input, {}), input); + output = ''; + equal(minify(input, { removeIgnored: true }), output); }); test('bootstrap\'s span > button > span', function() {