mirror of
https://github.com/element-hq/element-x-ios.git
synced 2025-03-10 13:37:11 +00:00
Fix for line breaks (#2224)
This commit is contained in:
parent
4cba5687c6
commit
a248bdbd95
@ -87,3 +87,27 @@ extension String {
|
||||
return "\(prefix(length))…"
|
||||
}
|
||||
}
|
||||
|
||||
extension String {
|
||||
func replacingHtmlBreaksOccurrences() -> String {
|
||||
var result = self
|
||||
let pattern = #"</p>(\n+)<p>"#
|
||||
|
||||
guard let regex = try? NSRegularExpression(pattern: pattern, options: []) else {
|
||||
return result
|
||||
}
|
||||
let matches = regex.matches(in: self, options: [], range: NSRange(location: 0, length: utf16.count))
|
||||
|
||||
for match in matches.reversed() {
|
||||
guard let range = Range(match.range, in: self),
|
||||
let innerMatchRange = Range(match.range(at: 1), in: self) else {
|
||||
continue
|
||||
}
|
||||
let numberOfBreaks = (self[innerMatchRange].components(separatedBy: "\n").count - 1)
|
||||
let replacement = "<br>" + String(repeating: "<br>", count: numberOfBreaks)
|
||||
result.replaceSubrange(range, with: replacement)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
@ -73,14 +73,16 @@ struct AttributedStringBuilder: AttributedStringBuilderProtocol {
|
||||
// that could happen with the default HTML renderer of NSAttributedString which is a
|
||||
// webview.
|
||||
func fromHTML(_ htmlString: String?) -> AttributedString? {
|
||||
guard let htmlString else {
|
||||
guard var originalHTMLString = htmlString else {
|
||||
return nil
|
||||
}
|
||||
|
||||
if let cached = Self.caches[cacheKey]?.value(forKey: htmlString) {
|
||||
if let cached = Self.caches[cacheKey]?.value(forKey: originalHTMLString) {
|
||||
return cached
|
||||
}
|
||||
|
||||
let htmlString = originalHTMLString.replacingHtmlBreaksOccurrences()
|
||||
|
||||
guard let data = htmlString.data(using: .utf8) else {
|
||||
return nil
|
||||
}
|
||||
|
@ -204,7 +204,8 @@ struct FormattedBodyText_Previews: PreviewProvider, TestablePreview {
|
||||
<code>Hello world</code>
|
||||
""",
|
||||
"<p>This is a list</p>\n<ul>\n<li>One</li>\n<li>Two</li>\n<li>And number 3</li>\n</ul>\n",
|
||||
"<ul><li>First item</li><li>Second item</li><li>Third item</li></ul>"
|
||||
"<ul><li>First item</li><li>Second item</li><li>Third item</li></ul>",
|
||||
"<p>test</p>\n<p>test</p>"
|
||||
]
|
||||
|
||||
let attributedStringBuilder = AttributedStringBuilder(permalinkBaseURL: ServiceLocator.shared.settings.permalinkBaseURL, mentionBuilder: MentionBuilder())
|
||||
|
@ -88,4 +88,27 @@ class StringTests: XCTestCase {
|
||||
func testEllipsizeNotNeeded() {
|
||||
XCTAssertEqual("ellipsize".ellipsize(length: 15), "ellipsize")
|
||||
}
|
||||
|
||||
func testReplaceBreakOccurrences() {
|
||||
let input0 = "</p><p>"
|
||||
let input1 = "</p>\n<p>"
|
||||
let input2 = "</p>\n\n<p>"
|
||||
let input3 = "</p>\n\n\n\n<p>"
|
||||
let input4 = "<p>a</p>\n<p>b</p>"
|
||||
let input5 = "empty"
|
||||
|
||||
let expectedOutput0 = input0
|
||||
let expectedOutput1 = "<br><br>"
|
||||
let expectedOutput2 = "<br><br><br>"
|
||||
let expectedOutput3 = "<br><br><br><br><br>"
|
||||
let expectedOutput4 = "<p>a<br><br>b</p>"
|
||||
let expectedOutput5 = input5
|
||||
|
||||
XCTAssertEqual(input0.replacingHtmlBreaksOccurrences(), expectedOutput0)
|
||||
XCTAssertEqual(input1.replacingHtmlBreaksOccurrences(), expectedOutput1)
|
||||
XCTAssertEqual(input2.replacingHtmlBreaksOccurrences(), expectedOutput2)
|
||||
XCTAssertEqual(input3.replacingHtmlBreaksOccurrences(), expectedOutput3)
|
||||
XCTAssertEqual(input4.replacingHtmlBreaksOccurrences(), expectedOutput4)
|
||||
XCTAssertEqual(input5.replacingHtmlBreaksOccurrences(), expectedOutput5)
|
||||
}
|
||||
}
|
||||
|
BIN
UnitTests/__Snapshots__/PreviewTests/test_formattedBodyText.1.png
(Stored with Git LFS)
BIN
UnitTests/__Snapshots__/PreviewTests/test_formattedBodyText.1.png
(Stored with Git LFS)
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user