feat(c14n): Canonical XML 1.1 (xml:base merging, no xml:id inheritance)
Add a canonical11 preset / mergeInheritedBase option. On a document subset, the omitted-ancestor xml:base chain is merged into the apex by RFC 3986 reference resolution (vs 1.0 nearest-wins), the apex's own relative xml:base is resolved against it, and xml:id is no longer inherited; xml:lang/xml:space still inherit nearest. A self-contained RFC 3986 resolver (parse, path merge, dot-segment removal) backs the merge, with tests covering the 1.1-vs-1.0 difference, dot segments, xml:id, and lang/space. Part of #105 (Tier 3)
This commit is contained in:
parent
56e889f790
commit
93f31d7758
@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
### Added
|
||||
|
||||
- Canonical XML 1.1 (#105, Tier 3): a `Canonical.Options.canonical11` preset and a `mergeInheritedBase` option. When canonicalizing a document subset, the `xml:base` values of the apex's omitted ancestors are merged into the apex by RFC 3986 reference resolution (vs. 1.0's nearest-ancestor rule), the apex's own relative `xml:base` is resolved against that chain, and `xml:id` is no longer inherited; `xml:lang` and `xml:space` still inherit the nearest. A self-contained RFC 3986 resolver (scheme/authority/path parsing, path merging, dot-segment removal) backs the merge.
|
||||
- Single-byte legacy encodings (#97): the byte decoder gains a table-based single-byte path and the full set of single-byte legacy encodings, selected by the XML declaration's `encoding` name for both the whole-buffer and streaming decoders: the complete ISO-8859 family (parts 2 through 16), Windows-1250/1251/1253/1254/1255/1256/1257/1258, and KOI8-R/U. Most are vendored verbatim from the authoritative `unicode.org` mapping files; ISO-8859-5/9/15 are derived exactly; Windows-1254 composes from Windows-1252. Multi-byte CJK encodings (#99) are tracked separately.
|
||||
|
||||
### Fixed
|
||||
|
||||
@ -88,7 +88,7 @@ flowchart TB
|
||||
OutEnc["#106 output encodings: complete"]:::done
|
||||
StreamVal["#107 streaming validation: DTD, RELAX NG, XSD"]:::done
|
||||
MoreEnc["#108 encodings: ISO-2022-JP, EUC-TW"]:::done
|
||||
Depth["Tier 2 conformance corpora; Tier 3 EXSLT, DTD PE depth, C14N 1.1; frontier #104"]:::todo
|
||||
Depth["Tier 2 conformance corpora; Tier 3 EXSLT, DTD PE depth (C14N 1.1 done); frontier #104"]:::todo
|
||||
Epic105 --> OutEnc
|
||||
OutEnc --> StreamVal
|
||||
StreamVal --> MoreEnc
|
||||
@ -99,7 +99,7 @@ flowchart TB
|
||||
|
||||
PureXML is a working, dependency-free XML library today: parse, emit, validate,
|
||||
query, and stream documents on macOS, Linux, Windows, and WASM. The test suite
|
||||
currently runs **790 tests in 121 suites** (`swift test`).
|
||||
currently runs **795 tests in 122 suites** (`swift test`).
|
||||
|
||||
### Shipped (libxml2-aligned surface)
|
||||
|
||||
|
||||
@ -59,6 +59,11 @@ public extension PureXML.Canonical {
|
||||
/// The Canonical XML 2.0 `QNameAware` labels: attributes or elements whose
|
||||
/// value is a QName, so its prefix is rewritten under sequential rewrite.
|
||||
public var qnameAwareLabels: [QNameAwareLabel]
|
||||
/// The Canonical XML 1.1 `xml:base` handling: when canonicalizing a document
|
||||
/// subset, the `xml:base` values of omitted ancestors are merged into the
|
||||
/// apex by RFC 3986 reference resolution (rather than 1.0's nearest-wins),
|
||||
/// and `xml:id` is not inherited. Off by default (1.0 behavior).
|
||||
public var mergeInheritedBase: Bool
|
||||
|
||||
public init(
|
||||
mode: Mode = .inclusive,
|
||||
@ -67,6 +72,7 @@ public extension PureXML.Canonical {
|
||||
trimTextNodes: Bool = false,
|
||||
prefixRewrite: PrefixRewrite = .retain,
|
||||
qnameAwareLabels: [QNameAwareLabel] = [],
|
||||
mergeInheritedBase: Bool = false,
|
||||
) {
|
||||
self.mode = mode
|
||||
self.includeComments = includeComments
|
||||
@ -74,6 +80,7 @@ public extension PureXML.Canonical {
|
||||
self.trimTextNodes = trimTextNodes
|
||||
self.prefixRewrite = prefixRewrite
|
||||
self.qnameAwareLabels = qnameAwareLabels
|
||||
self.mergeInheritedBase = mergeInheritedBase
|
||||
}
|
||||
|
||||
/// Inclusive C14N without comments.
|
||||
@ -82,5 +89,8 @@ public extension PureXML.Canonical {
|
||||
public static let exclusive = Options(mode: .exclusive)
|
||||
/// Canonical XML 2.0 with text-node trimming enabled.
|
||||
public static let canonical2 = Options(mode: .inclusive, trimTextNodes: true)
|
||||
/// Canonical XML 1.1: inclusive, with `xml:base` merging on subset apex
|
||||
/// inheritance and no `xml:id` inheritance.
|
||||
public static let canonical11 = Options(mode: .inclusive, mergeInheritedBase: true)
|
||||
}
|
||||
}
|
||||
|
||||
202
Sources/Canonical/CanonicalURI.swift
Normal file
202
Sources/Canonical/CanonicalURI.swift
Normal file
@ -0,0 +1,202 @@
|
||||
extension PureXML.Canonical.Canonicalizer {
|
||||
/// Canonical XML 1.1 inheritance: xml:lang and xml:space inherit nearest;
|
||||
/// xml:base is the RFC 3986 resolution of the omitted-ancestor chain folded
|
||||
/// with the apex's own; xml:id is not inherited.
|
||||
static func inherited11XMLAttributes(above node: PureXML.Model.TreeNode, apex: PureXML.Model.Element, present: Set<String>) -> [PureXML.Model.Attribute] {
|
||||
var result: [PureXML.Model.Attribute] = []
|
||||
for local in ["lang", "space"] where !present.contains("xml:\(local)") {
|
||||
if let attribute = nearestXMLAttribute(local, above: node) { result.append(attribute) }
|
||||
}
|
||||
if let base = mergedBase(above: node, apex: apex) {
|
||||
result.append(PureXML.Model.Attribute("xml:base", base))
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/// The nearest in-scope `xml:<local>` attribute above a node, or nil.
|
||||
private static func nearestXMLAttribute(_ local: String, above node: PureXML.Model.TreeNode) -> PureXML.Model.Attribute? {
|
||||
var current = node.parent
|
||||
while let ancestor = current {
|
||||
if let attribute = ancestor.attributes.first(where: { $0.name.prefix == "xml" && $0.name.localName == local }) {
|
||||
return attribute
|
||||
}
|
||||
current = ancestor.parent
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
/// The merged `xml:base` for the apex: the omitted-ancestor `xml:base` values
|
||||
/// (outermost first) resolved into each other, then the apex's own `xml:base`
|
||||
/// resolved against them. Nil when no `xml:base` is in scope.
|
||||
private static func mergedBase(above node: PureXML.Model.TreeNode, apex: PureXML.Model.Element) -> String? {
|
||||
var nearestFirst: [String] = []
|
||||
var current = node.parent
|
||||
while let ancestor = current {
|
||||
if let attribute = ancestor.attributes.first(where: { $0.name.prefix == "xml" && $0.name.localName == "base" }) {
|
||||
nearestFirst.append(attribute.value)
|
||||
}
|
||||
current = ancestor.parent
|
||||
}
|
||||
var chain = Array(nearestFirst.reversed())
|
||||
if let own = apex.attributes.first(where: { $0.name.prefix == "xml" && $0.name.localName == "base" }) {
|
||||
chain.append(own.value)
|
||||
}
|
||||
guard var merged = chain.first else { return nil }
|
||||
for reference in chain.dropFirst() {
|
||||
merged = resolveURI(reference, against: merged)
|
||||
}
|
||||
return merged
|
||||
}
|
||||
|
||||
/// Resolves a URI `reference` against a `base` per RFC 3986 section 5
|
||||
/// (strict). Used for Canonical XML 1.1 `xml:base` merging, where the
|
||||
/// `xml:base` values of omitted ancestors are combined into the apex.
|
||||
static func resolveURI(_ reference: String, against base: String) -> String {
|
||||
let ref = URIParts(reference)
|
||||
if ref.scheme != nil {
|
||||
return URIParts(scheme: ref.scheme, authority: ref.authority, path: removeDotSegments(ref.path), query: ref.query, fragment: ref.fragment).recomposed
|
||||
}
|
||||
let baseParts = URIParts(base)
|
||||
var target = URIParts(scheme: baseParts.scheme, authority: nil, path: "", query: nil, fragment: ref.fragment)
|
||||
if ref.authority != nil {
|
||||
target.authority = ref.authority
|
||||
target.path = removeDotSegments(ref.path)
|
||||
target.query = ref.query
|
||||
} else {
|
||||
target.authority = baseParts.authority
|
||||
if ref.path.isEmpty {
|
||||
target.path = baseParts.path
|
||||
target.query = ref.query ?? baseParts.query
|
||||
} else if ref.path.hasPrefix("/") {
|
||||
target.path = removeDotSegments(ref.path)
|
||||
target.query = ref.query
|
||||
} else {
|
||||
target.path = removeDotSegments(merge(base: baseParts, relativePath: ref.path))
|
||||
target.query = ref.query
|
||||
}
|
||||
}
|
||||
return target.recomposed
|
||||
}
|
||||
|
||||
/// Merges a relative path onto a base per RFC 3986 section 5.3.
|
||||
private static func merge(base: URIParts, relativePath: String) -> String {
|
||||
if base.authority != nil, base.path.isEmpty {
|
||||
return "/" + relativePath
|
||||
}
|
||||
if let slash = base.path.lastIndex(of: "/") {
|
||||
return String(base.path[...slash]) + relativePath
|
||||
}
|
||||
return relativePath
|
||||
}
|
||||
|
||||
/// Removes `.` and `..` segments per RFC 3986 section 5.2.4.
|
||||
private static func removeDotSegments(_ path: String) -> String {
|
||||
var input = Substring(path)
|
||||
var output = ""
|
||||
while !input.isEmpty {
|
||||
if input.hasPrefix("../") { input = input.dropFirst(3)
|
||||
continue
|
||||
}
|
||||
if input.hasPrefix("./") { input = input.dropFirst(2)
|
||||
continue
|
||||
}
|
||||
if input.hasPrefix("/./") { input = "/" + input.dropFirst(3)
|
||||
continue
|
||||
}
|
||||
if input == "/." { input = "/"
|
||||
continue
|
||||
}
|
||||
if input.hasPrefix("/../") { input = "/" + input.dropFirst(4)
|
||||
removeLastSegment(&output)
|
||||
continue
|
||||
}
|
||||
if input == "/.." { input = "/"
|
||||
removeLastSegment(&output)
|
||||
continue
|
||||
}
|
||||
if input == "." || input == ".." { break }
|
||||
let start = input.hasPrefix("/") ? input.index(after: input.startIndex) : input.startIndex
|
||||
let next = input[start...].firstIndex(of: "/") ?? input.endIndex
|
||||
output += input[..<next]
|
||||
input = input[next...]
|
||||
}
|
||||
return output
|
||||
}
|
||||
|
||||
private static func removeLastSegment(_ output: inout String) {
|
||||
if let slash = output.lastIndex(of: "/") {
|
||||
output = String(output[..<slash])
|
||||
} else {
|
||||
output = ""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The five components of a URI reference (RFC 3986 section 3).
|
||||
private struct URIParts {
|
||||
var scheme: String?
|
||||
var authority: String?
|
||||
var path: String
|
||||
var query: String?
|
||||
var fragment: String?
|
||||
|
||||
init(scheme: String?, authority: String?, path: String, query: String?, fragment: String?) {
|
||||
self.scheme = scheme
|
||||
self.authority = authority
|
||||
self.path = path
|
||||
self.query = query
|
||||
self.fragment = fragment
|
||||
}
|
||||
|
||||
/// Parses a URI reference into its components.
|
||||
init(_ text: String) {
|
||||
var rest = Substring(text)
|
||||
if let hash = rest.firstIndex(of: "#") {
|
||||
fragment = String(rest[rest.index(after: hash)...])
|
||||
rest = rest[..<hash]
|
||||
} else {
|
||||
fragment = nil
|
||||
}
|
||||
if let question = rest.firstIndex(of: "?") {
|
||||
query = String(rest[rest.index(after: question)...])
|
||||
rest = rest[..<question]
|
||||
} else {
|
||||
query = nil
|
||||
}
|
||||
let split = Self.splitScheme(rest)
|
||||
scheme = split.scheme
|
||||
rest = split.rest
|
||||
if rest.hasPrefix("//") {
|
||||
let afterSlashes = rest.dropFirst(2)
|
||||
let end = afterSlashes.firstIndex(where: { $0 == "/" }) ?? afterSlashes.endIndex
|
||||
authority = String(afterSlashes[..<end])
|
||||
rest = afterSlashes[end...]
|
||||
} else {
|
||||
authority = nil
|
||||
}
|
||||
path = String(rest)
|
||||
}
|
||||
|
||||
/// Splits a leading `scheme:` off a reference (scheme present only when a colon
|
||||
/// precedes any path separator and the candidate is a valid scheme).
|
||||
private static func splitScheme(_ text: Substring) -> (scheme: String?, rest: Substring) {
|
||||
guard let colon = text.firstIndex(of: ":") else { return (nil, text) }
|
||||
let candidate = text[..<colon]
|
||||
let valid = candidate.allSatisfy { $0.isLetter || $0.isNumber || $0 == "+" || $0 == "-" || $0 == "." }
|
||||
let startsWithLetter = candidate.first?.isLetter ?? false
|
||||
let beforeSlash = text.firstIndex(of: "/").map { colon < $0 } ?? true
|
||||
guard valid, startsWithLetter, beforeSlash else { return (nil, text) }
|
||||
return (String(candidate), text[text.index(after: colon)...])
|
||||
}
|
||||
|
||||
/// Recomposes the components into a URI string (RFC 3986 section 5.3).
|
||||
var recomposed: String {
|
||||
var result = ""
|
||||
if let scheme { result += scheme + ":" }
|
||||
if let authority { result += "//" + authority }
|
||||
result += path
|
||||
if let query { result += "?" + query }
|
||||
if let fragment { result += "#" + fragment }
|
||||
return result
|
||||
}
|
||||
}
|
||||
@ -40,14 +40,15 @@ public extension PureXML.Canonical {
|
||||
return canonicalize(subtree.node)
|
||||
}
|
||||
let ancestorNamespaces = Self.inScopeNamespaces(above: subtree)
|
||||
let inheritedXML = Self.inheritedXMLAttributes(above: subtree, apex: apex)
|
||||
let mergeBase = options.mergeInheritedBase
|
||||
let inheritedXML = Self.inheritedXMLAttributes(above: subtree, apex: apex, mergeBase: mergeBase)
|
||||
var output = ""
|
||||
switch options.mode {
|
||||
case .inclusive:
|
||||
let augmented = Self.augmentedApex(apex, inheritedXML: inheritedXML, mergingNamespaces: ancestorNamespaces)
|
||||
let augmented = Self.augmentedApex(apex, inheritedXML: inheritedXML, mergingNamespaces: ancestorNamespaces, stripApexBase: mergeBase)
|
||||
emit(augmented, inScope: [:], rendered: [:], output: &output)
|
||||
case .exclusive:
|
||||
let augmented = Self.augmentedApex(apex, inheritedXML: inheritedXML, mergingNamespaces: nil)
|
||||
let augmented = Self.augmentedApex(apex, inheritedXML: inheritedXML, mergingNamespaces: nil, stripApexBase: mergeBase)
|
||||
emit(augmented, inScope: ancestorNamespaces, rendered: [:], output: &output)
|
||||
}
|
||||
return output
|
||||
@ -187,8 +188,14 @@ public extension PureXML.Canonical {
|
||||
_ apex: PureXML.Model.Element,
|
||||
inheritedXML: [PureXML.Model.Attribute],
|
||||
mergingNamespaces ancestorNamespaces: [String: String]?,
|
||||
stripApexBase: Bool,
|
||||
) -> PureXML.Model.Element {
|
||||
var attributes = apex.attributes + inheritedXML
|
||||
// Under 1.1, the apex's own xml:base is folded into the merged base in
|
||||
// inheritedXML, so drop the original to avoid emitting it twice.
|
||||
let own = stripApexBase
|
||||
? apex.attributes.filter { !($0.name.prefix == "xml" && $0.name.localName == "base") }
|
||||
: apex.attributes
|
||||
var attributes = own + inheritedXML
|
||||
if let ancestorNamespaces {
|
||||
let declared = Set(apex.attributes.map(\.name.description))
|
||||
for (prefix, uri) in ancestorNamespaces {
|
||||
@ -216,8 +223,11 @@ public extension PureXML.Canonical {
|
||||
|
||||
/// The `xml:*` attributes in scope from a node's omitted ancestors that the
|
||||
/// apex does not already set, nearest ancestor winning.
|
||||
private static func inheritedXMLAttributes(above node: PureXML.Model.TreeNode, apex: PureXML.Model.Element) -> [PureXML.Model.Attribute] {
|
||||
private static func inheritedXMLAttributes(above node: PureXML.Model.TreeNode, apex: PureXML.Model.Element, mergeBase: Bool) -> [PureXML.Model.Attribute] {
|
||||
let present = Set(apex.attributes.map(\.name.description))
|
||||
if mergeBase { return inherited11XMLAttributes(above: node, apex: apex, present: present) }
|
||||
// Canonical XML 1.0: the nearest of every in-scope xml:* attribute the
|
||||
// apex does not already set.
|
||||
var seen: Set<String> = []
|
||||
var result: [PureXML.Model.Attribute] = []
|
||||
var current = node.parent
|
||||
|
||||
48
Tests/CanonicalC14N11Tests.swift
Normal file
48
Tests/CanonicalC14N11Tests.swift
Normal file
@ -0,0 +1,48 @@
|
||||
@testable import PureXML
|
||||
import Testing
|
||||
|
||||
@Suite("Canonical XML 1.1: xml:base merging and xml:id")
|
||||
struct CanonicalC14N11Tests {
|
||||
private func canonicalize(_ xml: String, at path: [Int], _ options: PureXML.Canonical.Options) throws -> String {
|
||||
let document = try PureXML.parseTree(xml)
|
||||
var node = document
|
||||
for index in path {
|
||||
node = node.children[index]
|
||||
}
|
||||
return PureXML.Canonical.Canonicalizer(options: options).canonicalize(node)
|
||||
}
|
||||
|
||||
@Test("1.1 merges the omitted-ancestor xml:base chain; 1.0 takes the nearest")
|
||||
func test_baseMerging() throws {
|
||||
let xml = "<root xml:base=\"http://example.org/a/\"><mid xml:base=\"b/\"><apex/></mid></root>"
|
||||
try #expect(canonicalize(xml, at: [0, 0, 0], .canonical11) == "<apex xml:base=\"http://example.org/a/b/\"></apex>")
|
||||
try #expect(canonicalize(xml, at: [0, 0, 0], .inclusive) == "<apex xml:base=\"b/\"></apex>")
|
||||
}
|
||||
|
||||
@Test("1.1 resolves the apex's own relative xml:base against the inherited chain")
|
||||
func test_apexRelativeBase() throws {
|
||||
let xml = "<root xml:base=\"http://example.org/a/\"><apex xml:base=\"c/d\"/></root>"
|
||||
try #expect(canonicalize(xml, at: [0, 0], .canonical11) == "<apex xml:base=\"http://example.org/a/c/d\"></apex>")
|
||||
// 1.0 keeps the apex's own base unresolved (no merge).
|
||||
try #expect(canonicalize(xml, at: [0, 0], .inclusive) == "<apex xml:base=\"c/d\"></apex>")
|
||||
}
|
||||
|
||||
@Test("1.1 resolves dot segments in the merged base")
|
||||
func test_dotSegments() throws {
|
||||
let xml = "<root xml:base=\"http://example.org/a/b/\"><apex xml:base=\"../x\"/></root>"
|
||||
try #expect(canonicalize(xml, at: [0, 0], .canonical11) == "<apex xml:base=\"http://example.org/a/x\"></apex>")
|
||||
}
|
||||
|
||||
@Test("1.1 does not inherit xml:id; 1.0 does")
|
||||
func test_xmlIdNotInherited() throws {
|
||||
let xml = "<root xml:id=\"r\"><apex/></root>"
|
||||
try #expect(canonicalize(xml, at: [0, 0], .canonical11) == "<apex></apex>")
|
||||
try #expect(canonicalize(xml, at: [0, 0], .inclusive) == "<apex xml:id=\"r\"></apex>")
|
||||
}
|
||||
|
||||
@Test("1.1 still inherits xml:lang and xml:space (nearest)")
|
||||
func test_langSpaceStillInherited() throws {
|
||||
let xml = "<root xml:lang=\"en\" xml:space=\"preserve\"><apex/></root>"
|
||||
try #expect(canonicalize(xml, at: [0, 0], .canonical11) == "<apex xml:lang=\"en\" xml:space=\"preserve\"></apex>")
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user