Make bare domain detection more robust in link markup logic (#2792)

This commit is contained in:
Levi Bard 2022-11-15 18:05:26 +01:00 committed by GitHub
parent 2d50ab6b5c
commit 21b3b53f93
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 14 deletions

View File

@ -72,19 +72,15 @@ fun markupHiddenUrls(context: Context, content: CharSequence): SpannableStringBu
val spannableContent = SpannableStringBuilder.valueOf(content) val spannableContent = SpannableStringBuilder.valueOf(content)
val originalSpans = spannableContent.getSpans(0, content.length, URLSpan::class.java) val originalSpans = spannableContent.getSpans(0, content.length, URLSpan::class.java)
val obscuredLinkSpans = originalSpans.filter { val obscuredLinkSpans = originalSpans.filter {
val text = spannableContent.subSequence(spannableContent.getSpanStart(it), spannableContent.getSpanEnd(it)) val start = spannableContent.getSpanStart(it)
val firstCharacter = text[0] val firstCharacter = content[start]
return@filter if (firstCharacter == '#' || firstCharacter == '@') { return@filter if (firstCharacter == '#' || firstCharacter == '@') {
false false
} else { } else {
var textDomain = getDomain(text.toString()) val text = spannableContent.subSequence(start, spannableContent.getSpanEnd(it)).toString()
var textDomain = getDomain(text)
if (textDomain.isBlank()) { if (textDomain.isBlank()) {
// Allow "some.domain" or "www.some.domain" without a domain notifier textDomain = getDomain("https://$text")
textDomain = if (text.startsWith("www.")) {
text.substring(4)
} else {
text.toString()
}
} }
getDomain(it.url) != textDomain getDomain(it.url) != textDomain
} }

View File

@ -203,11 +203,14 @@ class LinkHelperTest {
fun nonUriTextExactlyMatchingDomainIsNotMarkedUp() { fun nonUriTextExactlyMatchingDomainIsNotMarkedUp() {
val domain = "some.place" val domain = "some.place"
val content = SpannableStringBuilder() val content = SpannableStringBuilder()
.append(domain, URLSpan("https://some.place/"), 0) .append(domain, URLSpan("https://$domain/"), 0)
.append(domain, URLSpan("https://some.place"), 0) .append(domain, URLSpan("https://$domain"), 0)
.append(domain, URLSpan("https://www.some.place"), 0) .append(domain, URLSpan("https://www.$domain"), 0)
.append("www.$domain", URLSpan("https://some.place"), 0) .append("www.$domain", URLSpan("https://$domain"), 0)
.append("www.$domain", URLSpan("https://some.place/"), 0) .append("www.$domain", URLSpan("https://$domain/"), 0)
.append("$domain/", URLSpan("https://$domain/"), 0)
.append("$domain/", URLSpan("https://$domain"), 0)
.append("$domain/", URLSpan("https://www.$domain"), 0)
val markedUpContent = markupHiddenUrls(context, content) val markedUpContent = markupHiddenUrls(context, content)
Assert.assertFalse(markedUpContent.contains("🔗")) Assert.assertFalse(markedUpContent.contains("🔗"))