mirror of
https://github.com/element-hq/element-x-ios.git
synced 2025-03-10 21:39:12 +00:00
Prevent links from showing up in color within code blocks, simplify foreground color stripping
This commit is contained in:
parent
a401976b2b
commit
099b69d2f2
@ -49,9 +49,9 @@ struct AttributedStringBuilder: AttributedStringBuilderProtocol {
|
||||
}
|
||||
|
||||
let mutableAttributedString = NSMutableAttributedString(string: string)
|
||||
removeDefaultForegroundColors(mutableAttributedString)
|
||||
addLinksAndMentions(mutableAttributedString)
|
||||
detectPermalinks(mutableAttributedString)
|
||||
removeLinkColors(mutableAttributedString)
|
||||
|
||||
let result = try? AttributedString(mutableAttributedString, including: \.elementX)
|
||||
Self.cacheValue(result, forKey: string, cacheKey: cacheKey)
|
||||
@ -105,12 +105,11 @@ struct AttributedStringBuilder: AttributedStringBuilderProtocol {
|
||||
}
|
||||
|
||||
let mutableAttributedString = NSMutableAttributedString(attributedString: attributedString)
|
||||
removeDefaultForegroundColor(mutableAttributedString)
|
||||
removeDefaultForegroundColors(mutableAttributedString)
|
||||
addLinksAndMentions(mutableAttributedString)
|
||||
replaceMarkedBlockquotes(mutableAttributedString)
|
||||
replaceMarkedCodeBlocks(mutableAttributedString)
|
||||
detectPermalinks(mutableAttributedString)
|
||||
removeLinkColors(mutableAttributedString)
|
||||
removeDTCoreTextArtifacts(mutableAttributedString)
|
||||
|
||||
let result = try? AttributedString(mutableAttributedString, including: \.elementX)
|
||||
@ -140,12 +139,8 @@ struct AttributedStringBuilder: AttributedStringBuilderProtocol {
|
||||
return result
|
||||
}
|
||||
|
||||
private func removeDefaultForegroundColor(_ attributedString: NSMutableAttributedString) {
|
||||
attributedString.enumerateAttribute(.foregroundColor, in: .init(location: 0, length: attributedString.length), options: []) { value, range, _ in
|
||||
if value as? UIColor == UIColor.black {
|
||||
attributedString.removeAttribute(.foregroundColor, range: range)
|
||||
}
|
||||
}
|
||||
private func removeDefaultForegroundColors(_ attributedString: NSMutableAttributedString) {
|
||||
attributedString.removeAttribute(.foregroundColor, range: .init(location: 0, length: attributedString.length))
|
||||
}
|
||||
|
||||
private func addLinksAndMentions(_ attributedString: NSMutableAttributedString) {
|
||||
@ -293,14 +288,6 @@ struct AttributedStringBuilder: AttributedStringBuilderProtocol {
|
||||
}
|
||||
}
|
||||
|
||||
private func removeLinkColors(_ attributedString: NSMutableAttributedString) {
|
||||
attributedString.enumerateAttribute(.link, in: .init(location: 0, length: attributedString.length), options: []) { value, range, _ in
|
||||
if value != nil {
|
||||
attributedString.removeAttribute(.foregroundColor, range: range)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func removeDTCoreTextArtifacts(_ attributedString: NSMutableAttributedString) {
|
||||
guard attributedString.length > 0 else {
|
||||
return
|
||||
|
@ -207,15 +207,17 @@ class AttributedStringBuilderTests: XCTestCase {
|
||||
checkLinkIn(attributedString: attributedStringBuilder.fromPlain(string), expectedLink: expectedLink.absoluteString, expectedRuns: 3)
|
||||
}
|
||||
|
||||
// `Plain link in codeblock: https://www.matrix.org`, Link tag in codeblock: <a href=\"https://www.matrix.org/\">link</a>, plain link: https://www.matrix.org, link tag: <a href=\"https://www.matrix.org/\">link</a>
|
||||
|
||||
func testDefaultFont() {
|
||||
let htmlString = "<b>Test</b> <i>string</i>."
|
||||
let htmlString = "<b>Test</b> <i>string</i> "
|
||||
|
||||
guard let attributedString = attributedStringBuilder.fromHTML(htmlString) else {
|
||||
XCTFail("Could not build the attributed string")
|
||||
return
|
||||
}
|
||||
|
||||
XCTAssertEqual(attributedString.runs.count, 4)
|
||||
XCTAssertEqual(attributedString.runs.count, 3)
|
||||
|
||||
for run in attributedString.runs {
|
||||
XCTAssertEqual(run.uiKit.font?.familyName, UIFont.preferredFont(forTextStyle: .body).familyName)
|
||||
@ -223,14 +225,14 @@ class AttributedStringBuilderTests: XCTestCase {
|
||||
}
|
||||
|
||||
func testDefaultForegroundColor() {
|
||||
let htmlString = "<b>Test</b> <i>string</i>."
|
||||
let htmlString = "<b>Test</b> <i>string</i> <a href=\"https://www.matrix.org/\">link</a> <code><a href=\"https://www.matrix.org/\">link</a></code>"
|
||||
|
||||
guard let attributedString = attributedStringBuilder.fromHTML(htmlString) else {
|
||||
XCTFail("Could not build the attributed string")
|
||||
return
|
||||
}
|
||||
|
||||
XCTAssertEqual(attributedString.runs.count, 4)
|
||||
XCTAssertEqual(attributedString.runs.count, 7)
|
||||
|
||||
for run in attributedString.runs {
|
||||
XCTAssertNil(run.uiKit.foregroundColor)
|
||||
@ -246,16 +248,18 @@ class AttributedStringBuilderTests: XCTestCase {
|
||||
return
|
||||
}
|
||||
|
||||
XCTAssertEqual(attributedString.runs.count, 8)
|
||||
XCTAssertEqual(attributedString.runs.count, 3)
|
||||
|
||||
var foundLink = false
|
||||
// Foreground colors should be completely stripped from the attributed string
|
||||
// letting UI components chose the defaults (e.g. tintColor)
|
||||
for run in attributedString.runs {
|
||||
if run.link != nil {
|
||||
XCTAssertEqual(run.link?.host, "www.matrix.org")
|
||||
XCTAssertNil(run.uiKit.foregroundColor)
|
||||
foundLink = true
|
||||
} else {
|
||||
XCTAssertNotNil(run.uiKit.foregroundColor)
|
||||
XCTAssertNil(run.uiKit.foregroundColor)
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user