Fixes #4683 - Add support for nested lists

This commit is contained in:
Stefan Ceriu
2025-11-12 10:41:53 +02:00
committed by Stefan Ceriu
parent 6d57153136
commit 2f9275a0ef
2 changed files with 67 additions and 6 deletions

View File

@@ -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 {

View File

@@ -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 = "<ul><li>1</li><li>2</li><li>3</li></ul>"
guard let attributedString = attributedStringBuilder.fromHTML(htmlString) else {
XCTFail("Could not build the attributed string")
return
}
XCTAssertEqual(String(attributedString.characters), " • 1\n • 2\n • 3")
}
func testNestedUnorderedList() {
let htmlString = "<ul><li>A<ul><li>A1</li><li>A2</li><li>A3</li></ul></li><li>B</li><li>C</li></ul>"
guard let attributedString = attributedStringBuilder.fromHTML(htmlString) else {
XCTFail("Could not build the attributed string")
return
}
XCTAssertEqual(String(attributedString.characters), " • A\n • A1\n • A2\n • A3\n • B\n • C")
}
func testOrderedList() {
let htmlString = "<ol><li>1</li><li>2</li><li>3</li></ol>"
guard let attributedString = attributedStringBuilder.fromHTML(htmlString) else {
XCTFail("Could not build the attributed string")
return
}
XCTAssertEqual(String(attributedString.characters), " 1. 1\n 2. 2\n 3. 3")
}
func testNestedOrderedList() {
let htmlString = "<ol><li>A<ol><li>A1</li><li>A2</li><li>A3</li></ol></li><li>B</li><li>C</li></ol>"
guard let attributedString = attributedStringBuilder.fromHTML(htmlString) else {
XCTFail("Could not build the attributed string")
return
}
XCTAssertEqual(String(attributedString.characters), " 1. A\n 1. A1\n 2. A2\n 3. A3\n 2. B\n 3. C")
}
func testOutOfOrderListNubmering() {
@@ -697,7 +741,18 @@ class AttributedStringBuilderTests: XCTestCase {
return
}
XCTAssertEqual(String(attributedString.characters), " 2. this is a two")
XCTAssertEqual(String(attributedString.characters), " 2. this is a two")
}
func testNestedHeterogeneousLists() {
let htmlString = "<ol><li>A<ul><li>A1</li><li>A2</li><li>A3</li></ul></li><li>B</li><li>C</li></ol>"
guard let attributedString = attributedStringBuilder.fromHTML(htmlString) else {
XCTFail("Could not build the attributed string")
return
}
XCTAssertEqual(String(attributedString.characters), " 1. A\n • A1\n • A2\n • A3\n 2. B\n 3. C")
}
// MARK: - Phishing prevention