Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/lib/isRgbColor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand Down
28 changes: 28 additions & 0 deletions test/validators.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Loading