mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-04 19:44:05 +00:00
Fixed the link color and improved link dection in code blocks (#2466)
This commit is contained in:
@@ -151,8 +151,20 @@ struct AttributedStringBuilder: AttributedStringBuilderProtocol {
|
|||||||
if let value = value as? UIColor,
|
if let value = value as? UIColor,
|
||||||
value == temporaryCodeBlockMarkingColor {
|
value == temporaryCodeBlockMarkingColor {
|
||||||
attributedString.addAttribute(.backgroundColor, value: UIColor(.compound._bgCodeBlock) as Any, range: range)
|
attributedString.addAttribute(.backgroundColor, value: UIColor(.compound._bgCodeBlock) as Any, range: range)
|
||||||
// Codeblocks should not have links and all users mentions
|
// Codebloks should not have explicit links
|
||||||
attributedString.removeAttribute(.link, range: range)
|
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)
|
attributedString.removeAttribute(.MatrixAllUsersMention, range: range)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -493,11 +493,22 @@ class AttributedStringBuilderTests: XCTestCase {
|
|||||||
checkAttachment(attributedString: attributedStringFromPlain2, expectedRuns: 1)
|
checkAttachment(attributedString: attributedStringFromPlain2, expectedRuns: 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testLinksAreIgnoredInCode() {
|
func testURLsAreIgnoredInCode() {
|
||||||
let htmlString = "<pre><code>test https://matrix.org test</code></pre>"
|
var htmlString = "<pre><code>test https://matrix.org test</code></pre>"
|
||||||
let attributedStringFromHTML = attributedStringBuilder.fromHTML(htmlString)
|
var attributedStringFromHTML = attributedStringBuilder.fromHTML(htmlString)
|
||||||
XCTAssert(attributedStringFromHTML?.runs.count == 1)
|
XCTAssert(attributedStringFromHTML?.runs.count == 1)
|
||||||
XCTAssertNil(attributedStringFromHTML?.link)
|
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() {
|
func testUserMentionIsIgnoredInCode() {
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
Fixed link colours inside code blocks.
|
||||||
Reference in New Issue
Block a user