Fixes #529 - wrong font weight minification.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Wed, 15 Apr 2015 22:43:46 +0000 (23:43 +0100)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Wed, 15 Apr 2015 22:43:46 +0000 (23:43 +0100)
Actually reworked this minification so we should not have further
issues with it.

History.md
lib/selectors/optimizers/simple.js
test/selectors/optimizers/simple-test.js

index 8872ed9..0299202 100644 (file)
@@ -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)
 ==================
index 342113f..8cc7882 100644 (file)
@@ -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) {
index d2e4ead..5e962b9 100644 (file)
@@ -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']]
       ]
     })
   )