From ebef07d37ea1ff7ee8e70980041ee8c72dc787ca Mon Sep 17 00:00:00 2001 From: Jakub Pawlowicz Date: Fri, 24 Mar 2017 07:15:42 +0100 Subject: [PATCH] Fixes #905 - allows disabling selector sorting. Why: * It may be beneficial for gzip compression purposes or just a personal preference. --- History.md | 1 + README.md | 3 ++- lib/optimizer/level-1/sort-selectors.js | 12 +++++------- test/optimizer/level-1/optimize-test.js | 24 ++++++++++++++++++++++++ 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/History.md b/History.md index a8620dda..a8f3deee 100644 --- a/History.md +++ b/History.md @@ -3,6 +3,7 @@ * Fixed issue [#916](https://github.com/jakubpawlowicz/clean-css/issues/916) - maximum number of merged selectors. * Fixed issue [#908](https://github.com/jakubpawlowicz/clean-css/issues/908) - improved `minify` method signature. +* Fixed issue [#905](https://github.com/jakubpawlowicz/clean-css/issues/905) - allows disabling selector sorting. * Fixed issue [#893](https://github.com/jakubpawlowicz/clean-css/issues/893) - `inline: false` as alias to `inline: 'none'`. * Fixed issue [#890](https://github.com/jakubpawlowicz/clean-css/issues/890) - adds toggle to disable empty tokens removal. * Fixed issue [#886](https://github.com/jakubpawlowicz/clean-css/issues/886) - better multi pseudo class / element merging. diff --git a/README.md b/README.md index 09ddb907..7f9dbee7 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,7 @@ Once released clean-css 4.1 will introduce the following changes / features: * `removeEmpty` flag in level 2 optimizations controlling removal of rules and nested blocks; * `compatibility: { selectors: { mergeLimit: } }` flag in compatibility settings controlling maximum number of selectors in a single rule; * `minify` method improved signature accepting a list of hashes for a predictable traversal; +* `selectorsSortingMethod` level 1 optimization allows `false` or `'none'` for disabling selector sorting; ## Constructor options @@ -298,7 +299,7 @@ new CleanCSS({ replaceTimeUnits: true, // controls replacing time units with shorter values; defaults to `true` replaceZeroUnits: true, // controls replacing zero values with units; defaults to `true` roundingPrecision: false, // rounds pixel values to `N` decimal places; `false` disables rounding; defaults to `false` - selectorsSortingMethod: 'standard', // denotes selector sorting method; can be `natural` or `standard`; defaults to `standard` + selectorsSortingMethod: 'standard', // denotes selector sorting method; can be `'natural'` or `'standard'`, `'none'`, or false (the last two since 4.1.0-pre); defaults to `'standard'` specialComments: 'all', // denotes a number of /*! ... */ comments preserved; defaults to `all` tidyAtRules: true, // controls at-rules (e.g. `@charset`, `@import`) optimizing; defaults to `true` tidyBlockScopes: true, // controls block scopes (e.g. `@media`) optimizing; defaults to `true` diff --git a/lib/optimizer/level-1/sort-selectors.js b/lib/optimizer/level-1/sort-selectors.js index 2f179e6e..5b261dfb 100644 --- a/lib/optimizer/level-1/sort-selectors.js +++ b/lib/optimizer/level-1/sort-selectors.js @@ -9,17 +9,15 @@ function standardSorter(scope1, scope2) { } function sortSelectors(selectors, method) { - var sorter; - switch (method) { case 'natural': - sorter = naturalSorter; - break; + return selectors.sort(naturalSorter); case 'standard': - sorter = standardSorter; + return selectors.sort(standardSorter); + case 'none': + case false: + return selectors; } - - return selectors.sort(sorter); } module.exports = sortSelectors; diff --git a/test/optimizer/level-1/optimize-test.js b/test/optimizer/level-1/optimize-test.js index 204106e8..c6a03cba 100644 --- a/test/optimizer/level-1/optimize-test.js +++ b/test/optimizer/level-1/optimize-test.js @@ -175,6 +175,30 @@ vows.describe('level 1 optimizations') ], }, { level: { 1: { selectorsSortingMethod: 'natural' } } }) ) + .addBatch( + optimizerContext('selectors - no sorting', { + 'no numbers': [ + '.block,.another-block,.one-more-block{color:red}', + '.block,.another-block,.one-more-block{color:red}' + ], + 'complex numbers': [ + '.block-1__element-11,.block-1__element-2,.block-12__element-1,.block-3__element-1{color:red}', + '.block-1__element-11,.block-1__element-2,.block-12__element-1,.block-3__element-1{color:red}' + ], + }, { level: { 1: { selectorsSortingMethod: 'none' } } }) + ) + .addBatch( + optimizerContext('selectors - no sorting aliased as `false`', { + 'no numbers': [ + '.block,.another-block,.one-more-block{color:red}', + '.block,.another-block,.one-more-block{color:red}' + ], + 'complex numbers': [ + '.block-1__element-11,.block-1__element-2,.block-12__element-1,.block-3__element-1{color:red}', + '.block-1__element-11,.block-1__element-2,.block-12__element-1,.block-3__element-1{color:red}' + ], + }, { level: { 1: { selectorsSortingMethod: false } } }) + ) .addBatch( optimizerContext('selectors - ie8', { '+html': [ -- 2.34.1