class Nokogiri::CSS::XPathVisitor

def nth(node, options = {})

def nth(node, options = {})
  unless node.value.size == 4
    raise(ArgumentError, "expected an+b node to contain 4 tokens, but is #{node.value.inspect}")
  end
  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.zero?
    "(#{position} mod #{a})=0"
  else
    compare = a < 0 ? "<=" : ">="
    if a.abs == 1
      "#{position}#{compare}#{b}"
    else
      "(#{position}#{compare}#{b}) and (((#{position}-#{b}) mod #{a.abs})=0)"
    end
  end
end