From 21a5df0496f4c721f2cb14cf5f42b499312efff4 Mon Sep 17 00:00:00 2001 From: Jakub Pawlowicz Date: Mon, 5 Mar 2018 12:37:26 +0100 Subject: [PATCH] Fixes #988 - edge case in dropping `animation-duration`. When `animation-duration` is default but `animation-delay` isn't we shouldn't drop the former as both need to be given. --- History.md | 5 ++ lib/optimizer/level-2/compactable.js | 5 ++ lib/optimizer/level-2/restore.js | 18 ++++++- test/optimizer/level-2/restore-test.js | 72 ++++++++++++++++++++++++++ 4 files changed, 99 insertions(+), 1 deletion(-) diff --git a/History.md b/History.md index 10bc5cd2b..58b9db5e6 100644 --- a/History.md +++ b/History.md @@ -1,3 +1,8 @@ +[4.1.10 / 2018-xx-xx](https://github.com/jakubpawlowicz/clean-css/compare/v4.1.9...4.1) +================== + +* Fixed issue [#988](https://github.com/jakubpawlowicz/clean-css/issues/988) - edge case in dropping default animation-duration. + [4.1.9 / 2017-09-19](https://github.com/jakubpawlowicz/clean-css/compare/v4.1.8...v4.1.9) ================== diff --git a/lib/optimizer/level-2/compactable.js b/lib/optimizer/level-2/compactable.js index 97e7e2aca..c5f8c8a79 100644 --- a/lib/optimizer/level-2/compactable.js +++ b/lib/optimizer/level-2/compactable.js @@ -99,6 +99,7 @@ var compactable = { ], defaultValue: '0s', intoMultiplexMode: 'real', + keepUnlessDefault: 'animation-delay', vendorPrefixes: [ '-moz-', '-o-', @@ -955,6 +956,10 @@ function cloneDescriptor(propertyName, prefix) { }); } + if ('keepUnlessDefault' in clonedDescriptor) { + clonedDescriptor.keepUnlessDefault = prefix + clonedDescriptor.keepUnlessDefault; + } + return clonedDescriptor; } diff --git a/lib/optimizer/level-2/restore.js b/lib/optimizer/level-2/restore.js index 13f12e496..f9c2f0d33 100644 --- a/lib/optimizer/level-2/restore.js +++ b/lib/optimizer/level-2/restore.js @@ -264,8 +264,9 @@ function withoutDefaults(property, compactable) { var component = components[i]; var descriptor = compactable[component.name]; - if (component.value[0][1] != descriptor.defaultValue) + if (component.value[0][1] != descriptor.defaultValue || ('keepUnlessDefault' in descriptor) && !isDefault(components, compactable, descriptor.keepUnlessDefault)) { restored.unshift(component.value[0]); + } } if (restored.length === 0) @@ -277,6 +278,21 @@ function withoutDefaults(property, compactable) { return restored; } +function isDefault(components, compactable, propertyName) { + var component; + var i, l; + + for (i = 0, l = components.length; i < l; i++) { + component = components[i]; + + if (component.name == propertyName && component.value[0][1] == compactable[propertyName].defaultValue) { + return true; + } + } + + return false; +} + module.exports = { background: background, borderRadius: borderRadius, diff --git a/test/optimizer/level-2/restore-test.js b/test/optimizer/level-2/restore-test.js index b37c4bcb5..d39182eef 100644 --- a/test/optimizer/level-2/restore-test.js +++ b/test/optimizer/level-2/restore-test.js @@ -979,6 +979,78 @@ vows.describe(restore) ]); } } + }, + 'animation': { + 'with two time units where both are default': { + 'topic': function () { + return _restore( + _breakUp([ + 'property', + ['property-name', 'animation'], + ['property-value', '0s'], + ['property-value', 'ease-out'], + ['property-value', '0s'], + ['property-value', 'forwards'], + ['property-value', 'test-name'] + ]) + ); + }, + 'gives right value back': function (restoredValue) { + assert.deepEqual(restoredValue, [ + ['property-value', 'ease-out'], + ['property-value', 'forwards'], + ['property-value', 'test-name'] + ]); + } + }, + 'with two time units where first is default': { + 'topic': function () { + return _restore( + _breakUp([ + 'property', + ['property-name', 'animation'], + ['property-value', '0s'], + ['property-value', 'ease-out'], + ['property-value', '5s'], + ['property-value', 'forwards'], + ['property-value', 'test-name'] + ]) + ); + }, + 'gives right value back': function (restoredValue) { + assert.deepEqual(restoredValue, [ + ['property-value', '0s'], + ['property-value', 'ease-out'], + ['property-value', '5s'], + ['property-value', 'forwards'], + ['property-value', 'test-name'] + ]); + } + }, + 'with two vendor-prefixed time units where first is default': { + 'topic': function () { + return _restore( + _breakUp([ + 'property', + ['property-name', '-webkit-animation'], + ['property-value', '0s'], + ['property-value', 'ease-out'], + ['property-value', '5s'], + ['property-value', 'forwards'], + ['property-value', 'test-name'] + ]) + ); + }, + 'gives right value back': function (restoredValue) { + assert.deepEqual(restoredValue, [ + ['property-value', '0s'], + ['property-value', 'ease-out'], + ['property-value', '5s'], + ['property-value', 'forwards'], + ['property-value', 'test-name'] + ]); + } + } } }) .export(module);