mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-03 16:10:20 +00:00
Fixes #1418 - Preserve new lines when parsing html strings
This commit is contained in:
committed by
Stefan Ceriu
parent
0511bef33a
commit
35685db3c4
@@ -56,8 +56,7 @@ struct AttributedStringBuilder: AttributedStringBuilderProtocol {
|
|||||||
// that could happen with the default HTML renderer of NSAttributedString which is a
|
// that could happen with the default HTML renderer of NSAttributedString which is a
|
||||||
// webview.
|
// webview.
|
||||||
func fromHTML(_ htmlString: String?) -> AttributedString? {
|
func fromHTML(_ htmlString: String?) -> AttributedString? {
|
||||||
guard let htmlString,
|
guard let htmlString else {
|
||||||
let data = htmlString.data(using: .utf8) else {
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,6 +64,13 @@ struct AttributedStringBuilder: AttributedStringBuilderProtocol {
|
|||||||
return cached
|
return cached
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Trick DTCoreText into preserving newlines
|
||||||
|
let adjustedHTMLString = htmlString.replacingOccurrences(of: "\n", with: "<br>")
|
||||||
|
|
||||||
|
guard let data = adjustedHTMLString.data(using: .utf8) else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
let defaultFont = UIFont.preferredFont(forTextStyle: .body)
|
let defaultFont = UIFont.preferredFont(forTextStyle: .body)
|
||||||
|
|
||||||
let parsingOptions: [String: Any] = [
|
let parsingOptions: [String: Any] = [
|
||||||
|
|||||||
@@ -396,6 +396,17 @@ class AttributedStringBuilderTests: XCTestCase {
|
|||||||
XCTAssertEqual(numberOfBlockquotes, 3, "Couldn't find all the blockquotes")
|
XCTAssertEqual(numberOfBlockquotes, 3, "Couldn't find all the blockquotes")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testNewLinesArePreserved() {
|
||||||
|
let htmlString = "Bob's\nyour\nuncle\nand\nFanny's\nyour\naunt"
|
||||||
|
|
||||||
|
guard let attributedString = attributedStringBuilder.fromHTML(htmlString) else {
|
||||||
|
XCTFail("Could not build the attributed string")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
XCTAssertEqual(String(attributedString.characters), htmlString.replacingOccurrences(of: "\n", with: "\u{2028}"))
|
||||||
|
}
|
||||||
|
|
||||||
// MARK: - Private
|
// MARK: - Private
|
||||||
|
|
||||||
private func checkLinkIn(attributedString: AttributedString?, expectedLink: String, expectedRuns: Int) {
|
private func checkLinkIn(attributedString: AttributedString?, expectedLink: String, expectedRuns: Int) {
|
||||||
|
|||||||
Reference in New Issue
Block a user