Skip to content

Commit

Permalink
attribute: don't convert ' and ' with {attribute_quote: :quote}
Browse files Browse the repository at this point in the history
GitHub: fix GH-92

Reported by Edouard Brière. Thanks!!!
  • Loading branch information
kou committed Dec 8, 2022
1 parent c68d489 commit 20070d0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
12 changes: 7 additions & 5 deletions lib/rexml/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ class Attribute

# The element to which this attribute belongs
attr_reader :element
# The normalized value of this attribute. That is, the attribute with
# entities intact.
attr_writer :normalized
PATTERN = /\s*(#{NAME_STR})\s*=\s*(["'])(.*?)\2/um

NEEDS_A_SECOND_CHECK = /(<|&((#{Entity::NAME});|(#0*((?:\d+)|(?:x[a-fA-F0-9]+)));)?)/um
Expand Down Expand Up @@ -141,7 +138,6 @@ def to_s
return @normalized if @normalized

@normalized = Text::normalize( @unnormalized, doctype )
@unnormalized = nil
@normalized
end

Expand All @@ -150,10 +146,16 @@ def to_s
def value
return @unnormalized if @unnormalized
@unnormalized = Text::unnormalize( @normalized, doctype )
@normalized = nil
@unnormalized
end

# The normalized value of this attribute. That is, the attribute with
# entities intact.
def normalized=(new_normalized)
@normalized = new_normalized
@unnormalized = nil
end

# Returns a copy of this attribute
def clone
Attribute.new self
Expand Down
11 changes: 10 additions & 1 deletion test/test_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,18 +178,27 @@ def test_amp_and_lf_attributes
attr_test('name','value with LF &#x000a; &amp; ampersand')
end

def test_quoting
def test_quote_root
d = Document.new(%q{<a x='1' y="2"/>})
assert_equal( %q{<a x='1' y='2'/>}, d.to_s )
d.root.context[:attribute_quote] = :quote
assert_equal( %q{<a x="1" y="2"/>}, d.to_s )
end

def test_quote_sub_element
d = Document.new(%q{<a x='1' y="2"><b z='3'/></a>})
assert_equal( %q{<a x='1' y='2'><b z='3'/></a>}, d.to_s )
d.root.context[:attribute_quote] = :quote
assert_equal( %q{<a x="1" y="2"><b z="3"/></a>}, d.to_s )
end

def test_quote_to_s_value
doc = Document.new(%q{<root a="'"/>}, {attribute_quote: :quote})
assert_equal(%q{<root a="'"/>}, doc.to_s)
assert_equal("'", doc.root.attribute("a").value)
assert_equal(%q{<root a="'"/>}, doc.to_s)
end

def test_ticket_127
doc = Document.new
doc.add_element 'a', { 'v' => 'x & y' }
Expand Down

0 comments on commit 20070d0

Please sign in to comment.