Fix #4913 - Email addresses are detected as URLs

This commit is contained in:
Stefan Ceriu
2026-01-12 15:47:44 +02:00
committed by Stefan Ceriu
parent 29dfba140d
commit 8f9df47a2c
2 changed files with 20 additions and 3 deletions
@@ -330,12 +330,17 @@ struct AttributedStringBuilder: AttributedStringBuilderProtocol {
}) })
matches.append(contentsOf: MatrixEntityRegex.linkRegex.matches(in: string).compactMap { match in matches.append(contentsOf: MatrixEntityRegex.linkRegex.matches(in: string).compactMap { match in
guard let matchRange = Range(match.range, in: string) else { guard let matchRange = Range(match.range, in: string), let url = match.url else {
return nil return nil
} }
let link = String(string[matchRange]).asSanitizedLink // If the NSDataDetector found a hyperlink then sanitise it
return TextParsingMatch(type: .link(urlString: link), range: match.range) if url.scheme?.contains("http") ?? false {
// Use the underlying string so it gets an `https` scheme if it didn't have any
return TextParsingMatch(type: .link(urlString: String(string[matchRange]).asSanitizedLink), range: match.range)
} else { // otherwise use it as it is e.g. mailto: (https://github.com/element-hq/element-x-ios/issues/4913)
return TextParsingMatch(type: .link(urlString: url.absoluteString), range: match.range)
}
}) })
matches.append(contentsOf: MatrixEntityRegex.allUsersRegex.matches(in: attributedString.string).map { match in matches.append(contentsOf: MatrixEntityRegex.allUsersRegex.matches(in: attributedString.string).map { match in
@@ -114,6 +114,18 @@ class AttributedStringBuilderTests: XCTestCase {
XCTAssertEqual(link, "https://matrix.org") XCTAssertEqual(link, "https://matrix.org")
} }
func testMailToLinks() {
let plainString = "Linking to email addresses like stefan@matrix.org should work as well"
guard let attributedString = attributedStringBuilder.fromPlain(plainString) else {
XCTFail("Could not build the attributed string")
return
}
let link = attributedString.runs.first { $0.link != nil }?.link
XCTAssertEqual(link, "mailto:stefan@matrix.org")
}
func testRenderHTMLStringWithLinkInHeader() { func testRenderHTMLStringWithLinkInHeader() {
let h1HTMLString = "<h1><a href=\"https://matrix.org/\">Matrix.org</a></h1>" let h1HTMLString = "<h1><a href=\"https://matrix.org/\">Matrix.org</a></h1>"
let h2HTMLString = "<h2><a href=\"https://matrix.org/\">Matrix.org</a></h2>" let h2HTMLString = "<h2><a href=\"https://matrix.org/\">Matrix.org</a></h2>"