Default attribute namespaces.

When an attribute is prefixed with "xml" the default namespace should be used
automatically. This namespace is not registered on element level by default as
this namespace isn't registered manually, instead it's a "magic" namespace. This
also ensures we match the behaviour of libxml more closely, hopefully reducing
confusion.
This commit is contained in:
Yorick Peterse 2014-08-27 20:24:40 +02:00
parent d2f991538d
commit 7c41fa814f
3 changed files with 27 additions and 2 deletions

View File

@ -25,8 +25,8 @@ require_relative 'oga/xml/cdata'
require_relative 'oga/xml/xml_declaration' require_relative 'oga/xml/xml_declaration'
require_relative 'oga/xml/processing_instruction' require_relative 'oga/xml/processing_instruction'
require_relative 'oga/xml/doctype' require_relative 'oga/xml/doctype'
require_relative 'oga/xml/attribute'
require_relative 'oga/xml/namespace' require_relative 'oga/xml/namespace'
require_relative 'oga/xml/attribute'
require_relative 'oga/xml/element' require_relative 'oga/xml/element'
require_relative 'oga/xml/node_set' require_relative 'oga/xml/node_set'

View File

@ -21,6 +21,17 @@ module Oga
class Attribute class Attribute
attr_accessor :name, :namespace_name, :element, :value attr_accessor :name, :namespace_name, :element, :value
##
# The default namespace available to all attributes. This namespace can
# not be modified.
#
# @return [Oga::XML::Namespace]
#
DEFAULT_NAMESPACE = Namespace.new(
:name => 'xml',
:uri => 'http://www.w3.org/XML/1998/namespace'
).freeze
## ##
# @param [Hash] options # @param [Hash] options
# #
@ -44,7 +55,15 @@ module Oga
# @return [Oga::XML::Namespace] # @return [Oga::XML::Namespace]
# #
def namespace def namespace
return @namespace ||= element.available_namespaces[namespace_name] unless @namespace
if namespace_name == DEFAULT_NAMESPACE.name
@namespace = DEFAULT_NAMESPACE
else
@namespace = element.available_namespaces[namespace_name]
end
end
return @namespace
end end
## ##

View File

@ -24,11 +24,17 @@ describe Oga::XML::Attribute do
:name => 'a', :name => 'a',
:element => element :element => element
) )
@default = described_class.new(:namespace_name => 'xml', :name => 'x')
end end
example 'return a Namespace instance' do example 'return a Namespace instance' do
@attribute.namespace.should == @namespace @attribute.namespace.should == @namespace
end end
example 'return the default XML namespace when the "xml" prefix is used' do
@default.namespace.should == Oga::XML::Attribute::DEFAULT_NAMESPACE
end
end end
context '#to_s' do context '#to_s' do