Skip to content

Escape tab, newline and carriage return in attribute values - #381

Open
ihamzatarar wants to merge 1 commit into
ruby:masterfrom
ihamzatarar:escape-whitespace-in-attribute-values
Open

ihamzatarar wants to merge 1 commit into
ruby:masterfrom
ihamzatarar:escape-whitespace-in-attribute-values

Conversation

@ihamzatarar

Copy link
Copy Markdown

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

require "rexml/document"

document = REXML::Document.new("<root/>")
document.root.add_attribute("attr", "line one\nline two\tindented")
puts document.root.to_s

Today that writes the newline and tab as themselves:

<root attr='line one
line two	indented'/>

Reading it back with expat, through Python's ElementTree:

'line one line two indented'

The newline and the tab have become spaces. With this change the attribute is written as

<root attr='line one&#10;line two&#9;indented'/>

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_string now writes tab, line feed and carriage return as &#9;, &#10; and &#13;. 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:

master   841 tests, 2663 assertions, 0 failures, 0 errors, 5 omissions
patched  842 tests, 2664 assertions, 0 failures, 0 errors, 5 omissions

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.
Copilot AI balanced review requested due to automatic review settings November 16, 2026 20:37

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants