Added XML::Node#before and XML::Node#after

These methods can be used to insert nodes before/after other nodes without
manually having to mess around with node sets.

This fixes #28.
This commit is contained in:
Yorick Peterse committed 2014-09-09 22:48:31 +02:00
1 parent e0f8965616
commit e2dab952d0
4 files changed
+111

No files matched your search

+22
View File
@@ -142,6 +142,28 @@ module Oga
return node_set.delete(self) if node_set
end
##
# Inserts the given node before the current node.
#
# @param [Oga::XML::Node] other
#
def before(other)
index = node_set.index(self)
node_set.insert(index, other)
end
##
# Inserts the given node after the current node.
#
# @param [Oga::XML::Node] other
#
def after(other)
index = node_set.index(self) + 1
node_set.insert(index, other)
end
##
# @return [Symbol]
#
+14
View File
@@ -153,6 +153,20 @@ module Oga
return node
end
##
# Inserts a node into the set at the given index.
#
# @param [Fixnum] index The index to insert the node at.
# @param [Oga::XML::Node] node
#
def insert(index, node)
return if @nodes.include?(node)
@nodes.insert(index, node)
take_ownership(node)
end
##
# Returns the node for the given index.
#