lib/www/mechanize/form.rb in tenderlove-mechanize-0.9.3.20090617085936 vs lib/www/mechanize/form.rb in tenderlove-mechanize-0.9.3.20090623142847
- old
+ new
@@ -22,19 +22,19 @@
# Set the input field 'name' to "Aaron"
# form['name'] = 'Aaron'
# puts form['name']
class Form
attr_accessor :method, :action, :name
-
+
attr_reader :fields, :buttons, :file_uploads, :radiobuttons, :checkboxes
attr_accessor :enctype
alias :elements :fields
-
+
attr_reader :form_node
attr_reader :page
-
+
def initialize(node, mech=nil, page=nil)
@enctype = node['enctype'] || 'application/x-www-form-urlencoded'
@form_node = node
@action = Util.html_unescape(node['action'])
@method = (node['method'] || 'GET').upcase
@@ -135,23 +135,23 @@
def click_button(button = buttons.first)
submit(button)
end
# This method is sub-method of build_query.
- # It converts charset of query value of fields into excepted one.
+ # It converts charset of query value of fields into expected one.
def proc_query(field)
return unless field.query_value
- field.query_value.map{|(name, val)|
+ field.query_value.map{|(name, val)|
[from_native_charset(name), from_native_charset(val.to_s)]
}
end
private :proc_query
def from_native_charset(str, enc=nil)
if page
enc ||= page.encoding
- Util.from_native_charset(str,enc)
+ Util.from_native_charset(str,enc) rescue str
else
str
end
end
private :from_native_charset
@@ -159,40 +159,40 @@
# This method builds an array of arrays that represent the query
# parameters to be used with this form. The return value can then
# be used to create a query string for this form.
def build_query(buttons = [])
query = []
-
+
fields().each do |f|
qval = proc_query(f)
query.push(*qval)
end
-
+
checkboxes().each do |f|
if f.checked
qval = proc_query(f)
query.push(*qval)
end
end
-
+
radio_groups = {}
radiobuttons().each do |f|
fname = from_native_charset(f.name)
radio_groups[fname] ||= []
- radio_groups[fname] << f
+ radio_groups[fname] << f
end
-
+
# take one radio button from each group
radio_groups.each_value do |g|
checked = g.select {|f| f.checked}
-
+
if checked.size == 1
f = checked.first
qval = proc_query(f)
query.push(*qval)
- elsif checked.size > 1
- raise "multiple radiobuttons are checked in the same group!"
+ elsif checked.size > 1
+ raise "multiple radiobuttons are checked in the same group!"
end
end
@clicked_buttons.each { |b|
qval = proc_query(b)
@@ -204,11 +204,11 @@
# This method adds a button to the query. If the form needs to be
# submitted with multiple buttons, pass each button to this method.
def add_button_to_query(button)
@clicked_buttons << button
end
-
+
# This method calculates the request data to be sent back to the server
# for this form, depending on if this is a regular post, get, or a
# multi-part post,
def request_data
query_params = build_query()
@@ -223,12 +223,12 @@
"--#{boundary}--\r\n"
else
WWW::Mechanize::Util.build_query_string(query_params)
end
end
-
- # Removes all fields with name +field_name+.
+
+ # Removes all fields with name +field_name+.
def delete_field!(field_name)
@fields.delete_if{ |f| f.name == field_name}
end
{ :field => :fields,
@@ -253,19 +253,19 @@
f
end
alias :#{singular} :#{singular}_with
eomethod
end
-
+
private
def parse
@fields = []
@buttons = []
@file_uploads = []
@radiobuttons = []
@checkboxes = []
-
+
# Find all input tags
form_node.search('input').each do |node|
type = (node['type'] || 'text').downcase
name = node['name']
next if name.nil? && !(type == 'submit' || type =='button')
@@ -273,19 +273,19 @@
when 'radio'
@radiobuttons << RadioButton.new(node['name'], node['value'], !!node['checked'], self)
when 'checkbox'
@checkboxes << CheckBox.new(node['name'], node['value'], !!node['checked'], self)
when 'file'
- @file_uploads << FileUpload.new(node['name'], nil)
+ @file_uploads << FileUpload.new(node['name'], nil)
when 'submit'
@buttons << Button.new(node['name'], node['value'])
when 'button'
@buttons << Button.new(node['name'], node['value'])
when 'image'
@buttons << ImageButton.new(node['name'], node['value'])
else
- @fields << Field.new(node['name'], node['value'] || '')
+ @fields << Field.new(node['name'], node['value'] || '')
end
end
# Find all textarea tags
form_node.search('textarea').each do |node|
@@ -316,21 +316,21 @@
chars = ("a".."z").to_a + ("A".."Z").to_a
string = ""
1.upto(len) { |i| string << chars[rand(chars.size-1)] }
string
end
-
+
def mime_value_quote(str)
str.gsub(/(["\r\\])/){|s| '\\' + s}
end
def param_to_multipart(name, value)
return "Content-Disposition: form-data; name=\"" +
"#{mime_value_quote(name)}\"\r\n" +
"\r\n#{value}\r\n"
end
-
+
def file_to_multipart(file)
file_name = file.file_name ? ::File.basename(file.file_name) : ''
body = "Content-Disposition: form-data; name=\"" +
"#{mime_value_quote(file.name)}\"; " +
"filename=\"#{mime_value_quote(file_name)}\"\r\n" +
@@ -343,10 +343,10 @@
end
if file.mime_type != nil
body << "Content-Type: #{file.mime_type}\r\n"
end
-
+
body <<
if file.file_data.respond_to? :read
"\r\n#{file.file_data.read}\r\n"
else
"\r\n#{file.file_data}\r\n"