Skip to content

Commit 30f8898

Browse files
joevilchesfacebook-github-bot
authored andcommitted
Fix crash when link endOffset > line offset
Summary: I got notified that a recent change I made was causing a crash. This would happen when the endOffset of the link is larger than the end offset of the line. This can be repro'd with ``` /** * (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary. * * flow strict-local * format */ import {useState} from 'react'; import {Platform, StyleSheet, Text, View} from 'react-native'; export default function Playground() { const [state, setState] = useState(false); function toggle() { setState(old => !old); } return ( <View style={styles.container}> <Text style={[styles.paragraph, state && {backgroundColor: 'red'}]} numberOfLines={3}> <Text>Bacon Ipsum{'\n'}</Text> <Text>Dolor sit amet{'\n'}</Text> <Text>{'\n'}</Text> <Text accessibilityRole="link" onPress={toggle}> http://www.google.com </Text> </Text> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', paddingTop: 24, backgroundColor: '#ecf0f1', padding: 8, }, paragraph: { margin: 24, fontSize: 18, fontWeight: 'bold', textAlign: 'center', }, }); ``` This was encountered already by NickGerleman in D46206673 (facebook#37050) and the fix is fairly straightforward. I modified that fix to check for the endOffset as well. Changelog: [Internal] Differential Revision: D74823458
1 parent 2fa2c20 commit 30f8898

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,16 +224,17 @@ internal class ReactTextViewAccessibilityDelegate : ReactAccessibilityDelegate {
224224
// Ensure the link hasn't been ellipsized away; in such cases,
225225
// getPrimaryHorizontal will crash (and the link isn't rendered anyway).
226226
val startOffsetLineNumber = textViewLayout.getLineForOffset(startOffset)
227-
val lineEndOffset = textViewLayout.getLineEnd(startOffsetLineNumber)
228-
if (startOffset > lineEndOffset) {
227+
val startLineEndOffset = textViewLayout.getLineEnd(startOffsetLineNumber)
228+
val endOffsetLineNumber = textViewLayout.getLineForOffset(endOffset)
229+
val endLineEndOffset = textViewLayout.getLineEnd(endOffsetLineNumber)
230+
if (startOffset > startLineEndOffset || endOffset > endLineEndOffset) {
229231
return null
230232
}
231233

232234
val rootRect = Rect()
233235

234236
val startXCoordinates = textViewLayout.getPrimaryHorizontal(startOffset).toDouble()
235237

236-
val endOffsetLineNumber = textViewLayout.getLineForOffset(endOffset)
237238
val isMultiline = startOffsetLineNumber != endOffsetLineNumber
238239
textViewLayout.getLineBounds(startOffsetLineNumber, rootRect)
239240

0 commit comments

Comments
 (0)