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
This commit is contained in:
parent
bb70798049
commit
5351a5c777
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
50
Tests/LineEndingTests.swift
Normal file
50
Tests/LineEndingTests.swift
Normal file
@ -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("<a>x\r\ny</a>") == "x\ny")
|
||||
}
|
||||
|
||||
@Test("A lone carriage return is normalized to a line feed")
|
||||
func test_loneCR() throws {
|
||||
#expect(try rootText("<a>x\ry</a>") == "x\ny")
|
||||
}
|
||||
|
||||
@Test("In XML 1.0, NEL is preserved as data")
|
||||
func test_nel10() throws {
|
||||
#expect(try rootText("<a>x\u{85}y</a>") == "x\u{85}y")
|
||||
}
|
||||
|
||||
@Test("In XML 1.1, NEL is normalized to a line feed")
|
||||
func test_nel11() throws {
|
||||
#expect(try rootText("<?xml version=\"1.1\"?><a>x\u{85}y</a>") == "x\ny")
|
||||
}
|
||||
|
||||
@Test("In XML 1.1, LINE SEPARATOR is normalized to a line feed")
|
||||
func test_lineSeparator11() throws {
|
||||
#expect(try rootText("<?xml version=\"1.1\"?><a>x\u{2028}y</a>") == "x\ny")
|
||||
}
|
||||
|
||||
@Test("In XML 1.1, CR followed by NEL collapses to one line feed")
|
||||
func test_crNel11() throws {
|
||||
#expect(try rootText("<?xml version=\"1.1\"?><a>x\r\u{85}y</a>") == "x\ny")
|
||||
}
|
||||
|
||||
@Test("In XML 1.0, LINE SEPARATOR is preserved as data")
|
||||
func test_lineSeparator10() throws {
|
||||
#expect(try rootText("<a>x\u{2028}y</a>") == "x\u{2028}y")
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user