lib/mechanize.rb in mechanize-0.3.0 vs lib/mechanize.rb in mechanize-0.3.1

- old
+ new

@@ -4,11 +4,11 @@ # This is a quick hack, to get something like Perl's WWW::Mechanize. Sure, we # have Web::Unit, but, that does not work for me as expected, as it does not # set cookies (I might be wrong), does not automatically redirect and has # problems with some html documents. -Version = "0.3.0" +Version = "0.3.1" # required due to the missing get_fields method in Ruby 1.8.2 $LOAD_PATH.unshift File.join(File.dirname(__FILE__), "mechanize", "net-overrides") require 'net/http' require 'net/https' @@ -60,10 +60,14 @@ def initialize(name, value) @name, @value = name, value end + def add_to_query(query) + query[@name] = @value || "" if @name + end + # Returns an array of Button objects def self.extract_all_from(root_node) buttons = [] root_node.each_recursive {|node| if node.name.downcase == 'input' and @@ -73,10 +77,22 @@ } return buttons end end +class ImageButton < Button + attr_accessor :x, :y + + def add_to_query(query) + if @name + query[@name] = @value || "" + query[@name+".x"] = (@x || "0").to_s + query[@name+".y"] = (@y || "0").to_s + end + end +end + class RadioButton attr_accessor :name, :value, :checked def initialize(name, value, checked) @name, @value, @checked = name, value, checked @@ -208,10 +224,12 @@ @checkboxes << CheckBox.new(node.attributes['name'], node.attributes['value'], node.attributes.has_key?('checked')) when 'file' @file_uploads << FileUpload.new(node.attributes['name'], node.attributes['value']) when 'submit' @buttons << Button.new(node.attributes['name'], node.attributes['value']) + when 'image' + @buttons << ImageButton.new(node.attributes['name'], node.attributes['value']) end when 'textarea' @fields << Field.new(node.attributes['name'], node.all_text) when 'select' @fields << SelectList.new(node.attributes['name'], node) @@ -389,11 +407,10 @@ get(uri) end def submit(form, button=nil) query = form.build_query - - query[button.name] = button.value || "" if button and button.name + button.add_to_query(query) if button uri = to_absolute_uri(form.action) case form.method.upcase when 'POST' post(uri, query)