Skip to content

Commit

Permalink
[BUGFIX] Some namespace related bugs on JRuby
Browse files Browse the repository at this point in the history
  • Loading branch information
benlangfeld committed Jun 7, 2012
1 parent 179226c commit 16a4273
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/niceogiri/xml/node.rb
Expand Up @@ -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|
Expand Down
48 changes: 48 additions & 0 deletions spec/niceogiri/core_ext/nokogiri_spec.rb
Expand Up @@ -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.