diff --git a/ElementX/Sources/Other/HTMLParsing/AttributedStringBuilder.swift b/ElementX/Sources/Other/HTMLParsing/AttributedStringBuilder.swift index 8d4bc9bf3..e74e616f8 100644 --- a/ElementX/Sources/Other/HTMLParsing/AttributedStringBuilder.swift +++ b/ElementX/Sources/Other/HTMLParsing/AttributedStringBuilder.swift @@ -236,19 +236,25 @@ struct AttributedStringBuilder: AttributedStringBuilderProtocol { } content = attributedString(element: childElement, documentBody: documentBody, preserveFormatting: preserveFormatting, listTag: tag, listIndex: &listIndex, indentLevel: indentLevel + 1) + + if indentLevel > 0 { + content.insert(NSAttributedString("\n"), at: 0) + } case "li": - var bullet = "" + var bullet = String(repeating: " ", count: indentLevel) if listTag == "ol" { - bullet = "\(listIndex). " + bullet += "\(listIndex). " listIndex += 1 } else { - bullet = "• " + bullet += "• " } content = attributedString(element: childElement, documentBody: documentBody, preserveFormatting: preserveFormatting, listTag: listTag, listIndex: &childIndex, indentLevel: indentLevel + 1) content.insert(NSAttributedString(string: bullet), at: 0) - content.append(NSAttributedString(string: "\n")) + if !(content.string.last?.isNewline ?? false) { + content.append(NSAttributedString(string: "\n")) + } case "img": if let alt = try? childElement.attr("alt"), !alt.isEmpty { diff --git a/UnitTests/Sources/AttributedStringBuilderTests.swift b/UnitTests/Sources/AttributedStringBuilderTests.swift index 79fca3ddc..405fa057a 100644 --- a/UnitTests/Sources/AttributedStringBuilderTests.swift +++ b/UnitTests/Sources/AttributedStringBuilderTests.swift @@ -686,7 +686,51 @@ class AttributedStringBuilderTests: XCTestCase { return } - XCTAssertEqual(String(attributedString.characters), "like\n\n • this\ntest") + XCTAssertEqual(String(attributedString.characters), "like\n\n • this\ntest") + } + + func testUnorderedList() { + let htmlString = "