Skip to content

Commit 20070d0

Browse files
committed
attribute: don't convert ' and ' with {attribute_quote: :quote}
GitHub: fix GH-92 Reported by Edouard Brière. Thanks!!!
1 parent c68d489 commit 20070d0

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

lib/rexml/attribute.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ class Attribute
1313

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

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

143140
@normalized = Text::normalize( @unnormalized, doctype )
144-
@unnormalized = nil
145141
@normalized
146142
end
147143

@@ -150,10 +146,16 @@ def to_s
150146
def value
151147
return @unnormalized if @unnormalized
152148
@unnormalized = Text::unnormalize( @normalized, doctype )
153-
@normalized = nil
154149
@unnormalized
155150
end
156151

152+
# The normalized value of this attribute. That is, the attribute with
153+
# entities intact.
154+
def normalized=(new_normalized)
155+
@normalized = new_normalized
156+
@unnormalized = nil
157+
end
158+
157159
# Returns a copy of this attribute
158160
def clone
159161
Attribute.new self

test/test_attributes.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,18 +178,27 @@ def test_amp_and_lf_attributes
178178
attr_test('name','value with LF &#x000a; &amp; ampersand')
179179
end
180180

181-
def test_quoting
181+
def test_quote_root
182182
d = Document.new(%q{<a x='1' y="2"/>})
183183
assert_equal( %q{<a x='1' y='2'/>}, d.to_s )
184184
d.root.context[:attribute_quote] = :quote
185185
assert_equal( %q{<a x="1" y="2"/>}, d.to_s )
186+
end
186187

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

195+
def test_quote_to_s_value
196+
doc = Document.new(%q{<root a="'"/>}, {attribute_quote: :quote})
197+
assert_equal(%q{<root a="'"/>}, doc.to_s)
198+
assert_equal("'", doc.root.attribute("a").value)
199+
assert_equal(%q{<root a="'"/>}, doc.to_s)
200+
end
201+
193202
def test_ticket_127
194203
doc = Document.new
195204
doc.add_element 'a', { 'v' => 'x & y' }

0 commit comments

Comments
 (0)