Skip to content

Commit f8d122e

Browse files
committed
Accept unitless zero stops and reject invalid layers in backgroundImage strings
1 parent 25b05ac commit f8d122e

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

‎packages/react-native/Libraries/StyleSheet/__tests__/processBackgroundImage-itest.js‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,22 @@ describe('processBackgroundImage', () => {
353353
expect(result).toEqual([]);
354354
});
355355

356+
it('should process unitless zero color stop position', () => {
357+
const input = 'linear-gradient(red 0, blue 100%)';
358+
const result = processBackgroundImage(input);
359+
expect(result[0].colorStops).toEqual([
360+
{color: processColor('red'), position: 0},
361+
{color: processColor('blue'), position: '100%'},
362+
]);
363+
});
364+
365+
it('should return empty array if one of multiple gradients is invalid', () => {
366+
const input =
367+
'linear-gradient(red, blue), linear-gradient(notacolor, blue)';
368+
const result = processBackgroundImage(input);
369+
expect(result).toEqual([]);
370+
});
371+
356372
it('should process an array of style objects', () => {
357373
const input: ReadonlyArray<BackgroundImageValue> | string = [
358374
{

‎packages/react-native/Libraries/StyleSheet/processBackgroundImage.js‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,11 @@ function parseBackgroundImageCSSString(
271271
? parseRadialGradientCSSString(gradientContent)
272272
: parseLinearGradientCSSString(gradientContent);
273273

274-
if (gradient != null) {
275-
gradients.push(gradient);
274+
if (gradient == null) {
275+
// If a gradient is invalid, return an empty array and do not apply any gradient. Same as web.
276+
return [];
276277
}
278+
gradients.push(gradient);
277279
}
278280
}
279281
return gradients;
@@ -803,6 +805,10 @@ function getAngleInDegrees(angle?: string): ?number {
803805
}
804806

805807
function getPositionFromCSSValue(position: string) {
808+
if (position === '0') {
809+
return 0;
810+
}
811+
806812
if (position.endsWith('px')) {
807813
return parseFloat(position);
808814
}

0 commit comments

Comments
 (0)