Class: Gammo::CSSSelector::AST::Selector::Pseudo::NthChild

Inherits:
Gammo::CSSSelector::AST::Selector::Pseudo show all
Defined in:
lib/gammo/css_selector/ast/selector/pseudo_class.rb

Constant Summary collapse

InvalidExpression =
Class.new(ArgumentError)
CONVERT_TABLE =
{
  'odd'  => ['2n+1'], #'2n+1',
  'even' => ['2n']
}.freeze

Constants included from Subclassify

Subclassify::NotFoundError

Instance Attribute Summary

Attributes inherited from Gammo::CSSSelector::AST::Selector::Pseudo

#value

Instance Method Summary collapse

Methods inherited from Gammo::CSSSelector::AST::Selector::Pseudo

#initialize

Constructor Details

This class inherits a constructor from Gammo::CSSSelector::AST::Selector::Pseudo

Instance Method Details

#match?(context) ⇒ Boolean

TODO: AST-style

Returns:

  • (Boolean)


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/gammo/css_selector/ast/selector/pseudo_class.rb', line 71

def match?(context)
  exprs = @arguments[0]
  exprs = CONVERT_TABLE[exprs[0]] if CONVERT_TABLE[exprs[0]]

  case value = exprs.join
  when /\A\s*([\+\-])?([0-9]+)?\s*\z/ then
    # Raises an error if given value is not integer, but basically unreachable.
    context.position == Integer(value)
  when match = /\A\s*([\-\+])?([0-9]+)?(#{Parser::N})(?:\s*([\-\+])\s*([0-9]+))?\s*\z/
    d = (context.position - "#{$6}#{$7}".to_f) / "#{$1}#{$2 || 1}".to_f
    # Converts the value into integer in order to ignore float numbers.
    d >= 0 && d == d.to_i
  else
    raise InvalidExpression, 'invalid expression = %s' % value
  end
end