diff --git a/CHANGELOG.md b/CHANGELOG.md index 5adbcc4..5b043ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 07ea2c0..c637331 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/Sources/Canonical/CanonicalOptions.swift b/Sources/Canonical/CanonicalOptions.swift index 39f436a..29f3307 100644 --- a/Sources/Canonical/CanonicalOptions.swift +++ b/Sources/Canonical/CanonicalOptions.swift @@ -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) } } diff --git a/Sources/Canonical/CanonicalURI.swift b/Sources/Canonical/CanonicalURI.swift new file mode 100644 index 0000000..1e3009d --- /dev/null +++ b/Sources/Canonical/CanonicalURI.swift @@ -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) -> [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:` 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[.. (scheme: String?, rest: Substring) { + guard let colon = text.firstIndex(of: ":") else { return (nil, text) } + let candidate = text[.. 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 = [] var result: [PureXML.Model.Attribute] = [] var current = node.parent diff --git a/Tests/CanonicalC14N11Tests.swift b/Tests/CanonicalC14N11Tests.swift new file mode 100644 index 0000000..cc828a0 --- /dev/null +++ b/Tests/CanonicalC14N11Tests.swift @@ -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 = "" + try #expect(canonicalize(xml, at: [0, 0, 0], .canonical11) == "") + try #expect(canonicalize(xml, at: [0, 0, 0], .inclusive) == "") + } + + @Test("1.1 resolves the apex's own relative xml:base against the inherited chain") + func test_apexRelativeBase() throws { + let xml = "" + try #expect(canonicalize(xml, at: [0, 0], .canonical11) == "") + // 1.0 keeps the apex's own base unresolved (no merge). + try #expect(canonicalize(xml, at: [0, 0], .inclusive) == "") + } + + @Test("1.1 resolves dot segments in the merged base") + func test_dotSegments() throws { + let xml = "" + try #expect(canonicalize(xml, at: [0, 0], .canonical11) == "") + } + + @Test("1.1 does not inherit xml:id; 1.0 does") + func test_xmlIdNotInherited() throws { + let xml = "" + try #expect(canonicalize(xml, at: [0, 0], .canonical11) == "") + try #expect(canonicalize(xml, at: [0, 0], .inclusive) == "") + } + + @Test("1.1 still inherits xml:lang and xml:space (nearest)") + func test_langSpaceStillInherited() throws { + let xml = "" + try #expect(canonicalize(xml, at: [0, 0], .canonical11) == "") + } +}