Skip to content

Commit

Permalink
Merge pull request #98 from adam-fowler/xml-whitespace
Browse files Browse the repository at this point in the history
XML: If text is completely white space don't add a text node
  • Loading branch information
adam-fowler authored Jul 14, 2019
2 parents cfaa153 + 3562b1f commit 80cc521
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
5 changes: 4 additions & 1 deletion Sources/AWSSDKSwiftCore/XML.swift
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,10 @@ public class XML {
}

func parser(_ parser: XMLParser, foundCharacters: String) {
currentElement?.addChild(XML.Node.text(stringValue: foundCharacters))
// if string with white space removed still has characters, add text node
if foundCharacters.components(separatedBy: .whitespaces).joined().count > 0 {
currentElement?.addChild(XML.Node.text(stringValue: foundCharacters))
}
}

func parser(_ parser: XMLParser, foundComment comment: String) {
Expand Down
13 changes: 12 additions & 1 deletion Tests/AWSSDKSwiftCoreTests/XMLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,18 @@ class XMLTests: XCTestCase {
XCTFail(error.localizedDescription)
}
}


func testWhitespaceDecodeEncode() {
let xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><test> <a> before</a><b></b> <c>after </c></test>"
do {
let xmlDocument = try XML.Document(data: xml.data(using: .utf8)!)
let xml2 = xmlDocument.xmlString
XCTAssertEqual(xml2, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><test><a> before</a><b></b><c>after </c></test>")
} catch {
XCTFail(error.localizedDescription)
}
}

static var allTests : [(String, (XMLTests) -> () throws -> Void)] {
return [
("testAddChild", testAddChild),
Expand Down

0 comments on commit 80cc521

Please sign in to comment.