mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
fix(style)(backgroundSize) - Passing unsupported backgroundSize on android causes crash (#55904)
Summary: Below snippet would lead to divide by zero crash on android. Currently, "`cover` | `contain`" keywords are not supported for background size as these only work on actual images (`url()` syntax) and not on gradients. These keywords were supposed to be removed in this [commit](https://github.com/facebook/react-native/pull/52282/changes/34c041ce1e13eddbc51bd473c6d241e3ca040887) and added later in this [PR](https://github.com/facebook/react-native/pull/54994) ```jsx <View style={{ experimental_backgroundImage: "radial-gradient(circle, #f093fb, #f5576c)", experimental_backgroundSize: "cover", width: 100, height: 100 }} />; ``` ## Changelog: [ANDROID] [FIXED] - Unsupported background size value leading to crash <!-- Help reviewers and the release process by writing your own changelog entry. Pick one each for the category and type tags: For more details, see: https://reactnative.dev/contributing/changelogs-in-pull-requests Pull Request resolved: https://github.com/facebook/react-native/pull/55904 Test Plan: Tested the BackgroundImage example on RNTester. Reviewed By: christophpurrer Differential Revision: D95202400 Pulled By: NickGerleman fbshipit-source-id: 287f4710c4afc031b7f5e080f544e8dadfc007d2
This commit is contained in:
1 parent
540120a527
commit
fe046dc782
7 files changed
+53
-69
No files matched your search
@@ -423,13 +423,10 @@ type RadialGradientValue = {
|
||||
|
||||
export type BackgroundImageValue = LinearGradientValue | RadialGradientValue;
|
||||
|
||||
export type BackgroundSizeValue =
|
||||
| {
|
||||
x: string | number;
|
||||
y: string | number;
|
||||
}
|
||||
| 'cover'
|
||||
| 'contain';
|
||||
export type BackgroundSizeValue = {
|
||||
x: string | number;
|
||||
y: string | number;
|
||||
};
|
||||
|
||||
export type BackgroundRepeatKeyword =
|
||||
| 'repeat'
|
||||
|
||||
@@ -779,13 +779,10 @@ type RadialGradientValue = {
|
||||
|
||||
export type BackgroundImageValue = LinearGradientValue | RadialGradientValue;
|
||||
|
||||
export type BackgroundSizeValue =
|
||||
| {
|
||||
x: string | number,
|
||||
y: string | number,
|
||||
}
|
||||
| 'cover'
|
||||
| 'contain';
|
||||
export type BackgroundSizeValue = {
|
||||
x: string | number,
|
||||
y: string | number,
|
||||
};
|
||||
|
||||
export type BackgroundRepeatKeyword =
|
||||
| 'repeat'
|
||||
|
||||
+30
-33
@@ -34,30 +34,19 @@ describe('processBackgroundSize', () => {
|
||||
]);
|
||||
});
|
||||
|
||||
it('should handle array with cover', () => {
|
||||
expect(processBackgroundSize(['cover'])).toEqual(['cover']);
|
||||
});
|
||||
|
||||
it('should handle array with contain', () => {
|
||||
expect(processBackgroundSize(['contain'])).toEqual(['contain']);
|
||||
});
|
||||
|
||||
it('should handle array with mixed values', () => {
|
||||
expect(processBackgroundSize([{x: 100, y: 'auto'}, 'cover'])).toEqual([
|
||||
it('should handle array with multiple values', () => {
|
||||
expect(
|
||||
processBackgroundSize([
|
||||
{x: 100, y: 'auto'},
|
||||
{x: '50%', y: '50%'},
|
||||
]),
|
||||
).toEqual([
|
||||
{x: 100, y: 'auto'},
|
||||
'cover',
|
||||
{x: '50%', y: '50%'},
|
||||
]);
|
||||
});
|
||||
|
||||
// Test string input - single values
|
||||
it('should parse cover', () => {
|
||||
expect(processBackgroundSize('cover')).toEqual(['cover']);
|
||||
});
|
||||
|
||||
it('should parse contain', () => {
|
||||
expect(processBackgroundSize('contain')).toEqual(['contain']);
|
||||
});
|
||||
|
||||
it('should parse single length value', () => {
|
||||
expect(processBackgroundSize('100px')).toEqual([{x: 100, y: 'auto'}]);
|
||||
});
|
||||
@@ -111,9 +100,9 @@ describe('processBackgroundSize', () => {
|
||||
|
||||
// Test multiple background sizes (comma-separated)
|
||||
it('should parse multiple background sizes', () => {
|
||||
expect(processBackgroundSize('100px 200px, cover')).toEqual([
|
||||
expect(processBackgroundSize('100px 200px, 50%')).toEqual([
|
||||
{x: 100, y: 200},
|
||||
'cover',
|
||||
{x: '50%', y: 'auto'},
|
||||
]);
|
||||
});
|
||||
|
||||
@@ -125,16 +114,16 @@ describe('processBackgroundSize', () => {
|
||||
});
|
||||
|
||||
it('should parse multiple background sizes with newlines', () => {
|
||||
expect(processBackgroundSize('100px 200px,\nCover')).toEqual([
|
||||
expect(processBackgroundSize('100px 200px,\n50%')).toEqual([
|
||||
{x: 100, y: 200},
|
||||
'cover',
|
||||
{x: '50%', y: 'auto'},
|
||||
]);
|
||||
});
|
||||
|
||||
it('should parse multiple background sizes with extra whitespace', () => {
|
||||
expect(processBackgroundSize(' 100px 200px , cover ')).toEqual([
|
||||
expect(processBackgroundSize(' 100px 200px , 50% ')).toEqual([
|
||||
{x: 100, y: 200},
|
||||
'cover',
|
||||
{x: '50%', y: 'auto'},
|
||||
]);
|
||||
});
|
||||
|
||||
@@ -172,11 +161,11 @@ describe('processBackgroundSize', () => {
|
||||
});
|
||||
|
||||
it('should handle mixed case keywords', () => {
|
||||
expect(processBackgroundSize('COVER')).toEqual(['cover']);
|
||||
expect(processBackgroundSize('AUTO')).toEqual([{x: 'auto', y: 'auto'}]);
|
||||
});
|
||||
|
||||
it('should handle mixed case keywords with spaces', () => {
|
||||
expect(processBackgroundSize(' Cover ')).toEqual(['cover']);
|
||||
expect(processBackgroundSize(' Auto ')).toEqual([{x: 'auto', y: 'auto'}]);
|
||||
});
|
||||
|
||||
it('should handle invalid mixed values', () => {
|
||||
@@ -214,18 +203,26 @@ describe('processBackgroundSize', () => {
|
||||
|
||||
// Test complex scenarios
|
||||
it('should handle complex multiple background scenario', () => {
|
||||
expect(
|
||||
processBackgroundSize('100px 200px, 50% auto, cover, contain'),
|
||||
).toEqual([{x: 100, y: 200}, {x: '50%', y: 'auto'}, 'cover', 'contain']);
|
||||
expect(processBackgroundSize('100px 200px, 50% auto, auto')).toEqual([
|
||||
{x: 100, y: 200},
|
||||
{x: '50%', y: 'auto'},
|
||||
{x: 'auto', y: 'auto'},
|
||||
]);
|
||||
});
|
||||
|
||||
it('should handle all valid single value combinations', () => {
|
||||
expect(processBackgroundSize('100px, 50%, auto, cover, contain')).toEqual([
|
||||
expect(processBackgroundSize('100px, 50%, auto')).toEqual([
|
||||
{x: 100, y: 'auto'},
|
||||
{x: '50%', y: 'auto'},
|
||||
{x: 'auto', y: 'auto'},
|
||||
'cover',
|
||||
'contain',
|
||||
]);
|
||||
});
|
||||
|
||||
it('should return empty array for unsupported cover keyword', () => {
|
||||
expect(processBackgroundSize('cover')).toEqual([]);
|
||||
});
|
||||
|
||||
it('should return empty array for unsupported contain keyword', () => {
|
||||
expect(processBackgroundSize('contain')).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -57,19 +57,14 @@ function parseBackgroundSizeCSSString(
|
||||
return [];
|
||||
}
|
||||
} else if (parts.length === 1) {
|
||||
const part = parts[0].toLowerCase();
|
||||
if (part === 'cover' || part === 'contain') {
|
||||
result.push(part);
|
||||
const x = getValidLengthPercentageSizeOrNull(parts[0].toLowerCase());
|
||||
if (x != null) {
|
||||
result.push({
|
||||
x,
|
||||
y: 'auto',
|
||||
});
|
||||
} else {
|
||||
const x = getValidLengthPercentageSizeOrNull(parts[0].toLowerCase());
|
||||
if (x != null) {
|
||||
result.push({
|
||||
x,
|
||||
y: 'auto',
|
||||
});
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
return [];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+5
-3
@@ -134,9 +134,11 @@ internal class BackgroundImageDrawable(
|
||||
// So we draw in reverse (last drawn in canvas appears closest)
|
||||
for (index in layers.indices.reversed()) {
|
||||
val backgroundImageLayer = layers[index]
|
||||
val size = backgroundSize?.let { it.getOrNull(index % it.size) }
|
||||
val repeat = backgroundRepeat?.let { it.getOrNull(index % it.size) }
|
||||
val position = backgroundPosition?.let { it.getOrNull(index % it.size) }
|
||||
val size = backgroundSize?.takeIf { it.isNotEmpty() }?.let { it.getOrNull(index % it.size) }
|
||||
val repeat =
|
||||
backgroundRepeat?.takeIf { it.isNotEmpty() }?.let { it.getOrNull(index % it.size) }
|
||||
val position =
|
||||
backgroundPosition?.takeIf { it.isNotEmpty() }?.let { it.getOrNull(index % it.size) }
|
||||
|
||||
// 2. Calculate the size of a single tile.
|
||||
val (tileWidth, tileHeight) =
|
||||
|
||||
+3
-3
@@ -167,7 +167,7 @@ public open class ReactViewManager : ReactClippingViewManager<ReactViewGroup>()
|
||||
backgroundSizes.add(parsedBackgroundSize)
|
||||
}
|
||||
}
|
||||
BackgroundStyleApplicator.setBackgroundSize(view, backgroundSizes)
|
||||
BackgroundStyleApplicator.setBackgroundSize(view, backgroundSizes.ifEmpty { null })
|
||||
}
|
||||
} else {
|
||||
BackgroundStyleApplicator.setBackgroundSize(view, null)
|
||||
@@ -186,7 +186,7 @@ public open class ReactViewManager : ReactClippingViewManager<ReactViewGroup>()
|
||||
backgroundPositions.add(parsedBackgroundPosition)
|
||||
}
|
||||
}
|
||||
BackgroundStyleApplicator.setBackgroundPosition(view, backgroundPositions)
|
||||
BackgroundStyleApplicator.setBackgroundPosition(view, backgroundPositions.ifEmpty { null })
|
||||
} else {
|
||||
BackgroundStyleApplicator.setBackgroundPosition(view, null)
|
||||
}
|
||||
@@ -205,7 +205,7 @@ public open class ReactViewManager : ReactClippingViewManager<ReactViewGroup>()
|
||||
backgroundRepeats.add(parsedBackgroundRepeat)
|
||||
}
|
||||
}
|
||||
BackgroundStyleApplicator.setBackgroundRepeat(view, backgroundRepeats)
|
||||
BackgroundStyleApplicator.setBackgroundRepeat(view, backgroundRepeats.ifEmpty { null })
|
||||
} else {
|
||||
BackgroundStyleApplicator.setBackgroundRepeat(view, null)
|
||||
}
|
||||
|
||||
@@ -361,7 +361,6 @@ exports.examples = [
|
||||
style={{
|
||||
experimental_backgroundImage:
|
||||
'linear-gradient(45deg, #667eea, #764ba2)',
|
||||
experimental_backgroundSize: 'cover',
|
||||
borderRadius: 20,
|
||||
}}
|
||||
testID="background-image-borders-1"
|
||||
@@ -373,7 +372,6 @@ exports.examples = [
|
||||
style={{
|
||||
experimental_backgroundImage:
|
||||
'radial-gradient(circle, #f093fb, #f5576c)',
|
||||
experimental_backgroundSize: 'cover',
|
||||
borderWidth: 10,
|
||||
borderColor: 'red',
|
||||
}}
|
||||
@@ -386,7 +384,6 @@ exports.examples = [
|
||||
style={{
|
||||
experimental_backgroundImage:
|
||||
'radial-gradient(circle, #f093fb, #f5576c)',
|
||||
experimental_backgroundSize: 'cover',
|
||||
borderTopLeftRadius: 10,
|
||||
borderTopRightRadius: 20,
|
||||
borderBottomLeftRadius: 30,
|
||||
@@ -401,7 +398,6 @@ exports.examples = [
|
||||
style={{
|
||||
experimental_backgroundImage:
|
||||
'radial-gradient(circle, #f093fb, #f5576c)',
|
||||
experimental_backgroundSize: 'cover',
|
||||
borderTopWidth: 10,
|
||||
borderTopColor: 'red',
|
||||
borderBottomWidth: 20,
|
||||
|
||||
Reference in new issue
Block a user