From a1038f0ffe9d0b9e3f3589fce294a01845d8f2cd Mon Sep 17 00:00:00 2001 From: Stefan Ceriu Date: Tue, 16 Sep 2025 16:37:02 +0300 Subject: [PATCH] Remove extra new line before list items and useless paragraph style --- .../HTMLParsing/AttributedStringBuilderV2.swift | 8 -------- .../formattedBodyText.v2-iPad-en-GB.png | 4 ++-- .../formattedBodyText.v2-iPad-pseudo.png | 4 ++-- .../formattedBodyText.v2-iPhone-16-en-GB.png | 4 ++-- .../formattedBodyText.v2-iPhone-16-pseudo.png | 4 ++-- .../Sources/AttributedStringBuilderTests.swift | 15 +++++++++++++++ 6 files changed, 23 insertions(+), 16 deletions(-) diff --git a/ElementX/Sources/Other/HTMLParsing/AttributedStringBuilderV2.swift b/ElementX/Sources/Other/HTMLParsing/AttributedStringBuilderV2.swift index e90eb4edc..2565f98ae 100644 --- a/ElementX/Sources/Other/HTMLParsing/AttributedStringBuilderV2.swift +++ b/ElementX/Sources/Other/HTMLParsing/AttributedStringBuilderV2.swift @@ -207,9 +207,6 @@ struct AttributedStringBuilderV2: AttributedStringBuilderProtocol { case "ul", "ol": var listIndex = 1 content = attributedString(element: childElement, documentBody: documentBody, preserveFormatting: preserveFormatting, listTag: tag, listIndex: &listIndex, indentLevel: indentLevel + 1) - if listTag == nil { // If not within another list - content.insert(NSAttributedString(string: "\n"), at: 0) - } case "li": var bullet = "" @@ -224,11 +221,6 @@ struct AttributedStringBuilderV2: AttributedStringBuilderProtocol { content.insert(NSAttributedString(string: bullet), at: 0) content.append(NSAttributedString(string: "\n")) - let paragraphStyle = NSMutableParagraphStyle() - paragraphStyle.headIndent = CGFloat(indentLevel) * 20 - paragraphStyle.firstLineHeadIndent = CGFloat(indentLevel) * 20 - content.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: content.length)) - case "img": if let alt = try? childElement.attr("alt"), !alt.isEmpty { content = NSMutableAttributedString(string: "[img: \(alt)]") diff --git a/PreviewTests/Sources/__Snapshots__/PreviewTests/formattedBodyText.v2-iPad-en-GB.png b/PreviewTests/Sources/__Snapshots__/PreviewTests/formattedBodyText.v2-iPad-en-GB.png index ceba8a6c9..65198bceb 100644 --- a/PreviewTests/Sources/__Snapshots__/PreviewTests/formattedBodyText.v2-iPad-en-GB.png +++ b/PreviewTests/Sources/__Snapshots__/PreviewTests/formattedBodyText.v2-iPad-en-GB.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:c5597aa7bd2475300acdbf32ff6bc5c0781ef773a4ccc9f50c93fed3f4d67378 -size 1112093 +oid sha256:92079b7c81266f0d7fd47b97673dbac31a8c6781661dc520396ef7adcfdcdf85 +size 1111292 diff --git a/PreviewTests/Sources/__Snapshots__/PreviewTests/formattedBodyText.v2-iPad-pseudo.png b/PreviewTests/Sources/__Snapshots__/PreviewTests/formattedBodyText.v2-iPad-pseudo.png index ceba8a6c9..65198bceb 100644 --- a/PreviewTests/Sources/__Snapshots__/PreviewTests/formattedBodyText.v2-iPad-pseudo.png +++ b/PreviewTests/Sources/__Snapshots__/PreviewTests/formattedBodyText.v2-iPad-pseudo.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:c5597aa7bd2475300acdbf32ff6bc5c0781ef773a4ccc9f50c93fed3f4d67378 -size 1112093 +oid sha256:92079b7c81266f0d7fd47b97673dbac31a8c6781661dc520396ef7adcfdcdf85 +size 1111292 diff --git a/PreviewTests/Sources/__Snapshots__/PreviewTests/formattedBodyText.v2-iPhone-16-en-GB.png b/PreviewTests/Sources/__Snapshots__/PreviewTests/formattedBodyText.v2-iPhone-16-en-GB.png index f68da278d..aad1cc019 100644 --- a/PreviewTests/Sources/__Snapshots__/PreviewTests/formattedBodyText.v2-iPhone-16-en-GB.png +++ b/PreviewTests/Sources/__Snapshots__/PreviewTests/formattedBodyText.v2-iPhone-16-en-GB.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:c1ea06e567eb4549250863c7318003620a852d1144a993fd692500554c53bdb7 -size 1266526 +oid sha256:2d0942967dab15b03dc50f23be248cb23fee0d3c4baea535f71884af01fe54a6 +size 1264950 diff --git a/PreviewTests/Sources/__Snapshots__/PreviewTests/formattedBodyText.v2-iPhone-16-pseudo.png b/PreviewTests/Sources/__Snapshots__/PreviewTests/formattedBodyText.v2-iPhone-16-pseudo.png index f68da278d..aad1cc019 100644 --- a/PreviewTests/Sources/__Snapshots__/PreviewTests/formattedBodyText.v2-iPhone-16-pseudo.png +++ b/PreviewTests/Sources/__Snapshots__/PreviewTests/formattedBodyText.v2-iPhone-16-pseudo.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:c1ea06e567eb4549250863c7318003620a852d1144a993fd692500554c53bdb7 -size 1266526 +oid sha256:2d0942967dab15b03dc50f23be248cb23fee0d3c4baea535f71884af01fe54a6 +size 1264950 diff --git a/UnitTests/Sources/AttributedStringBuilderTests.swift b/UnitTests/Sources/AttributedStringBuilderTests.swift index 7b6c6553b..f8a977c27 100644 --- a/UnitTests/Sources/AttributedStringBuilderTests.swift +++ b/UnitTests/Sources/AttributedStringBuilderTests.swift @@ -738,6 +738,21 @@ class AttributedStringBuilderV1Tests: XCTestCase { } } + func testListTags() { + let htmlString = "

like

\n\n" + + guard let attributedString = attributedStringBuilder.fromHTML(htmlString) else { + XCTFail("Could not build the attributed string") + return + } + + if AttributedStringBuilder.useNextGenHTMLParser { + XCTAssertEqual(String(attributedString.characters), "like\n\n • this\ntest") + } else { + XCTAssertEqual(String(attributedString.characters), "like\n\t•\tthis\u{2028}test") + } + } + // MARK: - Phishing prevention func testPhishingLink() {