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

nokogiri.rb « utils « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4b37bb7e5eaba1120cf6f20de23f58c87923c7b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# frozen_string_literal: true

module Gitlab
  module Utils
    class Nokogiri
      class << self
        # Use Nokogiri to convert a css selector into an xpath selector.
        # Nokogiri can use css selectors with `doc.search()`.  However
        # for large node trees, it is _much_ slower than using xpath,
        # by several orders of magnitude.
        # https://gitlab.com/gitlab-org/gitlab/-/issues/329186
        def css_to_xpath(css)
          xpath = ::Nokogiri::CSS.xpath_for(css)

          # Due to https://github.com/sparklemotion/nokogiri/issues/572,
          # we remove the leading `//` and add `descendant-or-self::`
          # in order to ensure we're searching from this node and all
          # descendants.
          xpath.map { |t| "descendant-or-self::#{t[2..-1]}" }.join('|')
        end
      end
    end
  end
end