fix interaction between htmlmin:ignore & ignoreCustomComments (#699)
authorAlex Lam S.L <alexlamsl@gmail.com>
Sat, 30 Jul 2016 07:58:35 +0000 (16:58 +0900)
committerGitHub <noreply@github.com>
Sat, 30 Jul 2016 07:58:35 +0000 (16:58 +0900)
fixes #696

src/htmlminifier.js
tests/minifier.js

index f25b1d0..2a6f3f7 100644 (file)
@@ -845,8 +845,15 @@ function minify(value, options, partialMarkup) {
   value = value.replace(/<!-- htmlmin:ignore -->([\s\S]*?)<!-- htmlmin:ignore -->/g, function(match, group1) {
     if (!uidIgnore) {
       uidIgnore = uniqueId(value);
+      var pattern = new RegExp('^' + uidIgnore + '([0-9]+)$');
+      if (options.ignoreCustomComments) {
+        options.ignoreCustomComments.push(pattern);
+      }
+      else {
+        options.ignoreCustomComments = [pattern];
+      }
     }
-    var token = '<!--!' + uidIgnore + ignoredMarkupChunks.length + '-->';
+    var token = '<!--' + uidIgnore + ignoredMarkupChunks.length + '-->';
     ignoredMarkupChunks.push(group1);
     return token;
   });
@@ -1254,7 +1261,7 @@ function minify(value, options, partialMarkup) {
     });
   }
   if (uidIgnore) {
-    str = str.replace(new RegExp('<!--!' + uidIgnore + '([0-9]+)-->', 'g'), function(match, index) {
+    str = str.replace(new RegExp('<!--' + uidIgnore + '([0-9]+)-->', 'g'), function(match, index) {
       return ignoredMarkupChunks[+index];
     });
   }
index 5df325c..61c8ec2 100644 (file)
@@ -2247,10 +2247,36 @@ QUnit.test('collapse inline tag whitespace', function(assert) {
 });
 
 QUnit.test('ignore custom comments', function(assert) {
-  var input;
+  var input, output;
 
-  input = '<!-- ko if: someExpressionGoesHere --><li>test</li><!-- /ko -->';
+  input = '<!--! test -->';
+  assert.equal(minify(input), input);
+  assert.equal(minify(input, { removeComments: true }), input);
+  assert.equal(minify(input, { ignoreCustomComments: false }), input);
+  assert.equal(minify(input, {
+    removeComments: true,
+    ignoreCustomComments: []
+  }), '');
+  assert.equal(minify(input, {
+    removeComments: true,
+    ignoreCustomComments: false
+  }), '');
 
+  input = '<!-- htmlmin:ignore -->test<!-- htmlmin:ignore -->';
+  output = 'test';
+  assert.equal(minify(input), output);
+  assert.equal(minify(input, { removeComments: true }), output);
+  assert.equal(minify(input, { ignoreCustomComments: false }), output);
+  assert.equal(minify(input, {
+    removeComments: true,
+    ignoreCustomComments: []
+  }), output);
+  assert.equal(minify(input, {
+    removeComments: true,
+    ignoreCustomComments: false
+  }), output);
+
+  input = '<!-- ko if: someExpressionGoesHere --><li>test</li><!-- /ko -->';
   assert.equal(minify(input, {
     removeComments: true,
     // ignore knockout comments
@@ -2261,7 +2287,6 @@ QUnit.test('ignore custom comments', function(assert) {
   }), input);
 
   input = '<!--#include virtual="/cgi-bin/counter.pl" -->';
-
   assert.equal(minify(input, {
     removeComments: true,
     // ignore Apache SSI includes