Added Node#remove.

This method can be used to remove individual nodes without first having to
retrieve the NodeSet they are stored in.
This commit is contained in:
Yorick Peterse 2014-07-04 10:26:41 +02:00
parent e334e50ca6
commit 8b381ac970
2 changed files with 29 additions and 0 deletions

View File

@ -130,6 +130,15 @@ module Oga
return node return node
end end
##
# Removes the current node from the owning node set.
#
# @return [Oga::XML::Node]
#
def remove
return node_set.delete(self) if node_set
end
## ##
# Generates the inspect value for the current node. Sub classes can # Generates the inspect value for the current node. Sub classes can
# overwrite the {#extra_inspect_data} method to customize the output # overwrite the {#extra_inspect_data} method to customize the output

View File

@ -154,4 +154,24 @@ describe Oga::XML::Node do
@n4.root_node.should == @n3 @n4.root_node.should == @n3
end end
end end
context '#remove' do
before do
owner = described_class.new
@n1 = described_class.new
@set = Oga::XML::NodeSet.new([@n1], owner)
end
example 'return a node from the node set' do
@n1.remove
@set.empty?.should == true
end
example 'remove the reference to the set' do
@n1.remove
@n1.node_set.nil?.should == true
end
end
end end