public
Description: Nokogiri (鋸) is an HTML, XML, SAX, and Reader parser with XPath and CSS selector support.
Homepage: http://nokogiri.org/
Clone URL: git://github.com/tenderlove/nokogiri.git
nokogiri / lib / nokogiri / decorators / slop.rb
100644 34 lines (32 sloc) 0.932 kb
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
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
      ###
      # look for node with +name+. See Nokogiri.Slop
      def method_missing name, *args, &block
        if args.empty?
          list = xpath("./#{name}")
        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("./#{name}[#{conds}]")
          end
        else
          CSS::Parser.without_cache do
            list = xpath(
              *CSS.xpath_for("#{name}#{args.first}", :prefix => "./")
            )
          end
        end
 
        super if list.empty?
        list.length == 1 ? list.first : list
      end
    end
  end
end