diff --git a/ElementX/Sources/Other/HTMLParsing/AttributedStringBuilder.swift b/ElementX/Sources/Other/HTMLParsing/AttributedStringBuilder.swift index 7dab6d9f0..750c1935a 100644 --- a/ElementX/Sources/Other/HTMLParsing/AttributedStringBuilder.swift +++ b/ElementX/Sources/Other/HTMLParsing/AttributedStringBuilder.swift @@ -170,21 +170,7 @@ struct AttributedStringBuilder: AttributedStringBuilderProtocol { if let value = value as? UIColor, value == temporaryCodeBlockMarkingColor { attributedString.addAttribute(.backgroundColor, value: UIColor(.compound._bgCodeBlock) as Any, 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) + attributedString.removeAttribute(.link, range: range) } } } @@ -264,6 +250,11 @@ struct AttributedStringBuilder: AttributedStringBuilderProtocol { return } + // Don't add any extra attributes within codeblocks + if attributedString.attribute(.backgroundColor, at: match.range.location, effectiveRange: nil) as? UIColor == temporaryCodeBlockMarkingColor { + return + } + switch match.type { case .atRoom: attributedString.addAttribute(.MatrixAllUsersMention, value: true, range: match.range) diff --git a/UnitTests/Sources/AttributedStringBuilderTests.swift b/UnitTests/Sources/AttributedStringBuilderTests.swift index 74fdc123a..da1619921 100644 --- a/UnitTests/Sources/AttributedStringBuilderTests.swift +++ b/UnitTests/Sources/AttributedStringBuilderTests.swift @@ -491,10 +491,10 @@ class AttributedStringBuilderTests: XCTestCase { XCTAssertNil(attributedStringFromHTML?.link) } - func testHyperlinksAreNotIgnoredInCode() { + func testHyperlinksAreIgnoredInCode() { let htmlString = "
test matrix test
" let attributedStringFromHTML = attributedStringBuilder.fromHTML(htmlString) - checkLinkIn(attributedString: attributedStringFromHTML, expectedLink: "https://matrix.org", expectedRuns: 3) + XCTAssertNil(attributedStringFromHTML?.link) } func testUserMentionIsIgnoredInCode() { @@ -504,6 +504,13 @@ class AttributedStringBuilderTests: XCTestCase { XCTAssertNil(attributedStringFromHTML?.attachment) } + func testPlainTextUserMentionIsIgnoredInCode() { + let htmlString = "
Hey @some.user.ceriu:matrix.org
" + let attributedStringFromHTML = attributedStringBuilder.fromHTML(htmlString) + XCTAssert(attributedStringFromHTML?.runs.count == 1) + XCTAssertNil(attributedStringFromHTML?.attachment) + } + func testAllUsersIsIgnoredInCode() { let htmlString = "
test @room test
" let attributedStringFromHTML = attributedStringBuilder.fromHTML(htmlString)