import Testing
@testable import PureXML
@Suite("HTML5 document tree construction")
struct HTMLDocumentTests {
private func document(_ html: String) -> String {
PureXML.HTML.serialize(PureXML.HTML.parseDocument(html))
}
@Test("Implied html, head, and body wrap a bare fragment")
func test_impliedStructure() {
#expect(document("
hello
") == "hello
")
}
@Test("Head-only elements are routed into the head")
func test_headRouting() {
let html = "Tbody
"
let expected = "Tbody
"
#expect(document(html) == expected)
}
@Test("Explicit html, head, and body are honored")
func test_explicitStructure() {
let html = "THi
"
#expect(document(html) == "THi
")
}
@Test("Flow content before any head element starts the body")
func test_flowStartsBody() {
#expect(document("x
") == "x
")
}
@Test("A head element after body content stays in the body's flow")
func test_explicitBodyThenContent() {
let html = "one
two
"
#expect(document(html) == "one
two
")
}
@Test("html attributes are preserved on the implied structure")
func test_htmlAttributes() {
#expect(document("x
") == "x
")
}
// MARK: Table tree construction
/// Wraps a body fragment in the implied document structure for comparison.
private func bodyDocument(_ inner: String) -> String {
"\(inner)"
}
@Test("A bare table row gets an implied tbody")
func test_impliedTbody() {
#expect(document("") == bodyDocument(""))
}
@Test("A bare cell gets an implied tbody and row")
func test_impliedRowAndSection() {
#expect(document("") == bodyDocument(""))
}
@Test("An explicit section is not duplicated")
func test_explicitSection() {
#expect(document("") == bodyDocument(""))
}
@Test("Consecutive rows share one implied tbody")
func test_consecutiveRows() {
#expect(document("") == bodyDocument(""))
}
@Test("A stray element inside a table is foster-parented before it")
func test_fosterElement() {
#expect(document("") == bodyDocument("x"))
}
@Test("Stray text inside a table is foster-parented before it")
func test_fosterText() {
#expect(document("") == bodyDocument("text"))
}
@Test("Well-formed cell content is not foster-parented")
func test_noFosterInCell() {
#expect(document("") == bodyDocument(""))
}
@Test("A template's content nests inside it")
func test_templateContent() {
#expect(document("x
") == bodyDocument("x
"))
}
}