Class: Gitlab::Utils::Nokogiri

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/utils/nokogiri.rb

Class Method Summary collapse

Class Method Details

.css_to_xpath(css) ⇒ Object

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. gitlab.com/gitlab-org/gitlab/-/issues/329186



12
13
14
15
16
17
18
19
20
# File 'lib/gitlab/utils/nokogiri.rb', line 12

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..]}" }.join('|')
end