From 9a77c93f3cc7616fe033370ce9247ac2fb31c53d Mon Sep 17 00:00:00 2001 From: codebrulee Date: Mon, 14 Sep 2009 23:08:11 -0400 Subject: [PATCH] Fixing Hash#from_xml for empty hashes when using REXML --- activesupport/lib/active_support/xml_mini/rexml.rb | 6 +++++- activesupport/test/core_ext/hash_ext_test.rb | 13 +++++++++++++ 2 files changed, 18 insertions(+), 1 deletions(-) diff --git a/activesupport/lib/active_support/xml_mini/rexml.rb b/activesupport/lib/active_support/xml_mini/rexml.rb index 3db48ce..87dce09 100644 --- a/activesupport/lib/active_support/xml_mini/rexml.rb +++ b/activesupport/lib/active_support/xml_mini/rexml.rb @@ -71,7 +71,11 @@ module ActiveSupport # must use value to prevent double-escaping texts = '' element.texts.each { |t| texts << t.value } - merge!(hash, CONTENT_KEY, texts) + if texts.strip.blank? && hash.empty? + hash + else + merge!(hash, CONTENT_KEY, texts) + end end end diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb index eb4c37a..5759c78 100644 --- a/activesupport/test/core_ext/hash_ext_test.rb +++ b/activesupport/test/core_ext/hash_ext_test.rb @@ -673,6 +673,19 @@ class HashToXmlTest < Test::Unit::TestCase assert_equal expected_hash, Hash.from_xml(test_xml) 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.3.1