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 0ad7e497..39489e4a 100644 --- a/app/src/main/java/com/keylesspalace/tusky/util/LinkHelper.kt +++ b/app/src/main/java/com/keylesspalace/tusky/util/LinkHelper.kt @@ -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") 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 54b17112..6264d86b 100644 --- a/app/src/test/java/com/keylesspalace/tusky/util/LinkHelperTest.kt +++ b/app/src/test/java/com/keylesspalace/tusky/util/LinkHelperTest.kt @@ -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()