Module: Banzai::Querying

Defined in:
lib/banzai/querying.rb

Class Method Summary collapse

Class Method Details

.css(document, query) ⇒ Object

Searches a Nokogiri document using a CSS query, optionally optimizing it whenever possible.

document - A document/element to search. query - The CSS query to use.

Returns a Nokogiri::XML::NodeSet.


10
11
12
13
14
15
16
# File 'lib/banzai/querying.rb', line 10

def self.css(document, query)
  # When using "a.foo" Nokogiri compiles this to "//a[...]" but
  # "descendant::a[...]" is quite a bit faster and achieves the same result.
  xpath = Nokogiri::CSS.xpath_for(query)[0].gsub(%r{^//}, 'descendant::')

  document.xpath(xpath)
end