From 6cdb25d98f0426da77fcc4f1c895cd6c735d0a5e Mon Sep 17 00:00:00 2001 From: Jakub Pawlowicz Date: Wed, 15 Apr 2015 23:43:46 +0100 Subject: [PATCH] Fixes #529 - wrong font weight minification. Actually reworked this minification so we should not have further issues with it. --- History.md | 1 + lib/selectors/optimizers/simple.js | 33 ++++++++++++++++++++++-- test/selectors/optimizers/simple-test.js | 4 +++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/History.md b/History.md index 8872ed9e..02992029 100644 --- a/History.md +++ b/History.md @@ -21,6 +21,7 @@ * Fixed issue [#521](https://github.com/jakubpawlowicz/clean-css/issues/521) - unit optimizations inside `calc()`. * Fixed issue [#526](https://github.com/jakubpawlowicz/clean-css/issues/526) - shorthand overriding into a function. * Fixed issue [#528](https://github.com/jakubpawlowicz/clean-css/issues/528) - better support for IE<9 hacks. +* Fixed issue [#529](https://github.com/jakubpawlowicz/clean-css/issues/529) - wrong font weight minification. [3.1.9 / 2015-04-04](https://github.com/jakubpawlowicz/clean-css/compare/v3.1.8...v3.1.9) ================== diff --git a/lib/selectors/optimizers/simple.js b/lib/selectors/optimizers/simple.js index 342113f4..8cc78824 100644 --- a/lib/selectors/optimizers/simple.js +++ b/lib/selectors/optimizers/simple.js @@ -9,6 +9,9 @@ var DEFAULT_ROUNDING_PRECISION = 2; var CHARSET_TOKEN = '@charset'; var CHARSET_REGEXP = new RegExp('^' + CHARSET_TOKEN, 'i'); +var FONT_NUMERAL_WEIGHTS = ['100', '200', '300', '400', '500', '600', '700', '800', '900']; +var FONT_NAME_WEIGHTS = ['normal', 'bold', 'bolder', 'lighter']; + function SimpleOptimizer(options) { this.options = options; @@ -181,8 +184,34 @@ function minifyFilter(property) { } function minifyFont(property) { - if (property[2] && property[2][0] != 'normal' && property[2][0] != 'bold' && !/^[1-9]00/.test(property[2][0])) - property[1][0] = valueMinifiers['font-weight'](property[1][0]); + var hasNumeral = FONT_NUMERAL_WEIGHTS.indexOf(property[1][0]) > -1 || + property[2] && FONT_NUMERAL_WEIGHTS.indexOf(property[2][0]) > -1 || + property[3] && FONT_NUMERAL_WEIGHTS.indexOf(property[3][0]) > -1; + + if (hasNumeral) + return; + + var normalCount = 0; + if (property[1][0] == 'normal') + normalCount++; + if (property[2] && property[2][0] == 'normal') + normalCount++; + if (property[3] && property[3][0] == 'normal') + normalCount++; + + if (normalCount > 1) + return; + + var toOptimize; + if (FONT_NAME_WEIGHTS.indexOf(property[1][0]) > -1) + toOptimize = 1; + else if (property[2] && FONT_NAME_WEIGHTS.indexOf(property[2][0]) > -1) + toOptimize = 2; + else if (property[3] && FONT_NAME_WEIGHTS.indexOf(property[3][0]) > -1) + toOptimize = 3; + + if (toOptimize) + property[toOptimize][0] = valueMinifiers['font-weight'](property[toOptimize][0]); } function optimizeBody(properties, options) { diff --git a/test/selectors/optimizers/simple-test.js b/test/selectors/optimizers/simple-test.js index d2e4ead6..5e962b90 100644 --- a/test/selectors/optimizers/simple-test.js +++ b/test/selectors/optimizers/simple-test.js @@ -293,6 +293,10 @@ vows.describe(SimpleOptimizer) 'with mixed order of variant and style': [ 'a{font:normal 300 normal 13px/20px sans-serif}', [['font', 'normal', '300', 'normal', '13px', '/', '20px', 'sans-serif']] + ], + 'with mixed normal and weight': [ + 'a{font: normal small-caps 400 medium Georgia, sans-serif;}', + [['font', 'normal', 'small-caps', '400', 'medium', 'Georgia', ',', 'sans-serif']] ] }) ) -- 2.34.1