Return a correct list of available namespaces.

This ensures that inner namespaces take precedence over outer namespaces.

Fixes #40.
This commit is contained in:
Yorick Peterse 2014-09-14 18:42:02 +02:00
parent 96b6ef320b
commit 398aaf68bc
2 changed files with 19 additions and 7 deletions

View File

@ -263,8 +263,11 @@ module Oga
node = parent node = parent
while node && node.respond_to?(:namespaces) while node && node.respond_to?(:namespaces)
merged = merged.merge(node.namespaces) node.namespaces.each do |prefix, ns|
node = node.parent merged[prefix] = ns unless merged[prefix]
end
node = node.parent
end end
return merged return merged

View File

@ -358,19 +358,28 @@ describe Oga::XML::Element do
@child.node_set = Oga::XML::NodeSet.new([@child], @parent) @child.node_set = Oga::XML::NodeSet.new([@child], @parent)
@parent.register_namespace('foo', 'bar') @parent.register_namespace('foo', 'bar')
@parent.register_namespace('baz', 'yyy')
@child.register_namespace('baz', 'xxx') @child.register_namespace('baz', 'xxx')
@parent_ns = @parent.available_namespaces @parent_ns = @parent.available_namespaces
@child_ns = @child.available_namespaces @child_ns = @child.available_namespaces
end end
example 'return the available namespaces of the child node' do example 'inherit the "foo" namespace from the parent' do
@child_ns['foo'].is_a?(Oga::XML::Namespace).should == true @child_ns['foo'].uri.should == 'bar'
@child_ns['baz'].is_a?(Oga::XML::Namespace).should == true
end end
example 'return the available namespaces of the parent node' do example 'overwrite the "baz" namespace in the child' do
@parent_ns['foo'].is_a?(Oga::XML::Namespace).should == true @child_ns['baz'].uri.should == 'xxx'
end
example 'return the "foo" namespace for the parent' do
@parent_ns['foo'].uri.should == 'bar'
end
example 'return the "baz" namespace for the parent' do
@parent_ns['baz'].uri.should == 'yyy'
end end
end end
end end