Permalink
Browse files

[BUGFIX] Some namespace related bugs on JRuby

  • Loading branch information...
1 parent 179226c commit 16a4273b99bd7cb341b32cf56bd470fe2afa1dce @benlangfeld committed Jun 7, 2012
Showing with 50 additions and 1 deletion.
  1. +2 −1 lib/niceogiri/xml/node.rb
  2. +48 −0 spec/niceogiri/core_ext/nokogiri_spec.rb
@@ -65,7 +65,8 @@ def namespace=(namespaces)
when Nokogiri::XML::Namespace
self.nokogiri_namespace = namespaces
when String
- self.add_namespace nil, namespaces
+ ns = self.add_namespace nil, namespaces
+ self.nokogiri_namespace = ns
when Hash
self.add_namespace nil, ns if ns = namespaces.delete(nil)
namespaces.each do |p, n|
@@ -79,4 +79,52 @@
doc.root = subject
subject.find_first('/foo').should == subject.find('/foo').first
end
+
+ describe "JRuby bugs" do
+ describe '#to_xml' do
+ context 'with a namespace on a child node' do
+ let(:ns_href) { 'foo' }
+ let(:child_node) { Nokogiri::XML::Node.new 'bar', doc }
+
+ before do
+ child_node.add_namespace nil, ns_href
+ subject << child_node
+ end
+
+ it 'should have the correct namespace in the rendered XML' do
+ p subject.to_xml
+ subject.to_xml.should match(/xmlns="foo"/)
+ end
+ end
+ end
+
+ describe '#xpath' do
+ context 'looking for a namespaced element inside a prefixed element' do
+ let(:child_node) { Nokogiri::XML::Node.new 'bar', doc }
+
+ let(:outer_ns_prefix) { 'pref' }
+ let(:outer_ns_href) { 'outer_ns' }
+ let(:inner_ns_href) { 'inner_ns' }
+
+ before do
+ ns = subject.add_namespace outer_ns_prefix, outer_ns_href
+ subject.namespace = ns
+ child_node.add_namespace nil, inner_ns_href
+ subject << child_node
+ end
+
+ it 'should have the correct namespace' do
+ ns = subject.children.first.namespace
+ ns.should be_a Nokogiri::XML::Namespace
+ ns.prefix.should be == nil
+ ns.href.should be == inner_ns_href
+ end
+
+ it 'should find the element' do
+ x = subject.xpath("//inner_ns:bar", 'inner_ns' => inner_ns_href)
+ x.first.should be child_node
+ end
+ end
+ end
+ end
end

0 comments on commit 16a4273

Please sign in to comment.