- cdataAsText emits a CDATA section's content as escaped text instead of a <![CDATA[ ... ]]> section. - asciiOnly escapes every non-ASCII character as a numeric character reference, so the output is pure ASCII (libxml2's character-reference encoding). Both apply to the Serializer and the incremental Writer through the shared Escaping rules. Closes #91
39 lines
1.8 KiB
Swift
39 lines
1.8 KiB
Swift
@testable import PureXML
|
|
import Testing
|
|
|
|
@Suite("Serialization: CDATA-as-text and ASCII-only")
|
|
struct SerializationEscapeTests {
|
|
private func serialize(_ node: PureXML.Model.Node, _ options: PureXML.Emitting.Options) -> String {
|
|
PureXML.serialize(node, options: options)
|
|
}
|
|
|
|
@Test("cdataAsText emits a CDATA section as escaped text")
|
|
func test_cdataAsText() {
|
|
let element = PureXML.Model.Element("a", children: [.cdata("x < y & z")])
|
|
let asSection = serialize(.element(element), PureXML.Emitting.Options(prettyPrint: false))
|
|
#expect(asSection == "<a><![CDATA[x < y & z]]></a>")
|
|
let asText = serialize(.element(element), PureXML.Emitting.Options(prettyPrint: false, cdataAsText: true))
|
|
#expect(asText == "<a>x < y & z</a>")
|
|
}
|
|
|
|
@Test("asciiOnly escapes non-ASCII characters as numeric references")
|
|
func test_asciiOnlyText() {
|
|
let element = PureXML.Model.Element("a", children: [.text("café")])
|
|
let options = PureXML.Emitting.Options(prettyPrint: false, asciiOnly: true)
|
|
#expect(serialize(.element(element), options) == "<a>café</a>")
|
|
}
|
|
|
|
@Test("asciiOnly escapes non-ASCII in attribute values too")
|
|
func test_asciiOnlyAttribute() {
|
|
let element = PureXML.Model.Element("a", attributes: [.init("t", "ñ")])
|
|
let options = PureXML.Emitting.Options(prettyPrint: false, asciiOnly: true)
|
|
#expect(serialize(.element(element), options) == "<a t=\"ñ\"/>")
|
|
}
|
|
|
|
@Test("Without asciiOnly, non-ASCII is left verbatim")
|
|
func test_nonAsciiVerbatim() {
|
|
let element = PureXML.Model.Element("a", children: [.text("café")])
|
|
#expect(serialize(.element(element), PureXML.Emitting.Options(prettyPrint: false)) == "<a>café</a>")
|
|
}
|
|
}
|