mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-05 11:58:32 +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())
|
||||
|
||||
Reference in New Issue
Block a user