class Nokogiri::CSS::XPathVisitor

def nth node, options={}

def nth node, options={}
  raise ArgumentError, "expected an+b node to contain 4 tokens, but is #{node.value.inspect}" unless node.value.size == 4
  a, b = read_a_and_positive_b node.value
  position = if options[:child]
    options[:last] ? "(count(following-sibling::*) + 1)" : "(count(preceding-sibling::*) + 1)"
  else
    options[:last] ? "(last()-position()+1)" : "position()"
  end
  if (b == 0)
    return "(#{position} mod #{a}) = 0"
  else
    compare = (a < 0) ? "<=" : ">="
    return "(#{position} #{compare} #{b}) and (((#{position}-#{b}) mod #{a.abs}) = 0)"
  end
end