diff --git a/lib/oga/xml/element.rb b/lib/oga/xml/element.rb index 2d990c6..29a78f3 100644 --- a/lib/oga/xml/element.rb +++ b/lib/oga/xml/element.rb @@ -138,6 +138,20 @@ module Oga end end + ## + # Removes an attribute from the element. + # + # @param [String] name The name (optionally including namespace prefix) + # of the attribute to remove. + # + # @return [Oga::XML::Attribute] + # + def unset(name) + found = attribute(name) + + return attributes.delete(found) if found + end + ## # Returns the namespace of the element. # diff --git a/spec/oga/xml/element_spec.rb b/spec/oga/xml/element_spec.rb index 44c51c3..5d35cad 100644 --- a/spec/oga/xml/element_spec.rb +++ b/spec/oga/xml/element_spec.rb @@ -170,6 +170,29 @@ describe Oga::XML::Element do end end + context '#unset' do + before do + @element = described_class.new + + @element.register_namespace('x', 'test') + + @element.set('foo', 'bar') + @element.set('x:foo', 'bar') + end + + example 'remove an attribute by its name' do + @element.unset('foo') + + @element.get('foo').should be_nil + end + + example 'remove an attribute using a namespace' do + @element.unset('x:foo') + + @element.get('x:foo').should be_nil + end + end + context '#namespace' do example 'return the namespace' do namespace = Oga::XML::Namespace.new(:name => 'x')