Skip to content

Commit

Permalink
Fix reading of XML data with newlines
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler committed Mar 2, 2021
1 parent 53b8b1f commit 78e03eb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/SotoXML/XML.swift
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public enum XML {
}
.onCharacterData { characters in
// if string with white space removed still has characters, add text node
if characters.split(omittingEmptySubsequences: true, whereSeparator: { $0.isWhitespaceOrNewline }).joined().count > 0 {
if characters.split(omittingEmptySubsequences: true, whereSeparator: { $0.isWhitespace && !$0.isNewline }).joined().count > 0 {
currentElement?.addChild(XML.Node.text(stringValue: characters))
}
}
Expand Down
9 changes: 9 additions & 0 deletions Tests/SotoXMLTests/XMLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@ class XMLTests: XCTestCase {
XCTAssertEqual(xml2, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><test><a> before</a><b></b><c>after </c></test>")
}

func testNewlines() {
let xml = """
<?xml version=\"1.0\" encoding=\"UTF-8\"?><test>Hello
Goodbye</test>
"""
XCTAssertNoThrow(try self.testDecodeEncode(xml: xml))
}

func testDecodeRubbish() {
let xml = "{}"
XCTAssertThrowsError(try XML.Document(data: Data(xml.utf8)))
Expand Down

0 comments on commit 78e03eb

Please sign in to comment.