Skip to content

Commit

Permalink
Don't strip too many whitespaces from string contents. Fixes #120
Browse files Browse the repository at this point in the history
  • Loading branch information
ffried committed Mar 21, 2024
1 parent e30ce20 commit d016ce5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Sources/XMLWrangler/Parsing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
16 changes: 16 additions & 0 deletions Tests/XMLWranglerTests/GitHubIssueTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 = """
<?xml version="1.0" encoding="UTF-8"?>
<root myattr="myvalue">
<child1>One &amp; two</child1>
</root>
"""

let content = try XMLElement.parse(xml)
.element(at: "child1")
.stringContent()

XCTAssertEqual("One & two", content)
}
}

0 comments on commit d016ce5

Please sign in to comment.