Skip to content

Commit 32e87bd

Browse files
committed
add preserve custom fragments in scripts/style option
1 parent 10c5e80 commit 32e87bd

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/htmlminifier.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,12 @@ function minify(value, options, partialMarkup) {
865865
});
866866
}
867867

868+
function restoreCustomFragments(text) {
869+
return text.replace(uidPattern, function(match, prefix, index) {
870+
return ignoredCustomMarkupChunks[+index][0];
871+
});
872+
}
873+
868874
var customFragments = options.ignoreCustomFragments.map(function(re) {
869875
return re.source;
870876
});
@@ -878,12 +884,18 @@ function minify(value, options, partialMarkup) {
878884
var minifyCSS = options.minifyCSS;
879885
if (minifyCSS) {
880886
options.minifyCSS = function(text) {
887+
if (options.preserveCustomFragmentsInStyles) {
888+
text = restoreCustomFragments(text);
889+
}
881890
return minifyCSS(escapeFragments(text));
882891
};
883892
}
884893
var minifyJS = options.minifyJS;
885894
if (minifyJS) {
886895
options.minifyJS = function(text, inline) {
896+
if (options.preserveCustomFragmentsInScripts) {
897+
text = restoreCustomFragments(text);
898+
}
887899
return minifyJS(escapeFragments(text), inline);
888900
};
889901
}

tests/minifier.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1989,6 +1989,9 @@ QUnit.test('minification of scripts with custom fragments', function(assert) {
19891989
output = '<script>function f(){return"<?php ?>"}</script>';
19901990
assert.equal(minify(input, { minifyJS: true }), output);
19911991
assert.equal(minify(input, { collapseWhitespace: true, minifyJS: true }), output);
1992+
1993+
input = '<script>function a() { b() \n<% ... %>\n a()}</script>';
1994+
assert.equal(minify(input, { minifyJS: true, preserveCustomFragmentsInScripts: true }), input);
19921995
});
19931996

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

0 commit comments

Comments
 (0)