Skip to content

Commit 2efcdc4

Browse files
fix: unsupported background size causing divide by zero crash on android
1 parent c90843d commit 2efcdc4

File tree

5 files changed

+12
-14
lines changed

5 files changed

+12
-14
lines changed

packages/react-native/Libraries/StyleSheet/StyleSheetTypes.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,8 +428,6 @@ export type BackgroundSizeValue =
428428
x: string | number;
429429
y: string | number;
430430
}
431-
| 'cover'
432-
| 'contain';
433431

434432
export type BackgroundRepeatKeyword =
435433
| 'repeat'

packages/react-native/Libraries/StyleSheet/StyleSheetTypes.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -784,8 +784,6 @@ export type BackgroundSizeValue =
784784
x: string | number,
785785
y: string | number,
786786
}
787-
| 'cover'
788-
| 'contain';
789787

790788
export type BackgroundRepeatKeyword =
791789
| 'repeat'

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/drawable/BackgroundImageDrawable.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,12 @@ internal class BackgroundImageDrawable(
134134
// So we draw in reverse (last drawn in canvas appears closest)
135135
for (index in layers.indices.reversed()) {
136136
val backgroundImageLayer = layers[index]
137-
val size = backgroundSize?.let { it.getOrNull(index % it.size) }
138-
val repeat = backgroundRepeat?.let { it.getOrNull(index % it.size) }
139-
val position = backgroundPosition?.let { it.getOrNull(index % it.size) }
137+
val size =
138+
backgroundSize?.takeIf { it.isNotEmpty() }?.let { it.getOrNull(index % it.size) }
139+
val repeat =
140+
backgroundRepeat?.takeIf { it.isNotEmpty() }?.let { it.getOrNull(index % it.size) }
141+
val position =
142+
backgroundPosition?.takeIf { it.isNotEmpty() }?.let { it.getOrNull(index % it.size) }
140143

141144
// 2. Calculate the size of a single tile.
142145
val (tileWidth, tileHeight) =

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewManager.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ public open class ReactViewManager : ReactClippingViewManager<ReactViewGroup>()
167167
backgroundSizes.add(parsedBackgroundSize)
168168
}
169169
}
170-
BackgroundStyleApplicator.setBackgroundSize(view, backgroundSizes)
170+
BackgroundStyleApplicator.setBackgroundSize(
171+
view, backgroundSizes.ifEmpty { null })
171172
}
172173
} else {
173174
BackgroundStyleApplicator.setBackgroundSize(view, null)
@@ -186,7 +187,8 @@ public open class ReactViewManager : ReactClippingViewManager<ReactViewGroup>()
186187
backgroundPositions.add(parsedBackgroundPosition)
187188
}
188189
}
189-
BackgroundStyleApplicator.setBackgroundPosition(view, backgroundPositions)
190+
BackgroundStyleApplicator.setBackgroundPosition(
191+
view, backgroundPositions.ifEmpty { null })
190192
} else {
191193
BackgroundStyleApplicator.setBackgroundPosition(view, null)
192194
}
@@ -205,7 +207,8 @@ public open class ReactViewManager : ReactClippingViewManager<ReactViewGroup>()
205207
backgroundRepeats.add(parsedBackgroundRepeat)
206208
}
207209
}
208-
BackgroundStyleApplicator.setBackgroundRepeat(view, backgroundRepeats)
210+
BackgroundStyleApplicator.setBackgroundRepeat(
211+
view, backgroundRepeats.ifEmpty { null })
209212
} else {
210213
BackgroundStyleApplicator.setBackgroundRepeat(view, null)
211214
}

packages/rn-tester/js/examples/BackgroundImage/BackgroundImageExample.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,6 @@ exports.examples = [
361361
style={{
362362
experimental_backgroundImage:
363363
'linear-gradient(45deg, #667eea, #764ba2)',
364-
experimental_backgroundSize: 'cover',
365364
borderRadius: 20,
366365
}}
367366
testID="background-image-borders-1"
@@ -373,7 +372,6 @@ exports.examples = [
373372
style={{
374373
experimental_backgroundImage:
375374
'radial-gradient(circle, #f093fb, #f5576c)',
376-
experimental_backgroundSize: 'cover',
377375
borderWidth: 10,
378376
borderColor: 'red',
379377
}}
@@ -386,7 +384,6 @@ exports.examples = [
386384
style={{
387385
experimental_backgroundImage:
388386
'radial-gradient(circle, #f093fb, #f5576c)',
389-
experimental_backgroundSize: 'cover',
390387
borderTopLeftRadius: 10,
391388
borderTopRightRadius: 20,
392389
borderBottomLeftRadius: 30,
@@ -401,7 +398,6 @@ exports.examples = [
401398
style={{
402399
experimental_backgroundImage:
403400
'radial-gradient(circle, #f093fb, #f5576c)',
404-
experimental_backgroundSize: 'cover',
405401
borderTopWidth: 10,
406402
borderTopColor: 'red',
407403
borderBottomWidth: 20,

0 commit comments

Comments
 (0)