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
7 changes: 7 additions & 0 deletions packages/normalize-color/__tests__/normalizeColor-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ it('refuses non-spec compliant colors', () => {
expect(normalizeColor('#00gg00')).toBe(null);
expect(normalizeColor('rgb(1, 2, 3,)')).toBe(null);
expect(normalizeColor('rgb(1, 2, 3')).toBe(null);
// Functional forms must match the whole string (hex already did).
expect(normalizeColor('xxrgb(1, 2, 3)yy')).toBe(null);
expect(normalizeColor('rgb(1, 2, 3)yy')).toBe(null);
expect(normalizeColor('xxrgb(1, 2, 3)')).toBe(null);
expect(normalizeColor('rgba(1,2,3,0.5)extra')).toBe(null);
expect(normalizeColor('prefixhsl(0, 0%, 0%)')).toBe(null);
expect(normalizeColor('hwb(0 0% 0%)suffix')).toBe(null);

// Used to be accepted by normalizeColor
expect(normalizeColor('abc')).toBe(null);
Expand Down
16 changes: 9 additions & 7 deletions packages/normalize-color/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,23 +276,25 @@ function getMatchers() {
'|' +
callWithSlashSeparator(NUMBER, NUMBER, NUMBER, NUMBER);

// Anchor functional forms the same way as hex* so leading/trailing
// junk (e.g. "xxrgb(1, 2, 3)yy") cannot partially match.
cachedMatchers = {
rgb: new RegExp('rgb(' + rgbRegexPattern + ')'),
rgba: new RegExp('rgba(' + rgbRegexPattern + ')'),
hsl: new RegExp('hsl' + call(NUMBER, PERCENTAGE, PERCENTAGE)),
rgb: new RegExp('^rgb(' + rgbRegexPattern + ')$'),
rgba: new RegExp('^rgba(' + rgbRegexPattern + ')$'),
hsl: new RegExp('^hsl' + call(NUMBER, PERCENTAGE, PERCENTAGE) + '$'),
hsla: new RegExp(
'hsla(' +
'^hsla(' +
commaSeparatedCall(NUMBER, PERCENTAGE, PERCENTAGE, NUMBER) +
'|' +
callWithSlashSeparator(NUMBER, PERCENTAGE, PERCENTAGE, NUMBER) +
')',
')$',
),
hwb: new RegExp(
'hwb(' +
'^hwb(' +
callModern(NUMBER, PERCENTAGE, PERCENTAGE) +
'|' +
callWithSlashSeparator(NUMBER, PERCENTAGE, PERCENTAGE, NUMBER) +
')',
')$',
),
hex3: /^#([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/,
hex4: /^#([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/,
Expand Down
Loading