mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-02 03:06:37 +00:00
Fix for line breaks (#2224)
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:8388baf6d6dff565d85ce4e75053f465a923e3e3216649e860f8103fe2bf2293
|
||||
size 212392
|
||||
oid sha256:afb864a0a68877a81dc46f01add0830bb6de243de11c70181624bd48b3886916
|
||||
size 212427
|
||||
|
||||
Reference in New Issue
Block a user