Class: REXML::CSSSelector::Queries::AttributeMatcherQuery

Inherits:
Object
  • Object
show all
Defined in:
lib/rexml/css_selector/queries/attribute_matcher_query.rb

Instance Method Summary collapse

Constructor Details

#initialize(cont:, name:, namespace:, matcher:, value:, modifier:) ⇒ AttributeMatcherQuery

Returns a new instance of AttributeMatcherQuery.



7
8
9
10
11
12
13
14
# File 'lib/rexml/css_selector/queries/attribute_matcher_query.rb', line 7

def initialize(cont:, name:, namespace:, matcher:, value:, modifier:)
  @cont = cont
  @name = name
  @namespace = namespace
  @matcher = matcher
  @value = value
  @modifier = modifier
end

Instance Method Details

#call(node, context) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rexml/css_selector/queries/attribute_matcher_query.rb', line 16

def call(node, context)
  value =
    case @value
    in Substitution[name:]
      context.substitutions[name]
    in String[value:]
      value
    in Ident[value:]
      value
    in Bare[value:]
      value
    end
  return false unless value

  name = @name
  name = @name.downcase(:ascii) if context.options[:attribute_name_case] == :insensitive
  actual = context.adapter.get_attribute(node, name, @namespace, context.options[:attribute_name_case])
  return false unless actual

  if @modifier == :i || context.options[:case_sensitive_attribute_values].include?(name)
    value = value.downcase(:ascii)
    actual = actual.downcase(:ascii)
  end

  case @matcher
  in :"="
    value == actual && @cont.call(node, context)
  in :"~="
    actual.split(/\s+/).include?(value) && @cont.call(node, context)
  in :"|="
    /(?:^|\|)#{value}(?:$|[|-])/.match?(actual) && @cont.call(node, context)
  in :"^="
    actual.start_with?(value) && @cont.call(node, context)
  in :"$="
    actual.end_with?(value) && @cont.call(node, context)
  in :"*="
    actual.include?(value) && @cont.call(node, context)
  end
end