Added expanded_name for Element and Attribute

This commit is contained in:
Yorick Peterse 2015-08-18 14:07:09 +02:00
parent 25e2f57a8d
commit d408989499
6 changed files with 54 additions and 0 deletions

View File

@ -27,6 +27,7 @@ require 'oga/xml/html_void_elements'
require 'oga/xml/entities' require 'oga/xml/entities'
require 'oga/xml/querying' require 'oga/xml/querying'
require 'oga/xml/traversal' require 'oga/xml/traversal'
require 'oga/xml/expanded_name'
require 'oga/xml/node' require 'oga/xml/node'
require 'oga/xml/document' require 'oga/xml/document'
require 'oga/xml/character_node' require 'oga/xml/character_node'

View File

@ -4,6 +4,8 @@ module Oga
# Class for storing information about a single XML attribute. # Class for storing information about a single XML attribute.
# #
class Attribute class Attribute
include ExpandedName
# The name of the attribute. # The name of the attribute.
# @return [String] # @return [String]
attr_accessor :name attr_accessor :name

View File

@ -6,6 +6,7 @@ module Oga
# #
class Element < Node class Element < Node
include Querying include Querying
include ExpandedName
# @return [String] # @return [String]
attr_reader :namespace_name attr_reader :namespace_name

View File

@ -0,0 +1,14 @@
module Oga
module XML
module ExpandedName
##
# Returns the expanded name of the current Element or Attribute.
#
# @return [String]
#
def expanded_name
namespace_name ? "#{namespace_name}:#{name}" : name
end
end # ExpandedName
end # XML
end # Oga

View File

@ -164,4 +164,22 @@ Attribute(name: "a" namespace: Namespace(name: "b" uri: nil) value: "c")
EOF EOF
end end
end end
describe '#expanded_name' do
describe 'with a namespace' do
it 'returns a String' do
element = described_class.new(:namespace_name => 'foo', :name => 'bar')
element.expanded_name.should == 'foo:bar'
end
end
describe 'without a namespace' do
it 'returns a String' do
element = described_class.new(:name => 'bar')
element.expanded_name.should == 'bar'
end
end
end
end end

View File

@ -608,4 +608,22 @@ describe Oga::XML::Element do
parent.flush_namespaces_cache parent.flush_namespaces_cache
end end
end end
describe '#expanded_name' do
describe 'with a namespace' do
it 'returns a String' do
element = described_class.new(:namespace_name => 'foo', :name => 'bar')
element.expanded_name.should == 'foo:bar'
end
end
describe 'without a namespace' do
it 'returns a String' do
element = described_class.new(:name => 'bar')
element.expanded_name.should == 'bar'
end
end
end
end end