From 696d6fa0c0ffac837488976c7dcd64316b5ac71e Mon Sep 17 00:00:00 2001 From: alexlamsl Date: Sun, 6 Nov 2016 17:20:18 +0800 Subject: [PATCH] allow backreference usage in ignoreCustomFragments fixes #733 --- src/htmlminifier.js | 6 +++--- tests/minifier.js | 12 ++++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/htmlminifier.js b/src/htmlminifier.js index c5fee37..6848216 100644 --- a/src/htmlminifier.js +++ b/src/htmlminifier.js @@ -869,9 +869,9 @@ function minify(value, options, partialMarkup) { return re.source; }); if (customFragments.length) { - var reCustomIgnore = new RegExp('(\\s*)(?:' + customFragments.join('|') + ')+(\\s*)', 'g'); + var reCustomIgnore = new RegExp('\\s*(?:' + customFragments.join('|') + ')+\\s*', 'g'); // temporarily replace custom ignored fragments with unique attributes - value = value.replace(reCustomIgnore, function(match, prefix, suffix) { + value = value.replace(reCustomIgnore, function(match) { if (!uidAttr) { uidAttr = uniqueId(value); uidPattern = new RegExp('(\\s*)' + uidAttr + '([0-9]+)(\\s*)', 'g'); @@ -889,7 +889,7 @@ function minify(value, options, partialMarkup) { } } var token = uidAttr + ignoredCustomMarkupChunks.length; - ignoredCustomMarkupChunks.push([match, prefix, suffix]); + ignoredCustomMarkupChunks.push(/^(\s*)[\s\S]*?(\s*)$/.exec(match)); return '\t' + token + '\t'; }); } diff --git a/tests/minifier.js b/tests/minifier.js index f13c9af..91fe7c3 100644 --- a/tests/minifier.js +++ b/tests/minifier.js @@ -1813,6 +1813,18 @@ QUnit.test('Ignore custom fragments', function(assert) { collapseWhitespace: true, trimCustomFragments: true }), output); + + input = 'foo baz moo loo'; + assert.equal(minify(input, { + collapseWhitespace: true, + ignoreCustomFragments: [ + /<(WC@[\s\S]*?)>(.*?)<\/\1>/ + ] + }), input); + output = 'foobaz mooloo'; + assert.equal(minify(input, { + collapseWhitespace: true + }), output); }); QUnit.test('bootstrap\'s span > button > span', function(assert) { -- 2.34.1