Fixed the link color and improved link dection in code blocks (#2466)

This commit is contained in:
Mauro
2024-02-14 17:45:23 +01:00
committed by GitHub
parent 66f0b71381
commit 47e419a6a6
3 changed files with 29 additions and 5 deletions

View File

@@ -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)
}
}

View File

@@ -493,11 +493,22 @@ class AttributedStringBuilderTests: XCTestCase {
checkAttachment(attributedString: attributedStringFromPlain2, expectedRuns: 1)
}
func testLinksAreIgnoredInCode() {
let htmlString = "<pre><code>test https://matrix.org test</code></pre>"
let attributedStringFromHTML = attributedStringBuilder.fromHTML(htmlString)
func testURLsAreIgnoredInCode() {
var htmlString = "<pre><code>test https://matrix.org test</code></pre>"
var attributedStringFromHTML = attributedStringBuilder.fromHTML(htmlString)
XCTAssert(attributedStringFromHTML?.runs.count == 1)
XCTAssertNil(attributedStringFromHTML?.link)
htmlString = "<pre><code>matrix.org</code></pre>"
attributedStringFromHTML = attributedStringBuilder.fromHTML(htmlString)
XCTAssert(attributedStringFromHTML?.runs.count == 1)
XCTAssertNil(attributedStringFromHTML?.link)
}
func testHyperlinksAreNotIgnoredInCode() {
let htmlString = "<pre><code>test <a href=\"https://matrix.org\">matrix</a> test</code></pre>"
let attributedStringFromHTML = attributedStringBuilder.fromHTML(htmlString)
checkLinkIn(attributedString: attributedStringFromHTML, expectedLink: "https://matrix.org", expectedRuns: 3)
}
func testUserMentionIsIgnoredInCode() {

1
changelog.d/2379.bugfix Normal file
View File

@@ -0,0 +1 @@
Fixed link colours inside code blocks.