Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try avoiding trailing punctuation inside linkified URLs #4214

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from

Conversation

jmartinesp
Copy link
Member

Content

When an URL is linkified automatically by LinkifyCompat, try removing any trailing punctuation and other mismatched symbols from the link span.

This solution is not great and might fail in some cases, but it should cover the most common ones, since it seems like a 'smart' linkification library is hard to find as a replacement.

Motivation and context

It turns out the Linkify component from Google isn't as smart as we thought: as soon as a / char is found inside the URL, it'll greedily fetch as much text as possible. This is probably because a different regex is used internally than for a simple domain.

Should fix #4208.

Screenshots / GIFs

Before After
image image

Tests

You can play with the ClickableLinkTextWithParenthesisPreview composable preview, or send messages with URLs and problematic formats to be linkified.

Tested devices

  • Physical
  • Emulator
  • OS version(s): 14

Checklist

  • Changes have been tested on an Android device or Android emulator with API 24
  • UI change has been tested on both light and dark themes
  • Accessibility has been taken into account. See https://github.com/element-hq/element-x-android/blob/develop/CONTRIBUTING.md#accessibility
  • Pull request is based on the develop branch
  • Pull request title will be used in the release note, it clearly define what will change for the user
  • Pull request includes screenshots or videos if containing UI changes
  • You've made a self review of your PR

@jmartinesp jmartinesp added the PR-Bugfix For bug fix label Jan 29, 2025
@jmartinesp jmartinesp requested a review from a team as a code owner January 29, 2025 10:02
@jmartinesp jmartinesp requested review from bmarty and removed request for a team January 29, 2025 10:02
Copy link
Contributor

github-actions bot commented Jan 29, 2025

📱 Scan the QR code below to install the build (arm64 only) for this PR.
QR code
If you can't scan the QR code you can install the build via this link: https://i.diawi.com/MwE6rS

@Composable
internal fun ClickableLinkTextWithParenthesisPreview() = ElementThemedPreview {
ClickableLinkText(
annotatedString = AnnotatedString("Does this work (url: github.com/element-hq/element-x-android/README?)?!.", ParagraphStyle()),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add exemple where parenthesis is part of the url?

Copy link

codecov bot commented Jan 29, 2025

Codecov Report

Attention: Patch coverage is 86.04651% with 6 lines in your changes missing coverage. Please review.

Project coverage is 80.23%. Comparing base (346e364) to head (28c8e82).
Report is 2 commits behind head on develop.

Files with missing lines Patch % Lines
...droid/libraries/androidutils/text/LinkifyHelper.kt 84.61% 1 Missing and 5 partials ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #4214      +/-   ##
===========================================
- Coverage    80.24%   80.23%   -0.01%     
===========================================
  Files         2042     2043       +1     
  Lines        54079    54104      +25     
  Branches      6569     6580      +11     
===========================================
+ Hits         43393    43412      +19     
- Misses        8414     8415       +1     
- Partials      2272     2277       +5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@bmarty bmarty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I have tested and the target link need to be updated as well.

end = adjustEndIndex(spannable, start, end)
}.onFailure {
Timber.e(it, "Failed to adjust end index for link span")
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is adjusting the end of the span, so this is fixing the rendering issue, but it does this affect the URL itself, i.e. the target of the link.

I think the line annotation = span.url, below needs to be updated.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's maybe because it did not work with the example I tried:

image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the Event renderer use ClickableLinkText? It seems it's not the case 😅

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahhh damn, it used to use it, that's why I tried fixing it here. I'll move the code where it belongs 🥲 .

@jmartinesp jmartinesp force-pushed the fix/jme/avoid-trailing-punctuation-in-linkified-urls branch from 9ef2d3f to 323c811 Compare January 29, 2025 17:03
@jmartinesp jmartinesp requested a review from bmarty January 30, 2025 09:28
@jmartinesp
Copy link
Member Author

This should be ready to review again: it's now affecting the intended texts (🤦 ) and has some extra unit tests for the new component.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR-Bugfix For bug fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Linkified text is too greedy and gobbles punctuation
4 participants