Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/rexml/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@ def hash
# b.to_string # -> "ns:x='y'"
def to_string
value = to_s
# A conforming parser normalizes a literal tab, newline or carriage
# return in an attribute value to a space, so write them as character
# references to keep the value intact. See XML 1.0 section 3.3.3.
if value.match?(/[\t\n\r]/)
value = value.gsub(/[\t\n\r]/,
"\t" => "	",
"\n" => "
",
"\r" => "
")
end
if @element and @element.context and @element.context[:attribute_quote] == :quote
value = value.gsub('"', '"') if value.include?('"')
%Q^#@expanded_name="#{value}"^
Expand Down
6 changes: 6 additions & 0 deletions test/test_attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,11 @@ def test_to_string_entity
document.root.add_attribute("attr", "abc aaa")
assert_equal("<root attr='abc &a;'/>", document.root.to_s)
end

def test_to_string_whitespace
document = REXML::Document.new("<root/>")
document.root.add_attribute("attr", "a\tb\nc\rd")
assert_equal("<root attr='a&#9;b&#10;c&#13;d'/>", document.root.to_s)
end
end
end