From 74baa2912f9bfe23c6f00bdff109dcedcd397c31 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Tue, 16 Jan 2024 19:26:40 -0500 Subject: [PATCH] Fix extra line breaks being inserted before lists --- .../HTMLStreamer/AttributedStringConverter.swift | 2 +- .../AttributedStringConverterTests.swift | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/Sources/HTMLStreamer/AttributedStringConverter.swift b/Sources/HTMLStreamer/AttributedStringConverter.swift index a4d2436..528291a 100644 --- a/Sources/HTMLStreamer/AttributedStringConverter.swift +++ b/Sources/HTMLStreamer/AttributedStringConverter.swift @@ -135,7 +135,7 @@ public struct AttributedStringConverter { finishRun() styleStack.append(.unorderedList) case "li": - if str.length != 0 || !currentRun.isEmpty { + if !currentRun.isEmpty { currentRun.append("\n") } let marker: String diff --git a/Tests/HTMLStreamerTests/AttributedStringConverterTests.swift b/Tests/HTMLStreamerTests/AttributedStringConverterTests.swift index 2c5147d..1821e3e 100644 --- a/Tests/HTMLStreamerTests/AttributedStringConverterTests.swift +++ b/Tests/HTMLStreamerTests/AttributedStringConverterTests.swift @@ -319,4 +319,19 @@ final class AttributedStringConverterTests: XCTestCase { XCTAssertEqual(convert("

inside
quote
after

"), result) } + func testParagraphFollowedByList() { + let result = NSMutableAttributedString() + result.append(NSAttributedString(string: "a\n\n", attributes: [ + .font: font, + .paragraphStyle: NSParagraphStyle.default, + .foregroundColor: color, + ])) + result.append(NSAttributedString(string: "\t1.\tb", attributes: [ + .font: font, + .paragraphStyle: listParagraphStyle, + .foregroundColor: color, + ])) + XCTAssertEqual(convert("

a

  1. b
"), result) + } + }