Repository navigation
Escape tab, newline and carriage return in attribute values - #381
Open
ihamzatarar wants to merge 1 commit into
Open
ihamzatarar wants to merge 1 commit into
ihamzatarar wants to merge 1 commit into
Conversation
XML 1.0 section 3.3.3 requires a parser to replace a literal tab, line feed or carriage return in an attribute value with a space before the value reaches the application. REXML wrote those characters literally, so an attribute set to "a\tb" came back as "a b" from any conforming parser, with nothing to report the change. Write them as &ruby#9;, &ruby#10; and &ruby#13; instead, so the value survives the round trip. Verified against expat, which returned the original string only after the change. REXML's own parser does not normalize attribute values, so a REXML to REXML round trip looked correct and hid this.
This branch has not been deployed
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.
A literal tab, newline or carriage return written into an attribute value does not survive a round trip through a conforming parser.
XML 1.0 section 3.3.3 requires a parser to replace any literal tab, line feed or carriage return in an attribute value with a space before handing the value to the application. REXML writes those characters literally, so the value the next reader sees is not the value that was set, and nothing reports it.
Reproduction
Today that writes the newline and tab as themselves:
Reading it back with expat, through Python's
ElementTree:The newline and the tab have become spaces. With this change the attribute is written as
and expat returns
'line one\nline two\tindented', which is what was set.Why this has not been noticed
REXML's own parser does not apply attribute-value normalization, so REXML reads both forms back identically and a REXML-to-REXML round trip looks correct. The value only changes once the document reaches a parser that follows the spec.
Change
Attribute#to_stringnow writes tab, line feed and carriage return as	, and . Other characters are untouched, and the existing quoting and entity handling are unchanged. The guard means the substitution only runs for values that actually contain one of the three.Tests
One test added to
test/test_attribute.rb. It fails on master with the literal characters in the output and passes with the change.Full suite before and after, on Ruby 3.4.6: