Fix #4856, fix #4431 - Normalise HTML whitespaces using SwiftSoup's own text()/TextNode.normaliseWhitespace methods

This commit is contained in:
Stefan Ceriu
2026-01-20 16:44:56 +02:00
committed by Stefan Ceriu
parent 7d47a290ee
commit 40b655bde6
16 changed files with 68 additions and 43 deletions
@@ -122,9 +122,9 @@ struct AttributedStringBuilder: AttributedStringBuilderProtocol {
for node in element.getChildNodes() {
if let textNode = node as? TextNode {
// If this node is plain text just append its preformatted contents
// If this node is plain text append the whitespace normalised version
if node.parent() == documentBody {
result.append(NSAttributedString(string: textNode.getWholeText()))
result.append(NSAttributedString(string: textNode.text()))
continue
}
@@ -237,7 +237,7 @@ struct AttributedStringBuilder: AttributedStringBuilderProtocol {
content = attributedString(element: childElement, documentBody: documentBody, preserveFormatting: preserveFormatting, listTag: tag, listIndex: &listIndex, indentLevel: indentLevel + 1)
if indentLevel > 0 {
if indentLevel > 0 || !element.ownText().isEmpty {
content.insert(NSAttributedString("\n"), at: 0)
}
@@ -29,11 +29,11 @@ enum HTMLFixtures: String, CaseIterable {
"""
case .headers:
"""
<h1>H1 Header</h1>
<h2>H2 Header</h2>
<h3>H3 Header</h3>
<h4>H4 Header</h4>
<h5>H5 Header</h5>
<h1>H1 Header</h1>\
<h2>H2 Header</h2>\
<h3>H3 Header</h3>\
<h4>H4 Header</h4>\
<h5>H5 Header</h5>\
<h6>H6 Header</h6>
"""
case .paragraphs:
@@ -199,11 +199,10 @@ struct MessageText_Previews: PreviewProvider, TestablePreview {
private static let htmlStringWithQuote =
"""
<blockquote>A blockquote that is long and goes onto multiple lines as the first item in the message</blockquote>
<p>Then another line of text here to reply to the blockquote, which is also a multiline component.</p>
<blockquote>A blockquote that is long and goes onto multiple lines as the first item in the message</blockquote><p>Then another line of text here to reply to the blockquote, which is also a multiline component.</p>
"""
private static let htmlStringWithList = "<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"
private static let htmlStringWithList = "<p>This is a list</p>\n<ul><li>One</li>\n<li>Two</li>\n<li>And number 3</li>\n</ul>\n"
private static let attributedStringBuilder = AttributedStringBuilder(mentionBuilder: MentionBuilder())