From 20070d047ddc8a3a8abbd0666fbdaa2ff7d8e4d6 Mon Sep 17 00:00:00 2001 From: Sutou Kouhei Date: Fri, 9 Dec 2022 05:28:32 +0900 Subject: [PATCH] attribute: don't convert ' and ' with {attribute_quote: :quote} MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit GitHub: fix GH-92 Reported by Edouard Brière. Thanks!!! --- lib/rexml/attribute.rb | 12 +++++++----- test/test_attributes.rb | 11 ++++++++++- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/lib/rexml/attribute.rb b/lib/rexml/attribute.rb index 8933a013..c198e00a 100644 --- a/lib/rexml/attribute.rb +++ b/lib/rexml/attribute.rb @@ -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 @@ -141,7 +138,6 @@ def to_s return @normalized if @normalized @normalized = Text::normalize( @unnormalized, doctype ) - @unnormalized = nil @normalized end @@ -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 diff --git a/test/test_attributes.rb b/test/test_attributes.rb index 91fc68a5..09fde442 100644 --- a/test/test_attributes.rb +++ b/test/test_attributes.rb @@ -178,18 +178,27 @@ def test_amp_and_lf_attributes attr_test('name','value with LF & ampersand') end - def test_quoting + def test_quote_root d = Document.new(%q{}) assert_equal( %q{}, d.to_s ) d.root.context[:attribute_quote] = :quote assert_equal( %q{}, d.to_s ) + end + def test_quote_sub_element d = Document.new(%q{}) assert_equal( %q{}, d.to_s ) d.root.context[:attribute_quote] = :quote assert_equal( %q{}, d.to_s ) end + def test_quote_to_s_value + doc = Document.new(%q{}, {attribute_quote: :quote}) + assert_equal(%q{}, doc.to_s) + assert_equal("'", doc.root.attribute("a").value) + assert_equal(%q{}, doc.to_s) + end + def test_ticket_127 doc = Document.new doc.add_element 'a', { 'v' => 'x & y' }