diff --git a/Sources/XMLWrangler/Parsing.swift b/Sources/XMLWrangler/Parsing.swift index 5fe4b4be..4b2f24c1 100644 --- a/Sources/XMLWrangler/Parsing.swift +++ b/Sources/XMLWrangler/Parsing.swift @@ -123,7 +123,10 @@ extension XMLElement { let leftTrimmed = string.drop(while: { $0.isNewline || $0.isWhitespace }) currentElem.content.append(.string(String(leftTrimmed))) } else { - currentElem.appendString(string.trimmingCharacters(in: .whitespaces)) + // We must not trim whitespaces here! + // For e.g. "One & Two", the parser passes us "One ", "&", " Two". + // Cleanup of the string will be taken care of at the end of the element. + currentElem.appendString(string) } elementStack.append(currentElem) } diff --git a/Tests/XMLWranglerTests/GitHubIssueTests.swift b/Tests/XMLWranglerTests/GitHubIssueTests.swift index db7b17b0..3c937d22 100644 --- a/Tests/XMLWranglerTests/GitHubIssueTests.swift +++ b/Tests/XMLWranglerTests/GitHubIssueTests.swift @@ -13,4 +13,20 @@ final class GitHubIssueTests: XCTestCase { XCTAssertEqual(try XWElement.parse(str).stringContent(), "Ich bin zerknirscht und verzweifelt\nüber meine schwere Schuld.\nSolch ein Opfer gefällt dir, o Gott,\ndu wirst es nicht ablehnen.") } + + // https://github.com/sersoft-gmbh/xmlwrangler/issues/120 + func testIssue120() throws { + let xml = """ + + + One & two + + """ + + let content = try XMLElement.parse(xml) + .element(at: "child1") + .stringContent() + + XCTAssertEqual("One & two", content) + } }