Skip to content

Commit 47a9917

Browse files
hryhoriiK97claude
andauthored
Parse textDecorationLine without compiling a regex on Android (#59733)
TextAttributeProps.setTextDecorationLine split the value with "-".toRegex(), which compiles a java.util.regex.Pattern for every text fragment that has a textDecorationLine, each time the text is laid out. Splitting on the '-' character gives the same parts without a regex. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent b23e9a2 commit 47a9917

2 files changed

Lines changed: 19 additions & 5 deletions

File tree

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextAttributeProps.kt‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -283,11 +283,7 @@ public class TextAttributeProps private constructor() {
283283
isUnderlineTextDecorationSet = false
284284
isLineThroughTextDecorationSet = false
285285
if (textDecorationLineString != null) {
286-
for (textDecorationLineSubString in
287-
textDecorationLineString
288-
.split("-".toRegex())
289-
.dropLastWhile { it.isEmpty() }
290-
.toTypedArray()) {
286+
for (textDecorationLineSubString in textDecorationLineString.split('-')) {
291287
if ("underline" == textDecorationLineSubString) {
292288
isUnderlineTextDecorationSet = true
293289
} else if ("strikethrough" == textDecorationLineSubString) {

‎packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/text/TextAttributePropsTest.kt‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,24 @@ class TextAttributePropsTest {
198198
assertThat(textAttributes.lineHeight).isCloseTo(20f * DENSITY, within(0.001f))
199199
}
200200

201+
@Test
202+
fun mapBufferSetsTextDecorationLine() {
203+
assertThat(textDecorationLine("none")).isEqualTo(false to false)
204+
assertThat(textDecorationLine("underline")).isEqualTo(true to false)
205+
assertThat(textDecorationLine("strikethrough")).isEqualTo(false to true)
206+
assertThat(textDecorationLine("underline-strikethrough")).isEqualTo(true to true)
207+
}
208+
209+
/** Returns whether underline and line-through are set for the given textDecorationLine. */
210+
private fun textDecorationLine(value: String): Pair<Boolean, Boolean> {
211+
val textAttributes =
212+
TextAttributeProps.fromMapBuffer(
213+
WritableMapBuffer().put(TextAttributeProps.TA_KEY_TEXT_DECORATION_LINE, value),
214+
)
215+
return textAttributes.isUnderlineTextDecorationSet to
216+
textAttributes.isLineThroughTextDecorationSet
217+
}
218+
201219
private fun setFontScale(fontScale: Float) {
202220
DisplayMetricsHolder.setScreenDisplayMetrics(
203221
DisplayMetrics().apply {

0 commit comments

Comments
 (0)