Skip to content

Commit 67166b8

Browse files
Match functional colors against the whole string in normalize-colors
normalize-colors anchors its hex matchers but not rgb, rgba, hsl, hsla and hwb, so a string that only contains one of those calls normalizes to that color. AnimatedInterpolation reads an output range that normalizes as a color, so a JS-driven interpolation of a box shadow such as `inset 0 0 0 1px rgba(124, 58, 237, 1)` returns the bare color, which processBoxShadow parses to no shadow at all. Anchor the five functional matchers, as the hex ones already are.
1 parent 83eb67a commit 67166b8

3 files changed

Lines changed: 30 additions & 7 deletions

File tree

‎packages/normalize-color/__tests__/normalizeColor-test.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ it('refuses non-spec compliant colors', () => {
5353
expect(normalizeColor('rgb(1%, 2%, 3%)')).toBe(null);
5454
});
5555

56+
it('refuses a string that only contains a functional color', () => {
57+
expect(normalizeColor('xxrgb(1, 2, 3)yy')).toBe(null);
58+
expect(normalizeColor('rgba(1, 2, 3, 0.5) extra')).toBe(null);
59+
expect(normalizeColor('0 0 0 1px rgba(124, 58, 237, 1)')).toBe(null);
60+
expect(normalizeColor('drop-shadow(0 0 4px hsl(0, 100%, 50%))')).toBe(null);
61+
expect(normalizeColor('hsla(0, 100%, 50%, 1);')).toBe(null);
62+
expect(normalizeColor('x hwb(0 0% 0%)')).toBe(null);
63+
});
64+
5665
it('handles hex6 properly', () => {
5766
expect(normalizeColor('#000000')).toBe(0x000000ff);
5867
expect(normalizeColor('#ffffff')).toBe(0xffffffff);

‎packages/normalize-color/index.js‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -277,22 +277,22 @@ function getMatchers() {
277277
callWithSlashSeparator(NUMBER, NUMBER, NUMBER, NUMBER);
278278

279279
cachedMatchers = {
280-
rgb: new RegExp('rgb(' + rgbRegexPattern + ')'),
281-
rgba: new RegExp('rgba(' + rgbRegexPattern + ')'),
282-
hsl: new RegExp('hsl' + call(NUMBER, PERCENTAGE, PERCENTAGE)),
280+
rgb: new RegExp('^rgb(' + rgbRegexPattern + ')$'),
281+
rgba: new RegExp('^rgba(' + rgbRegexPattern + ')$'),
282+
hsl: new RegExp('^hsl' + call(NUMBER, PERCENTAGE, PERCENTAGE) + '$'),
283283
hsla: new RegExp(
284-
'hsla(' +
284+
'^hsla(' +
285285
commaSeparatedCall(NUMBER, PERCENTAGE, PERCENTAGE, NUMBER) +
286286
'|' +
287287
callWithSlashSeparator(NUMBER, PERCENTAGE, PERCENTAGE, NUMBER) +
288-
')',
288+
')$',
289289
),
290290
hwb: new RegExp(
291-
'hwb(' +
291+
'^hwb(' +
292292
callModern(NUMBER, PERCENTAGE, PERCENTAGE) +
293293
'|' +
294294
callWithSlashSeparator(NUMBER, PERCENTAGE, PERCENTAGE, NUMBER) +
295-
')',
295+
')$',
296296
),
297297
hex3: /^#([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/,
298298
hex4: /^#([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/,

‎packages/react-native/Libraries/Animated/__tests__/Interpolation-itest.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,20 @@ describe('Interpolation', () => {
333333
expect(interpolation(1)).toBe('translate(20px, 10px)');
334334
});
335335

336+
it('should keep the rest of a string range that contains a color', () => {
337+
const interpolation = createInterpolation({
338+
inputRange: [0, 1],
339+
outputRange: [
340+
'inset 0 0 0 1px rgba(124, 58, 237, 1)',
341+
'inset 0 0 0 3px rgba(124, 58, 237, 0.5)',
342+
],
343+
});
344+
345+
expect(interpolation(0)).toBe('inset 0 0 0 1px rgba(124, 58, 237, 1)');
346+
expect(interpolation(0.5)).toBe('inset 0 0 0 2px rgba(124, 58, 237, 0.75)');
347+
expect(interpolation(1)).toBe('inset 0 0 0 3px rgba(124, 58, 237, 0.5)');
348+
});
349+
336350
it('should crash when chaining an interpolation that returns a string', () => {
337351
const interpolation = createInterpolation({
338352
inputRange: [0, 1],

0 commit comments

Comments
 (0)