From 1254b668c0555c72cc2203c86eb946b18130c0df Mon Sep 17 00:00:00 2001 From: Pranav Sharma Date: Wed, 10 Jul 2024 11:40:04 +0530 Subject: [PATCH 1/3] Fix leading whitespace in color string issue --- src/color.cc | 2 ++ test/canvas.test.js | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/src/color.cc b/src/color.cc index 86cfb73c8..9a6dba300 100644 --- a/src/color.cc +++ b/src/color.cc @@ -739,6 +739,7 @@ rgba_from_hex_string(const char *str, short *ok) { static int32_t rgba_from_name_string(const char *str, short *ok) { + WHITESPACE; std::string lowered(str); std::transform(lowered.begin(), lowered.end(), lowered.begin(), tolower); auto color = named_colors.find(lowered); @@ -765,6 +766,7 @@ rgba_from_name_string(const char *str, short *ok) { int32_t rgba_from_string(const char *str, short *ok) { + WHITESPACE; if ('#' == str[0]) return rgba_from_hex_string(++str, ok); if (str == strstr(str, "rgba")) diff --git a/test/canvas.test.js b/test/canvas.test.js index a4a8a5b77..b1886f925 100644 --- a/test/canvas.test.js +++ b/test/canvas.test.js @@ -163,6 +163,13 @@ describe('Canvas', function () { ctx.fillStyle = '#FGG' assert.equal('#ff0000', ctx.fillStyle) + ctx.fillStyle = ' #FCA' + assert.equal('#ffccaa', ctx.fillStyle) + + ctx.fillStyle = ' #ffccaa' + assert.equal('#ffccaa', ctx.fillStyle) + + ctx.fillStyle = '#fff' ctx.fillStyle = 'afasdfasdf' assert.equal('#ffffff', ctx.fillStyle) @@ -282,6 +289,8 @@ describe('Canvas', function () { ctx.fillStyle = 'rgb( 255 200 90 0.1)' assert.equal('rgba(255, 200, 90, 0.10)', ctx.fillStyle) + ctx.fillStyle = ' rgb( 255 100 90 0.1)' + assert.equal('rgba(255, 100, 90, 0.10)', ctx.fillStyle) // hsl / hsla tests @@ -306,6 +315,9 @@ describe('Canvas', function () { ctx.fillStyle = 'hsl(237, 76%, 25%)' assert.equal('#0f1470', ctx.fillStyle) + ctx.fillStyle = ' hsl(0, 150%, 150%)' + assert.equal('#ffffff', ctx.fillStyle) + ctx.fillStyle = 'hsl(240, 73%, 25%)' assert.equal('#11116e', ctx.fillStyle) From dea98ef6a26065257cce755fd73c05edcb747b7b Mon Sep 17 00:00:00 2001 From: Pranav Sharma Date: Fri, 12 Jul 2024 00:10:07 +0530 Subject: [PATCH 2/3] Add parsing support for floating point numbers in rbg function --- src/color.cc | 5 +++-- test/canvas.test.js | 11 +++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/color.cc b/src/color.cc index 9a6dba300..f82629460 100644 --- a/src/color.cc +++ b/src/color.cc @@ -159,8 +159,9 @@ wrap_float(T value, T limit) { static bool parse_rgb_channel(const char** pStr, uint8_t *pChannel) { - int channel; - if (parse_integer(pStr, &channel)) { + float f_channel; + if (parse_css_number(pStr, &f_channel)) { + int channel = (int) ceil(f_channel); *pChannel = clip(channel, 0, 255); return true; } diff --git a/test/canvas.test.js b/test/canvas.test.js index b1886f925..1de5134a7 100644 --- a/test/canvas.test.js +++ b/test/canvas.test.js @@ -292,6 +292,17 @@ describe('Canvas', function () { ctx.fillStyle = ' rgb( 255 100 90 0.1)' assert.equal('rgba(255, 100, 90, 0.10)', ctx.fillStyle) + ctx.fillStyle = 'rgb(124.00, 58, 26, 0)'; + assert.equal('rgba(124, 58, 26, 0.00)', ctx.fillStyle); + + ctx.fillStyle = 'rgb( 255, 200.09, 90, 40%)' + assert.equal('rgba(255, 201, 90, 0.40)', ctx.fillStyle) + + ctx.fillStyle = 'rgb( 255.00, 199.03, 90, 50 %)' + assert.equal('rgba(255, 200, 90, 0.50)', ctx.fillStyle) + + ctx.fillStyle = 'rgb( 255, 300.09, 90, 40%)' + assert.equal('rgba(255, 255, 90, 0.40)', ctx.fillStyle) // hsl / hsla tests ctx.fillStyle = 'hsl(0, 0%, 0%)' From 5a71dfda0c13ab5e683a758d299e365fc107987a Mon Sep 17 00:00:00 2001 From: Pranav Sharma <43780292+pranav1344@users.noreply.github.com> Date: Sat, 13 Jul 2024 14:51:34 +0530 Subject: [PATCH 3/3] Update CHANGELOG.md --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e8ba2150..77cc5db8e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ project adheres to [Semantic Versioning](http://semver.org/). ### Added ### Fixed + 3.0.0 ================== @@ -36,6 +37,8 @@ This release notably changes to using N-API. 🎉 * Fix a potential memory leak. (#2229) * Fix the wrong type of setTransform * Fix the improper parsing of rgb functions issue. (#2300) +* Fix issue related to improper parsing of leading and trailing whitespaces in CSS color. (#2301) +* RGB functions should support real numbers now instead of just integers. (#2339) 2.11.2 ==================