From f557f941f51e30fba26f0346f39fdf718589db78 Mon Sep 17 00:00:00 2001 From: Elad Meidar Date: Sun, 27 Sep 2009 01:13:02 -0400 Subject: [PATCH] LH3205 - Fix Hash#from_xml with empty hash (using REXML backend) --- activesupport/lib/active_support/xml_mini/rexml.rb | 9 ++++++++- activesupport/test/core_ext/hash_ext_test.rb | 12 ++++++++++++ 2 files changed, 20 insertions(+), 1 deletions(-) diff --git a/activesupport/lib/active_support/xml_mini/rexml.rb b/activesupport/lib/active_support/xml_mini/rexml.rb index a8fdeca..cd80dab 100644 --- a/activesupport/lib/active_support/xml_mini/rexml.rb +++ b/activesupport/lib/active_support/xml_mini/rexml.rb @@ -55,8 +55,15 @@ module ActiveSupport unless element.has_text? hash else + if (element.texts.collect {|txt| txt.to_s.strip }.reject(&:blank?).blank? && hash.empty?) + hash + else + # must use value to prevent double-escaping + merge!(hash, CONTENT_KEY, element.texts.sum(&:value)) + end + # must use value to prevent double-escaping - merge!(hash, CONTENT_KEY, element.texts.sum(&:value)) + #merge!(hash, CONTENT_KEY, element.texts.sum(&:value)) end end diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb index 736c1c1..8862ea4 100644 --- a/activesupport/test/core_ext/hash_ext_test.rb +++ b/activesupport/test/core_ext/hash_ext_test.rb @@ -702,6 +702,18 @@ class HashToXmlTest < Test::Unit::TestCase assert_equal expected_topic_hash, Hash.from_xml(topic_xml)["rsp"]["photos"]["photo"] end + def test_empty_key_with_new_line_from_xml_is_nil + test_xml = <<-EOT + + + EOT + + expected_hash = { + "key" => nil + } + assert_equal expected_hash, Hash.from_xml(test_xml) + end + def test_empty_array_from_xml blog_xml = <<-XML -- 1.6.0.2