Class: REXML::CSSSelector::Queries::NthLastChildQuery

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

Instance Method Summary collapse

Constructor Details

#initialize(cont:, a:, b:) ⇒ NthLastChildQuery

Returns a new instance of NthLastChildQuery.



7
8
9
10
11
# File 'lib/rexml/css_selector/queries/nth_last_child_query.rb', line 7

def initialize(cont:, a:, b:)
  @cont = cont
  @a = a
  @b = b
end

Instance Method Details

#call(node, context) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rexml/css_selector/queries/nth_last_child_query.rb', line 13

def call(node, context)
  return false unless context.adapter.element?(node)

  parent = context.adapter.get_parent_node(node)
  return false unless parent

  children_size = context.adapter.get_children_elements(parent).size
  index = context.adapter.get_element_index(parent, node)
  return false unless index
  index = children_size - index

  if @a.zero?
    index == @b && @cont.call(node, context)
  else
    ((index - @b) % @a).zero? && (index - @b) / @a >= 0 && @cont.call(node, context)
  end
end