Skip to content

Commit

Permalink
Adds a way to switch off @media merging.
Browse files Browse the repository at this point in the history
* In API mode, set `mediaMerging` option to false.
* In CLI mode, add `--skip-media-merging` switch.
  • Loading branch information
jakubpawlowicz committed Feb 10, 2015
1 parent 7ac74d5 commit e508483
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -58,6 +58,7 @@ cleancss [options] source-file, [source-file, ...]
--skip-advanced Disable advanced optimizations - selector & property merging,
reduction, etc.
--skip-aggressive-merging Disable properties merging based on their order
--skip-media-merging Disable `@media` merging
--skip-shorthand-compacting Disable shorthand compacting
--rounding-precision [N] Rounds to `N` decimal places. Defaults to 2. -1 disables rounding.
-c, --compatibility [ie7|ie8] Force compatibility mode (see Readme for advanced examples)
Expand Down Expand Up @@ -117,6 +118,7 @@ CleanCSS constructor accepts a hash as a parameter, i.e.,
* `inliner` - a hash of options for `@import` inliner, see test/protocol-imports-test.js for examples
* `keepBreaks` - whether to keep line breaks (default is false)
* `keepSpecialComments` - `*` for keeping all (default), `1` for keeping first one only, `0` for removing all
* `mediaMerging` - whether to merge `@media` blocks (default is true)
* `processImport` - whether to process `@import` rules
* `rebase` - set to false to skip URL rebasing
* `relativeTo` - path to __resolve__ relative `@import` rules and URLs
Expand Down
2 changes: 2 additions & 0 deletions bin/cleancss
Expand Up @@ -25,6 +25,7 @@ commands
.option('--skip-rebase', 'Disable URLs rebasing')
.option('--skip-advanced', 'Disable advanced optimizations - selector & property merging, reduction, etc.')
.option('--skip-aggressive-merging', 'Disable properties merging based on their order')
.option('--skip-media-merging', 'Disable @media merging')
.option('--skip-shorthand-compacting', 'Disable shorthand compacting')
.option('--rounding-precision [n]', 'Rounds to `N` decimal places. Defaults to 2. -1 disables rounding.', parseInt)
.option('-c, --compatibility [ie7|ie8]', 'Force compatibility mode (see Readme for advanced examples)')
Expand Down Expand Up @@ -64,6 +65,7 @@ var options = {
inliner: commands.timeout ? { timeout: parseFloat(commands.timeout) * 1000 } : undefined,
keepBreaks: !!commands.keepLineBreaks,
keepSpecialComments: commands.s0 ? 0 : (commands.s1 ? 1 : '*'),
mediaMerging: commands.skipMediaMerging ? false : true,
processImport: commands.skipImport ? false : true,
rebase: commands.skipRebase ? false : true,
root: commands.root,
Expand Down
1 change: 1 addition & 0 deletions lib/clean.js
Expand Up @@ -35,6 +35,7 @@ var CleanCSS = module.exports = function CleanCSS(options) {
inliner: options.inliner || {},
keepBreaks: options.keepBreaks || false,
keepSpecialComments: 'keepSpecialComments' in options ? options.keepSpecialComments : '*',
mediaMerging: undefined === options.mediaMerging ? true : !!options.mediaMerging,
processImport: undefined === options.processImport ? true : !!options.processImport,
rebase: undefined === options.rebase ? true : !!options.rebase,
relativeTo: options.relativeTo,
Expand Down
8 changes: 5 additions & 3 deletions lib/selectors/optimizers/advanced.js
Expand Up @@ -469,9 +469,11 @@ AdvancedOptimizer.prototype.optimize = function (tokens) {
self.mergeNonAdjacentBySelector(tokens);
self.mergeNonAdjacentByBody(tokens);

var reduced = self.mergeMediaQueries(tokens);
for (var i = reduced.length - 1; i >= 0; i--) {
_optimize(reduced[i].body);
if (self.options.mediaMerging) {
var reduced = self.mergeMediaQueries(tokens);
for (var i = reduced.length - 1; i >= 0; i--) {
_optimize(reduced[i].body);
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions test/binary-test.js
Expand Up @@ -331,6 +331,11 @@ exports.commandsSuite = vows.describe('binary commands').addBatch({
}
})
},
'@media merging': pipedContext('@media screen{a{color:red}}@media screen{a{display:block}}', '--skip-media-merging', {
'gets right result': function (error, stdout) {
assert.equal(stdout, '@media screen{a{color:red}}@media screen{a{display:block}}');
}
}),
'shorthand compacting': {
'of (yet) unmergeable properties': pipedContext('a{background:url(image.png);background-color:red}', '--skip-shorthand-compacting', {
'gets right result': function(error, stdout) {
Expand Down
8 changes: 8 additions & 0 deletions test/media-queries-test.js
Expand Up @@ -100,4 +100,12 @@ vows.describe('media queries')
assert.equal(minified.styles, '/*! a comment */@media screen{a{color:red;display:block}}');
}
}
})
.addBatch({
'disabled': {
topic: new CleanCSS({ mediaMerging: false }).minify('@media screen{a{color:red}}@media screen{a{display:block}}'),
'keeps @media intact': function(minified) {
assert.equal(minified.styles, '@media screen{a{color:red}}@media screen{a{display:block}}');
}
}
}).export(module);

0 comments on commit e508483

Please sign in to comment.