Module: CSS::Selectors::Matcher

Extended by:
Matcher
Included in:
Matcher
Defined in:
lib/css/selectors/matcher.rb

Overview

Matches a Selector AST against any duck-typed element. Required methods on the element:

- `name` (or `tag_name`)            — tag name
- `[](attr)`                         — attribute value or nil
- `parent`                           — parent element or non-element
- `previous_element` (or `previous_element_sibling`) — preceding
  element sibling
- `next_element` (or `next_element_sibling`)         — following
  element sibling
- `children` (and optionally `element_children`) — child nodes

‘Nokogiri::XML::Element` and `Nokogiri::HTML::Element` satisfy this protocol out of the box.

Pseudo-classes that depend on user-agent state (‘:hover`, `:focus`, `:visited`, validity-API states, `:fullscreen`, etc.) always return false; this matcher is intended for stateless analysis.

Constant Summary collapse

DISABLEABLE_TAGS =
%w[button input select textarea optgroup option fieldset].freeze
INPUT_TAGS =
%w[input textarea select].freeze
%w[a area link].freeze
RO_INPUT_TYPES =
%w[hidden range color checkbox radio file submit image reset button].freeze

Instance Method Summary collapse

Instance Method Details

#matches?(element, selector) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/css/selectors/matcher.rb', line 29

def matches?(element, selector)
  sel = selector.is_a?(String) ? Parser.parse_selector_list(selector) : selector

  case sel
  when SelectorList
    sel.selectors.any? { match_complex(element, it) }
  when ComplexSelector
    match_complex(element, sel)
  when CompoundSelector
    match_compound(element, sel)
  else
    raise ArgumentError, "expected a selector node or string, got #{sel.class}"
  end
end