From 21b3b53f93f607f2bf0029f60a791dc8de7a81ae Mon Sep 17 00:00:00 2001 From: Levi Bard Date: Tue, 15 Nov 2022 18:05:26 +0100 Subject: [PATCH] Make bare domain detection more robust in link markup logic (#2792) --- .../com/keylesspalace/tusky/util/LinkHelper.kt | 14 +++++--------- .../com/keylesspalace/tusky/util/LinkHelperTest.kt | 13 ++++++++----- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/util/LinkHelper.kt b/app/src/main/java/com/keylesspalace/tusky/util/LinkHelper.kt index 582a85e19..e4fea85a1 100644 --- a/app/src/main/java/com/keylesspalace/tusky/util/LinkHelper.kt +++ b/app/src/main/java/com/keylesspalace/tusky/util/LinkHelper.kt @@ -72,19 +72,15 @@ fun markupHiddenUrls(context: Context, content: CharSequence): SpannableStringBu val spannableContent = SpannableStringBuilder.valueOf(content) val originalSpans = spannableContent.getSpans(0, content.length, URLSpan::class.java) val obscuredLinkSpans = originalSpans.filter { - val text = spannableContent.subSequence(spannableContent.getSpanStart(it), spannableContent.getSpanEnd(it)) - val firstCharacter = text[0] + val start = spannableContent.getSpanStart(it) + val firstCharacter = content[start] return@filter if (firstCharacter == '#' || firstCharacter == '@') { false } else { - var textDomain = getDomain(text.toString()) + val text = spannableContent.subSequence(start, spannableContent.getSpanEnd(it)).toString() + var textDomain = getDomain(text) if (textDomain.isBlank()) { - // Allow "some.domain" or "www.some.domain" without a domain notifier - textDomain = if (text.startsWith("www.")) { - text.substring(4) - } else { - text.toString() - } + textDomain = getDomain("https://$text") } getDomain(it.url) != textDomain } diff --git a/app/src/test/java/com/keylesspalace/tusky/util/LinkHelperTest.kt b/app/src/test/java/com/keylesspalace/tusky/util/LinkHelperTest.kt index d7a61ccbe..6bff793ab 100644 --- a/app/src/test/java/com/keylesspalace/tusky/util/LinkHelperTest.kt +++ b/app/src/test/java/com/keylesspalace/tusky/util/LinkHelperTest.kt @@ -203,11 +203,14 @@ class LinkHelperTest { fun nonUriTextExactlyMatchingDomainIsNotMarkedUp() { val domain = "some.place" val content = SpannableStringBuilder() - .append(domain, URLSpan("https://some.place/"), 0) - .append(domain, URLSpan("https://some.place"), 0) - .append(domain, URLSpan("https://www.some.place"), 0) - .append("www.$domain", URLSpan("https://some.place"), 0) - .append("www.$domain", URLSpan("https://some.place/"), 0) + .append(domain, URLSpan("https://$domain/"), 0) + .append(domain, URLSpan("https://$domain"), 0) + .append(domain, URLSpan("https://www.$domain"), 0) + .append("www.$domain", URLSpan("https://$domain"), 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) Assert.assertFalse(markedUpContent.contains("🔗"))