Class: Gammo::CSSSelector::AST::Selector::Pseudo::NthChild
- Inherits:
-
Gammo::CSSSelector::AST::Selector::Pseudo
- Object
- Gammo::CSSSelector::AST::Selector::Pseudo
- Gammo::CSSSelector::AST::Selector::Pseudo::NthChild
- 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
Instance Attribute Summary
Attributes inherited from Gammo::CSSSelector::AST::Selector::Pseudo
Instance Method Summary collapse
-
#match?(context) ⇒ Boolean
TODO: AST-style.
Methods inherited from Gammo::CSSSelector::AST::Selector::Pseudo
Constructor Details
This class inherits a constructor from Gammo::CSSSelector::AST::Selector::Pseudo
Instance Method Details
#match?(context) ⇒ Boolean
TODO: AST-style
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 |