fix corner case in `decodeEntities` (#894)
authorAlex Lam S.L <alexlamsl@gmail.com>
Mon, 19 Mar 2018 18:13:53 +0000 (02:13 +0800)
committerGitHub <noreply@github.com>
Mon, 19 Mar 2018 18:13:53 +0000 (02:13 +0800)
fixes #893

src/htmlminifier.js
tests/minifier.js

index 4bdef7e..e16c772 100644 (file)
@@ -1153,11 +1153,6 @@ function minify(value, options, partialMarkup) {
             trimTrailingWhitespace(buffer.length - 1, nextTag);
           }
         }
-        else if (uidPattern) {
-          text = text.replace(uidPattern, function(match, prefix, index) {
-            return ignoredCustomMarkupChunks[+index][0];
-          });
-        }
         if (!stackNoCollapseWhitespace.length && nextTag !== 'html' && !(prevTag && nextTag)) {
           text = collapseWhitespace(text, options, false, false, true);
         }
@@ -1191,6 +1186,11 @@ function minify(value, options, partialMarkup) {
         // https://mathiasbynens.be/notes/ambiguous-ampersands
         text = text.replace(/&(#?[0-9a-zA-Z]+;)/g, '&amp$1').replace(/</g, '&lt;');
       }
+      if (uidPattern && options.collapseWhitespace && stackNoTrimWhitespace.length) {
+        text = text.replace(uidPattern, function(match, prefix, index) {
+          return ignoredCustomMarkupChunks[+index][0];
+        });
+      }
       currentChars += text;
       if (text) {
         hasChars = true;
index 4df35e5..5a3b837 100644 (file)
@@ -3250,6 +3250,14 @@ QUnit.test('decode entity characters', function(assert) {
   assert.equal(minify(input, { decodeEntities: false }), input);
   output = '<a href="/?foo=1&bar=<2>">baz&lt;moo>\u00a9</a>';
   assert.equal(minify(input, { decodeEntities: true }), output);
+
+  input = '<? &amp; ?>&amp;<pre><? &amp; ?>&amp;</pre>';
+  assert.equal(minify(input), input);
+  assert.equal(minify(input, { collapseWhitespace: false, decodeEntities: false }), input);
+  assert.equal(minify(input, { collapseWhitespace: true, decodeEntities: false }), input);
+  output = '<? &amp; ?>&<pre><? &amp; ?>&</pre>';
+  assert.equal(minify(input, { collapseWhitespace: false, decodeEntities: true }), output);
+  assert.equal(minify(input, { collapseWhitespace: true, decodeEntities: true }), output);
 });
 
 QUnit.test('tests from PHPTAL', function(assert) {