Fixes #695 - edge case in shorthand overriding.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Mon, 2 Nov 2015 08:18:38 +0000 (08:18 +0000)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Tue, 10 Nov 2015 14:56:56 +0000 (14:56 +0000)
A longhand value should not be overriden by a shorthand with
a function value, e.g.

```css
div {
  background-color: red;
  background: linear-gradient(red, blue);
}
```

History.md
lib/properties/override-compactor.js
test/properties/override-compacting-test.js

index 489f110..e09607f 100644 (file)
@@ -3,6 +3,11 @@
 
 * Requires Node.js 4.0+ to run.
 
+[3.4.7 / 2015-xx-xx](https://github.com/jakubpawlowicz/clean-css/compare/v3.4.6...3.4)
+==================
+
+* Fixed issue [#695](https://github.com/jakubpawlowicz/clean-css/issues/695) - shorthand overriding edge case.
+
 [3.4.6 / 2015-10-14](https://github.com/jakubpawlowicz/clean-css/compare/v3.4.5...v3.4.6)
 ==================
 
index d795be6..3028bfc 100644 (file)
@@ -122,9 +122,9 @@ function moreSameShorthands(properties, startAt, name) {
   return count > 1;
 }
 
-function mergingIntoFunction(left, right, validator) {
-  for (var i = 0, l = left.components.length; i < l; i++) {
-    if (anyValue(validator.isValidFunction, left.components[i]))
+function overridingFunction(shorthand, validator) {
+  for (var i = 0, l = shorthand.components.length; i < l; i++) {
+    if (anyValue(validator.isValidFunction, shorthand.components[i]))
       return true;
   }
 
@@ -260,6 +260,9 @@ function compactOverrides(properties, compatibility, validator) {
         if (!sameVendorPrefixesIn([left], right.components))
           continue;
 
+        if (!anyValue(validator.isValidFunction, left) && overridingFunction(right, validator))
+          continue;
+
         component = right.components.filter(nameMatchFilter(left))[0];
         mayOverride = (compactable[left.name] && compactable[left.name].canOverride) || canOverride.sameValue;
         if (everyCombination(mayOverride, left, component, validator)) {
@@ -274,7 +277,7 @@ function compactOverrides(properties, compatibility, validator) {
         if (moreSameShorthands(properties, i - 1, left.name))
           continue;
 
-        if (mergingIntoFunction(left, right, validator))
+        if (overridingFunction(left, validator))
           continue;
 
         component = left.components.filter(nameMatchFilter(right))[0];
index 0c4aa45..ff9c2dd 100644 (file)
@@ -81,6 +81,15 @@ vows.describe(optimize)
         ]);
       }
     },
+    'longhand then shorthand - with unprefixed function 123': {
+      'topic': 'p{background-color:red;background:linear-gradient(red,blue)}',
+      'into': function (topic) {
+        assert.deepEqual(_optimize(topic), [
+          [['background-color'], ['red']],
+          [['background'], ['linear-gradient(red,blue)']]
+        ]);
+      }
+    },
     'shorthand then longhand': {
       'topic': 'p{background:__ESCAPED_URL_CLEAN_CSS0__ repeat;background-repeat:no-repeat}',
       'into': function (topic) {