From 16a4273b99bd7cb341b32cf56bd470fe2afa1dce Mon Sep 17 00:00:00 2001 From: Ben Langfeld Date: Thu, 7 Jun 2012 15:02:34 -0400 Subject: [PATCH] [BUGFIX] Some namespace related bugs on JRuby --- lib/niceogiri/xml/node.rb | 3 +- spec/niceogiri/core_ext/nokogiri_spec.rb | 48 ++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/lib/niceogiri/xml/node.rb b/lib/niceogiri/xml/node.rb index f6f60af..1d8e27c 100644 --- a/lib/niceogiri/xml/node.rb +++ b/lib/niceogiri/xml/node.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| diff --git a/spec/niceogiri/core_ext/nokogiri_spec.rb b/spec/niceogiri/core_ext/nokogiri_spec.rb index 61d21c2..2b75ced 100644 --- a/spec/niceogiri/core_ext/nokogiri_spec.rb +++ b/spec/niceogiri/core_ext/nokogiri_spec.rb @@ -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