Permalink
Browse files

allow the query part not in quote

  • Loading branch information...
1 parent 16ade43 commit 88548e303c063941b6f51d5e6e4b219bb3e169db @siuying committed Feb 12, 2011
Showing with 34 additions and 12 deletions.
  1. +2 −2 README.md
  2. +1 −1 VERSION
  3. +25 −9 lib/ruby_query.rb
  4. +6 −0 spec/rquery/rquery_spec.rb
View
@@ -60,12 +60,12 @@
Get second li's text:
- $ echo '<ul><li>Apple</li><li>Orange</li><li>Cat</li></ul>' | rquery 'ul li' get 1 text
+ $ echo '<ul><li>Apple</li><li>Orange</li><li>Cat</li></ul>' | rquery ul li get 1 text
Orange
Get third li's text using `next`:
- $ echo '<ul><li>Apple</li><li>Orange</li><li>Cat</li></ul>' | rquery 'ul li' get 1 next text
+ $ echo '<ul><li>Apple</li><li>Orange</li><li>Cat</li></ul>' | rquery ul li get 1 next text
Cat
Get length:
View
@@ -1 +1 @@
-0.1.0
+0.1.1
View
@@ -35,7 +35,12 @@ class Query
: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|
@@ -58,21 +63,32 @@ class Query
ALIAS = {
:html => :to_html,
- :len => :length,
+ :len => :size,
+ :length => :size,
:count => :size,
:get => :at,
:"has-class" => :hasClass,
- :val => :value
+ :val => :value,
+ :next => :next_sibling,
+ :previous => :previous_sibling
}
def self.query(html, *query)
- doc = Nokogiri::HTML(html)
- ctx = doc.search(query.shift)
+ ctx = Nokogiri::HTML(html)
while method = query.shift
- command = COMMANDS[method.to_sym] || COMMANDS[ALIAS[method.to_sym]] || COMMANDS[:to_html]
- 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)
+ 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)
@@ -116,7 +132,7 @@ def self.handle_command(context, name, type, param=nil, query=nil)
end
context[param.to_i]
else
- if context.is_a?(Nokogiri::XML::Element) || context.is_a?(Nokogiri::XML::NodeSet)
+ if context.is_a?(Nokogiri::XML::Element) || context.is_a?(Nokogiri::XML::NodeSet) || context.is_a?(Nokogiri::HTML::Document)
if param
context.send(name, param)
else
@@ -44,11 +44,13 @@
it "get second li's text" do
RubyQuery::Query.query(HTML_LIST, "ul li", "get", "1", "text").should == "Orange"
+ RubyQuery::Query.query(HTML_LIST, "ul", "li", "get", "1", "text").should == "Orange"
RubyQuery::Query.query(HTML_LIST, "ul li", "get", "10", "text").should be_nil
end
it "get third li's text using next" do
RubyQuery::Query.query(HTML_LIST, "ul li", "get", "1", "next", "text").should == "Cat"
+ RubyQuery::Query.query(HTML_LIST, "ul", "li", "get", "1", "next", "text").should == "Cat"
end
it "get length" do
@@ -62,4 +64,8 @@
RubyQuery::Query.query(HTML_LIST, "li", "html").should == "<li>Apple</li>\n<li>Orange</li>\n<li>Cat</li>"
end
+ it "should return empty for strange query" do
+ RubyQuery::Query.query(HTML_LIST, "liaa").should be_empty
+ RubyQuery::Query.query(HTML_LIST, "lixaa", "len").should 0
+ end
end

0 comments on commit 88548e3

Please sign in to comment.