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

View File

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