Added XML::Element#default_namespace?
This commit is contained in:
parent
b6fcd326ef
commit
f175414917
|
@ -166,6 +166,16 @@ module Oga
|
||||||
return @namespace
|
return @namespace
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# Returns true if the current element resides in the default XML
|
||||||
|
# namespace.
|
||||||
|
#
|
||||||
|
# @return [TrueClass|FalseClass]
|
||||||
|
#
|
||||||
|
def default_namespace?
|
||||||
|
return namespace == DEFAULT_NAMESPACE || namespace.nil?
|
||||||
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
# Returns the text of all child nodes joined together.
|
# Returns the text of all child nodes joined together.
|
||||||
#
|
#
|
||||||
|
|
|
@ -1,48 +1,41 @@
|
||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
describe Oga::XML::Element do
|
describe Oga::XML::Element do
|
||||||
describe 'setting attributes' do
|
describe '#initialize' do
|
||||||
it 'sets the name via the constructor' do
|
it 'sets the name via the constructor' do
|
||||||
described_class.new(:name => 'p').name.should == 'p'
|
described_class.new(:name => 'p').name.should == 'p'
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'sets the name via a setter' do
|
|
||||||
instance = described_class.new
|
|
||||||
instance.name = 'p'
|
|
||||||
|
|
||||||
instance.name.should == 'p'
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'sets the default attributes' do
|
it 'sets the default attributes' do
|
||||||
described_class.new.attributes.should == []
|
described_class.new.attributes.should == []
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
describe 'setting namespaces via attributes' do
|
describe 'setting namespaces' do
|
||||||
before do
|
before do
|
||||||
attr = Oga::XML::Attribute.new(:name => 'foo', :namespace_name => 'xmlns')
|
attr = Oga::XML::Attribute.new(:name => 'foo', :namespace_name => 'xmlns')
|
||||||
|
|
||||||
@element = described_class.new(:attributes => [attr])
|
@element = described_class.new(:attributes => [attr])
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'registers the "foo" namespace' do
|
||||||
|
@element.namespaces['foo'].is_a?(Oga::XML::Namespace).should == true
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'keeps the attributes after registering the namespaces' do
|
||||||
|
@element.attributes.empty?.should == false
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'registers the "foo" namespace' do
|
describe 'setting the default namespace without a prefix' do
|
||||||
@element.namespaces['foo'].is_a?(Oga::XML::Namespace).should == true
|
before do
|
||||||
end
|
attr = Oga::XML::Attribute.new(:name => 'xmlns', :value => 'foo')
|
||||||
|
|
||||||
it 'keeps the attributes after registering the namespaces' do
|
@element = described_class.new(:attributes => [attr])
|
||||||
@element.attributes.empty?.should == false
|
end
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe 'setting the default namespace without a prefix' do
|
it 'registers the default namespace' do
|
||||||
before do
|
@element.namespaces['xmlns'].is_a?(Oga::XML::Namespace).should == true
|
||||||
attr = Oga::XML::Attribute.new(:name => 'xmlns', :value => 'foo')
|
end
|
||||||
|
|
||||||
@element = described_class.new(:attributes => [attr])
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'registers the default namespace' do
|
|
||||||
@element.namespaces['xmlns'].is_a?(Oga::XML::Namespace).should == true
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -214,6 +207,29 @@ describe Oga::XML::Element do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe '#default_namespace?' do
|
||||||
|
it 'returns true when an element has no explicit namespace' do
|
||||||
|
described_class.new(:name => 'a').default_namespace?.should == true
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns true when an element has an explicit default namespace' do
|
||||||
|
element = described_class.new(:name => 'a')
|
||||||
|
namespace = Oga::XML::DEFAULT_NAMESPACE
|
||||||
|
|
||||||
|
element.register_namespace(namespace.name, namespace.uri)
|
||||||
|
|
||||||
|
element.default_namespace?.should == true
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns false when an element resides in a custom namespace' do
|
||||||
|
element = described_class.new(:name => 'a')
|
||||||
|
|
||||||
|
element.register_namespace('xmlns', 'foo')
|
||||||
|
|
||||||
|
element.default_namespace?.should == false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe '#text' do
|
describe '#text' do
|
||||||
before do
|
before do
|
||||||
t1 = Oga::XML::Text.new(:text => 'Foo')
|
t1 = Oga::XML::Text.new(:text => 'Foo')
|
||||||
|
|
Loading…
Reference in New Issue