instance method select

Ruby on Rails 4.1.16

Since v2.2.3 Last seen in v4.1.16

Available in: v2.2.3 v2.3.18 v3.0.20 v3.1.12 v3.2.22.5 v4.0.13 v4.1.16

Signature

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

Parameters

root req
Source
# File actionview/lib/action_view/vendor/html-scanner/html/selector.rb, line 455
    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

Defined in actionview/lib/action_view/vendor/html-scanner/html/selector.rb line 455 · View on GitHub · Improve this page · Find usages on GitHub

Defined in HTML::Selector

Type at least 2 characters to search.

↑↓ navigate · open · esc close