Fix converting self-closing br tags

This commit is contained in:
Shadowfacts 2023-12-22 18:43:53 -05:00
parent 395f5965bc
commit 601c9f2cd8
2 changed files with 15 additions and 8 deletions

View File

@ -58,10 +58,7 @@ public struct AttributedStringConverter<Callbacks: AttributedStringCallbacks> {
case .startTag(let name, let selfClosing, let attributes):
let action = Callbacks.elementAction(name: name, attributes: attributes)
actionStack.append(action)
// self closing tags are ignored since they have no content
if !selfClosing {
handleStartTag(name, attributes: attributes)
}
handleStartTag(name, selfClosing: selfClosing, attributes: attributes)
case .endTag(let name):
handleEndTag(name)
// if we have a non-default action for the current element, the run finishes here
@ -80,10 +77,17 @@ public struct AttributedStringConverter<Callbacks: AttributedStringCallbacks> {
return str
}
private mutating func handleStartTag(_ name: String, attributes: [HTMLStreamer.Attribute]) {
switch name {
case "br":
private mutating func handleStartTag(_ name: String, selfClosing: Bool, attributes: [HTMLStreamer.Attribute]) {
if name == "br" {
currentRun.append("\n")
return
}
// self closing tags are ignored since they have no content
guard !selfClosing else {
return
}
switch name {
case "a":
// we need to always insert in attribute, because we need to always have one
// to remove from the stack in handleEndTag
@ -195,7 +199,6 @@ public struct AttributedStringConverter<Callbacks: AttributedStringCallbacks> {
private mutating func finishBlockElement() {
if str.length != 0 {
previouslyFinishedBlockElement = true
// currentRun.append("\n\n")
}
}

View File

@ -57,6 +57,10 @@ final class AttributedStringConverterTests: XCTestCase {
.font: font,
.paragraphStyle: NSParagraphStyle.default,
]))
XCTAssertEqual(convert("a<br />b"), NSAttributedString(string: "a\nb", attributes: [
.font: font,
.paragraphStyle: NSParagraphStyle.default,
]))
}
func testConvertA() {