Repository navigation
Escape whitespace characters that XML parsers normalize - #140
Merged
Merged
Conversation
OutputXML wrote carriage returns in text, and tabs, newlines and carriage returns in attribute values, as raw characters. An XML parser reads a raw carriage return back as a newline, and a conforming parser reads a raw tab or newline in an attribute value as a space, so the document no longer held the same values after a round trip. Write them as character references instead.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
OutputXMLpasses text and attribute values throughhtml.EscapeString, which leaves tabs, newlines and carriage returns as they are. Parsers rewrite some of those raw characters, so the output no longer holds the same values when read back:xcomes back as"tab\tnl\ncr\nend"and the text as"one\ntwo", because the carriage returns are read as newlines.xas"tab nl cr end".This change writes
\rin text as , and\t,\nand\rin attribute values as	, and . Everythinghtml.EscapeStringalready escaped is unchanged, and newlines and tabs in text are still written raw, since they survive parsing.Testing:
TestOutputXMLKeepsWhitespaceCharactersparses the document above, writes it withOutputXML, parses it again, and compares the attribute and the text. It fails on master (both values differ) and passes with the change.go test .passes, and so doesgo vet .. I also checked the before and after output with lxml, which reads back the original values only after the change. I only ran this on Go 1.25.