Module: CSS::Selectors::Matcher
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`, etc.) return false by default; pass an explicit `state:` mapping to opt into stateful matching. Validity-API and viewport- only states (`:fullscreen`, `:valid`, …) are not exposed.
Defined Under Namespace
Classes: Context
Constant Summary collapse
- DISABLEABLE_TAGS =
%w[button input select textarea optgroup option fieldset].freeze
- INPUT_TAGS =
%w[input textarea select].freeze
- LINK_TAGS =
%w[a area link].freeze
- RO_INPUT_TYPES =
%w[hidden range color checkbox radio file submit image reset button].freeze
- STATEFUL_PSEUDOS =
User-agent state pseudos. The matcher returns ‘false` for these unless the caller passes a `state:` Hash describing which elements (or “all”) should match.
%w[hover focus focus-within focus-visible active visited target].to_set.freeze
- PROPAGATING_STATEFUL_PSEUDOS =
Per spec these states propagate up the ancestor chain — if a descendant is hovered/active/contains-focus, the ancestors share the state for selector-matching purposes.
%w[hover active focus-within].to_set.freeze
- EMPTY_CLASS_SET =
Set.new.freeze
Instance Method Summary collapse
- #classes_of(element, cache = nil) ⇒ Object
- #id_of(element, cache = nil) ⇒ Object
- #matches?(element, selector, cache: nil, state: nil) ⇒ Boolean
- #tag_of(element, cache = nil) ⇒ Object
Instance Method Details
#classes_of(element, cache = nil) ⇒ Object
134 135 136 137 |
# File 'lib/css/selectors/matcher.rb', line 134 def classes_of(element, cache = nil) ctx = context_for(element, cache) ctx ? ctx.classes : build_class_set(element) end |
#id_of(element, cache = nil) ⇒ Object
129 130 131 132 |
# File 'lib/css/selectors/matcher.rb', line 129 def id_of(element, cache = nil) ctx = context_for(element, cache) ctx ? ctx.id : attr(element, 'id') end |
#matches?(element, selector, cache: nil, state: nil) ⇒ Boolean
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/css/selectors/matcher.rb', line 48 def matches?(element, selector, cache: nil, state: nil) sel = selector.is_a?(String) ? Parser.parse_selector_list(selector) : selector case sel when SelectorList sel.selectors.any? { match_complex(element, it, cache, state) } when ComplexSelector match_complex(element, sel, cache, state) when CompoundSelector match_compound(element, sel, cache, state) else raise ArgumentError, "expected a selector node or string, got #{sel.class}" end end |
#tag_of(element, cache = nil) ⇒ Object
124 125 126 127 |
# File 'lib/css/selectors/matcher.rb', line 124 def tag_of(element, cache = nil) ctx = context_for(element, cache) ctx ? ctx.tag : tag(element) end |