From: GoalSmashers Date: Thu, 30 Jan 2014 08:05:26 +0000 (+0000) Subject: Fixes out of bounds values in rgb / rgba declarations. See #216. X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=33d844d76e388880d082c1272625d95fdda3163a;p=clean-css.git Fixes out of bounds values in rgb / rgba declarations. See #216. --- diff --git a/lib/colors/rgb-to-hex.js b/lib/colors/rgb-to-hex.js index 479e6208..acfbf011 100644 --- a/lib/colors/rgb-to-hex.js +++ b/lib/colors/rgb-to-hex.js @@ -1,7 +1,11 @@ module.exports = function RGBToHex(data) { return { process: function() { - return data.replace(/rgb\((\d+),(\d+),(\d+)\)/g, function(match, red, green, blue) { + return data.replace(/rgb\((\-?\d+),(\-?\d+),(\-?\d+)\)/g, function(match, red, green, blue) { + red = Math.max(0, Math.min(~~red, 255)); + green = Math.max(0, Math.min(~~green, 255)); + blue = Math.max(0, Math.min(~~blue, 255)); + // Credit: Asen http://jsbin.com/UPUmaGOc/2/edit?js,console return '#' + ('00000' + (red << 16 | green << 8 | blue).toString(16)).slice(-6); }); diff --git a/test/unit-test.js b/test/unit-test.js index 2b663c6b..1a56bf89 100644 --- a/test/unit-test.js +++ b/test/unit-test.js @@ -686,6 +686,14 @@ vows.describe('clean-units').addBatch({ 'hsl out of bounds #7': [ 'a{color:hsl(0,0%,-10%)}', 'a{color:#000}' + ], + 'rgb out of a lower bound': [ + 'a{color:rgb(-1,-1,-1)}', + 'a{color:#000}' + ], + 'rgb out of an upper bound': [ + 'a{color:rgb(256,256,256)}', + 'a{color:#fff}' ] }), 'shortening colors': colorShorteningContext(),