From 99d6babc30d06841215fbf091f7036463c513383 Mon Sep 17 00:00:00 2001 From: Mauro <34335419+Velin92@users.noreply.github.com> Date: Wed, 14 Feb 2024 17:45:23 +0100 Subject: [PATCH] Fixed the link color and improved link dection in code blocks (#2466) --- .../HTMLParsing/AttributedStringBuilder.swift | 16 ++++++++++++++-- .../Sources/AttributedStringBuilderTests.swift | 17 ++++++++++++++--- changelog.d/2379.bugfix | 1 + 3 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 changelog.d/2379.bugfix diff --git a/ElementX/Sources/Other/HTMLParsing/AttributedStringBuilder.swift b/ElementX/Sources/Other/HTMLParsing/AttributedStringBuilder.swift index cb125f3c4..681bdc930 100644 --- a/ElementX/Sources/Other/HTMLParsing/AttributedStringBuilder.swift +++ b/ElementX/Sources/Other/HTMLParsing/AttributedStringBuilder.swift @@ -151,8 +151,20 @@ struct AttributedStringBuilder: AttributedStringBuilderProtocol { if let value = value as? UIColor, value == temporaryCodeBlockMarkingColor { attributedString.addAttribute(.backgroundColor, value: UIColor(.compound._bgCodeBlock) as Any, range: range) - // Codeblocks should not have links and all users mentions - attributedString.removeAttribute(.link, range: range) + // Codebloks should not have explicit links + attributedString.enumerateAttribute(.link, in: range, options: []) { value, range, _ in + if let link = value as? URL { + var text = attributedString.attributedSubstring(from: range).string + if !text.contains("://") { + // we sanitize links by always them use https:// + text.insert(contentsOf: "https://", at: text.startIndex) + } + if text == link.absoluteString { + attributedString.removeAttribute(.link, range: range) + } + } + } + // Codeblocks should not have all users mentions attributedString.removeAttribute(.MatrixAllUsersMention, range: range) } } diff --git a/UnitTests/Sources/AttributedStringBuilderTests.swift b/UnitTests/Sources/AttributedStringBuilderTests.swift index 69bbacf87..8680ebc3d 100644 --- a/UnitTests/Sources/AttributedStringBuilderTests.swift +++ b/UnitTests/Sources/AttributedStringBuilderTests.swift @@ -493,11 +493,22 @@ class AttributedStringBuilderTests: XCTestCase { checkAttachment(attributedString: attributedStringFromPlain2, expectedRuns: 1) } - func testLinksAreIgnoredInCode() { - let htmlString = "
test https://matrix.org test"
- let attributedStringFromHTML = attributedStringBuilder.fromHTML(htmlString)
+ func testURLsAreIgnoredInCode() {
+ var htmlString = "test https://matrix.org test"
+ var attributedStringFromHTML = attributedStringBuilder.fromHTML(htmlString)
XCTAssert(attributedStringFromHTML?.runs.count == 1)
XCTAssertNil(attributedStringFromHTML?.link)
+
+ htmlString = "matrix.org"
+ attributedStringFromHTML = attributedStringBuilder.fromHTML(htmlString)
+ XCTAssert(attributedStringFromHTML?.runs.count == 1)
+ XCTAssertNil(attributedStringFromHTML?.link)
+ }
+
+ func testHyperlinksAreNotIgnoredInCode() {
+ let htmlString = "test matrix test"
+ let attributedStringFromHTML = attributedStringBuilder.fromHTML(htmlString)
+ checkLinkIn(attributedString: attributedStringFromHTML, expectedLink: "https://matrix.org", expectedRuns: 3)
}
func testUserMentionIsIgnoredInCode() {
diff --git a/changelog.d/2379.bugfix b/changelog.d/2379.bugfix
new file mode 100644
index 000000000..612548849
--- /dev/null
+++ b/changelog.d/2379.bugfix
@@ -0,0 +1 @@
+Fixed link colours inside code blocks.
\ No newline at end of file