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") + } +}