From 486dca4e8e547e7920cb5deca6aa66e03f75cd1f Mon Sep 17 00:00:00 2001 From: Jakub Pawlowicz Date: Mon, 2 Nov 2015 08:18:38 +0000 Subject: [PATCH] Fixes #695 - edge case in shorthand overriding. 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 | 5 +++++ lib/properties/override-compactor.js | 11 +++++++---- test/properties/override-compacting-test.js | 9 +++++++++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/History.md b/History.md index 489f1103..e09607f2 100644 --- a/History.md +++ b/History.md @@ -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) ================== diff --git a/lib/properties/override-compactor.js b/lib/properties/override-compactor.js index d795be6c..3028bfc4 100644 --- a/lib/properties/override-compactor.js +++ b/lib/properties/override-compactor.js @@ -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]; diff --git a/test/properties/override-compacting-test.js b/test/properties/override-compacting-test.js index 0c4aa450..ff9c2ddf 100644 --- a/test/properties/override-compacting-test.js +++ b/test/properties/override-compacting-test.js @@ -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) { -- 2.34.1