PureXML/Tests/XSLTOutputMethodTests.swift
Mihaela Mihaljevic 2b5080e30e fix(xslt): gate raw-output markers, cache pattern table, add CI tests
Raw-output marker resolution and 16.4 stripping run only when the
stylesheet mentions disable-output-escaping, so source text containing
U+E000/U+E001 survives every other transform untouched, and the text
method removes markers without unescaping content that was never
escaped. The pattern-matching function table is built once per
transform instead of per (template, node) membership test, and a dead
variable left in the numbering engine is gone. Eight focused unit
tests cover d-o-e (with the fragment rule and marker safety), CDATA
terminator splitting, simplified syntax, the html default method with
META injection, per-document key(), and attribute-start axes, so the
burn-down behaviors are CI-visible without the opt-in corpus.
2026-06-10 15:01:07 +02:00

126 lines
5.7 KiB
Swift

@testable import PureXML
import Testing
/// The output-method behaviors of XSLT 1.0 section 16 added in the xalan
/// burn-down (#130): disable-output-escaping (including its 16.4 fragment
/// rule and the marker-safety gate), CDATA "]]>" splitting, the simplified
/// stylesheet syntax, the html default method with META injection, key()
/// inside document() trees, and the attribute-start following/preceding axes.
@Suite("XSLT output methods and runtime corners")
struct XSLTOutputMethodTests {
private let xsl = "xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\""
@Test("disable-output-escaping writes text through unescaped")
func test_disableOutputEscaping() throws {
let style = """
<xsl:stylesheet version="1.0" \(xsl)>
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="/"><out><xsl:text disable-output-escaping="yes">&lt;P&gt;&amp;nbsp;&lt;/P&gt;</xsl:text></out></xsl:template>
</xsl:stylesheet>
"""
#expect(try PureXML.XSLT.transform(stylesheet: style, source: "<x/>") == "<out><P>&nbsp;</P></out>")
}
@Test("disable-output-escaping does not survive a fragment round-trip (16.4)")
func test_rawTextFragmentRoundTrip() throws {
let style = """
<xsl:stylesheet version="1.0" \(xsl)>
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="/">
<out>
<xsl:variable name="held"><xsl:text disable-output-escaping="yes">a &lt;b&gt; c</xsl:text></xsl:variable>
<xsl:value-of select="$held"/>
</out>
</xsl:template>
</xsl:stylesheet>
"""
#expect(try PureXML.XSLT.transform(stylesheet: style, source: "<x/>") == "<out>a &lt;b&gt; c</out>")
}
@Test("Source text holding the private-use marker characters is untouched")
func test_markerCharactersInSource() throws {
let style = """
<xsl:stylesheet version="1.0" \(xsl)>
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="/"><out><xsl:value-of select="d"/></out></xsl:template>
</xsl:stylesheet>
"""
let source = "<d>a\u{E000}&lt;kept&gt;\u{E001}b</d>"
#expect(try PureXML.XSLT.transform(stylesheet: style, source: source) == "<out>a\u{E000}&lt;kept&gt;\u{E001}b</out>")
}
@Test("cdata-section-elements split ]]> across section boundaries")
func test_cdataTerminatorSplitting() throws {
let style = """
<xsl:stylesheet version="1.0" \(xsl)>
<xsl:output omit-xml-declaration="yes" cdata-section-elements="example"/>
<xsl:template match="/"><out><example><xsl:text>]]&gt;</xsl:text></example></out></xsl:template>
</xsl:stylesheet>
"""
#expect(try PureXML.XSLT.transform(stylesheet: style, source: "<x/>") == "<out><example><![CDATA[]]]]><![CDATA[>]]></example></out>")
}
@Test("A literal result element with xsl:version is a one-template stylesheet (2.3)")
func test_simplifiedSyntax() throws {
let style = """
<out xsl:version="1.0" \(xsl)>value: <xsl:value-of select="doc/v"/></out>
"""
let result = try PureXML.XSLT.transform(stylesheet: style, source: "<doc><v>7</v></doc>")
#expect(result.hasSuffix("<out>value: 7</out>"))
}
@Test("A null-namespace html root selects the html method and gains META (16.1/16.2)")
func test_htmlDefaultMethod() throws {
let style = """
<xsl:stylesheet version="1.0" \(xsl)>
<xsl:template match="/"><HTML><HEAD><TITLE>t</TITLE></HEAD><BODY><BR/></BODY></HTML></xsl:template>
</xsl:stylesheet>
"""
let result = try PureXML.XSLT.transform(stylesheet: style, source: "<x/>")
#expect(result == "<HTML><HEAD><META http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><TITLE>t</TITLE></HEAD><BODY><BR></BODY></HTML>")
}
@Test("key() indexes the current node's own document, document() trees included")
func test_keyPerDocument() throws {
let style = """
<xsl:stylesheet version="1.0" \(xsl)>
<xsl:output omit-xml-declaration="yes"/>
<xsl:key name="k" match="item" use="@id"/>
<xsl:template match="/">
<out>
<xsl:for-each select="document('other.xml')">
<xsl:value-of select="key('k', 'b')"/>
</xsl:for-each>
</out>
</xsl:template>
</xsl:stylesheet>
"""
let result = try PureXML.XSLT.transform(
stylesheet: style,
source: "<doc><item id=\"b\">local</item></doc>",
documentLoader: { $0 == "other.xml" ? "<doc><item id=\"b\">external</item></doc>" : nil },
)
#expect(result == "<out>external</out>")
}
@Test("following and preceding work from attribute starts")
func test_axesFromAttributes() throws {
let style = """
<xsl:stylesheet version="1.0" \(xsl)>
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="/">
<out>
<f><xsl:for-each select="//b/@m/following::*"><xsl:value-of select="name()"/><xsl:text> </xsl:text></xsl:for-each></f>
<p><xsl:for-each select="//b/@m/preceding::*"><xsl:value-of select="name()"/><xsl:text> </xsl:text></xsl:for-each></p>
</out>
</xsl:template>
</xsl:stylesheet>
"""
let source = "<r><a/><b m=\"1\"><inner/></b><c/></r>"
// Document order places the attribute after b and before b's children,
// so following includes inner and c; preceding holds a only (r is an
// ancestor).
#expect(try PureXML.XSLT.transform(stylesheet: style, source: source) == "<out><f>inner c </f><p>a </p></out>")
}
}