File: slop.rb

package info (click to toggle)
ruby-nokogiri 1.18.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 8,076 kB
  • sloc: ansic: 38,893; xml: 27,665; ruby: 27,285; java: 15,348; cpp: 7,107; yacc: 244; sh: 208; makefile: 154; sed: 14
file content (42 lines) | stat: -rw-r--r-- 1,208 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# frozen_string_literal: true

module Nokogiri
  module Decorators
    ###
    # The Slop decorator implements method missing such that a methods may be
    # used instead of XPath or CSS.  See Nokogiri.Slop
    module Slop
      # The default XPath search context for Slop
      XPATH_PREFIX = "./"

      ###
      # look for node with +name+.  See Nokogiri.Slop
      def method_missing(name, *args, &block)
        if args.empty?
          list = xpath("#{XPATH_PREFIX}#{name.to_s.sub(/^_/, "")}")
        elsif args.first.is_a?(Hash)
          hash = args.first
          if hash[:css]
            list = css("#{name}#{hash[:css]}")
          elsif hash[:xpath]
            conds = Array(hash[:xpath]).join(" and ")
            list = xpath("#{XPATH_PREFIX}#{name}[#{conds}]")
          end
        else
          list = xpath(
            *CSS.xpath_for("#{name}#{args.first}", prefix: XPATH_PREFIX, cache: false),
          )
        end

        super if list.empty?
        list.length == 1 ? list.first : list
      end

      def respond_to_missing?(name, include_private = false)
        list = xpath("#{XPATH_PREFIX}#{name.to_s.sub(/^_/, "")}")

        !list.empty?
      end
    end
  end
end