Skip to content

Commit a2ae883

Browse files
authored
supply context to minifyCSS custom processor (#909)
1 parent 78ae1a0 commit a2ae883

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Most of the options are disabled by default.
5858
| `includeAutoGeneratedTags` | Insert tags generated by HTML parser | `true` |
5959
| `keepClosingSlash` | Keep the trailing slash on singleton elements | `false` |
6060
| `maxLineLength` | Specify a maximum line length. Compressed output will be split by newlines at valid HTML split-points |
61-
| `minifyCSS` | Minify CSS in style elements and style attributes (uses [clean-css](https://github.com/jakubpawlowicz/clean-css)) | `false` (could be `true`, `Object`, `Function(text)`) |
61+
| `minifyCSS` | Minify CSS in style elements and style attributes (uses [clean-css](https://github.com/jakubpawlowicz/clean-css)) | `false` (could be `true`, `Object`, `Function(text, type)`) |
6262
| `minifyJS` | Minify JavaScript in script elements and event attributes (uses [UglifyJS](https://github.com/mishoo/UglifyJS2)) | `false` (could be `true`, `Object`, `Function(text, inline)`) |
6363
| `minifyURLs` | Minify URLs in various attributes (uses [relateurl](https://github.com/stevenvachon/relateurl)) | `false` (could be `String`, `Object`, `Function(text)`) |
6464
| `preserveLineBreaks` | Always collapse to 1 line break (never remove it entirely) when whitespace between tags include a line break. Must be used in conjunction with `collapseWhitespace=true` | `false` |

src/htmlminifier.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ function cleanAttributeValue(tag, attrName, attrValue, options, attrs) {
285285
if (/;$/.test(attrValue) && !/&#?[0-9a-zA-Z]+;$/.test(attrValue)) {
286286
attrValue = attrValue.replace(/\s*;$/, ';');
287287
}
288-
attrValue = unwrapInlineCSS(options.minifyCSS(wrapInlineCSS(attrValue)));
288+
attrValue = options.minifyCSS(attrValue, 'inline');
289289
}
290290
return attrValue;
291291
}
@@ -322,7 +322,7 @@ function cleanAttributeValue(tag, attrName, attrValue, options, attrs) {
322322
}
323323
else if (isMediaQuery(tag, attrs, attrName)) {
324324
attrValue = trimWhitespace(attrValue);
325-
return unwrapMediaQuery(options.minifyCSS(wrapMediaQuery(attrValue)));
325+
return options.minifyCSS(attrValue, 'media');
326326
}
327327
return attrValue;
328328
}
@@ -694,12 +694,25 @@ function processOptions(options) {
694694
if (typeof minifyCSS !== 'object') {
695695
minifyCSS = {};
696696
}
697-
options.minifyCSS = function(text) {
697+
options.minifyCSS = function(text, type) {
698698
text = text.replace(/(url\s*\(\s*)("|'|)(.*?)\2(\s*\))/ig, function(match, prefix, quote, url, suffix) {
699699
return prefix + quote + options.minifyURLs(url) + quote + suffix;
700700
});
701701
try {
702-
return new CleanCSS(minifyCSS).minify(text).styles;
702+
if (type === 'inline') {
703+
text = wrapInlineCSS(text);
704+
}
705+
else if (type === 'media') {
706+
text = wrapMediaQuery(text);
707+
}
708+
text = new CleanCSS(minifyCSS).minify(text).styles;
709+
if (type === 'inline') {
710+
text = unwrapInlineCSS(text);
711+
}
712+
else if (type === 'media') {
713+
text = unwrapMediaQuery(text);
714+
}
715+
return text;
703716
}
704717
catch (err) {
705718
options.log(err);
@@ -867,8 +880,8 @@ function minify(value, options, partialMarkup) {
867880
uidPattern = new RegExp('(\\s*)' + uidAttr + '([0-9]+)(\\s*)', 'g');
868881
var minifyCSS = options.minifyCSS;
869882
if (minifyCSS) {
870-
options.minifyCSS = function(text) {
871-
return minifyCSS(escapeFragments(text));
883+
options.minifyCSS = function(text, type) {
884+
return minifyCSS(escapeFragments(text), type);
872885
};
873886
}
874887
var minifyJS = options.minifyJS;

tests/minifier.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -776,36 +776,36 @@ QUnit.test('remove CDATA sections from scripts/styles', function(assert) {
776776
QUnit.test('custom processors', function(assert) {
777777
var input, output;
778778

779-
function css() {
780-
return 'Some CSS';
779+
function css(text, type) {
780+
return (type || 'Normal') + ' CSS';
781781
}
782782

783783
input = '<style>\n.foo { font: 12pt "bar" } </style>';
784784
assert.equal(minify(input), input);
785785
assert.equal(minify(input, { minifyCSS: null }), input);
786786
assert.equal(minify(input, { minifyCSS: false }), input);
787-
output = '<style>Some CSS</style>';
787+
output = '<style>Normal CSS</style>';
788788
assert.equal(minify(input, { minifyCSS: css }), output);
789789

790790
input = '<p style="font: 12pt \'bar\'"></p>';
791791
assert.equal(minify(input), input);
792792
assert.equal(minify(input, { minifyCSS: null }), input);
793793
assert.equal(minify(input, { minifyCSS: false }), input);
794-
output = '<p style="Some CSS"></p>';
794+
output = '<p style="inline CSS"></p>';
795795
assert.equal(minify(input, { minifyCSS: css }), output);
796796

797797
input = '<link rel="stylesheet" href="css/style-mobile.css" media="(max-width: 737px)">';
798798
assert.equal(minify(input), input);
799799
assert.equal(minify(input, { minifyCSS: null }), input);
800800
assert.equal(minify(input, { minifyCSS: false }), input);
801-
output = '<link rel="stylesheet" href="css/style-mobile.css" media="Some CSS">';
801+
output = '<link rel="stylesheet" href="css/style-mobile.css" media="media CSS">';
802802
assert.equal(minify(input, { minifyCSS: css }), output);
803803

804804
input = '<style media="(max-width: 737px)"></style>';
805805
assert.equal(minify(input), input);
806806
assert.equal(minify(input, { minifyCSS: null }), input);
807807
assert.equal(minify(input, { minifyCSS: false }), input);
808-
output = '<style media="Some CSS">Some CSS</style>';
808+
output = '<style media="media CSS">Normal CSS</style>';
809809
assert.equal(minify(input, { minifyCSS: css }), output);
810810

811811
function js(text, inline) {

0 commit comments

Comments
 (0)