Handle registering of default XML namespaces.
When the default namespace is registered (using xmlns="...") Oga now properly sets the namespace of the container and all child elements. This fixes #44.
This commit is contained in:
parent
ad2e040f05
commit
abbd8d6f84
|
@ -144,7 +144,13 @@ module Oga
|
||||||
# @return [Oga::XML::Namespace]
|
# @return [Oga::XML::Namespace]
|
||||||
#
|
#
|
||||||
def namespace
|
def namespace
|
||||||
return @namespace ||= available_namespaces[namespace_name]
|
unless @namespace
|
||||||
|
available = available_namespaces
|
||||||
|
|
||||||
|
@namespace = available[namespace_name] || available[XMLNS_PREFIX]
|
||||||
|
end
|
||||||
|
|
||||||
|
return @namespace
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
|
@ -283,7 +289,14 @@ module Oga
|
||||||
self.attributes = attributes.reject do |attr|
|
self.attributes = attributes.reject do |attr|
|
||||||
# We're using `namespace_name` opposed to `namespace.name` as "xmlns"
|
# We're using `namespace_name` opposed to `namespace.name` as "xmlns"
|
||||||
# is not a registered namespace.
|
# is not a registered namespace.
|
||||||
remove = attr.namespace_name && attr.namespace_name == XMLNS_PREFIX
|
remove = attr.name == XMLNS_PREFIX ||
|
||||||
|
attr.namespace_name == XMLNS_PREFIX
|
||||||
|
|
||||||
|
# If the attribute sets the default namespace we'll also overwrite the
|
||||||
|
# namespace_name.
|
||||||
|
if attr.name == XMLNS_PREFIX
|
||||||
|
@namespace_name = XMLNS_PREFIX
|
||||||
|
end
|
||||||
|
|
||||||
register_namespace(attr.name, attr.value) if remove
|
register_namespace(attr.name, attr.value) if remove
|
||||||
|
|
||||||
|
|
|
@ -34,6 +34,22 @@ describe Oga::XML::Element do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'setting the default namespace without a prefix' do
|
||||||
|
before do
|
||||||
|
attr = Oga::XML::Attribute.new(:name => 'xmlns', :value => 'foo')
|
||||||
|
|
||||||
|
@element = described_class.new(:attributes => [attr])
|
||||||
|
end
|
||||||
|
|
||||||
|
example 'register the default namespace' do
|
||||||
|
@element.namespaces['xmlns'].is_a?(Oga::XML::Namespace).should == true
|
||||||
|
end
|
||||||
|
|
||||||
|
example 'remove the namespace attribute from the list of attributes' do
|
||||||
|
@element.attributes.empty?.should == true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context '#attribute' do
|
context '#attribute' do
|
||||||
before do
|
before do
|
||||||
attributes = [
|
attributes = [
|
||||||
|
@ -159,16 +175,23 @@ describe Oga::XML::Element do
|
||||||
end
|
end
|
||||||
|
|
||||||
context '#namespace' do
|
context '#namespace' do
|
||||||
before do
|
example 'return the namespace' do
|
||||||
@namespace = Oga::XML::Namespace.new(:name => 'x')
|
namespace = Oga::XML::Namespace.new(:name => 'x')
|
||||||
@element = described_class.new(
|
element = described_class.new(
|
||||||
:namespace_name => 'x',
|
:namespace_name => 'x',
|
||||||
:namespaces => {'x' => @namespace}
|
:namespaces => {'x' => namespace}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
element.namespace.should == namespace
|
||||||
end
|
end
|
||||||
|
|
||||||
example 'return the namespace' do
|
example 'return the default namespace if available' do
|
||||||
@element.namespace.should == @namespace
|
namespace = Oga::XML::Namespace.new(:name => 'xmlns')
|
||||||
|
element = described_class.new(
|
||||||
|
:namespaces => {'xmlns' => namespace}
|
||||||
|
)
|
||||||
|
|
||||||
|
element.namespace.should == namespace
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -37,6 +37,31 @@ describe Oga::XML::Parser do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'elements with default namespaces' do
|
||||||
|
before :all do
|
||||||
|
@document = parse('<foo xmlns="bar"><bar></bar></foo>')
|
||||||
|
|
||||||
|
@foo = @document.children[0]
|
||||||
|
@bar = @foo.children[0]
|
||||||
|
end
|
||||||
|
|
||||||
|
example 'set the namespace name of the <foo> element' do
|
||||||
|
@foo.namespace.name.should == 'xmlns'
|
||||||
|
end
|
||||||
|
|
||||||
|
example 'set the namespace URI of the <foo> element' do
|
||||||
|
@foo.namespace.uri.should == 'bar'
|
||||||
|
end
|
||||||
|
|
||||||
|
example 'set the namespace name of the <bar> element' do
|
||||||
|
@bar.namespace.name.should == 'xmlns'
|
||||||
|
end
|
||||||
|
|
||||||
|
example 'set the namespace URI of the <bar> element' do
|
||||||
|
@bar.namespace.uri.should == 'bar'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'elements with attributes' do
|
context 'elements with attributes' do
|
||||||
before :all do
|
before :all do
|
||||||
@element = parse('<foo bar="baz"></foo>').children[0]
|
@element = parse('<foo bar="baz"></foo>').children[0]
|
||||||
|
|
Loading…
Reference in New Issue