Method: HTML::Selector#select

Defined in:
lib/action_controller/vendor/html-scanner/html/selector.rb

#select(root) ⇒ Object

:call-seq:

select(root) => array

Selects and returns an array with all matching elements, beginning with one node and traversing through all children depth-first. Returns an empty array if no match is found.

The root node may be any element in the document, or the document itself.

For example:

selector = HTML::Selector.new "input[type=text]"
matches = selector.select(element)
matches.each do |match|
  puts "Found text field with name #{match.attributes['name']}"
end

453
454
455
456
457
458
459
460
461
462
463
464
465
466
# File 'lib/action_controller/vendor/html-scanner/html/selector.rb', line 453

def select(root)
  matches = []
  stack = [root]
  while node = stack.pop
    if node.tag? && subset = match(node, false)
      subset.each do |match|
        matches << match unless matches.any? { |item| item.equal?(match) }
      end
    elsif children = node.children
      stack.concat children.reverse
    end
  end
  matches
end