Skip to content
Browse files

attributes no longer disappear if a tag contains whitespace

old:
Hash.from_xml("<tag foo='bar'>\n</tag>")
=> {"tag"=>"\n"}

new:
Hash.from_xml("<tag foo='bar'>\n</tag>")
=> {"tag"=>{"foo"=>"bar", "__content__"=>"\n"}
  • Loading branch information...
1 parent 3940e34 commit b41d8f30debc92ac24003d42c0b50a1d6e36cfa1 @indirect indirect committed with dhh Apr 14, 2011
View
2 activesupport/lib/active_support/core_ext/hash/conversions.rb
@@ -96,7 +96,7 @@ def typecast_xml_value(value)
raise "can't typecast #{entries.inspect}"
end
end
- elsif value.has_key?("__content__")
+ elsif value.has_key?("__content__") && value["__content__"].gsub(/\s/, '').present?
content = value["__content__"]
if parser = ActiveSupport::XmlMini::PARSING[value["type"]]
parser.arity == 1 ? parser.call(content) : parser.call(content, value)
View
9 activesupport/test/core_ext/hash_ext_test.rb
@@ -857,6 +857,15 @@ def test_file_from_xml_with_defaults
assert_equal 'application/octet-stream', file.content_type
end
+ def test_tag_with_attrs_and_whitespace
+ xml = <<-XML
+ <blog name="bacon is the best">
+ </blog>
+ XML
+ hash = Hash.from_xml(xml)
+ assert_equal "bacon is the best", hash['blog']['name']
+ end
+
def test_xsd_like_types_from_xml
bacon_xml = <<-EOT
<bacon>

0 comments on commit b41d8f3

Please sign in to comment.
Something went wrong with that request. Please try again.