Added Node#previous_element and Node#next_element.
These methods can be used similar to #previous and #next expect that they only return Element instances opposed to all Node instances.
This commit is contained in:
parent
94965961ce
commit
e334e50ca6
|
@ -80,6 +80,36 @@ module Oga
|
||||||
return index <= length ? node_set[index] : nil
|
return index <= length ? node_set[index] : nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# Returns the previous element node or nil if there is none.
|
||||||
|
#
|
||||||
|
# @return [Oga::XML::Element]
|
||||||
|
#
|
||||||
|
def previous_element
|
||||||
|
node = self
|
||||||
|
|
||||||
|
while node = node.previous
|
||||||
|
return node if node.is_a?(Element)
|
||||||
|
end
|
||||||
|
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# Returns the next element node or nil if there is none.
|
||||||
|
#
|
||||||
|
# @return [Oga::XML::Element]
|
||||||
|
#
|
||||||
|
def next_element
|
||||||
|
node = self
|
||||||
|
|
||||||
|
while node = node.next
|
||||||
|
return node if node.is_a?(Element)
|
||||||
|
end
|
||||||
|
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
# Returns the root document/node of the current node. The node is
|
# Returns the root document/node of the current node. The node is
|
||||||
# retrieved by traversing upwards in the DOM tree from the current node.
|
# retrieved by traversing upwards in the DOM tree from the current node.
|
||||||
|
|
|
@ -29,16 +29,6 @@ describe Oga::XML::Node do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context '#parent' do
|
|
||||||
example 'return the parent of the node' do
|
|
||||||
owner = described_class.new
|
|
||||||
set = Oga::XML::NodeSet.new([], owner)
|
|
||||||
node = described_class.new(:node_set => set)
|
|
||||||
|
|
||||||
node.parent.should == owner
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context '#children=' do
|
context '#children=' do
|
||||||
example 'set the child nodes using an Array' do
|
example 'set the child nodes using an Array' do
|
||||||
child = described_class.new
|
child = described_class.new
|
||||||
|
@ -59,6 +49,16 @@ describe Oga::XML::Node do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context '#parent' do
|
||||||
|
example 'return the parent of the node' do
|
||||||
|
owner = described_class.new
|
||||||
|
set = Oga::XML::NodeSet.new([], owner)
|
||||||
|
node = described_class.new(:node_set => set)
|
||||||
|
|
||||||
|
node.parent.should == owner
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context '#previous' do
|
context '#previous' do
|
||||||
before do
|
before do
|
||||||
owner = described_class.new
|
owner = described_class.new
|
||||||
|
@ -93,6 +93,50 @@ describe Oga::XML::Node do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context '#previous_element' do
|
||||||
|
before do
|
||||||
|
owner = described_class.new
|
||||||
|
@n1 = Oga::XML::Element.new
|
||||||
|
@n2 = Oga::XML::Text.new
|
||||||
|
@n3 = described_class.new
|
||||||
|
@set = Oga::XML::NodeSet.new([@n1, @n2, @n3], owner)
|
||||||
|
end
|
||||||
|
|
||||||
|
example 'return the previous element of a generic node' do
|
||||||
|
@n3.previous_element.should == @n1
|
||||||
|
end
|
||||||
|
|
||||||
|
example 'return the previous element of a text node' do
|
||||||
|
@n2.previous_element.should == @n1
|
||||||
|
end
|
||||||
|
|
||||||
|
example 'return nil if there is no previous element' do
|
||||||
|
@n1.previous_element.nil?.should == true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context '#next_element' do
|
||||||
|
before do
|
||||||
|
owner = described_class.new
|
||||||
|
@n1 = described_class.new
|
||||||
|
@n2 = Oga::XML::Text.new
|
||||||
|
@n3 = Oga::XML::Element.new
|
||||||
|
@set = Oga::XML::NodeSet.new([@n1, @n2, @n3], owner)
|
||||||
|
end
|
||||||
|
|
||||||
|
example 'return the next element of a generic node' do
|
||||||
|
@n1.next_element.should == @n3
|
||||||
|
end
|
||||||
|
|
||||||
|
example 'return the next element of a text node' do
|
||||||
|
@n2.next_element.should == @n3
|
||||||
|
end
|
||||||
|
|
||||||
|
example 'return nil if there is no next element' do
|
||||||
|
@n3.next_element.nil?.should == true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context '#root_node' do
|
context '#root_node' do
|
||||||
before do
|
before do
|
||||||
@n4 = described_class.new
|
@n4 = described_class.new
|
||||||
|
|
Loading…
Reference in New Issue