Skip to content

Commit

Permalink
Add default schemes for detected links that don't have any
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanceriu committed Sep 7, 2023
1 parent fad608c commit 29b2b7f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
12 changes: 10 additions & 2 deletions ElementX/Sources/Other/HTMLParsing/AttributedStringBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ struct AttributedStringBuilder: AttributedStringBuilderProtocol {
// As of right now we do not handle event id links in any way so there is no need to add them as links
// matches.append(contentsOf: MatrixEntityRegex.eventIdentifierRegex.matches(in: string, options: []))
matches.append(contentsOf: MatrixEntityRegex.roomAliasRegex.matches(in: string, options: []))
matches.append(contentsOf: MatrixEntityRegex.linkRegex.matches(in: string, options: []))

let linkMatches = MatrixEntityRegex.linkRegex.matches(in: string, options: [])
matches.append(contentsOf: linkMatches)
guard matches.count > 0 else {
return
}
Expand All @@ -188,7 +190,13 @@ struct AttributedStringBuilder: AttributedStringBuilderProtocol {
return
}

attributedString.addAttribute(.link, value: string[matchRange] as Any, range: match.range)
var link = String(string[matchRange])

if linkMatches.contains(match), !link.contains("://") {
link.insert(contentsOf: "https://", at: link.startIndex)
}

attributedString.addAttribute(.link, value: link as Any, range: match.range)
}
}

Expand Down
19 changes: 18 additions & 1 deletion UnitTests/Sources/AttributedStringBuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,23 @@ class AttributedStringBuilderTests: XCTestCase {
XCTAssertEqual(link?.host, "www.matrix.org")
}

func testLinkDefaultScheme() {
let plainString = "This text contains a matrix.org link."

guard let attributedString = attributedStringBuilder.fromPlain(plainString) else {
XCTFail("Could not build the attributed string")
return
}

XCTAssertEqual(String(attributedString.characters), plainString)

XCTAssertEqual(attributedString.runs.count, 3)

let link = attributedString.runs.first(where: { $0.link != nil })?.link

XCTAssertEqual(link, "https://matrix.org")
}

func testRenderHTMLStringWithLinkInHeader() {
let h1HTMLString = "<h1><a href=\"https://www.matrix.org/\">Matrix.org</a></h1>"
let h2HTMLString = "<h2><a href=\"https://www.matrix.org/\">Matrix.org</a></h2>"
Expand Down Expand Up @@ -244,7 +261,7 @@ class AttributedStringBuilderTests: XCTestCase {
var foundLink = false
for run in attributedString.runs {
if run.link != nil {
XCTAssertEqual(run.link?.path, "www.matrix.org")
XCTAssertEqual(run.link?.host, "www.matrix.org")
XCTAssertNil(run.uiKit.foregroundColor)
foundLink = true
} else {
Expand Down
1 change: 1 addition & 0 deletions changelog.d/pr-1651.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add default schemes for detected links that don't have any

0 comments on commit 29b2b7f

Please sign in to comment.