-
Notifications
You must be signed in to change notification settings - Fork 187
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
Try avoiding trailing punctuation inside linkified URLs #4214
Conversation
📱 Scan the QR code below to install the build (arm64 only) for this PR. |
@Composable | ||
internal fun ClickableLinkTextWithParenthesisPreview() = ElementThemedPreview { | ||
ClickableLinkText( | ||
annotatedString = AnnotatedString("Does this work (url: github.com/element-hq/element-x-android/README?)?!.", ParagraphStyle()), |
There was a problem hiding this comment.
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?
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #4214 +/- ##
===========================================
- Coverage 80.13% 80.13% -0.01%
===========================================
Files 2052 2053 +1
Lines 54576 54601 +25
Branches 6672 6683 +11
===========================================
+ Hits 43733 43752 +19
- Misses 8560 8561 +1
- Partials 2283 2288 +5 ☔ View full report in Codecov by Sentry. |
There was a problem hiding this 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") | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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 😅
There was a problem hiding this comment.
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 🥲 .
9ef2d3f
to
323c811
Compare
This should be ready to review again: it's now affecting the intended texts (🤦 ) and has some extra unit tests for the new component. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks and sorry for the late review. Good to be merged, I believe that I wrote only non-blocking remarks.
return spannable | ||
} | ||
private fun CharSequence.withFixedURLSpans(): CharSequence { | ||
return LinkifyHelper.linkify(this, Linkify.WEB_URLS or Linkify.PHONE_NUMBERS or Linkify.EMAIL_ADDRESSES) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe remove the second param, since it's the default value.
Also maybe move this extension to the file LinkifyHelper
and rename it to safeLinkify()
, cleverLinkify()
, betterLinkify()
or something better (can't find something good).
@Composable | ||
internal fun TimelineItemTextViewWithLinkifiedUrlPreview() = ElementPreview { | ||
val content = aTimelineItemTextContent( | ||
pillifiedBody = LinkifyHelper.linkify("Does this work (url: github.com/element-hq/element-x-android/README?)?.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe change the text to
pillifiedBody = LinkifyHelper.linkify("Does this work (url: github.com/element-hq/element-x-android/README?)?.") | |
pillifiedBody = LinkifyHelper.linkify("The link should end after the first '?' (url: github.com/element-hq/element-x-android/README?)?.") |
@Composable | ||
internal fun TimelineItemTextViewWithLinkifiedUrlAndNestedParenthesisPreview() = ElementPreview { | ||
val content = aTimelineItemTextContent( | ||
pillifiedBody = LinkifyHelper.linkify("Does this work ((url: github.com/element-hq/element-x-android/READ(ME)))!") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe change the text to
pillifiedBody = LinkifyHelper.linkify("Does this work ((url: github.com/element-hq/element-x-android/READ(ME)))!") | |
pillifiedBody = LinkifyHelper.linkify("The link should end after the '(ME)' ((url: github.com/element-hq/element-x-android/READ(ME)))!") |
fun `linkification finds URL`() { | ||
val text = "A url https://matrix.org" | ||
val result = LinkifyHelper.linkify(text) | ||
val urlSpans = result.toSpannable().getSpans<URLSpan>(start = 0, end = result.length) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All the start = 0, end = result.length
in all tests are not necessary, the 2 parameters are the default values.
// Restore old spans, remove new ones if there is a conflict | ||
for ((urlSpan, location) in oldURLSpans) { | ||
val (start, end) = location | ||
val addedConflictingSpans = spannable.getSpans<URLSpan>(start, end).orEmpty() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.orEmpty()
does not seem to be necessary.
For tests, I think the function will return before, at line 29.
assertThat(urlSpans.size).isEqualTo(1) | ||
assertThat(urlSpans.first().url).isEqualTo("https://github.com/element-hq/element-android/READ(ME)") | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for all those tests!
Create `LinkfierHelper` and post-process URLSpans added to make sure they honor the actual URLs in text by removing unnecessarily added trailing punctuation.
28c8e82
to
a35a96c
Compare
|
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
Tests
You can play with the
ClickableLinkTextWithParenthesisPreview
composable preview, or send messages with URLs and problematic formats to be linkified.Tested devices
Checklist