feat(xslt): emit xsl:output doctype-public/doctype-system
When doctype-system (optionally with doctype-public) is set on xsl:output, a <!DOCTYPE> for the result's root element is emitted before the serialized output: PUBLIC when both identifiers are given, SYSTEM otherwise. Works for the XML and HTML output methods. Part of #82.
This commit is contained in:
parent
9545802be6
commit
2a40300479
@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
### Added
|
||||
|
||||
- XSLT `xsl:output` `doctype-public`/`doctype-system` (#82). When either is set, a `<!DOCTYPE>` for the result's root element (PUBLIC when both are given, SYSTEM otherwise) is emitted before the serialized output, for XML and HTML methods.
|
||||
- XSLT `key()` over a node-set second argument (#82). `key(name, node-set)` (including `key('k', .)`) now unions the matches for every node's string value and de-duplicates, rather than using only the first node's string value.
|
||||
- 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.
|
||||
|
||||
@ -98,6 +98,10 @@ public extension PureXML.XSLT {
|
||||
public var encoding: String?
|
||||
public var version: String?
|
||||
public var standalone: Bool?
|
||||
/// The `doctype-public` / `doctype-system` identifiers: when either is set,
|
||||
/// a `<!DOCTYPE>` for the result's root element is emitted before it.
|
||||
public var doctypePublic: String?
|
||||
public var doctypeSystem: String?
|
||||
|
||||
public init(
|
||||
method: String? = nil,
|
||||
@ -106,6 +110,8 @@ public extension PureXML.XSLT {
|
||||
encoding: String? = nil,
|
||||
version: String? = nil,
|
||||
standalone: Bool? = nil,
|
||||
doctypePublic: String? = nil,
|
||||
doctypeSystem: String? = nil,
|
||||
) {
|
||||
self.method = method
|
||||
self.indent = indent
|
||||
@ -113,6 +119,8 @@ public extension PureXML.XSLT {
|
||||
self.encoding = encoding
|
||||
self.version = version
|
||||
self.standalone = standalone
|
||||
self.doctypePublic = doctypePublic
|
||||
self.doctypeSystem = doctypeSystem
|
||||
}
|
||||
|
||||
/// This output's settings with `other`'s non-nil settings layered over them.
|
||||
@ -124,6 +132,8 @@ public extension PureXML.XSLT {
|
||||
encoding: other.encoding ?? encoding,
|
||||
version: other.version ?? version,
|
||||
standalone: other.standalone ?? standalone,
|
||||
doctypePublic: other.doctypePublic ?? doctypePublic,
|
||||
doctypeSystem: other.doctypeSystem ?? doctypeSystem,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@ -106,9 +106,7 @@ extension PureXML.XSLT {
|
||||
return top
|
||||
}
|
||||
|
||||
/// Compiles a stylesheet element, recursively folding in `xsl:include`
|
||||
/// (same import precedence) and `xsl:import` (one lower) resolved through
|
||||
/// `loader`.
|
||||
/// Compiles a stylesheet element, folding in `xsl:include`/`xsl:import`.
|
||||
private static func compile(_ top: Tree, loader: (String) -> String?, precedence: Int) -> Stylesheet {
|
||||
var parts = Parts()
|
||||
for child in XSLTNode.elementChildren(top) where XSLTNode.isXSL(child) {
|
||||
@ -167,6 +165,8 @@ extension PureXML.XSLT {
|
||||
encoding: XSLTNode.attribute(node, "encoding"),
|
||||
version: XSLTNode.attribute(node, "version"),
|
||||
standalone: XSLTNode.attribute(node, "standalone").map { $0 == "yes" },
|
||||
doctypePublic: XSLTNode.attribute(node, "doctype-public"),
|
||||
doctypeSystem: XSLTNode.attribute(node, "doctype-system"),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@ -17,8 +17,26 @@ 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))
|
||||
let body = sheet.output.method == "html"
|
||||
? PureXML.HTML.serialize(result)
|
||||
: PureXML.serialize(result, options: options.applying(sheet.output))
|
||||
return doctype(for: result, sheet.output) + body
|
||||
}
|
||||
|
||||
/// The `<!DOCTYPE …>` prologue for the result's root element when the output
|
||||
/// declares `doctype-system` (optionally with `doctype-public`), else empty.
|
||||
private static func doctype(for result: PureXML.Model.Node, _ output: Output) -> String {
|
||||
guard let system = output.doctypeSystem, let name = rootName(of: result) else { return "" }
|
||||
let external = output.doctypePublic.map { "PUBLIC \"\($0)\" \"\(system)\"" } ?? "SYSTEM \"\(system)\""
|
||||
return "<!DOCTYPE \(name) \(external)>\n"
|
||||
}
|
||||
|
||||
private static func rootName(of node: PureXML.Model.Node) -> String? {
|
||||
switch node {
|
||||
case let .element(element): element.name.description
|
||||
case let .document(children): children.compactMap(\.element).first?.name.description
|
||||
default: nil
|
||||
}
|
||||
}
|
||||
|
||||
/// Transforms `source` with `stylesheet`, returning the result tree.
|
||||
|
||||
46
Tests/XSLTDoctypeOutputTests.swift
Normal file
46
Tests/XSLTDoctypeOutputTests.swift
Normal file
@ -0,0 +1,46 @@
|
||||
@testable import PureXML
|
||||
import Testing
|
||||
|
||||
@Suite("XSLT xsl:output doctype-public / doctype-system")
|
||||
struct XSLTDoctypeOutputTests {
|
||||
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)
|
||||
}
|
||||
|
||||
@Test("doctype-system emits a SYSTEM doctype before the root")
|
||||
func test_systemOnly() throws {
|
||||
let style = """
|
||||
<xsl:stylesheet version="1.0" \(xsl)>
|
||||
<xsl:output method="xml" omit-xml-declaration="yes" doctype-system="note.dtd"/>
|
||||
<xsl:template match="/"><note/></xsl:template>
|
||||
</xsl:stylesheet>
|
||||
"""
|
||||
#expect(try transform(style, "<x/>") == "<!DOCTYPE note SYSTEM \"note.dtd\">\n<note/>")
|
||||
}
|
||||
|
||||
@Test("doctype-public and doctype-system emit a PUBLIC doctype")
|
||||
func test_publicAndSystem() throws {
|
||||
let style = """
|
||||
<xsl:stylesheet version="1.0" \(xsl)>
|
||||
<xsl:output method="html" doctype-public="-//W3C//DTD HTML 4.01//EN" doctype-system="http://www.w3.org/TR/html4/strict.dtd"/>
|
||||
<xsl:template match="/"><html/></xsl:template>
|
||||
</xsl:stylesheet>
|
||||
"""
|
||||
let out = try transform(style, "<x/>")
|
||||
#expect(out.hasPrefix("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n"))
|
||||
#expect(out.contains("<html></html>"))
|
||||
}
|
||||
|
||||
@Test("Without doctype-system no doctype is emitted")
|
||||
func test_none() throws {
|
||||
let style = """
|
||||
<xsl:stylesheet version="1.0" \(xsl)>
|
||||
<xsl:output method="xml" omit-xml-declaration="yes"/>
|
||||
<xsl:template match="/"><note/></xsl:template>
|
||||
</xsl:stylesheet>
|
||||
"""
|
||||
#expect(try transform(style, "<x/>") == "<note/>")
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user