From f46b338de7f90a8ab7ad0b63031a51fe4e74aae2 Mon Sep 17 00:00:00 2001 From: JSap0914 Date: Tue, 30 Jun 2026 12:38:09 +0900 Subject: [PATCH] fix(isRgbColor): accept alpha values with more than 2 decimal digits MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The alpha regex (0?\.\d\d?|1(\.0)?|0(\.0)?) limited alpha to at most 2 decimal digits and did not allow trailing zeros on the boundary values 1 and 0, causing valid CSS rgba() colors to be incorrectly rejected: isRgbColor('rgba(255,255,255,.123)') // false — should be true isRgbColor('rgba(255,255,255,1.00)') // false — should be true isRgbColor('rgba(0,0,0,0.123)') // false — should be true The CSS specification defines as a CSS (https://www.w3.org/TR/css-color-4/#typedef-alpha-value), which imposes no limit on decimal precision. Fix: widen the alpha sub-pattern to: (0?\.\d+|1(\.0+)?|0(\.0+)?) so that any number of decimal digits after the point is accepted, and both 1.0 and 1.00 (and similarly for 0) are valid. Closes #2792 --- src/lib/isRgbColor.js | 4 ++-- test/validators.test.js | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/lib/isRgbColor.js b/src/lib/isRgbColor.js index a672df279..e9fb60253 100644 --- a/src/lib/isRgbColor.js +++ b/src/lib/isRgbColor.js @@ -2,9 +2,9 @@ import assertString from './util/assertString'; const rgbColor = /^rgb\((([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]),){2}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\)$/; -const rgbaColor = /^rgba\((([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]),){3}(0?\.\d\d?|1(\.0)?|0(\.0)?)\)$/; +const rgbaColor = /^rgba\((([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]),){3}(0?\.\d+|1(\.0+)?|0(\.0+)?)\)$/; 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\d?|1(\.0)?|0(\.0)?)\)$/; +const rgbaColorPercent = /^rgba\((([0-9]%|[1-9][0-9]%|100%),){3}(0?\.\d+|1(\.0+)?|0(\.0+)?)\)$/; const startsWithRgb = /^rgba?/; export default function isRgbColor(str, options) { diff --git a/test/validators.test.js b/test/validators.test.js index 9c867efae..afd9a0998 100644 --- a/test/validators.test.js +++ b/test/validators.test.js @@ -5119,9 +5119,13 @@ describe('Validators', () => { 'rgba(255,255,255,.1)', 'rgba(255,255,255,0.1)', 'rgba(255,255,255,.12)', + 'rgba(255,255,255,.123)', + 'rgba(255,255,255,1.00)', + 'rgba(0,0,0,0.123)', 'rgb(5%,5%,5%)', 'rgba(5%,5%,5%,.3)', 'rgba(5%,5%,5%,.32)', + 'rgba(5%,5%,5%,.321)', ], invalid: [ 'rgb(0,0,0,)', @@ -5130,12 +5134,10 @@ describe('Validators', () => { 'rgb()', 'rgba(0,0,0)', 'rgba(255,255,255,2)', - 'rgba(255,255,255,.123)', 'rgba(255,255,256,0.1)', 'rgb(4,4,5%)', 'rgba(5%,5%,5%)', 'rgba(3,3,3%,.3)', - 'rgba(5%,5%,5%,.321)', 'rgb(101%,101%,101%)', 'rgba(3%,3%,101%,0.3)', 'rgb(101%,101%,101%) additional invalid string part',