diff --git a/lib/oga/xml/node.rb b/lib/oga/xml/node.rb index 74ef039..1647d23 100644 --- a/lib/oga/xml/node.rb +++ b/lib/oga/xml/node.rb @@ -130,6 +130,15 @@ module Oga return node 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 # overwrite the {#extra_inspect_data} method to customize the output diff --git a/spec/oga/xml/node_spec.rb b/spec/oga/xml/node_spec.rb index fe1f6fd..fee205e 100644 --- a/spec/oga/xml/node_spec.rb +++ b/spec/oga/xml/node_spec.rb @@ -154,4 +154,24 @@ describe Oga::XML::Node do @n4.root_node.should == @n3 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