From 112989da0e7306eeb405a5c52616e41c2164af24 Mon Sep 17 00:00:00 2001 From: adroitwhiz <adroitwhiz@protonmail.com> Date: Fri, 1 May 2020 14:38:18 -0400 Subject: [PATCH 1/3] Cast malformed color hex strings to black --- src/util/cast.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/util/cast.js b/src/util/cast.js index ab7c274c1..49c189039 100644 --- a/src/util/cast.js +++ b/src/util/cast.js @@ -93,6 +93,9 @@ class Cast { let color; if (typeof value === 'string' && value.substring(0, 1) === '#') { color = Color.hexToRgb(value); + + // If the color wasn't *actually* a hex color, cast to black + if (!color) color = {r: 0, g: 0, b: 0, a: 255}; } else { color = Color.decimalToRgb(Cast.toNumber(value)); } From 277a355ce411223606998e79ab1bbe6f4a3b144f Mon Sep 17 00:00:00 2001 From: adroitwhiz <adroitwhiz@protonmail.com> Date: Fri, 1 May 2020 14:38:31 -0400 Subject: [PATCH 2/3] Add unit test for malformed hex color string cast --- test/unit/util_cast.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/unit/util_cast.js b/test/unit/util_cast.js index 9ce5981ba..d2675e721 100644 --- a/test/unit/util_cast.js +++ b/test/unit/util_cast.js @@ -73,7 +73,7 @@ test('toString', t => { t.end(); }); -test('toRbgColorList', t => { +test('toRgbColorList', t => { // Hex (minimal, see "color" util tests) t.deepEqual(cast.toRgbColorList('#000'), [0, 0, 0]); t.deepEqual(cast.toRgbColorList('#000000'), [0, 0, 0]); @@ -91,7 +91,7 @@ test('toRbgColorList', t => { t.end(); }); -test('toRbgColorObject', t => { +test('toRgbColorObject', t => { // Hex (minimal, see "color" util tests) t.deepEqual(cast.toRgbColorObject('#000'), {r: 0, g: 0, b: 0}); t.deepEqual(cast.toRgbColorObject('#000000'), {r: 0, g: 0, b: 0}); @@ -107,6 +107,7 @@ test('toRbgColorObject', t => { // Malformed t.deepEqual(cast.toRgbColorObject('ffffff'), {a: 255, r: 0, g: 0, b: 0}); t.deepEqual(cast.toRgbColorObject('foobar'), {a: 255, r: 0, g: 0, b: 0}); + t.deepEqual(cast.toRgbColorObject('#nothex'), {a: 255, r: 0, g: 0, b: 0}); t.end(); }); From cbe912e1bed0ec41bb9f4b03595addbbf3623464 Mon Sep 17 00:00:00 2001 From: adroitwhiz <adroitwhiz@protonmail.com> Date: Fri, 1 May 2020 15:06:52 -0400 Subject: [PATCH 3/3] Add malformed hex RGB color *list* test --- test/unit/util_cast.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/unit/util_cast.js b/test/unit/util_cast.js index d2675e721..bf2bbdff7 100644 --- a/test/unit/util_cast.js +++ b/test/unit/util_cast.js @@ -88,6 +88,7 @@ test('toRgbColorList', t => { // Malformed t.deepEqual(cast.toRgbColorList('ffffff'), [0, 0, 0]); t.deepEqual(cast.toRgbColorList('foobar'), [0, 0, 0]); + t.deepEqual(cast.toRgbColorList('#nothex'), [0, 0, 0]); t.end(); });