From 32e87bd89667e9d271596ac601535b6fc4c59f00 Mon Sep 17 00:00:00 2001 From: jasonhzhang Date: Fri, 23 Sep 2016 19:20:55 +0800 Subject: [PATCH] add preserve custom fragments in scripts/style option --- src/htmlminifier.js | 12 ++++++++++++ tests/minifier.js | 3 +++ 2 files changed, 15 insertions(+) diff --git a/src/htmlminifier.js b/src/htmlminifier.js index 8f4f4973..b3261680 100644 --- a/src/htmlminifier.js +++ b/src/htmlminifier.js @@ -865,6 +865,12 @@ function minify(value, options, partialMarkup) { }); } + function restoreCustomFragments(text) { + return text.replace(uidPattern, function(match, prefix, index) { + return ignoredCustomMarkupChunks[+index][0]; + }); + } + var customFragments = options.ignoreCustomFragments.map(function(re) { return re.source; }); @@ -878,12 +884,18 @@ function minify(value, options, partialMarkup) { var minifyCSS = options.minifyCSS; if (minifyCSS) { options.minifyCSS = function(text) { + if (options.preserveCustomFragmentsInStyles) { + text = restoreCustomFragments(text); + } return minifyCSS(escapeFragments(text)); }; } var minifyJS = options.minifyJS; if (minifyJS) { options.minifyJS = function(text, inline) { + if (options.preserveCustomFragmentsInScripts) { + text = restoreCustomFragments(text); + } return minifyJS(escapeFragments(text), inline); }; } diff --git a/tests/minifier.js b/tests/minifier.js index b6b6a3fc..70bdb720 100644 --- a/tests/minifier.js +++ b/tests/minifier.js @@ -1989,6 +1989,9 @@ QUnit.test('minification of scripts with custom fragments', function(assert) { output = ''; assert.equal(minify(input, { minifyJS: true }), output); assert.equal(minify(input, { collapseWhitespace: true, minifyJS: true }), output); + + input = ''; + assert.equal(minify(input, { minifyJS: true, preserveCustomFragmentsInScripts: true }), input); }); QUnit.test('event minification', function(assert) {