diff --git a/lib/oga/xml/element.rb b/lib/oga/xml/element.rb index 4947f98..13cdb50 100644 --- a/lib/oga/xml/element.rb +++ b/lib/oga/xml/element.rb @@ -55,7 +55,8 @@ module Oga end ## - # Returns the attribute of the given name. + # Returns an attribute matching the given name (with or without the + # namespace). # # @example # # find an attribute that only has the name "foo" @@ -64,8 +65,10 @@ module Oga # # find an attribute with namespace "foo" and name bar" # attribute('foo:bar') # - # @param [String] name - # @return [String] + # @param [String|Symbol] name The name (with or without the namespace) + # of the attribute. + # + # @return [Oga::XML::Attribute] # def attribute(name) name, ns = split_name(name) @@ -79,6 +82,20 @@ module Oga alias_method :attr, :attribute + ## + # Returns the value of the given attribute. + # + # @example + # element.get('class') # => "container" + # + # @see [#attribute] + # + def get(name) + found = attribute(name) + + return found ? found.value : nil + end + ## # Returns the namespace of the element. # diff --git a/spec/oga/xml/element_spec.rb b/spec/oga/xml/element_spec.rb index 0dfc56a..764e710 100644 --- a/spec/oga/xml/element_spec.rb +++ b/spec/oga/xml/element_spec.rb @@ -89,6 +89,18 @@ describe Oga::XML::Element do end end + context '#get' do + before do + attr = Oga::XML::Attribute.new(:name => 'foo', :value => 'bar') + + @element = described_class.new(:attributes => [attr]) + end + + example 'return the value of an attribute' do + @element.get('foo').should == 'bar' + end + end + context '#namespace' do before do @namespace = Oga::XML::Namespace.new(:name => 'x')