Class: RubyQuery::Query
- Inherits:
-
Object
- Object
- RubyQuery::Query
- Defined in:
- lib/ruby_query.rb
Constant Summary collapse
- COMMANDS =
supported commands
{ :to_html => {:type => 'method'}, :inner_html => {:type => 'method'}, :text => {:type => 'method'}, :size => {:type => 'method'}, :width => {:type => 'attribute'}, :height => {:type => 'attribute'}, :value => {:type => 'attribute'}, :class => {:type => 'attribute'}, :first => {:type => 'traverse'}, :last => {:type => 'traverse'}, :parent => {:type => 'traverse'}, :next_sibling => {:type => 'traverse'}, :previous_sibling => {:type => 'traverse'}, :at => {:type => 'traverse', :arity => 1}, :search => {:type => 'proc', :proc => Proc.new {|context, param, query| context.css(param) } }, :attr => {:type => 'proc', :arity => 1, :proc => Proc.new {|context, param, query| context.attr(param).to_s rescue "" } }, :hasClass => {:type => 'proc', :arity => 1, :proc => Proc.new {|context, param, query| if context.is_a?(Nokogiri::XML::Element) !!context["class"].split(" ").find{|x| x == param} rescue false elsif context.is_a?(Nokogiri::XML::NodeSet) !!context.first["class"].split(" ").find{|x| x == param} rescue false else raise QueryError.new(context, name, type, param, "Cannot get hasClass from #{context.class}") end } } }
- ALIAS =
{ :html => :to_html, :len => :size, :length => :size, :count => :size, :get => :at, :"has-class" => :hasClass, :val => :value, :next => :next_sibling, :previous => :previous_sibling }
Class Method Summary collapse
Class Method Details
.query(html, *query) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/ruby_query.rb', line 76 def self.query(html, *query) ctx = Nokogiri::HTML(html) while method = query.shift if COMMANDS.keys.include?(method.to_sym) || ALIAS.keys.include?(method.to_sym) command = COMMANDS[method.to_sym] || COMMANDS[ALIAS[method.to_sym]] param = query.length > 0 ? (command[:arity] ? query.shift : nil) : nil ctx = handle_command(ctx, ALIAS[method.to_sym] || method.to_sym, command[:type], param, query) elsif command = COMMANDS[:search] ctx = handle_command(ctx, :search, command[:type], method, query) end ctx end if ctx.is_a?(Nokogiri::XML::Element) || ctx.is_a?(Nokogiri::XML::NodeSet) ctx = ctx.to_html else ctx end end |