Adds a way to switch off `@media` merging.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Tue, 10 Feb 2015 07:55:19 +0000 (07:55 +0000)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Tue, 10 Feb 2015 20:44:02 +0000 (20:44 +0000)
* In API mode, set `mediaMerging` option to false.
* In CLI mode, add `--skip-media-merging` switch.

README.md
bin/cleancss
lib/clean.js
lib/selectors/optimizers/advanced.js
test/binary-test.js
test/media-queries-test.js

index 3eae040..9afa012 100644 (file)
--- a/README.md
+++ b/README.md
@@ -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)
@@ -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
index 8100306..3dd2267 100755 (executable)
@@ -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)')
@@ -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,
index c84beb2..caf9e29 100644 (file)
@@ -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,
index 4cd6fb3..b2e2836 100644 (file)
@@ -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);
+      }
     }
   }
 
index 42aa75f..5f4b650 100644 (file)
@@ -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) {
index 4d411c2..0c2212f 100644 (file)
@@ -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);