import Testing
@testable import PureXML
@Suite("HTML")
struct HTMLTests {
private func roundTrip(_ html: String) -> String {
PureXML.HTML.serialize(PureXML.HTML.parse(html))
}
@Test("Void elements need no end tag and serialize without a slash")
func test_voidElements() {
#expect(roundTrip("
a bc
") == "
a bc
")
}
@Test("Optional end tags close implicitly")
func test_optionalEndTags() {
#expect(roundTrip("
one
two
") == "
one
two
")
#expect(roundTrip("
first
second") == "
first
second
")
}
@Test("Table rows and cells close implicitly")
func test_tableImpliedClose() {
let html = "
a
b
c
"
#expect(roundTrip(html) == "
a
b
c
")
}
@Test("Raw-text elements keep their content unescaped")
func test_rawText() {
let html = ""
let parsed = PureXML.HTML.parse(html)
#expect(PureXML.HTML.serialize(parsed) == html)
}
@Test("Attributes parse quoted, unquoted, and boolean forms")
func test_attributes() {
let parsed = PureXML.HTML.parse("")
guard case let .document(children) = parsed, case let .element(element) = children.first else {
Issue.record("expected an element")
return
}
#expect(element.attributes.count == 3)
#expect(element.attributes.contains { $0.name.description == "type" && $0.value == "text" })
#expect(element.attributes.contains { $0.name.description == "disabled" && $0.value == "" })
}
@Test("Tag names are lowercased")
func test_caseInsensitive() {
#expect(roundTrip("
x
") == "
x
")
}
@Test("Character references are decoded and re-escaped")
func test_entities() {
#expect(roundTrip("
a & b < c A
") == "
a & b < c A
")
// Latin-1 characters re-encode to their HTML 4.01 names on output.
#expect(roundTrip("