Merge: XSLT method=html output (#82)

This commit is contained in:
Mihaela Mihaljevic 2026-06-09 02:48:00 +02:00
commit f9b82bb749
3 changed files with 52 additions and 0 deletions

View File

@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- XSLT `method="html"` output (#82). An `xsl:output method="html"` result is now serialized by the HTML rules (void elements like `<br>` emitted without a self-closing slash, raw-text elements like `<script>`/`<style>` left unescaped) instead of as XML.
- XSLT `xsl:sort case-order` (#82). When `case-order="upper-first"`/`"lower-first"` is set, the sort comparison is case-insensitive with the case used only to break ties among otherwise-equal strings; without it the default codepoint order is kept.
- XSLT functions `generate-id`, `system-property`, `element-available`,
`function-available`, and an `unparsed-entity-uri` stub (#82). `generate-id`

View File

@ -17,6 +17,7 @@ public extension PureXML.XSLT {
let result = transformer.run()
if let message = transformer.terminationMessage { throw XSLTError.terminated(message) }
if sheet.output.method == "text" { return textValue(of: result) }
if sheet.output.method == "html" { return PureXML.HTML.serialize(result) }
return PureXML.serialize(result, options: options.applying(sheet.output))
}

View File

@ -0,0 +1,50 @@
@testable import PureXML
import Testing
@Suite("XSLT method=html output")
struct XSLTHTMLOutputTests {
private let xsl = "xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\""
private func transform(_ stylesheet: String, _ source: String) throws -> String {
try PureXML.XSLT.transform(stylesheet: stylesheet, source: source)
}
private func htmlStyle(_ body: String) -> String {
"""
<xsl:stylesheet version="1.0" \(xsl)>
<xsl:output method="html"/>
<xsl:template match="/">\(body)</xsl:template>
</xsl:stylesheet>
"""
}
@Test("A void element is emitted without a self-closing slash")
func test_voidElement() throws {
let out = try transform(htmlStyle("<br/>"), "<x/>")
#expect(out == "<br>")
}
@Test("A non-void empty element keeps an explicit end tag")
func test_nonVoid() throws {
let out = try transform(htmlStyle("<div/>"), "<x/>")
#expect(out == "<div></div>")
}
@Test("Raw-text element content is not escaped")
func test_rawText() throws {
let out = try transform(htmlStyle("<script>if (a &lt; b) x()</script>"), "<x/>")
#expect(out.contains("if (a < b) x()"))
#expect(!out.contains("&lt;"))
}
@Test("The XML output method still self-closes empty elements")
func test_xmlStillSelfCloses() throws {
let style = """
<xsl:stylesheet version="1.0" \(xsl)>
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:template match="/"><br/></xsl:template>
</xsl:stylesheet>
"""
#expect(try transform(style, "<x/>") == "<br/>")
}
}