Permalink
Browse files

Fix issue #770.

DocumentFragment parsing which depends on Xml::Node::parse didn't
properly handle namespaces in context. Ideally we should be using
Xerces parsing with a hint to the context node, but unfortunately
this feature isn't implemented yet
https://issues.apache.org/jira/browse/XERCESJ-1429 which is why we
manually add namespace declarations to the fake root that encapsulates
the document fragment.
  • Loading branch information...
1 parent 4229826 commit 0b07e6e1316bc5e885d86bff92df6631bd5c07dd @jvshahid jvshahid committed Oct 21, 2012
Showing with 13 additions and 2 deletions.
  1. +1 −0 CHANGELOG.rdoc
  2. +8 −1 lib/nokogiri/xml/document_fragment.rb
  3. +4 −1 test/xml/test_builder.rb
View
@@ -21,6 +21,7 @@
* JRuby Unknown namespace are ignore when the recover option is used. #748
* JRuby XPath queries for namespaces should not throw exceptions when called twice in a row. #764
* [jruby] More consistent (with libxml2) whitespace formatting when emitting XML (#771)
+ * JRuby namespaced attributes broken when appending raw xml to builder. #770
== 1.5.5 / 2012-06-24
@@ -13,7 +13,7 @@ def initialize document, tags = nil, ctx = nil
children = if ctx
# Fix for issue#490
if Nokogiri.jruby?
- ctx.parse("<root>#{tags}</root>").xpath("/root/node()")
+ ctx.parse("<root #{namespace_declarations(ctx)}>#{tags}</root>").children
else
ctx.parse(tags)
end
@@ -93,6 +93,13 @@ def parse tags
private
+ def namespace_declarations ctx
+ ctx.namespace_scopes.map do |namespace|
+ prefix = namespace.prefix.nil? ? "" : ":#{namespace.prefix}"
+ %Q{xmlns#{prefix}="#{namespace.href}"}
+ end.join ' '
+ end
+
def coerce data
return super unless String === data
View
@@ -211,14 +211,17 @@ def test_raw_xml_append
def test_raw_xml_append_with_namespaces
doc = Nokogiri::XML::Builder.new do |xml|
- xml.root("xmlns:foo" => "x") do
+ xml.root("xmlns:foo" => "x", "xmlns" => "y") do
xml << '<Element foo:bar="bazz"/>'
end
end.doc
el = doc.at 'Element'
assert_not_nil el
+ assert_equal 'y', el.namespace.href
+ assert_nil el.namespace.prefix
+
attr = el.attributes["bar"]
assert_not_nil attr
assert_not_nil attr.namespace

0 comments on commit 0b07e6e

Please sign in to comment.