From 398aaf68bc80220b7563ed3a3bfd295efc6a01c0 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Sun, 14 Sep 2014 18:42:02 +0200 Subject: [PATCH] Return a correct list of available namespaces. This ensures that inner namespaces take precedence over outer namespaces. Fixes #40. --- lib/oga/xml/element.rb | 7 +++++-- spec/oga/xml/element_spec.rb | 19 ++++++++++++++----- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/lib/oga/xml/element.rb b/lib/oga/xml/element.rb index cb328f9..054f864 100644 --- a/lib/oga/xml/element.rb +++ b/lib/oga/xml/element.rb @@ -263,8 +263,11 @@ module Oga node = parent while node && node.respond_to?(:namespaces) - merged = merged.merge(node.namespaces) - node = node.parent + node.namespaces.each do |prefix, ns| + merged[prefix] = ns unless merged[prefix] + end + + node = node.parent end return merged diff --git a/spec/oga/xml/element_spec.rb b/spec/oga/xml/element_spec.rb index 41179b2..6c561ae 100644 --- a/spec/oga/xml/element_spec.rb +++ b/spec/oga/xml/element_spec.rb @@ -358,19 +358,28 @@ describe Oga::XML::Element do @child.node_set = Oga::XML::NodeSet.new([@child], @parent) @parent.register_namespace('foo', 'bar') + @parent.register_namespace('baz', 'yyy') + @child.register_namespace('baz', 'xxx') @parent_ns = @parent.available_namespaces @child_ns = @child.available_namespaces end - example 'return the available namespaces of the child node' do - @child_ns['foo'].is_a?(Oga::XML::Namespace).should == true - @child_ns['baz'].is_a?(Oga::XML::Namespace).should == true + example 'inherit the "foo" namespace from the parent' do + @child_ns['foo'].uri.should == 'bar' end - example 'return the available namespaces of the parent node' do - @parent_ns['foo'].is_a?(Oga::XML::Namespace).should == true + example 'overwrite the "baz" namespace in the child' do + @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