diff --git a/lib/oga.rb b/lib/oga.rb index a82b723..af07b65 100644 --- a/lib/oga.rb +++ b/lib/oga.rb @@ -27,6 +27,7 @@ require 'oga/xml/html_void_elements' require 'oga/xml/entities' require 'oga/xml/querying' require 'oga/xml/traversal' +require 'oga/xml/expanded_name' require 'oga/xml/node' require 'oga/xml/document' require 'oga/xml/character_node' diff --git a/lib/oga/xml/attribute.rb b/lib/oga/xml/attribute.rb index 5e5d7f5..99dc96d 100644 --- a/lib/oga/xml/attribute.rb +++ b/lib/oga/xml/attribute.rb @@ -4,6 +4,8 @@ module Oga # Class for storing information about a single XML attribute. # class Attribute + include ExpandedName + # The name of the attribute. # @return [String] attr_accessor :name diff --git a/lib/oga/xml/element.rb b/lib/oga/xml/element.rb index da9cbcf..04abc13 100644 --- a/lib/oga/xml/element.rb +++ b/lib/oga/xml/element.rb @@ -6,6 +6,7 @@ module Oga # class Element < Node include Querying + include ExpandedName # @return [String] attr_reader :namespace_name diff --git a/lib/oga/xml/expanded_name.rb b/lib/oga/xml/expanded_name.rb new file mode 100644 index 0000000..63484d9 --- /dev/null +++ b/lib/oga/xml/expanded_name.rb @@ -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 diff --git a/spec/oga/xml/attribute_spec.rb b/spec/oga/xml/attribute_spec.rb index fbd9185..0bb22cf 100644 --- a/spec/oga/xml/attribute_spec.rb +++ b/spec/oga/xml/attribute_spec.rb @@ -164,4 +164,22 @@ Attribute(name: "a" namespace: Namespace(name: "b" uri: nil) value: "c") EOF 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 diff --git a/spec/oga/xml/element_spec.rb b/spec/oga/xml/element_spec.rb index db92080..286a1d4 100644 --- a/spec/oga/xml/element_spec.rb +++ b/spec/oga/xml/element_spec.rb @@ -608,4 +608,22 @@ describe Oga::XML::Element do parent.flush_namespaces_cache 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