Skip to content

Commit

Permalink
add preserve custom fragments in scripts/style option
Browse files Browse the repository at this point in the history
  • Loading branch information
gzzhanghao committed Sep 23, 2016
1 parent 10c5e80 commit 634d0d9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/htmlminifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand All @@ -878,12 +884,18 @@ function minify(value, options, partialMarkup) {
var minifyCSS = options.minifyCSS;
if (minifyCSS) {
options.minifyCSS = function(text) {
if (options.preserveCustomFragmentsInStyle) {
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);
};
}
Expand Down
7 changes: 7 additions & 0 deletions tests/minifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -1989,6 +1989,13 @@ QUnit.test('minification of scripts with custom fragments', function(assert) {
output = '<script>function f(){return"<?php ?>"}</script>';
assert.equal(minify(input, { minifyJS: true }), output);
assert.equal(minify(input, { collapseWhitespace: true, minifyJS: true }), output);

input = '<script>function a() {\n console.log( "<% ... %>" )\n}</script>';
output = '<script>function a(){console.log("<% ... %>")}</script>';
assert.equal(minify(input, { minifyJS: true, preserveCustomFragmentsInScripts: true }), output);

input = '<script>function a() { b() \n<% ... %>\n a()}</script>';
assert.equal(minify(input, { minifyJS: true, preserveCustomFragmentsInScripts: true }), input);
});

QUnit.test('event minification', function(assert) {
Expand Down

0 comments on commit 634d0d9

Please sign in to comment.