From 5655fcf9733e1594c5e9abe2b144520aa3356823 Mon Sep 17 00:00:00 2001 From: Greyson Parrelli Date: Tue, 12 May 2026 10:05:38 -0400 Subject: [PATCH] Create new linkifier. Co-authored-by: Cody Henthorne --- .../v2/items/V2ConversationItemUtils.kt | 18 +- .../groups/v2/GroupDescriptionUtil.java | 26 +- .../linkpreview/LinkPreviewUtil.java | 19 +- .../logsubmit/SubmitDebugLogActivity.java | 16 +- .../securesms/nicknames/ViewNoteSheet.kt | 17 +- .../viewer/page/StoryViewerPageFragment.kt | 24 +- .../securesms/util/Linkification.kt | 57 +++ .../securesms/util/PlaceholderURLSpan.kt | 6 +- .../securesms/util/RemoteConfig.kt | 11 + .../java/org/signal/core/util/Linkifier.kt | 386 ++++++++++++++++++ .../org/signal/core/util/LinkifierTest.kt | 198 +++++++++ .../core/util/LinkifierSpannableExtensions.kt | 78 ++++ .../util/LinkifierSpannableExtensionsTest.kt | 155 +++++++ 13 files changed, 937 insertions(+), 74 deletions(-) create mode 100644 app/src/main/java/org/thoughtcrime/securesms/util/Linkification.kt create mode 100644 core/util-jvm/src/main/java/org/signal/core/util/Linkifier.kt create mode 100644 core/util-jvm/src/test/java/org/signal/core/util/LinkifierTest.kt create mode 100644 core/util/src/main/java/org/signal/core/util/LinkifierSpannableExtensions.kt create mode 100644 core/util/src/test/java/org/signal/core/util/LinkifierSpannableExtensionsTest.kt diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/items/V2ConversationItemUtils.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/items/V2ConversationItemUtils.kt index 7a395978a3..df6a932b88 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/items/V2ConversationItemUtils.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/items/V2ConversationItemUtils.kt @@ -14,6 +14,7 @@ import androidx.core.text.util.LinkifyCompat import org.thoughtcrime.securesms.database.model.MessageRecord import org.thoughtcrime.securesms.util.InterceptableLongClickCopyLinkSpan import org.thoughtcrime.securesms.util.LinkUtil +import org.thoughtcrime.securesms.util.Linkification import org.thoughtcrime.securesms.util.UrlClickHandler import org.thoughtcrime.securesms.util.hasOnlyThumbnail @@ -28,24 +29,21 @@ object V2ConversationItemUtils { @JvmStatic fun linkifyUrlLinks(messageBody: Spannable, shouldLinkifyAllLinks: Boolean, urlClickHandler: UrlClickHandler) { - val linkPattern = Linkify.WEB_URLS or Linkify.EMAIL_ADDRESSES or Linkify.PHONE_NUMBERS - val hasLinks = LinkifyCompat.addLinks(messageBody, if (shouldLinkifyAllLinks) linkPattern else 0) - - if (!hasLinks) { + if (!shouldLinkifyAllLinks) { return } - messageBody.getSpans(0, messageBody.length, URLSpan::class.java) - .filterNot { LinkUtil.isLegalUrl(it.url) } - .forEach(messageBody::removeSpan) + LinkifyCompat.addLinks(messageBody, Linkify.EMAIL_ADDRESSES or Linkify.PHONE_NUMBERS) + Linkification.applyWebUrlSpans(messageBody) messageBody.getSpans(0, messageBody.length, URLSpan::class.java).forEach { urlSpan -> + val url = urlSpan.url val start = messageBody.getSpanStart(urlSpan) val end = messageBody.getSpanEnd(urlSpan) - val span = InterceptableLongClickCopyLinkSpan(urlSpan.url, urlClickHandler) - - messageBody.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) messageBody.removeSpan(urlSpan) + if (LinkUtil.isLegalUrl(url)) { + messageBody.setSpan(InterceptableLongClickCopyLinkSpan(url, urlClickHandler), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) + } } } } diff --git a/app/src/main/java/org/thoughtcrime/securesms/groups/v2/GroupDescriptionUtil.java b/app/src/main/java/org/thoughtcrime/securesms/groups/v2/GroupDescriptionUtil.java index 33eef9ea30..974901600b 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/groups/v2/GroupDescriptionUtil.java +++ b/app/src/main/java/org/thoughtcrime/securesms/groups/v2/GroupDescriptionUtil.java @@ -15,11 +15,10 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.core.text.util.LinkifyCompat; -import java.util.stream.Stream; - import org.thoughtcrime.securesms.R; import org.thoughtcrime.securesms.components.emoji.EmojiTextView; import org.thoughtcrime.securesms.util.LinkUtil; +import org.thoughtcrime.securesms.util.Linkification; import org.thoughtcrime.securesms.util.LongClickCopySpan; public final class GroupDescriptionUtil { @@ -38,21 +37,16 @@ public final class GroupDescriptionUtil { SpannableString descriptionSpannable = new SpannableString(scrubbedDescription); if (linkify) { - int linkPattern = Linkify.WEB_URLS | Linkify.EMAIL_ADDRESSES | Linkify.PHONE_NUMBERS; - boolean hasLinks = LinkifyCompat.addLinks(descriptionSpannable, linkPattern); + LinkifyCompat.addLinks(descriptionSpannable, Linkify.EMAIL_ADDRESSES | Linkify.PHONE_NUMBERS); + Linkification.applyWebUrlSpans(descriptionSpannable); - if (hasLinks) { - Stream.of(descriptionSpannable.getSpans(0, descriptionSpannable.length(), URLSpan.class)) - .filter(url -> !LinkUtil.isLegalUrl(url.getURL())) - .forEach(descriptionSpannable::removeSpan); - - URLSpan[] urlSpans = descriptionSpannable.getSpans(0, descriptionSpannable.length(), URLSpan.class); - - for (URLSpan urlSpan : urlSpans) { - int start = descriptionSpannable.getSpanStart(urlSpan); - int end = descriptionSpannable.getSpanEnd(urlSpan); - URLSpan span = new LongClickCopySpan(urlSpan.getURL()); - descriptionSpannable.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + for (URLSpan urlSpan : descriptionSpannable.getSpans(0, descriptionSpannable.length(), URLSpan.class)) { + String url = urlSpan.getURL(); + int start = descriptionSpannable.getSpanStart(urlSpan); + int end = descriptionSpannable.getSpanEnd(urlSpan); + descriptionSpannable.removeSpan(urlSpan); + if (LinkUtil.isLegalUrl(url)) { + descriptionSpannable.setSpan(new LongClickCopySpan(url), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } } } diff --git a/app/src/main/java/org/thoughtcrime/securesms/linkpreview/LinkPreviewUtil.java b/app/src/main/java/org/thoughtcrime/securesms/linkpreview/LinkPreviewUtil.java index dd544b4f68..fc7ad779ba 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/linkpreview/LinkPreviewUtil.java +++ b/app/src/main/java/org/thoughtcrime/securesms/linkpreview/LinkPreviewUtil.java @@ -1,19 +1,17 @@ package org.thoughtcrime.securesms.linkpreview; import android.annotation.SuppressLint; -import android.text.SpannableString; -import android.text.style.URLSpan; -import android.text.util.Linkify; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.core.text.HtmlCompat; -import androidx.core.text.util.LinkifyCompat; import java.util.stream.Collectors; +import org.signal.core.util.Linkifier; import org.thoughtcrime.securesms.util.DateUtils; import org.thoughtcrime.securesms.util.LinkUtil; +import org.thoughtcrime.securesms.util.Linkification; import org.signal.core.util.Util; import org.whispersystems.signalservice.api.util.OptionalUtil; @@ -53,16 +51,15 @@ public final class LinkPreviewUtil { * @return All URLs allowed as previews in the source text. */ public static @NonNull Links findValidPreviewUrls(@NonNull String text) { - SpannableString spannable = new SpannableString(text); - boolean found = LinkifyCompat.addLinks(spannable, Linkify.WEB_URLS); - - if (!found) { + List detected = Linkification.findWebLinks(text); + if (detected.isEmpty()) { return Links.EMPTY; } - return new Links(Stream.of(spannable.getSpans(0, spannable.length(), URLSpan.class)) - .map(span -> new Link(span.getURL(), spannable.getSpanStart(span))) - .filter(link -> LinkUtil.isValidPreviewUrl(link.url)).collect(Collectors.toList())); + return new Links(detected.stream() + .map(d -> new Link(d.getUrl(), d.getStart())) + .filter(link -> LinkUtil.isValidPreviewUrl(link.url)) + .collect(Collectors.toList())); } public static @NonNull OpenGraph parseOpenGraphFields(@Nullable String html) { diff --git a/app/src/main/java/org/thoughtcrime/securesms/logsubmit/SubmitDebugLogActivity.java b/app/src/main/java/org/thoughtcrime/securesms/logsubmit/SubmitDebugLogActivity.java index ff3ca5e734..9edf5d2b3e 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/logsubmit/SubmitDebugLogActivity.java +++ b/app/src/main/java/org/thoughtcrime/securesms/logsubmit/SubmitDebugLogActivity.java @@ -7,8 +7,6 @@ import android.net.Uri; import android.os.Bundle; import android.text.SpannableString; import android.text.Spanned; -import android.text.style.URLSpan; -import android.text.util.Linkify; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -23,11 +21,11 @@ import androidx.appcompat.app.AlertDialog; import androidx.core.app.ActivityCompat; import androidx.core.app.ShareCompat; import androidx.core.content.ContextCompat; -import androidx.core.text.util.LinkifyCompat; import androidx.lifecycle.ViewModelProvider; import com.google.android.material.dialog.MaterialAlertDialogBuilder; +import org.signal.core.util.Linkifier; import org.signal.core.util.ThreadUtil; import org.signal.debuglogsviewer.DebugLogsViewer; import org.thoughtcrime.securesms.BaseActivity; @@ -36,6 +34,7 @@ import org.thoughtcrime.securesms.components.ConversationSearchBottomBar; import org.thoughtcrime.securesms.components.ProgressCard; import org.thoughtcrime.securesms.components.SearchView; import org.thoughtcrime.securesms.util.DynamicTheme; +import org.thoughtcrime.securesms.util.Linkification; import org.thoughtcrime.securesms.util.LongClickCopySpan; import org.thoughtcrime.securesms.util.LongClickMovementMethod; import org.signal.core.ui.util.ThemeUtil; @@ -449,15 +448,8 @@ public class SubmitDebugLogActivity extends BaseActivity { TextView dialogView = new TextView(builder.getContext()); LongClickCopySpan longClickUrl = new LongClickCopySpan(url); - - LinkifyCompat.addLinks(spannableDialogText, Linkify.WEB_URLS); - - URLSpan[] spans = spannableDialogText.getSpans(0, spannableDialogText.length(), URLSpan.class); - for (URLSpan span : spans) { - int start = spannableDialogText.getSpanStart(span); - int end = spannableDialogText.getSpanEnd(span); - - spannableDialogText.setSpan(longClickUrl, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + for (Linkifier.DetectedLink link : Linkification.findWebLinks(dialogText)) { + spannableDialogText.setSpan(longClickUrl, link.getStart(), link.getEnd(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } dialogView.setText(spannableDialogText); diff --git a/app/src/main/java/org/thoughtcrime/securesms/nicknames/ViewNoteSheet.kt b/app/src/main/java/org/thoughtcrime/securesms/nicknames/ViewNoteSheet.kt index def0c6fb17..c95227ae9c 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/nicknames/ViewNoteSheet.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/nicknames/ViewNoteSheet.kt @@ -6,6 +6,7 @@ package org.thoughtcrime.securesms.nicknames import android.os.Bundle +import android.text.SpannableString import android.text.util.Linkify import androidx.activity.result.ActivityResultLauncher import androidx.compose.foundation.layout.Column @@ -40,6 +41,7 @@ import org.signal.core.util.getParcelableCompat import org.thoughtcrime.securesms.R import org.thoughtcrime.securesms.components.emoji.EmojiTextView import org.thoughtcrime.securesms.recipients.RecipientId +import org.thoughtcrime.securesms.util.Linkification import org.thoughtcrime.securesms.util.viewModel import org.signal.core.ui.R as CoreUiR @@ -142,11 +144,7 @@ private fun ViewNoteBottomSheetContent( ) ) - val mask = if (LocalInspectionMode.current) { - Linkify.WEB_URLS - } else { - Linkify.WEB_URLS or Linkify.EMAIL_ADDRESSES or Linkify.PHONE_NUMBERS - } + val isInspection = LocalInspectionMode.current AndroidView( factory = { context -> @@ -161,9 +159,12 @@ private fun ViewNoteBottomSheetContent( .fillMaxWidth() .padding(bottom = 48.dp) ) { - it.text = note - - LinkifyCompat.addLinks(it, mask) + val spannable = SpannableString(note) + if (!isInspection) { + LinkifyCompat.addLinks(spannable, Linkify.EMAIL_ADDRESSES or Linkify.PHONE_NUMBERS) + } + Linkification.applyWebUrlSpans(spannable) + it.text = spannable } } } diff --git a/app/src/main/java/org/thoughtcrime/securesms/stories/viewer/page/StoryViewerPageFragment.kt b/app/src/main/java/org/thoughtcrime/securesms/stories/viewer/page/StoryViewerPageFragment.kt index 90f5f99389..886faf3797 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/stories/viewer/page/StoryViewerPageFragment.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/stories/viewer/page/StoryViewerPageFragment.kt @@ -94,6 +94,7 @@ import org.thoughtcrime.securesms.util.AvatarUtil import org.thoughtcrime.securesms.util.DateUtils import org.thoughtcrime.securesms.util.Debouncer import org.thoughtcrime.securesms.util.LinkUtil +import org.thoughtcrime.securesms.util.Linkification import org.thoughtcrime.securesms.util.LongClickCopySpan import org.thoughtcrime.securesms.util.LongClickMovementMethod import org.thoughtcrime.securesms.util.Projection @@ -993,20 +994,16 @@ class StoryViewerPageFragment : } fun linkifyUrlLinks(spannable: Spannable) { - val hasLinks = LinkifyCompat.addLinks(spannable, CAPTION_LINK_PATTERN) + LinkifyCompat.addLinks(spannable, Linkify.EMAIL_ADDRESSES or Linkify.PHONE_NUMBERS) + Linkification.applyWebUrlSpans(spannable) - if (hasLinks) { - spannable.getSpans(0, spannable.length, URLSpan::class.java) - .filterNot { url -> LinkUtil.isLegalUrl(url.url) } - .forEach { spannable.removeSpan(it) } - - val urlSpans = spannable.getSpans(0, spannable.length, URLSpan::class.java) - - for (urlSpan in urlSpans) { - val start = spannable.getSpanStart(urlSpan) - val end = spannable.getSpanEnd(urlSpan) - val span = LongClickCopySpan(urlSpan.url) - spannable.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) + spannable.getSpans(0, spannable.length, URLSpan::class.java).forEach { urlSpan -> + val url = urlSpan.url + val start = spannable.getSpanStart(urlSpan) + val end = spannable.getSpanEnd(urlSpan) + spannable.removeSpan(urlSpan) + if (LinkUtil.isLegalUrl(url)) { + spannable.setSpan(LongClickCopySpan(url), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) } } } @@ -1315,7 +1312,6 @@ class StoryViewerPageFragment : private val ONBOARDING_DURATION = TimeUnit.SECONDS.toMillis(10) private const val SMALL_CAPTION_TEXT_MAX_LENGTH = 280 private const val SMALL_CAPTION_TEXT_MAX_LINES = 5 - private const val CAPTION_LINK_PATTERN = Linkify.WEB_URLS or Linkify.EMAIL_ADDRESSES or Linkify.PHONE_NUMBERS private const val ARGS = "args" diff --git a/app/src/main/java/org/thoughtcrime/securesms/util/Linkification.kt b/app/src/main/java/org/thoughtcrime/securesms/util/Linkification.kt new file mode 100644 index 0000000000..9f6edf3cdb --- /dev/null +++ b/app/src/main/java/org/thoughtcrime/securesms/util/Linkification.kt @@ -0,0 +1,57 @@ +/* + * Copyright 2026 Signal Messenger, LLC + * SPDX-License-Identifier: AGPL-3.0-only + */ + +package org.thoughtcrime.securesms.util + +import android.text.Spannable +import android.text.SpannableString +import android.text.style.URLSpan +import android.text.util.Linkify +import androidx.core.text.util.LinkifyCompat +import org.signal.core.util.Linkifier +import org.signal.core.util.Linkifier.DetectedLink +import org.signal.core.util.addDetectedLinks + +/** + * Temporary abstraction while we switch over to the new [Linkifier] via remote config. + * When the remote config is off, we fallback to the pre-existing android link logic. + */ +object Linkification { + + /** + * Adds [URLSpan]s for web URLs in [spannable]. Returns `true` if at least one span was added. + */ + @JvmStatic + fun applyWebUrlSpans(spannable: Spannable): Boolean { + return if (RemoteConfig.useNewLinkifier) { + spannable.addDetectedLinks() + } else { + LinkifyCompat.addLinks(spannable, Linkify.WEB_URLS) + } + } + + /** + * Finds web URLs in [text]. + */ + @JvmStatic + fun findWebLinks(text: CharSequence): List { + if (RemoteConfig.useNewLinkifier) { + return Linkifier.findLinks(text) + } + + val spannable = SpannableString(text) + if (!LinkifyCompat.addLinks(spannable, Linkify.WEB_URLS)) { + return emptyList() + } + + return spannable.getSpans(0, spannable.length, URLSpan::class.java).map { span -> + DetectedLink( + start = spannable.getSpanStart(span), + end = spannable.getSpanEnd(span), + url = span.url + ) + } + } +} diff --git a/app/src/main/java/org/thoughtcrime/securesms/util/PlaceholderURLSpan.kt b/app/src/main/java/org/thoughtcrime/securesms/util/PlaceholderURLSpan.kt index a48f73ddb1..9f73f10db5 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/util/PlaceholderURLSpan.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/util/PlaceholderURLSpan.kt @@ -1,8 +1,8 @@ package org.thoughtcrime.securesms.util /** - * LinkifyCompat.addLinks() will strip pre-existing URLSpans. This acts as a way to - * indicate where a link should be added without being stripped. The consumer is - * responsible for replacing the placeholder with an actual URLSpan. + * Marks a region where a link should later be applied, without storing it as an actual [URLSpan]. + * The consumer is responsible for replacing the placeholder with an appropriate URLSpan when the + * text is rendered. */ class PlaceholderURLSpan(url: String) : android.text.Annotation("placeholderUrl", url) diff --git a/app/src/main/java/org/thoughtcrime/securesms/util/RemoteConfig.kt b/app/src/main/java/org/thoughtcrime/securesms/util/RemoteConfig.kt index 086464ec53..e41e11ce0b 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/util/RemoteConfig.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/util/RemoteConfig.kt @@ -1365,5 +1365,16 @@ object RemoteConfig { hotSwappable = true ) + /** + * Whether to use our custom [org.signal.core.util.Linkifier] for web URL detection. + */ + @JvmStatic + @get:JvmName("useNewLinkifier") + val useNewLinkifier: Boolean by remoteBoolean( + key = "android.useNewLinkifier", + defaultValue = false, + hotSwappable = true + ) + // endregion } diff --git a/core/util-jvm/src/main/java/org/signal/core/util/Linkifier.kt b/core/util-jvm/src/main/java/org/signal/core/util/Linkifier.kt new file mode 100644 index 0000000000..14acc78153 --- /dev/null +++ b/core/util-jvm/src/main/java/org/signal/core/util/Linkifier.kt @@ -0,0 +1,386 @@ +/* + * Copyright 2026 Signal Messenger, LLC + * SPDX-License-Identifier: AGPL-3.0-only + */ + +package org.signal.core.util + +import java.net.IDN +import java.util.regex.Pattern + +/** + * Detects links in text. + * + * This was created to patch up some of the shoddy link detection in Android's built-in linkifier. + * Things like trailing `-` not being included in a URL in certain cases, which can break group links. + * + * Note that if you want to do additional filtering (like requiring https for link previews), you still + * need to do that at a higher level. + */ +object Linkifier { + + /** Punctuation we trim from the end of a candidate URL */ + private val TRAILING_PUNCTUATION_TO_TRIM = setOf( + '.', ',', '!', '?', ';', ':', '\'', '"', '`', '|', '<', '>' + ) + + /** Closing brackets that are trimmed only if the URL has no matching opener inside */ + private val CLOSING_BRACKETS = mapOf(')' to '(', ']' to '[', '}' to '{') + + /** + * Characters we treat as definitely-not-part-of-a-URL when extending past the host. Excluding + * commas/semicolons here lets `https://a.com,https://b.com` resolve to two URLs rather than one. + */ + private const val URL_CHAR = + "[^\\s\\u0085\\u00A0\\u1680\\u2000-\\u200D\\u2028\\u2029\\u202A-\\u202F\\u205F\\u2066-\\u2069\\u3000\\uFEFF<>\"'`,;|\\\\]" + + /** + * A single domain label: letter/digit, optional letter/digit/hyphen body. Used for intermediate + * labels. Note that the TLD validity is enforced separately by [TOP_LEVEL_DOMAINS]. + */ + private const val DOMAIN_LABEL = "[\\p{L}\\p{N}][\\p{L}\\p{N}\\-]*" + + /** + * Like [DOMAIN_LABEL] but disallows a trailing hyphen, matching RFC 1035. Used in the TLD slot of + * the bare-domain variant so that text like `signal.com-` doesn't get rejected because the TLD + * lookup sees `com-` instead of `com`. + */ + private const val TLD_LABEL = "[\\p{L}\\p{N}](?:[\\p{L}\\p{N}\\-]*[\\p{L}\\p{N}])?" + + /** + * Match http://..., https://..., www....., or a bare `domain.tld` form. + * Note that TLD validity is enforced separately by [TOP_LEVEL_DOMAINS]. + */ + private val WEB_URL_PATTERN: Pattern = Pattern.compile( + "(?i)" + + "(?:" + + // Variant 1: explicit scheme (http or https). User intent is unambiguous; no TLD check. + "https?://" + URL_CHAR + "+" + + "|" + + // Variant 2: starts with www. — also unambiguous; no TLD check. + "www\\." + DOMAIN_LABEL + "(?:\\." + DOMAIN_LABEL + ")+" + + "(?:[/?#]" + URL_CHAR + "*)?" + + "|" + + // Variant 3: bare `domain.tld`. The last label is post-validated against the IANA TLD set. + DOMAIN_LABEL + "(?:\\." + DOMAIN_LABEL + ")*\\." + TLD_LABEL + + "(?:[/?#]" + URL_CHAR + "*)?" + + ")" + ) + + /** + * Finds all web URLs in [text], in left-to-right order. + */ + @JvmStatic + fun findLinks(text: CharSequence): List { + if (text.isEmpty()) { + return emptyList() + } + + val matcher = WEB_URL_PATTERN.matcher(text) + val out = ArrayList() + while (matcher.find()) { + val start = matcher.start() + val rawEnd = matcher.end() + + if (!isAtLeadingBoundary(text, start)) { + continue + } + + val end = trimTrailingNonUrlChars(text, start, rawEnd) + if (end <= start) { + continue + } + + // Reject if the URL is butted up against a bidi/format/zero-width char on either side — + // those are used for visual spoofing and shouldn't appear adjacent to a real URL. + if (end < text.length && isFormatOrZeroWidth(text[end])) { + continue + } + + val raw = text.subSequence(start, end).toString() + if (!isAcceptableCandidate(raw)) { + continue + } + + val normalized = if (raw.contains("://")) raw else "http://$raw" + out.add(DetectedLink(start, end, normalized)) + } + return out + } + + private fun isAtLeadingBoundary(text: CharSequence, start: Int): Boolean { + if (start == 0) { + return true + } + + val prev = text[start - 1] + + // Check @ so that we don't pick up the host of an email address + if (prev.isLetterOrDigit() || prev == '_' || prev == '@') { + return false + } + + if (isFormatOrZeroWidth(prev)) { + return false + } + + return true + } + + private fun isFormatOrZeroWidth(c: Char): Boolean { + return when (c) { + '​', '‌', '‍', '' -> true // zero-width space / non-joiner / joiner / BOM + in '‪'..'‮' -> true // bidi formatting overrides + in '⁦'..'⁩' -> true // bidi isolate controls + else -> false + } + } + + /** + * Strips trailing characters that are very likely not part of the URL (sentence-ending + * punctuation, unmatched closing brackets, etc.). Note that '-' is intentionally left alone — + * that's the bug fix this class is built around. + */ + private fun trimTrailingNonUrlChars(text: CharSequence, start: Int, initialEnd: Int): Int { + var end = initialEnd + while (end > start) { + val c = text[end - 1] + if (c in TRAILING_PUNCTUATION_TO_TRIM) { + end-- + continue + } + + val opener = CLOSING_BRACKETS[c] + if (opener != null) { + val opens = countChar(text, start, end - 1, opener) + val closes = countChar(text, start, end - 1, c) + if (closes >= opens) { + end-- + continue + } + } + + break + } + return end + } + + private fun countChar(text: CharSequence, start: Int, end: Int, target: Char): Int { + var count = 0 + var i = start + while (i < end) { + if (text[i] == target) count++ + i++ + } + return count + } + + private fun isAcceptableCandidate(candidate: String): Boolean { + // If the user has specified a scheme, we're much more lenient with url detection, + // since scheme implies the user really wants it to be a link. + val schemeIndex = candidate.indexOf("://") + if (schemeIndex >= 0) { + val rest = candidate.substring(schemeIndex + 3) + val host = hostPortion(rest) + return host.isNotEmpty() + } + + if (candidate.startsWith("www.", ignoreCase = true)) { + return true + } + + val host = hostPortion(candidate) + if (!host.contains('.')) { + return false + } + + val tld = host.substringAfterLast('.') + return isKnownTld(tld) + } + + private fun hostPortion(candidate: String): String { + val end = candidate.indexOfAny(charArrayOf('/', '?', '#')) + return if (end < 0) candidate else candidate.substring(0, end) + } + + private fun isKnownTld(tld: String): Boolean { + if (tld.isEmpty()) { + return false + } + return tld.lowercase() in TOP_LEVEL_DOMAINS + } + + /** Returns a set containing every entry in [unicode] plus its punycode form (where applicable). */ + private fun withPunycode(unicode: Set): Set { + val out: MutableSet = HashSet(unicode.size * 2) + out.addAll(unicode) + + for (tld in unicode) { + runCatching { IDN.toASCII(tld) }.getOrNull()?.let { out.add(it) } + } + return out + } + + /** + * A region of text that has been detected as a URL. + * + * @property start Inclusive start offset within the source text. + * @property end Exclusive end offset within the source text. + * @property url The link target. `http://` is added if no scheme was present in the source. + */ + data class DetectedLink( + val start: Int, + val end: Int, + val url: String + ) { + init { + require(start in 0..end) { "start=$start, end=$end" } + } + } + + /** + * IANA root top-level domains, used to validate bare-domain candidates. + * + * Sourced from the Mozilla Public Suffix List bundled with the JDK + * (`$JAVA_HOME/lib/security/public_suffix_list.dat`). The literal entries below are unicode + * forms; the corresponding punycode (`xn--…`) forms are added at init time via [IDN.toASCII] so + * both spellings of an IDN TLD are accepted. + */ + private val TOP_LEVEL_DOMAINS: Set = withPunycode( + setOf( + "닷넷", "aaa", "aarp", "abb", "abbott", "abbvie", "abc", "able", "abogado", "abudhabi", "ac", "academy", + "accenture", "accountant", "accountants", "aco", "actor", "ad", "ads", "adult", "ae", "aeg", "aero", + "aetna", "af", "afl", "africa", "ag", "agakhan", "agency", "ai", "aig", "airbus", "airforce", "airtel", + "akdn", "al", "alibaba", "alipay", "allfinanz", "allstate", "ally", "alsace", "alstom", "am", "amazon", + "americanexpress", "americanfamily", "amex", "amfam", "amica", "amsterdam", "analytics", "android", + "anquan", "anz", "ao", "aol", "apartments", "app", "apple", "aq", "aquarelle", "ar", "arab", "aramco", + "archi", "army", "arpa", "art", "arte", "as", "asda", "asia", "associates", "at", "athleta", "attorney", + "au", "auction", "audi", "audible", "audio", "auspost", "author", "auto", "autos", "aw", "aws", "ax", + "axa", "az", "azure", "ba", "baby", "baidu", "banamex", "band", "bank", "bar", "barcelona", "barclaycard", + "barclays", "barefoot", "bargains", "baseball", "basketball", "bauhaus", "bayern", "bb", "bbc", "bbt", + "bbva", "bcg", "bcn", "bd", "be", "beats", "beauty", "beer", "bentley", "berlin", "best", "bestbuy", + "bet", "bf", "bg", "bh", "bharti", "bi", "bible", "bid", "bike", "bing", "bingo", "bio", "biz", "bj", + "black", "blackfriday", "blockbuster", "blog", "bloomberg", "blue", "bm", "bms", "bmw", "bn", + "bnpparibas", "bo", "boats", "boehringer", "bofa", "bom", "bond", "boo", "book", "booking", "bosch", + "bostik", "boston", "bot", "boutique", "box", "br", "bradesco", "bridgestone", "broadway", "broker", + "brother", "brussels", "bs", "bt", "build", "builders", "business", "buy", "buzz", "bv", "bw", "by", "bz", + "bzh", "ca", "cab", "cafe", "cal", "call", "calvinklein", "cam", "camera", "camp", "canon", "capetown", + "capital", "capitalone", "car", "caravan", "cards", "care", "career", "careers", "cars", "casa", "case", + "cash", "casino", "cat", "catering", "catholic", "cba", "cbn", "cbre", "cc", "cd", "center", "ceo", + "cern", "cf", "cfa", "cfd", "cg", "ch", "chanel", "channel", "charity", "chase", "chat", "cheap", + "chintai", "christmas", "chrome", "church", "ci", "cipriani", "circle", "cisco", "citadel", "citi", + "citic", "city", "ck", "cl", "claims", "cleaning", "click", "clinic", "clinique", "clothing", "cloud", + "club", "clubmed", "cm", "cn", "co", "coach", "codes", "coffee", "college", "cologne", "com", "commbank", + "community", "company", "compare", "computer", "comsec", "condos", "construction", "consulting", + "contact", "contractors", "cooking", "cool", "coop", "corsica", "country", "coupon", "coupons", "courses", + "cpa", "cr", "credit", "creditcard", "creditunion", "cricket", "crown", "crs", "cruise", "cruises", "cu", + "cuisinella", "cv", "cw", "cx", "cy", "cymru", "cyou", "cz", "dabur", "dad", "dance", "data", "date", + "dating", "datsun", "day", "dclk", "dds", "de", "deal", "dealer", "deals", "degree", "delivery", "dell", + "deloitte", "delta", "democrat", "dental", "dentist", "desi", "design", "dev", "dhl", "diamonds", "diet", + "digital", "direct", "directory", "discount", "discover", "dish", "diy", "dj", "dk", "dm", "dnp", "do", + "docs", "doctor", "dog", "domains", "dot", "download", "drive", "dtv", "dubai", "dunlop", "dupont", + "durban", "dvag", "dvr", "dz", "earth", "eat", "ec", "eco", "edeka", "edu", "education", "ee", "eg", + "email", "emerck", "energy", "engineer", "engineering", "enterprises", "epson", "equipment", "er", + "ericsson", "erni", "es", "esq", "estate", "et", "eu", "eurovision", "eus", "events", "exchange", + "expert", "exposed", "express", "extraspace", "fage", "fail", "fairwinds", "faith", "family", "fan", + "fans", "farm", "farmers", "fashion", "fast", "fedex", "feedback", "ferrari", "ferrero", "fi", "fidelity", + "fido", "film", "final", "finance", "financial", "fire", "firestone", "firmdale", "fish", "fishing", + "fit", "fitness", "fj", "fk", "flickr", "flights", "flir", "florist", "flowers", "fly", "fm", "fo", "foo", + "food", "football", "ford", "forex", "forsale", "forum", "foundation", "fox", "fr", "free", "fresenius", + "frl", "frogans", "frontier", "ftr", "fujitsu", "fun", "fund", "furniture", "futbol", "fyi", "ga", "gal", + "gallery", "gallo", "gallup", "game", "games", "gap", "garden", "gay", "gb", "gbiz", "gd", "gdn", "ge", + "gea", "gent", "genting", "george", "gf", "gg", "ggee", "gh", "gi", "gift", "gifts", "gives", "giving", + "gl", "glass", "gle", "global", "globo", "gm", "gmail", "gmbh", "gmo", "gmx", "gn", "godaddy", "gold", + "goldpoint", "golf", "goo", "goodyear", "goog", "google", "gop", "got", "gov", "gp", "gq", "gr", + "grainger", "graphics", "gratis", "green", "gripe", "grocery", "group", "gs", "gt", "gu", "gucci", "guge", + "guide", "guitars", "guru", "gw", "gy", "hair", "hamburg", "hangout", "haus", "hbo", "hdfc", "hdfcbank", + "health", "healthcare", "help", "helsinki", "here", "hermes", "hiphop", "hisamitsu", "hitachi", "hiv", + "hk", "hkt", "hm", "hn", "hockey", "holdings", "holiday", "homedepot", "homegoods", "homes", "homesense", + "honda", "horse", "hospital", "host", "hosting", "hot", "hotels", "hotmail", "house", "how", "hr", "hsbc", + "ht", "hu", "hughes", "hyatt", "hyundai", "ibm", "icbc", "ice", "icu", "id", "ie", "ieee", "ifm", "ikano", + "il", "im", "imamat", "imdb", "immo", "immobilien", "in", "inc", "industries", "infiniti", "info", "ing", + "ink", "institute", "insurance", "insure", "int", "international", "intuit", "investments", "io", + "ipiranga", "iq", "ir", "irish", "is", "ismaili", "ist", "istanbul", "it", "itau", "itv", "jaguar", + "java", "jcb", "je", "jeep", "jetzt", "jewelry", "jio", "jll", "jm", "jmp", "jnj", "jo", "jobs", "joburg", + "jot", "joy", "jp", "jpmorgan", "jprs", "juegos", "juniper", "kaufen", "kddi", "ke", "kerryhotels", + "kerrylogistics", "kerryproperties", "kfh", "kg", "kh", "ki", "kia", "kids", "kim", "kindle", "kitchen", + "kiwi", "km", "kn", "koeln", "komatsu", "kosher", "kp", "kpmg", "kpn", "kr", "krd", "kred", "kuokgroup", + "kw", "ky", "kyoto", "kz", "la", "lacaixa", "lamborghini", "lamer", "lancaster", "land", "landrover", + "lanxess", "lasalle", "lat", "latino", "latrobe", "law", "lawyer", "lb", "lc", "lds", "lease", "leclerc", + "lefrak", "legal", "lego", "lexus", "lgbt", "li", "lidl", "life", "lifeinsurance", "lifestyle", + "lighting", "like", "lilly", "limited", "limo", "lincoln", "link", "lipsy", "live", "living", "lk", "llc", + "llp", "loan", "loans", "locker", "locus", "lol", "london", "lotte", "lotto", "love", "lpl", + "lplfinancial", "lr", "ls", "lt", "ltd", "ltda", "lu", "lundbeck", "luxe", "luxury", "lv", "ly", "ma", + "madrid", "maif", "maison", "makeup", "man", "management", "mango", "map", "market", "marketing", + "markets", "marriott", "marshalls", "mattel", "mba", "mc", "mckinsey", "md", "me", "med", "media", "meet", + "melbourne", "meme", "memorial", "men", "menu", "merckmsd", "mg", "mh", "miami", "microsoft", "mil", + "mini", "mint", "mit", "mitsubishi", "mk", "ml", "mlb", "mls", "mm", "mma", "mn", "mo", "mobi", "mobile", + "moda", "moe", "moi", "mom", "monash", "money", "monster", "mormon", "mortgage", "moscow", "moto", + "motorcycles", "mov", "movie", "mp", "mq", "mr", "ms", "msd", "mt", "mtn", "mtr", "mu", "museum", "music", + "mv", "mw", "mx", "my", "mz", "na", "nab", "nagoya", "name", "natura", "navy", "nba", "nc", "ne", "nec", + "net", "netbank", "netflix", "network", "neustar", "new", "news", "next", "nextdirect", "nexus", "nf", + "nfl", "ng", "ngo", "nhk", "ni", "nico", "nike", "nikon", "ninja", "nissan", "nissay", "nl", "no", + "nokia", "norton", "now", "nowruz", "nowtv", "np", "nr", "nra", "nrw", "ntt", "nu", "nyc", "nz", "obi", + "observer", "office", "okinawa", "olayan", "olayangroup", "ollo", "om", "omega", "one", "ong", "onion", + "onl", "online", "ooo", "open", "oracle", "orange", "org", "organic", "origins", "osaka", "otsuka", "ott", + "ovh", "pa", "page", "panasonic", "paris", "pars", "partners", "parts", "party", "pay", "pccw", "pe", + "pet", "pf", "pfizer", "pg", "ph", "pharmacy", "phd", "philips", "phone", "photo", "photography", + "photos", "physio", "pics", "pictet", "pictures", "pid", "pin", "ping", "pink", "pioneer", "pizza", "pk", + "pl", "place", "play", "playstation", "plumbing", "plus", "pm", "pn", "pnc", "pohl", "poker", "politie", + "porn", "post", "pr", "pramerica", "praxi", "press", "prime", "pro", "prod", "productions", "prof", + "progressive", "promo", "properties", "property", "protection", "pru", "prudential", "ps", "pt", "pub", + "pw", "pwc", "py", "qa", "qpon", "quebec", "quest", "racing", "radio", "re", "read", "realestate", + "realtor", "realty", "recipes", "red", "redstone", "redumbrella", "rehab", "reise", "reisen", "reit", + "reliance", "ren", "rent", "rentals", "repair", "report", "republican", "rest", "restaurant", "review", + "reviews", "rexroth", "rich", "richardli", "ricoh", "ril", "rio", "rip", "ro", "rocks", "rodeo", "rogers", + "room", "rs", "rsvp", "ru", "rugby", "ruhr", "run", "rw", "rwe", "ryukyu", "sa", "saarland", "safe", + "safety", "sakura", "sale", "salon", "samsclub", "samsung", "sandvik", "sandvikcoromant", "sanofi", "sap", + "sarl", "sas", "save", "saxo", "sb", "sbi", "sbs", "sc", "scb", "schaeffler", "schmidt", "scholarships", + "school", "schule", "schwarz", "science", "scot", "sd", "se", "search", "seat", "secure", "security", + "seek", "select", "sener", "services", "seven", "sew", "sex", "sexy", "sfr", "sg", "sh", "shangrila", + "sharp", "shaw", "shell", "shia", "shiksha", "shoes", "shop", "shopping", "shouji", "show", "si", "silk", + "sina", "singles", "site", "sj", "sk", "ski", "skin", "sky", "skype", "sl", "sling", "sm", "smart", + "smile", "sn", "sncf", "so", "soccer", "social", "softbank", "software", "sohu", "solar", "solutions", + "song", "sony", "soy", "spa", "space", "sport", "spot", "sr", "srl", "ss", "st", "stada", "staples", + "star", "statebank", "statefarm", "stc", "stcgroup", "stockholm", "storage", "store", "stream", "studio", + "study", "style", "su", "sucks", "supplies", "supply", "support", "surf", "surgery", "suzuki", "sv", + "swatch", "swiss", "sx", "sy", "sydney", "systems", "sz", "tab", "taipei", "talk", "taobao", "target", + "tatamotors", "tatar", "tattoo", "tax", "taxi", "tc", "tci", "td", "tdk", "team", "tech", "technology", + "tel", "temasek", "tennis", "teva", "tf", "tg", "th", "thd", "theater", "theatre", "tiaa", "tickets", + "tienda", "tips", "tires", "tirol", "tj", "tjmaxx", "tjx", "tk", "tkmaxx", "tl", "tm", "tmall", "tn", + "to", "today", "tokyo", "tools", "top", "toray", "toshiba", "total", "tours", "town", "toyota", "toys", + "tr", "trade", "trading", "training", "travel", "travelers", "travelersinsurance", "trust", "trv", "tt", + "tube", "tui", "tunes", "tushu", "tv", "tvs", "tw", "tz", "ua", "ubank", "ubs", "ug", "uk", "unicom", + "university", "uno", "uol", "ups", "us", "uy", "uz", "va", "vacations", "vana", "vanguard", "vc", "ve", + "vegas", "ventures", "verisign", "vermögensberater", "vermögensberatung", "versicherung", "vet", "vg", + "vi", "viajes", "video", "vig", "viking", "villas", "vin", "vip", "virgin", "visa", "vision", "viva", + "vivo", "vlaanderen", "vn", "vodka", "volvo", "vote", "voting", "voto", "voyage", "vu", "wales", + "walmart", "walter", "wang", "wanggou", "watch", "watches", "weather", "weatherchannel", "webcam", + "weber", "website", "wed", "wedding", "weibo", "weir", "wf", "whoswho", "wien", "wiki", "williamhill", + "win", "windows", "wine", "winners", "wme", "wolterskluwer", "woodside", "work", "works", "world", "wow", + "ws", "wtc", "wtf", "xbox", "xerox", "xihuan", "xin", "xxx", "xyz", "yachts", "yahoo", "yamaxun", + "yandex", "ye", "yodobashi", "yoga", "yokohama", "you", "youtube", "yt", "yun", "za", "zappos", "zara", + "zero", "zip", "zm", "zone", "zuerich", "zw", "ελ", "ευ", "бг", "бел", "дети", "ею", + "католик", "ком", "қаз", "мкд", "мон", "москва", "онлайн", "орг", + "рус", "рф", "сайт", "срб", "укр", "გე", "հայ", "ישראל", "קום", + "ابوظبي", "ارامكو", "الاردن", "البحرين", "الجزائر", "السعودية", + "السعوديه", "السعودیة", "السعودیۃ", "العليان", "المغرب", + "اليمن", "امارات", "ايران", "ایران", "بارت", "بازار", "بھارت", + "بيتك", "پاكستان", "پاکستان", "ڀارت", "تونس", "سودان", "سوريا", + "سورية", "شبكة", "عراق", "عرب", "عمان", "فلسطين", "قطر", "كاثوليك", + "كوم", "مصر", "مليسيا", "موريتانيا", "موقع", "همراه", "कॉम", + "नेट", "भारत", "भारतम्", "भारोत", "संगठन", + "বাংলা", "ভারত", "ভাৰত", "ਭਾਰਤ", "ભારત", "ଭାରତ", + "இந்தியா", "இலங்கை", "சிங்கப்பூர்", "భారత్", + "ಭಾರತ", "ഭാരതം", "ලංකා", "คอม", "ไทย", "ລາວ", "アマゾン", + "グーグル", "クラウド", "コム", "ストア", "セール", "ファッション", "ポイント", + "みんな", "世界", "中信", "中国", "中國", "中文网", "亚马逊", "企业", "佛山", + "信息", "健康", "八卦", "公司", "公益", "台湾", "台灣", "商城", "商店", "商标", + "嘉里", "嘉里大酒店", "在线", "大拿", "天主教", "娱乐", "家電", "广东", "微博", + "慈善", "我爱你", "手机", "招聘", "政务", "政府", "新加坡", "新闻", "时尚", "書籍", + "机构", "淡马锡", "游戏", "澳門", "澳门", "点看", "移动", "组织机构", "网址", + "网店", "网站", "网络", "联通", "臺灣", "谷歌", "购物", "通販", "集团", "電訊盈科", + "飞利浦", "食品", "餐厅", "香格里拉", "香港" + ) + ) +} diff --git a/core/util-jvm/src/test/java/org/signal/core/util/LinkifierTest.kt b/core/util-jvm/src/test/java/org/signal/core/util/LinkifierTest.kt new file mode 100644 index 0000000000..3c06e0e7d1 --- /dev/null +++ b/core/util-jvm/src/test/java/org/signal/core/util/LinkifierTest.kt @@ -0,0 +1,198 @@ +/* + * Copyright 2026 Signal Messenger, LLC + * SPDX-License-Identifier: AGPL-3.0-only + */ + +package org.signal.core.util + +import assertk.assertFailure +import assertk.assertThat +import assertk.assertions.hasSize +import assertk.assertions.isEqualTo +import assertk.assertions.isInstanceOf +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.Parameterized + +/** + * Each row supplies an input string and the exact list of [Expected] detections in left-to-right + * order. An empty list means we expect no detections (i.e. a false-positive guard). + */ +@RunWith(Parameterized::class) +class LinkifierTest(private val case: Case) { + + data class Case( + val name: String, + val input: String, + val expected: List + ) { + override fun toString(): String = name + } + + /** + * @property spanText The substring of [Case.input] that should be marked as a link. + * @property url The normalized URL the [Linkifier.DetectedLink] should expose. Defaults to + * [spanText] for cases where the input already includes a scheme. + */ + data class Expected( + val spanText: String, + val url: String = spanText + ) + + @Test + fun findLinks() { + val actual: List = Linkifier.findLinks(case.input) + + assertThat(actual).hasSize(case.expected.size) + + // Walk forward through the input as we go, so that repeated spans are matched in order. + var cursor = 0 + for ((i, expected) in case.expected.withIndex()) { + val expectedStart = case.input.indexOf(expected.spanText, cursor) + if (expectedStart < 0) { + throw AssertionError("expected span <${expected.spanText}> not found in <${case.input}> at or after index $cursor") + } + val expectedEnd = expectedStart + expected.spanText.length + + val link = actual[i] + assertThat(link.start).isEqualTo(expectedStart) + assertThat(link.end).isEqualTo(expectedEnd) + assertThat(link.url).isEqualTo(expected.url) + + cursor = expectedEnd + } + } + + companion object { + + private fun web(spanText: String, url: String = spanText) = Expected(spanText, url) + + @Parameterized.Parameters(name = "{0}") + @JvmStatic + fun cases(): Collection> = listOf( + + // ----- empty / no-match ----- + Case("empty text", "", emptyList()), + Case("plain text with no urls", "just some words, nothing fancy.", emptyList()), + + // ----- basic web URLs ----- + Case("https url", "https://signal.org", listOf(web("https://signal.org"))), + Case("http url", "http://signal.org", listOf(web("http://signal.org"))), + Case("bare domain gets http prefix", "signal.org", listOf(web("signal.org", url = "http://signal.org"))), + Case("www-prefixed url gets http prefix", "www.signal.org", listOf(web("www.signal.org", url = "http://www.signal.org"))), + Case("url with path query and fragment", "https://example.com/foo/bar?x=1&y=2#frag", listOf(web("https://example.com/foo/bar?x=1&y=2#frag"))), + Case("url with port", "http://localhost:8080/path", listOf(web("http://localhost:8080/path"))), + Case("unicode IDN domain", "https://кц.рф/path", listOf(web("https://кц.рф/path"))), + Case("url embedded in sentence", "Check out https://signal.org for details", listOf(web("https://signal.org"))), + + // ----- the trailing-hyphen bug fix ----- + Case("trailing hyphen before space is preserved", "see https://example.com/foo- and bar", listOf(web("https://example.com/foo-"))), + Case("trailing hyphen at end of string is preserved", "https://example.com/foo-", listOf(web("https://example.com/foo-"))), + Case("multiple trailing hyphens are preserved", "https://example.com/foo--- end", listOf(web("https://example.com/foo---"))), + Case("bare domain followed by trailing hyphen excludes the hyphen", "see signal.com- today", listOf(web("signal.com", url = "http://signal.com"))), + Case("hyphen in middle of url is preserved", "https://example-site.com/a-b-c", listOf(web("https://example-site.com/a-b-c"))), + Case("url ending in dash followed by another url", "https://example.com/foo- https://other.com", listOf(web("https://example.com/foo-"), web("https://other.com"))), + + // ----- trailing punctuation trimming ----- + Case("trailing period is trimmed", "Visit https://signal.org.", listOf(web("https://signal.org"))), + Case("trailing comma is trimmed", "https://signal.org, then go", listOf(web("https://signal.org"))), + Case("trailing exclamation is trimmed", "https://signal.org!", listOf(web("https://signal.org"))), + Case("trailing question mark is trimmed", "Have you tried https://signal.org?", listOf(web("https://signal.org"))), + Case("trailing semicolon is trimmed", "Go to https://signal.org; thanks", listOf(web("https://signal.org"))), + Case("trailing colon is trimmed", "https://signal.org: nice", listOf(web("https://signal.org"))), + Case("multiple trailing punctuation chars are all trimmed", "Wow https://signal.org!!! amazing", listOf(web("https://signal.org"))), + Case("trailing slash is preserved", "https://signal.org/ end", listOf(web("https://signal.org/"))), + Case("trailing underscore is preserved", "https://example.com/path_ tail", listOf(web("https://example.com/path_"))), + + // ----- bracket / paren handling ----- + Case("trailing closing paren without opener is trimmed", "(see https://signal.org)", listOf(web("https://signal.org"))), + Case("trailing closing paren with matching opener inside is preserved", "https://en.wikipedia.org/wiki/Foo_(bar) and more", listOf(web("https://en.wikipedia.org/wiki/Foo_(bar)"))), + Case("trailing closing bracket without opener is trimmed", "[https://signal.org]", listOf(web("https://signal.org"))), + Case("trailing closing brace without opener is trimmed", "{https://signal.org}", listOf(web("https://signal.org"))), + Case("parenthesized url with trailing punctuation has both trimmed", "(see https://signal.org).", listOf(web("https://signal.org"))), + + // ----- multiple URLs in one input ----- + Case("two urls separated by text", "First https://a.com then https://b.com", listOf(web("https://a.com"), web("https://b.com"))), + Case("two urls separated only by comma", "https://a.com,https://b.com", listOf(web("https://a.com"), web("https://b.com"))), + Case( + name = "multi-line text with several urls per line", + input = """ + Line one https://a.com + Line two https://b.com and https://c.com + Line three: nothing + """.trimIndent(), + expected = listOf(web("https://a.com"), web("https://b.com"), web("https://c.com")) + ), + Case( + name = "github compare url with triple-dot path is captured fully", + input = "diff: https://github.com/signalapp/Signal-Android/compare/v6.23.2...v6.23.3", + expected = listOf(web("https://github.com/signalapp/Signal-Android/compare/v6.23.2...v6.23.3")) + ), + + // ----- boundary handling ----- + Case("explicit-scheme url glued to a leading word is rejected", "foohttps://signal.org", emptyList()), + Case("url after newline is matched", "first line\nhttps://signal.org", listOf(web("https://signal.org"))), + Case("url after open paren is matched", "(https://signal.org", listOf(web("https://signal.org"))), + Case("url after digit-only token keeps boundary", "12345 https://signal.org", listOf(web("https://signal.org"))), + Case("email's host half is not also matched as a url", "Hi user@example.com bye", emptyList()), + + // ----- false-positive guards ----- + Case("single dot in a sentence is not a url", "Hello. World", emptyList()), + Case("decimal number is not a url", "value is 3.14", emptyList()), + Case("version string is not a url", "v1.2.3 release", emptyList()), + Case("single-letter TLD is rejected", "abc.x next", emptyList()), + Case("bare scheme is not a url", "https:// is not a url", emptyList()), + + // ----- TLD validation against the IANA list ----- + Case("Mr.Smith style false positive is rejected", "Mr.Smith said hello", emptyList()), + Case("bare domain with unknown TLD is rejected", "see signal.notatld today", emptyList()), + Case("bare domain with valid IDN TLD is accepted", "visit example.рф", listOf(web("example.рф", url = "http://example.рф"))), + Case("bare domain with punycode TLD is accepted", "visit example.xn--p1ai", listOf(web("example.xn--p1ai", url = "http://example.xn--p1ai"))), + Case("fake punycode-shaped TLD is rejected", "see signal.xn--notarealtld today", emptyList()), + Case("scheme with unknown TLD is still accepted", "https://buildbot.notatld/", listOf(web("https://buildbot.notatld/"))), + Case("www-prefixed unknown TLD is still accepted", "www.foo.notatld", listOf(web("www.foo.notatld", url = "http://www.foo.notatld"))), + Case("e.g. abbreviation is not a url", "e.g. you can do this", emptyList()), + + // ----- additional cases mirroring AOSP LinkifyTest ----- + Case("mixed-case scheme is matched", "hTtP://android.com", listOf(web("hTtP://android.com"))), + Case("punycode url with scheme is matched", "http://xn--fsqu00a.xn--unup4y", listOf(web("http://xn--fsqu00a.xn--unup4y"))), + Case("bare domain with leading dash on TLD label is rejected", "xn--fsqu00a.-xn--unup4y next", emptyList()), + Case("tilde in path is preserved", "http://www.example.com:8080/~user/?foo=bar", listOf(web("http://www.example.com:8080/~user/?foo=bar"))), + Case("dollar sign in path is preserved", "http://android.com/path\$?v=\$val", listOf(web("http://android.com/path\$?v=\$val"))), + Case("port plus query is matched", "http://www.example.com:8080/?foo=bar", listOf(web("http://www.example.com:8080/?foo=bar"))), + Case("schemed url with empty path and query is matched", "http://android.com?q=v", listOf(web("http://android.com?q=v"))), + Case("bare domain with empty path and query is matched", "android.com?q=v", listOf(web("android.com?q=v", url = "http://android.com?q=v"))), + Case("bare domain with internal dashes is matched", "see a-nd.r-oid.com today", listOf(web("a-nd.r-oid.com", url = "http://a-nd.r-oid.com"))), + Case("bare domain with underscores is rejected", "a_nd.r_oid.com next", emptyList()), + Case("schemed url with underscores in host is matched", "http://a_nd.r_oid.com/x", listOf(web("http://a_nd.r_oid.com/x"))), + + // ----- unicode whitespace acts as a URL boundary ----- + Case("en-space terminates url", "http://and rest", listOf(web("http://and"))), + Case("ideographic space terminates url", "http://and rest", listOf(web("http://and"))), + Case("nbsp terminates url", "http://and rest", listOf(web("http://and"))), + + // ----- bidi/zero-width format chars on either side reject the url ----- + Case("leading bidi override rejects url", "‬moc.diordna.com next", emptyList()), + Case("trailing bidi override rejects url", "moc.diordna.com‭ next", emptyList()), + Case("internal bidi override rejects both halves", "moc.diordna.com‮moc.diordna.com", emptyList()), + Case("zero-width space adjacent to url rejects it", "see signal.org​ next", emptyList()) + ).map { arrayOf(it) } + } +} + +/** + * Tests for behavior that doesn't fit the row-based shape of [LinkifierTest] — exception contracts + * on the public API. + */ +class LinkifierContractTest { + + @Test + fun `DetectedLink with negative start throws`() { + assertFailure { Linkifier.DetectedLink(-1, 0, "x") }.isInstanceOf() + } + + @Test + fun `DetectedLink with end before start throws`() { + assertFailure { Linkifier.DetectedLink(5, 4, "x") }.isInstanceOf() + } +} diff --git a/core/util/src/main/java/org/signal/core/util/LinkifierSpannableExtensions.kt b/core/util/src/main/java/org/signal/core/util/LinkifierSpannableExtensions.kt new file mode 100644 index 0000000000..2f3855922f --- /dev/null +++ b/core/util/src/main/java/org/signal/core/util/LinkifierSpannableExtensions.kt @@ -0,0 +1,78 @@ +/* + * Copyright 2026 Signal Messenger, LLC + * SPDX-License-Identifier: AGPL-3.0-only + */ + +package org.signal.core.util + +import android.text.Spannable +import android.text.Spanned +import android.text.style.URLSpan +import org.signal.core.util.Linkifier.DetectedLink + +/** + * Detects web URLs in this [Spannable] and applies a span for each one. + * + * Overlapping spans are pruned using the same longest-wins rule. Equal-length overlapping spans are both kept. + * + * @param filter caller-supplied filter; only links for which this returns `true` are added + * @param spanFactory builds the span to apply for each link; defaults to a plain [URLSpan] + * + * @return `true` if at least one span was added. + */ +@JvmOverloads +fun Spannable.addDetectedLinks( + filter: (DetectedLink) -> Boolean = { true }, + spanFactory: (DetectedLink) -> Any = { URLSpan(it.url) } +): Boolean { + val detected = Linkifier.findLinks(this).filter(filter) + if (detected.isEmpty()) return false + + val regions = ArrayList(detected.size) + for (span in getSpans(0, length, URLSpan::class.java)) { + regions += Region(getSpanStart(span), getSpanEnd(span), existingSpan = span, detected = null) + } + for (link in detected) { + regions += Region(link.start, link.end, existingSpan = null, detected = link) + } + + // Sort by start ascending, then length descending — matches LinkifyCompat's COMPARATOR. + regions.sortWith(compareBy({ it.start }, { -(it.end - it.start) })) + + var i = 0 + while (i < regions.size - 1) { + val current = regions[i] + val next = regions[i + 1] + if (current.start <= next.start && current.end > next.start) { + val currentLength = current.end - current.start + val nextLength = next.end - next.start + val remove: Int = when { + next.end <= current.end -> i + 1 + currentLength > nextLength -> i + 1 + currentLength < nextLength -> i + else -> -1 + } + if (remove >= 0) { + regions[remove].existingSpan?.let { removeSpan(it) } + regions.removeAt(remove) + continue + } + } + i++ + } + + var added = false + for (region in regions) { + val link = region.detected ?: continue + setSpan(spanFactory(link), link.start, link.end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) + added = true + } + return added +} + +private data class Region( + val start: Int, + val end: Int, + val existingSpan: URLSpan?, + val detected: DetectedLink? +) diff --git a/core/util/src/test/java/org/signal/core/util/LinkifierSpannableExtensionsTest.kt b/core/util/src/test/java/org/signal/core/util/LinkifierSpannableExtensionsTest.kt new file mode 100644 index 0000000000..7e7659521a --- /dev/null +++ b/core/util/src/test/java/org/signal/core/util/LinkifierSpannableExtensionsTest.kt @@ -0,0 +1,155 @@ +/* + * Copyright 2026 Signal Messenger, LLC + * SPDX-License-Identifier: AGPL-3.0-only + */ + +package org.signal.core.util + +import android.app.Application +import android.text.SpannableString +import android.text.Spanned +import android.text.style.URLSpan +import assertk.assertThat +import assertk.assertions.containsExactly +import assertk.assertions.hasSize +import assertk.assertions.isEmpty +import assertk.assertions.isEqualTo +import assertk.assertions.isFalse +import assertk.assertions.isTrue +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(manifest = Config.NONE, application = Application::class) +class LinkifierSpannableExtensionsTest { + + @Test + fun `empty text returns false and adds nothing`() { + val spannable = SpannableString("") + assertThat(spannable.addDetectedLinks()).isFalse() + assertThat(spannable.urlSpans()).isEmpty() + } + + @Test + fun `text with no urls returns false`() { + val spannable = SpannableString("just words and punctuation, nothing more.") + assertThat(spannable.addDetectedLinks()).isFalse() + assertThat(spannable.urlSpans()).isEmpty() + } + + @Test + fun `single url is added with correct range and url`() { + val text = "visit https://signal.org for more" + val spannable = SpannableString(text) + + assertThat(spannable.addDetectedLinks()).isTrue() + + val spans = spannable.urlSpans() + assertThat(spans).hasSize(1) + val span = spans.single() + assertThat(spannable.getSpanStart(span)).isEqualTo(text.indexOf("https")) + assertThat(spannable.getSpanEnd(span)).isEqualTo(text.indexOf("https") + "https://signal.org".length) + assertThat(span.url).isEqualTo("https://signal.org") + } + + @Test + fun `bare domain gets http scheme prefix`() { + val spannable = SpannableString("see signal.org today") + assertThat(spannable.addDetectedLinks()).isTrue() + + val span = spannable.urlSpans().single() + assertThat(span.url).isEqualTo("http://signal.org") + } + + @Test + fun `multiple urls each get their own span`() { + val spannable = SpannableString("first https://a.com then https://b.com") + assertThat(spannable.addDetectedLinks()).isTrue() + + val urls = spannable.urlSpans().map { it.url } + assertThat(urls).containsExactly("https://a.com", "https://b.com") + } + + @Test + fun `filter excludes links and returns false when all rejected`() { + val spannable = SpannableString("https://a.com and https://b.com") + val added = spannable.addDetectedLinks(filter = { false }) + assertThat(added).isFalse() + assertThat(spannable.urlSpans()).isEmpty() + } + + @Test + fun `filter excludes only some links`() { + val spannable = SpannableString("https://a.com and https://b.com") + spannable.addDetectedLinks(filter = { it.url == "https://b.com" }) + + val urls = spannable.urlSpans().map { it.url } + assertThat(urls).containsExactly("https://b.com") + } + + @Test + fun `custom spanFactory is used for each accepted link`() { + val spannable = SpannableString("https://a.com plus https://b.com") + val tagged = mutableListOf() + spannable.addDetectedLinks(spanFactory = { link -> + tagged += link.url + URLSpan("custom:${link.url}") + }) + + val urls = spannable.urlSpans().map { it.url } + assertThat(urls).containsExactly("custom:https://a.com", "custom:https://b.com") + assertThat(tagged).containsExactly("https://a.com", "https://b.com") + } + + @Test + fun `longer existing URLSpan wins over shorter overlapping detection`() { + val text = "call tel://+15551234.com for help" + val spannable = SpannableString(text) + + val phoneStart = text.indexOf("tel://") + val phoneEnd = text.indexOf(" for") + spannable.setSpan(URLSpan("tel:+15551234"), phoneStart, phoneEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) + + spannable.addDetectedLinks() + + val spans = spannable.urlSpans() + assertThat(spans).hasSize(1) + assertThat(spans.single().url).isEqualTo("tel:+15551234") + } + + @Test + fun `longer detection wins over shorter existing URLSpan`() { + val text = "see https://example.com/long/path here" + val spannable = SpannableString(text) + + // Pre-existing short span covering only the host portion. + val shortStart = text.indexOf("example") + val shortEnd = shortStart + "example.com".length + spannable.setSpan(URLSpan("http://shortwins"), shortStart, shortEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) + + spannable.addDetectedLinks() + + val spans = spannable.urlSpans() + assertThat(spans).hasSize(1) + assertThat(spans.single().url).isEqualTo("https://example.com/long/path") + } + + @Test + fun `non-overlapping detection is added even when other URLSpan exists`() { + val text = "call mailto and visit https://signal.org now" + val spannable = SpannableString(text) + + val mailStart = text.indexOf("mailto") + val mailEnd = mailStart + "mailto".length + spannable.setSpan(URLSpan("mailto:foo@bar.com"), mailStart, mailEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) + + spannable.addDetectedLinks() + + val urls = spannable.urlSpans().map { it.url }.sorted() + assertThat(urls).containsExactly("https://signal.org", "mailto:foo@bar.com") + } + + private fun SpannableString.urlSpans(): Array = getSpans(0, length, URLSpan::class.java) +}