module Nokogiri::CSS - nokogiri-1.15.2 Documentation

# module Nokogiri::CSS

Translate a `CSS` selector into an XPath 1.0 query

### Public Class Methods

xpath_for(selector) → String click to toggle source
xpath_for(selector [, prefix:] [, visitor:] [, ns:]) → String

Translate a `CSS` selector to the equivalent XPath query.

Parameters

- `selector` (String) The `CSS` selector to be translated into XPath

- `prefix:` (String)

  The XPath prefix for the query, see `Nokogiri::XML::XPath` for some options. Default is `XML::XPath::GLOBAL_SEARCH_PREFIX`.

- `visitor:` (`Nokogiri::CSS::XPathVisitor`)

  The visitor class to use to transform the AST into XPath. Default is `Nokogiri::CSS::XPathVisitor.new`.

- `ns:` (Hash<String ⇒ String>)

  The namespaces that are referenced in the query, if any. This is a hash where the keys are the namespace prefix and the values are the namespace URIs. Default is an empty Hash.

Returns

  (String) The equivalent XPath query for `selector`

💡 Note that translated queries are cached for performance concerns.

    # File lib/nokogiri/css.rb, line 42
    def xpath_for(selector, options = {})
      raise TypeError, "no implicit conversion of #{selector.inspect} to String" unless selector.respond_to?(:to_str)

      selector = selector.to_str
      raise Nokogiri::CSS::SyntaxError, "empty CSS selector" if selector.empty?

      prefix = options.fetch(:prefix, Nokogiri::XML::XPath::GLOBAL_SEARCH_PREFIX)
      visitor = options.fetch(:visitor) { Nokogiri::CSS::XPathVisitor.new }
      ns = options.fetch(:ns, {})

      Parser.new(ns).xpath_for(selector, prefix, visitor)
    end