From 3eee6e7d3cf4bf4b8f38bbbd247a4e0c3f1e99d9 Mon Sep 17 00:00:00 2001 From: emme1t <149944796+emme1t@users.noreply.github.com> Date: Fri, 11 Sep 2026 22:51:34 -0700 Subject: [PATCH] fix(isRgbColor): keep numeric values contiguous when allowing spaces --- src/lib/isRgbColor.js | 3 ++- test/validators.test.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/lib/isRgbColor.js b/src/lib/isRgbColor.js index e9fb60253..ea896af6f 100644 --- a/src/lib/isRgbColor.js +++ b/src/lib/isRgbColor.js @@ -6,6 +6,7 @@ const rgbaColor = /^rgba\((([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]),){ const rgbColorPercent = /^rgb\((([0-9]%|[1-9][0-9]%|100%),){2}([0-9]%|[1-9][0-9]%|100%)\)$/; const rgbaColorPercent = /^rgba\((([0-9]%|[1-9][0-9]%|100%),){3}(0?\.\d+|1(\.0+)?|0(\.0+)?)\)$/; const startsWithRgb = /^rgba?/; +const whitespaceWithinValue = /[\d.]\s+[\d.%]/; export default function isRgbColor(str, options) { assertString(str); @@ -24,7 +25,7 @@ export default function isRgbColor(str, options) { if (allowSpaces) { // make sure it starts with continuous rgba? without spaces before stripping - if (!startsWithRgb.test(str)) { + if (!startsWithRgb.test(str) || whitespaceWithinValue.test(str)) { return false; } // strip all whitespace diff --git a/test/validators.test.js b/test/validators.test.js index 98d2a12ff..090312a6c 100644 --- a/test/validators.test.js +++ b/test/validators.test.js @@ -5361,6 +5361,34 @@ describe('Validators', () => { }); }); + it('should keep RGB color values contiguous when allowing spaces', () => { + [true, false].forEach((includePercentValues) => { + test({ + validator: 'isRgbColor', + args: [{ allowSpaces: true, includePercentValues }], + valid: [ + 'rgb( 255 , 0 , 0 )', + 'rgba(255,\t0,\n0, .125)', + ], + invalid: [ + 'rgb(2 55,0,0)', + 'rgb(0,2\t55,0)', + 'rgb(0,0,2\n55)', + 'rgba(0,0,0,0 .5)', + 'rgba(0,0,0,0. 5)', + 'rgba(0,0,0,. 5)', + ], + }); + }); + + test({ + validator: 'isRgbColor', + args: [{ allowSpaces: true }], + valid: ['rgb( 25% , 50% , 100% )'], + invalid: ['rgb(2 5%,0%,0%)', 'rgb(25 %,0%,0%)'], + }); + }); + it('should validate ISRC code strings', () => { test({ validator: 'isISRC',