Welcome to mirror list, hosted at ThFree Co, Russian Federation.

querying.rb « banzai « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1e1b51e683e0f56ad391d94c00f9c762124581c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
module Banzai
  module Querying
    # 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.
    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
  end
end