mirror of
https://github.com/linagora/tmail-flutter.git
synced 2026-12-06 00:05:29 +00:00
TF-5098 Keep the closing quote of a double-quoted URL out of the link
Addresses: 4717004896 https://github.com/linagora/tmail-flutter/pull/5182#discussion_r4717004896
This commit is contained in:
1 parent
5d33ef2669
commit
417bc00a4b
2 files changed
+30
-1
No files matched your search
@@ -24,6 +24,7 @@ class SanitizeAutolinkFilter {
|
||||
|
||||
// The href is always double-quoted, so attribute mode is enough.
|
||||
static const _attributeEscape = HtmlEscape(HtmlEscapeMode.attribute);
|
||||
static const _quote = '"';
|
||||
|
||||
SanitizeAutolinkFilter(this.htmlEscape);
|
||||
|
||||
@@ -51,13 +52,31 @@ class SanitizeAutolinkFilter {
|
||||
} else if (element is EmailElement) {
|
||||
buffer.write(_buildEmailLinkTag(mailToLink: element.url, value: element.text));
|
||||
} else if (element is UrlElement) {
|
||||
buffer.write(_buildUrlLinkTag(urlLink: element.url, value: element.text));
|
||||
_writeUrl(buffer, element);
|
||||
}
|
||||
}
|
||||
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
// The loose URL pattern takes the closing quote of "https://…"; a lone trailing quote is never part of the URL.
|
||||
void _writeUrl(StringBuffer buffer, UrlElement element) {
|
||||
final origin = element.originText;
|
||||
final endsWithLoneQuote = origin.length > 1 &&
|
||||
origin.indexOf(_quote) == origin.length - 1 &&
|
||||
element.url.endsWith(_quote) &&
|
||||
element.text.endsWith(_quote);
|
||||
if (!endsWithLoneQuote) {
|
||||
buffer.write(_buildUrlLinkTag(urlLink: element.url, value: element.text));
|
||||
return;
|
||||
}
|
||||
buffer.write(_buildUrlLinkTag(
|
||||
urlLink: element.url.substring(0, element.url.length - 1),
|
||||
value: element.text.substring(0, element.text.length - 1),
|
||||
));
|
||||
buffer.write(htmlEscape.convert(_quote));
|
||||
}
|
||||
|
||||
String _buildUrlLinkTag({required String urlLink, required String value}) {
|
||||
final href = _attributeEscape.convert(urlLink);
|
||||
final text = htmlEscape.convert(value);
|
||||
|
||||
@@ -100,6 +100,16 @@ void main() {
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'SHOULD keep the closing quote of a double-quoted URL outside the link',
|
||||
() {
|
||||
expect(
|
||||
filter.process('Open "https://example.com/docs" now'),
|
||||
equals('Open "<a href="https://example.com/docs" target="_blank" rel="noreferrer" style="white-space: nowrap; word-break: keep-all;">example.com/docs</a>" now'),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'SHOULD return empty string for empty input',
|
||||
() {
|
||||
|
||||
Reference in new issue
Block a user