Fixes #821 - edge case in reducing non-adjacent selectors.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Wed, 16 Nov 2016 10:43:17 +0000 (11:43 +0100)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Wed, 16 Nov 2016 10:51:09 +0000 (11:51 +0100)
Why:

* In case of previously optimized empty rules, the join positions
  were not calculated correctly resulting in tokens being
  incorrectly optimized.

History.md
lib/selectors/reduce-non-adjacent.js
test/selectors/reduce-non-adjacent-test.js

index 3e9d33f..de259d1 100644 (file)
@@ -6,6 +6,7 @@
 [3.4.21 / 2016-xx-xx](https://github.com/jakubpawlowicz/clean-css/compare/v3.4.20...3.4)
 ==================
 
+* Fixed issue [#821](https://github.com/jakubpawlowicz/clean-css/issues/821) - reducing non-adjacent rules.
 * Fixed issue [#830](https://github.com/jakubpawlowicz/clean-css/issues/830) - reordering border-* properties.
 * Fixed issue [#833](https://github.com/jakubpawlowicz/clean-css/issues/833) - moving `@media` queries.
 
index 7a2d181..44b540c 100644 (file)
@@ -147,7 +147,7 @@ function reduceSelector(tokens, selector, data, context, options, outerContext)
 
   for (j = 0, m = bodiesAsList.length; j < m; j++) {
     if (bodiesAsList[j].length > 0)
-      joinsAt.push((joinsAt[j - 1] || 0) + bodiesAsList[j].length);
+      joinsAt.push((joinsAt.length > 0 ? joinsAt[joinsAt.length - 1] : 0) + bodiesAsList[j].length);
   }
 
   optimizeProperties(selector, bodies, joinsAt, false, options, outerContext);
index 73f0ff4..627bafd 100644 (file)
@@ -103,6 +103,10 @@ vows.describe('remove duplicates')
       '!important values': [
         '.one,.two{margin:0!important}.one{margin:1px!important}',
         '.one,.two{margin:0!important}.one{margin:1px!important}'
+      ],
+      'multiple backgrounds': [
+        '.two{background-color:#000}.one,.two{background-color:#fff;background-image:url(x),-moz-linear-gradient(top,#aaa,#aaa);background-image:url(x),linear-gradient(to bottom,#aaa,#aaa)}.two{background-image:url(x),-moz-linear-gradient(top,#bbb,#bbb);background-image:url(x),linear-gradient(to bottom,#bbb,#bbb)}.one,.two{display:block}',
+        '.one,.two{background-color:#fff;background-image:url(x),-moz-linear-gradient(top,#aaa,#aaa);background-image:url(x),linear-gradient(to bottom,#aaa,#aaa);display:block}.two{background-image:url(x),-moz-linear-gradient(top,#bbb,#bbb);background-image:url(x),linear-gradient(to bottom,#bbb,#bbb)}'
       ]
     })
   )