Make URLSpans with trailing URL not marked up (#2813)

This commit is contained in:
kyori19 2022-11-17 02:28:00 +09:00 committed by GitHub
parent 3bc1fc4606
commit d1482324cc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 0 deletions

View file

@ -78,6 +78,7 @@ fun markupHiddenUrls(context: Context, content: CharSequence): SpannableStringBu
false
} else {
val text = spannableContent.subSequence(start, spannableContent.getSpanEnd(it)).toString()
.split(' ').lastOrNull() ?: ""
var textDomain = getDomain(text)
if (textDomain.isBlank()) {
textDomain = getDomain("https://$text")

View file

@ -218,6 +218,41 @@ class LinkHelperTest {
Assert.assertFalse(markedUpContent.contains("🔗"))
}
@Test
fun spanEndsWithUrlIsNotMarkedUp() {
val content = SpannableStringBuilder()
.append("Some Place: some.place", URLSpan("https://some.place"), 0)
.append("Some Place: some.place/", URLSpan("https://some.place/"), 0)
.append("Some Place - https://some.place", URLSpan("https://some.place"), 0)
.append("Some Place | https://some.place/", URLSpan("https://some.place/"), 0)
.append("Some Place https://some.place/path", URLSpan("https://some.place/path"), 0)
val markedUpContent = markupHiddenUrls(context, content)
Assert.assertFalse(markedUpContent.contains("🔗"))
}
@Test
fun spanEndsWithFraudulentUrlIsMarkedUp() {
val content = SpannableStringBuilder()
.append("Another Place: another.place", URLSpan("https://some.place"), 0)
.append("Another Place: another.place/", URLSpan("https://some.place/"), 0)
.append("Another Place - https://another.place", URLSpan("https://some.place"), 0)
.append("Another Place | https://another.place/", URLSpan("https://some.place/"), 0)
.append("Another Place https://another.place/path", URLSpan("https://some.place/path"), 0)
val markedUpContent = markupHiddenUrls(context, content)
val asserts = listOf(
"Another Place: another.place",
"Another Place: another.place/",
"Another Place - https://another.place",
"Another Place | https://another.place/",
"Another Place https://another.place/path",
)
asserts.forEach {
Assert.assertTrue(markedUpContent.contains(context.getString(R.string.url_domain_notifier, it, "some.place")))
}
}
@Test
fun validMentionsAreNotMarkedUp() {
val builder = SpannableStringBuilder()