class Nokogiri::CSS::XPathVisitor

def visit_pseudo_class node

def visit_pseudo_class node
  if node.value.first.is_a?(Nokogiri::CSS::Node) and node.value.first.type == :FUNCTION
    node.value.first.accept(self)
  else
    msg = :"visit_pseudo_class_#{node.value.first.gsub(/[(]/, '')}"
    return self.send(msg, node) if self.respond_to?(msg)
    case node.value.first
    when "first" then "position() = 1"
    when "first-child" then "count(preceding-sibling::*) = 0"
    when "last" then "position() = last()"
    when "last-child" then "count(following-sibling::*) = 0"
    when "first-of-type" then "position() = 1"
    when "last-of-type" then "position() = last()"
    when "only-child" then "count(preceding-sibling::*) = 0 and count(following-sibling::*) = 0"
    when "only-of-type" then "last() = 1"
    when "empty" then "not(node())"
    when "parent" then "node()"
    when "root" then "not(parent::*)"
    else
      node.value.first + "(.)"
    end
  end
end