Changed NodeSet to behave more like an Array.

This commit is contained in:
Yorick Peterse 2014-06-26 09:37:54 +02:00
parent a98f50b63b
commit eb9d4fbccc
1 changed files with 27 additions and 5 deletions

View File

@ -31,13 +31,21 @@ module Oga
alias_method :<<, :push alias_method :<<, :push
def attr(name) def unshift(node)
return @nodes.map { |node| node.attr(name) } @nodes.unshift(node)
end
def shift
return @noes.shift
end
def pop
return @nodes.pop
end end
def remove def remove
@nodes.each do |node| @nodes.each do |node|
node.node_set.delete!(node) node.node_set.delete(node)
node.node_set = nil node.node_set = nil
end end
end end
@ -45,10 +53,24 @@ module Oga
## ##
# Removes a node from the current set only. # Removes a node from the current set only.
# #
def delete!(node) def delete(node)
@nodes.delete(node) @nodes.delete(node)
end end
def attribute(name)
values = []
@nodes.each do |node|
if node.node_type == :element
values << node.attribute(name)
end
end
return values
end
alias_method :attr, :attribute
def text def text
text = '' text = ''
@ -59,7 +81,7 @@ module Oga
return text return text
end end
def associate_nodes def associate_nodes!
@nodes.each_with_index do |node, index| @nodes.each_with_index do |node, index|
node.node_set = self node.node_set = self
end end