Allow removal of element attributes.

This commit is contained in:
Yorick Peterse 2014-11-17 09:00:40 +01:00
parent 104576b4ad
commit cd86d5d294
2 changed files with 37 additions and 0 deletions

View File

@ -138,6 +138,20 @@ module Oga
end end
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. # Returns the namespace of the element.
# #

View File

@ -170,6 +170,29 @@ describe Oga::XML::Element do
end end
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 context '#namespace' do
example 'return the namespace' do example 'return the namespace' do
namespace = Oga::XML::Namespace.new(:name => 'x') namespace = Oga::XML::Namespace.new(:name => 'x')