diff --git a/Sources/HTMLStreamer/AttributedStringConverter.swift b/Sources/HTMLStreamer/AttributedStringConverter.swift index a4d2436..594db7a 100644 --- a/Sources/HTMLStreamer/AttributedStringConverter.swift +++ b/Sources/HTMLStreamer/AttributedStringConverter.swift @@ -28,6 +28,7 @@ public struct AttributedStringConverter { private var styleStack: [Style] = [] private var previouslyFinishedBlockElement = false private var currentElementIsEmpty = true + private var previouslyFinishedListItem = false // The current run of text w/o styles changing private var currentRun: String = "" @@ -135,7 +136,7 @@ public struct AttributedStringConverter { finishRun() styleStack.append(.unorderedList) case "li": - if str.length != 0 || !currentRun.isEmpty { + if previouslyFinishedListItem { currentRun.append("\n") } let marker: String @@ -186,12 +187,15 @@ public struct AttributedStringConverter { finishRun() removeLastStyle(.orderedList) finishBlockElement() + previouslyFinishedListItem = false case "ul": finishRun() removeLastStyle(.unorderedList) finishBlockElement() + previouslyFinishedListItem = false case "li": finishRun() + previouslyFinishedListItem = true default: break } diff --git a/Tests/HTMLStreamerTests/AttributedStringConverterTests.swift b/Tests/HTMLStreamerTests/AttributedStringConverterTests.swift index 2c5147d..f3a520d 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\n\t2.\tc", attributes: [ + .font: font, + .paragraphStyle: listParagraphStyle, + .foregroundColor: color, + ])) + XCTAssertEqual(convert("

a

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