Fix linkify helper index out of bounds with parenthesis (#6140)
When parenthesis were mismatched by having more opening parenthesis than closing ones, we could end up incorrectly making the URLSpan `end` value greater than the actual text length, causing an `IndexOutOfBoundsException`
This commit is contained in:
committed by
GitHub
parent
2e1153cd7b
commit
a805e0ef6d
@@ -97,7 +97,7 @@ object LinkifyHelper {
|
||||
val closingParenthesisCount = linkifiedTextLastPath.count { it == ')' }
|
||||
val openingParenthesisCount = linkifiedTextLastPath.count { it == '(' }
|
||||
// If it's not part of a pair, remove it from the link span by adjusting the end index
|
||||
end -= closingParenthesisCount - openingParenthesisCount
|
||||
end -= (closingParenthesisCount - openingParenthesisCount).coerceAtLeast(0)
|
||||
}
|
||||
return end
|
||||
}
|
||||
|
||||
@@ -122,4 +122,22 @@ class LinkifierHelperTest {
|
||||
assertThat(urlSpans.size).isEqualTo(1)
|
||||
assertThat(urlSpans.first().url).isEqualTo("https://github.com/element-hq/element-android/READ(ME)")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `linkification handles mismatched opening parenthesis in URL`() {
|
||||
val text = "A url: (https://github.com/element-hq/element-android/READ((((((ME))"
|
||||
val result = LinkifyHelper.linkify(text)
|
||||
val urlSpans = result.toSpannable().getSpans<URLSpan>()
|
||||
assertThat(urlSpans.size).isEqualTo(1)
|
||||
assertThat(urlSpans.first().url).isEqualTo("https://github.com/element-hq/element-android/READ((((((ME))")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `linkification handles mismatched closing parenthesis in URL`() {
|
||||
val text = "A url: (https://github.com/element-hq/element-android/READ(ME)))))"
|
||||
val result = LinkifyHelper.linkify(text)
|
||||
val urlSpans = result.toSpannable().getSpans<URLSpan>()
|
||||
assertThat(urlSpans.size).isEqualTo(1)
|
||||
assertThat(urlSpans.first().url).isEqualTo("https://github.com/element-hq/element-android/READ(ME)")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user