Fix crash when converting malformed HTML with unmatched closing tag
This commit is contained in:
parent
aed61d84d3
commit
1bfacb8fe9
|
@ -75,10 +75,12 @@ public struct AttributedStringConverter<Callbacks: HTMLConversionCallbacks> {
|
|||
case .endTag(let name):
|
||||
handleEndTag(name)
|
||||
// if we have a non-default action for the current element, the run finishes here
|
||||
if actionStack.last != .default {
|
||||
if let action = actionStack.last {
|
||||
if action != .default {
|
||||
finishRun()
|
||||
}
|
||||
actionStack.removeLast()
|
||||
}
|
||||
case .doctype:
|
||||
// ignored
|
||||
continue
|
||||
|
|
|
@ -55,10 +55,12 @@ public struct TextConverter<Callbacks: HTMLConversionCallbacks> {
|
|||
handleStartTag(name, selfClosing: selfClosing, attributes: attributes)
|
||||
case .endTag(let name):
|
||||
handleEndTag(name)
|
||||
if actionStack.last != .default {
|
||||
if let action = actionStack.last {
|
||||
if action != .default {
|
||||
finishRun()
|
||||
}
|
||||
actionStack.removeLast()
|
||||
}
|
||||
case .comment, .doctype:
|
||||
break
|
||||
}
|
||||
|
|
|
@ -352,4 +352,8 @@ final class AttributedStringConverterTests: XCTestCase {
|
|||
XCTAssertEqual(convert(#"<a href="https://example.com"><span class="invisible">https://</span><span>example.com</span><span class="invisible"></span></a>"#, callbacks: Callbacks.self), result)
|
||||
}
|
||||
|
||||
func testMalformedOnlyClosingTag() {
|
||||
XCTAssertEqual(convert("</span>"), .init())
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -83,4 +83,8 @@ final class TextConverterTests: XCTestCase {
|
|||
XCTAssertEqual(convert(#"<a href="https://example.com"><span class="invisible">https://</span><span>example.com</span><span class="invisible"></span></a>"#, callbacks: Callbacks.self), "example.com")
|
||||
}
|
||||
|
||||
func testMalformedOnlyClosingTag() {
|
||||
XCTAssertEqual(convert("</span>"), "")
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue