From 5351a5c777e6f22c91c3ecf0078749de4c99f0c5 Mon Sep 17 00:00:00 2001 From: Mihaela Mihaljevic Date: Mon, 8 Jun 2026 15:07:21 +0200 Subject: [PATCH] feat(parsing): line-ending normalization and XML 1.1 line terminators Normalize line endings in the Reader as the spec requires, which was not done before: - CR and CRLF fold to a single line feed (XML 1.0), so every downstream scan sees line feeds only. - When the declaration names version 1.1, NEL (U+0085) and LINE SEPARATOR (U+2028), and a CR followed by NEL, also fold to a line feed; in 1.0 those characters are preserved as data. The XML 1.1 name-character productions are identical to XML 1.0 Fifth Edition, which the character classifier already implements, so names need no change. The 1.1 character range (C0 controls permitted only as references) is not yet enforced. Closes #96 --- Sources/Parsing/EventReaderRecovery.swift | 1 + Sources/Parsing/Reader.swift | 36 +++++++++++++++- Tests/LineEndingTests.swift | 50 +++++++++++++++++++++++ 3 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 Tests/LineEndingTests.swift diff --git a/Sources/Parsing/EventReaderRecovery.swift b/Sources/Parsing/EventReaderRecovery.swift index b284a80..ce097eb 100644 --- a/Sources/Parsing/EventReaderRecovery.swift +++ b/Sources/Parsing/EventReaderRecovery.swift @@ -7,6 +7,7 @@ extension PureXML.Parsing.EventReader { throw PureXML.Parsing.ParseError.malformedDeclaration(mark) } xmlDeclaration = declaration + if declaration.version == "1.1" { reader.xml11 = true } } /// The current source offset, used to tell whether a failing ``next()`` made diff --git a/Sources/Parsing/Reader.swift b/Sources/Parsing/Reader.swift index d359107..dbb3a28 100644 --- a/Sources/Parsing/Reader.swift +++ b/Sources/Parsing/Reader.swift @@ -10,6 +10,13 @@ extension PureXML.Parsing { private(set) var offset = 0 private(set) var line = 1 private(set) var column = 1 + /// When true, also fold the XML 1.1 line terminators NEL (U+0085) and LINE + /// SEPARATOR (U+2028) to a line feed. Set once the declaration is known to + /// name version 1.1. + var xml11 = false + /// A raw character read past a carriage return that was not part of the + /// line ending, held back to be returned next. + private var pendingRaw: Character? init(pulling pull: @escaping () -> Character?) { self.pull = pull @@ -25,7 +32,7 @@ extension PureXML.Parsing { /// any single scan needs, never by the document size. private mutating func ensure(_ count: Int) { while buffer.count < count, !exhausted { - if let character = pull() { + if let character = normalizedPull() { buffer.append(character) } else { exhausted = true @@ -33,6 +40,33 @@ extension PureXML.Parsing { } } + /// Pulls the next character with XML line-ending normalization: a carriage + /// return, alone or followed by a line feed (or by NEL in 1.1), becomes a + /// single line feed, as do a lone NEL and LINE SEPARATOR in 1.1. So every + /// scan downstream sees line feeds only, as the spec requires. + private mutating func normalizedPull() -> Character? { + guard let character = rawNext() else { return nil } + // Swift coalesces a CR+LF pair into one grapheme-cluster character. + if character == "\r\n" { return "\n" } + if character == "\r" { + let next = rawNext() + if next != "\n", !(xml11 && next == "\u{85}") { pendingRaw = next } + return "\n" + } + if xml11, character == "\u{85}" || character == "\u{2028}" { + return "\n" + } + return character + } + + private mutating func rawNext() -> Character? { + if let pending = pendingRaw { + pendingRaw = nil + return pending + } + return pull() + } + mutating func peek(_ ahead: Int = 0) -> Character? { ensure(ahead + 1) return ahead < buffer.count ? buffer[ahead] : nil diff --git a/Tests/LineEndingTests.swift b/Tests/LineEndingTests.swift new file mode 100644 index 0000000..b9097cc --- /dev/null +++ b/Tests/LineEndingTests.swift @@ -0,0 +1,50 @@ +@testable import PureXML +import Testing + +@Suite("Line-ending normalization and XML 1.1") +struct LineEndingTests { + private func rootText(_ xml: String) throws -> String { + let node = try PureXML.parse(xml) + guard case let .document(children) = node, let root = children.compactMap(\.element).first else { + return "" + } + return root.children.reduce(into: "") { result, child in + if case let .text(text) = child { result += text } + } + } + + @Test("CRLF is normalized to a single line feed") + func test_crlf() throws { + #expect(try rootText("x\r\ny") == "x\ny") + } + + @Test("A lone carriage return is normalized to a line feed") + func test_loneCR() throws { + #expect(try rootText("x\ry") == "x\ny") + } + + @Test("In XML 1.0, NEL is preserved as data") + func test_nel10() throws { + #expect(try rootText("x\u{85}y") == "x\u{85}y") + } + + @Test("In XML 1.1, NEL is normalized to a line feed") + func test_nel11() throws { + #expect(try rootText("x\u{85}y") == "x\ny") + } + + @Test("In XML 1.1, LINE SEPARATOR is normalized to a line feed") + func test_lineSeparator11() throws { + #expect(try rootText("x\u{2028}y") == "x\ny") + } + + @Test("In XML 1.1, CR followed by NEL collapses to one line feed") + func test_crNel11() throws { + #expect(try rootText("x\r\u{85}y") == "x\ny") + } + + @Test("In XML 1.0, LINE SEPARATOR is preserved as data") + func test_lineSeparator10() throws { + #expect(try rootText("x\u{2028}y") == "x\u{2028}y") + } +}