diamond-mechanize 2.1 → 2.2
Sign up to get free protection for your applications and to get access to all the features.
- metadata +222 -167
- data/Rakefile +0 -49
- data/lib/mechanize/content_type_error.rb +0 -13
- data/lib/mechanize/cookie.rb +0 -232
- data/lib/mechanize/cookie_jar.rb +0 -194
- data/lib/mechanize/download.rb +0 -59
- data/lib/mechanize/element_matcher.rb +0 -36
- data/lib/mechanize/file.rb +0 -65
- data/lib/mechanize/file_connection.rb +0 -17
- data/lib/mechanize/file_request.rb +0 -26
- data/lib/mechanize/file_response.rb +0 -74
- data/lib/mechanize/file_saver.rb +0 -39
- data/lib/mechanize/form/button.rb +0 -6
- data/lib/mechanize/form/check_box.rb +0 -12
- data/lib/mechanize/form/field.rb +0 -54
- data/lib/mechanize/form/file_upload.rb +0 -21
- data/lib/mechanize/form/hidden.rb +0 -3
- data/lib/mechanize/form/image_button.rb +0 -19
- data/lib/mechanize/form/keygen.rb +0 -34
- data/lib/mechanize/form/multi_select_list.rb +0 -94
- data/lib/mechanize/form/option.rb +0 -50
- data/lib/mechanize/form/radio_button.rb +0 -55
- data/lib/mechanize/form/reset.rb +0 -3
- data/lib/mechanize/form/select_list.rb +0 -44
- data/lib/mechanize/form/submit.rb +0 -3
- data/lib/mechanize/form/text.rb +0 -3
- data/lib/mechanize/form/textarea.rb +0 -3
- data/lib/mechanize/form.rb +0 -543
- data/lib/mechanize/headers.rb +0 -23
- data/lib/mechanize/history.rb +0 -82
- data/lib/mechanize/http/agent.rb +0 -1004
- data/lib/mechanize/http/auth_challenge.rb +0 -59
- data/lib/mechanize/http/auth_realm.rb +0 -31
- data/lib/mechanize/http/content_disposition_parser.rb +0 -188
- data/lib/mechanize/http/www_authenticate_parser.rb +0 -155
- data/lib/mechanize/http.rb +0 -8
- data/lib/mechanize/monkey_patch.rb +0 -16
- data/lib/mechanize/page/base.rb +0 -7
- data/lib/mechanize/page/frame.rb +0 -27
- data/lib/mechanize/page/image.rb +0 -30
- data/lib/mechanize/page/label.rb +0 -20
- data/lib/mechanize/page/link.rb +0 -98
- data/lib/mechanize/page/meta_refresh.rb +0 -68
- data/lib/mechanize/page.rb +0 -440
- data/lib/mechanize/parser.rb +0 -173
- data/lib/mechanize/pluggable_parsers.rb +0 -144
- data/lib/mechanize/redirect_limit_reached_error.rb +0 -19
- data/lib/mechanize/redirect_not_get_or_head_error.rb +0 -21
- data/lib/mechanize/response_code_error.rb +0 -21
- data/lib/mechanize/response_read_error.rb +0 -27
- data/lib/mechanize/robots_disallowed_error.rb +0 -28
- data/lib/mechanize/test_case.rb +0 -663
- data/lib/mechanize/unauthorized_error.rb +0 -3
- data/lib/mechanize/unsupported_scheme_error.rb +0 -6
- data/lib/mechanize/util.rb +0 -101
- data/lib/mechanize.rb +0 -1079
- data/test/data/htpasswd +0 -1
- data/test/data/server.crt +0 -16
- data/test/data/server.csr +0 -12
- data/test/data/server.key +0 -15
- data/test/data/server.pem +0 -15
- data/test/htdocs/alt_text.html +0 -10
- data/test/htdocs/bad_form_test.html +0 -9
- data/test/htdocs/button.jpg +0 -0
- data/test/htdocs/canonical_uri.html +0 -9
- data/test/htdocs/dir with spaces/foo.html +0 -1
- data/test/htdocs/empty_form.html +0 -6
- data/test/htdocs/file_upload.html +0 -26
- data/test/htdocs/find_link.html +0 -41
- data/test/htdocs/form_multi_select.html +0 -16
- data/test/htdocs/form_multival.html +0 -37
- data/test/htdocs/form_no_action.html +0 -18
- data/test/htdocs/form_no_input_name.html +0 -16
- data/test/htdocs/form_order_test.html +0 -11
- data/test/htdocs/form_select.html +0 -16
- data/test/htdocs/form_set_fields.html +0 -14
- data/test/htdocs/form_test.html +0 -188
- data/test/htdocs/frame_referer_test.html +0 -10
- data/test/htdocs/frame_test.html +0 -30
- data/test/htdocs/google.html +0 -13
- data/test/htdocs/index.html +0 -6
- data/test/htdocs/link with space.html +0 -5
- data/test/htdocs/meta_cookie.html +0 -11
- data/test/htdocs/no_title_test.html +0 -6
- data/test/htdocs/noindex.html +0 -9
- data/test/htdocs/rails_3_encoding_hack_form_test.html +0 -27
- data/test/htdocs/relative/tc_relative_links.html +0 -21
- data/test/htdocs/robots.html +0 -8
- data/test/htdocs/robots.txt +0 -2
- data/test/htdocs/tc_bad_charset.html +0 -9
- data/test/htdocs/tc_bad_links.html +0 -5
- data/test/htdocs/tc_base_link.html +0 -8
- data/test/htdocs/tc_blank_form.html +0 -11
- data/test/htdocs/tc_charset.html +0 -6
- data/test/htdocs/tc_checkboxes.html +0 -19
- data/test/htdocs/tc_encoded_links.html +0 -5
- data/test/htdocs/tc_field_precedence.html +0 -11
- data/test/htdocs/tc_follow_meta.html +0 -8
- data/test/htdocs/tc_form_action.html +0 -48
- data/test/htdocs/tc_links.html +0 -19
- data/test/htdocs/tc_meta_in_body.html +0 -9
- data/test/htdocs/tc_pretty_print.html +0 -17
- data/test/htdocs/tc_referer.html +0 -16
- data/test/htdocs/tc_relative_links.html +0 -19
- data/test/htdocs/tc_textarea.html +0 -23
- data/test/htdocs/test_click.html +0 -11
- data/test/htdocs/unusual______.html +0 -5
- data/test/test_mechanize.rb +0 -1164
- data/test/test_mechanize_cookie.rb +0 -451
- data/test/test_mechanize_cookie_jar.rb +0 -483
- data/test/test_mechanize_download.rb +0 -43
- data/test/test_mechanize_file.rb +0 -61
- data/test/test_mechanize_file_connection.rb +0 -21
- data/test/test_mechanize_file_request.rb +0 -19
- data/test/test_mechanize_file_saver.rb +0 -21
- data/test/test_mechanize_form.rb +0 -875
- data/test/test_mechanize_form_check_box.rb +0 -38
- data/test/test_mechanize_form_encoding.rb +0 -114
- data/test/test_mechanize_form_field.rb +0 -63
- data/test/test_mechanize_form_file_upload.rb +0 -20
- data/test/test_mechanize_form_image_button.rb +0 -12
- data/test/test_mechanize_form_keygen.rb +0 -32
- data/test/test_mechanize_form_multi_select_list.rb +0 -84
- data/test/test_mechanize_form_option.rb +0 -55
- data/test/test_mechanize_form_radio_button.rb +0 -78
- data/test/test_mechanize_form_select_list.rb +0 -76
- data/test/test_mechanize_form_textarea.rb +0 -52
- data/test/test_mechanize_headers.rb +0 -35
- data/test/test_mechanize_history.rb +0 -103
- data/test/test_mechanize_http_agent.rb +0 -1225
- data/test/test_mechanize_http_auth_challenge.rb +0 -39
- data/test/test_mechanize_http_auth_realm.rb +0 -49
- data/test/test_mechanize_http_content_disposition_parser.rb +0 -118
- data/test/test_mechanize_http_www_authenticate_parser.rb +0 -146
- data/test/test_mechanize_link.rb +0 -80
- data/test/test_mechanize_page.rb +0 -118
- data/test/test_mechanize_page_encoding.rb +0 -182
- data/test/test_mechanize_page_frame.rb +0 -16
- data/test/test_mechanize_page_link.rb +0 -390
- data/test/test_mechanize_page_meta_refresh.rb +0 -127
- data/test/test_mechanize_parser.rb +0 -289
- data/test/test_mechanize_pluggable_parser.rb +0 -52
- data/test/test_mechanize_redirect_limit_reached_error.rb +0 -24
- data/test/test_mechanize_redirect_not_get_or_head_error.rb +0 -14
- data/test/test_mechanize_subclass.rb +0 -22
- data/test/test_mechanize_util.rb +0 -103
- data/test/test_multi_select.rb +0 -119
@@ -1,36 +0,0 @@
|
|
1
|
-
module Mechanize::ElementMatcher
|
2
|
-
|
3
|
-
def elements_with singular, plural = "#{singular}s"
|
4
|
-
class_eval <<-CODE
|
5
|
-
def #{plural}_with criteria = {}
|
6
|
-
criteria = if String === criteria then
|
7
|
-
{:name => criteria}
|
8
|
-
else
|
9
|
-
criteria.map do |k, v|
|
10
|
-
k = :dom_id if k.to_sym == :id
|
11
|
-
k = :dom_class if k.to_sym == :class
|
12
|
-
[k, v]
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
f = #{plural}.find_all do |thing|
|
17
|
-
criteria.all? do |k,v|
|
18
|
-
v === thing.send(k)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
yield f if block_given?
|
22
|
-
f
|
23
|
-
end
|
24
|
-
|
25
|
-
def #{singular}_with criteria = {}
|
26
|
-
f = #{plural}_with(criteria).first
|
27
|
-
yield f if block_given?
|
28
|
-
f
|
29
|
-
end
|
30
|
-
|
31
|
-
alias :#{singular} :#{singular}_with
|
32
|
-
CODE
|
33
|
-
end
|
34
|
-
|
35
|
-
end
|
36
|
-
|
data/lib/mechanize/file.rb
DELETED
@@ -1,65 +0,0 @@
|
|
1
|
-
##
|
2
|
-
# This is the base class for the Pluggable Parsers. If Mechanize cannot find
|
3
|
-
# an appropriate class to use for the content type, this class will be used.
|
4
|
-
# For example, if you download an image/jpeg, Mechanize will not know how to
|
5
|
-
# parse it, so this class will be instantiated.
|
6
|
-
#
|
7
|
-
# This is a good class to use as the base class for building your own
|
8
|
-
# pluggable parsers.
|
9
|
-
#
|
10
|
-
# == Example
|
11
|
-
#
|
12
|
-
# require 'mechanize'
|
13
|
-
#
|
14
|
-
# agent = Mechanize.new
|
15
|
-
# agent.get('http://example.com/foo.jpg').class #=> Mechanize::File
|
16
|
-
|
17
|
-
class Mechanize::File
|
18
|
-
|
19
|
-
include Mechanize::Parser
|
20
|
-
|
21
|
-
##
|
22
|
-
# The HTTP response body, the raw file contents
|
23
|
-
|
24
|
-
attr_accessor :body
|
25
|
-
|
26
|
-
##
|
27
|
-
# The filename for this file based on the content-disposition of the
|
28
|
-
# response or the basename of the URL
|
29
|
-
|
30
|
-
attr_accessor :filename
|
31
|
-
|
32
|
-
alias content body
|
33
|
-
|
34
|
-
##
|
35
|
-
# Creates a new file retrieved from the given +uri+ and +response+ object.
|
36
|
-
# The +body+ is the HTTP response body and +code+ is the HTTP status.
|
37
|
-
|
38
|
-
def initialize uri = nil, response = nil, body = nil, code = nil
|
39
|
-
@uri = uri
|
40
|
-
@body = body
|
41
|
-
@code = code
|
42
|
-
|
43
|
-
@full_path = false unless defined? @full_path
|
44
|
-
|
45
|
-
fill_header response
|
46
|
-
extract_filename
|
47
|
-
|
48
|
-
yield self if block_given?
|
49
|
-
end
|
50
|
-
|
51
|
-
##
|
52
|
-
# Use this method to save the content of this object to +filename+
|
53
|
-
|
54
|
-
def save filename = nil
|
55
|
-
filename = find_free_name filename
|
56
|
-
|
57
|
-
open filename, 'wb' do |f|
|
58
|
-
f.write body
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
alias save_as save
|
63
|
-
|
64
|
-
end
|
65
|
-
|
@@ -1,17 +0,0 @@
|
|
1
|
-
##
|
2
|
-
# Wrapper to make a file URI work like an http URI
|
3
|
-
|
4
|
-
class Mechanize::FileConnection
|
5
|
-
|
6
|
-
@instance = nil
|
7
|
-
|
8
|
-
def self.new *a
|
9
|
-
@instance ||= super
|
10
|
-
end
|
11
|
-
|
12
|
-
def request uri, request
|
13
|
-
yield Mechanize::FileResponse.new Mechanize::Util.uri_unescape uri.path
|
14
|
-
end
|
15
|
-
|
16
|
-
end
|
17
|
-
|
@@ -1,26 +0,0 @@
|
|
1
|
-
##
|
2
|
-
# A wrapper for a file URI that makes a request that works like a
|
3
|
-
# Net::HTTPRequest
|
4
|
-
|
5
|
-
class Mechanize::FileRequest
|
6
|
-
|
7
|
-
attr_accessor :uri
|
8
|
-
|
9
|
-
def initialize uri
|
10
|
-
@uri = uri
|
11
|
-
end
|
12
|
-
|
13
|
-
def add_field *a
|
14
|
-
end
|
15
|
-
|
16
|
-
alias []= add_field
|
17
|
-
|
18
|
-
def path
|
19
|
-
@uri.path
|
20
|
-
end
|
21
|
-
|
22
|
-
def each_header
|
23
|
-
end
|
24
|
-
|
25
|
-
end
|
26
|
-
|
@@ -1,74 +0,0 @@
|
|
1
|
-
##
|
2
|
-
# Fake response for dealing with file:/// requests
|
3
|
-
|
4
|
-
class Mechanize::FileResponse
|
5
|
-
def initialize(file_path)
|
6
|
-
@file_path = file_path
|
7
|
-
end
|
8
|
-
|
9
|
-
def read_body
|
10
|
-
raise Mechanize::ResponseCodeError, self unless File.exist? @file_path
|
11
|
-
|
12
|
-
if directory?
|
13
|
-
yield dir_body
|
14
|
-
else
|
15
|
-
open @file_path, 'rb' do |io|
|
16
|
-
yield io.read
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
def code
|
22
|
-
File.exist?(@file_path) ? 200 : 404
|
23
|
-
end
|
24
|
-
|
25
|
-
def content_length
|
26
|
-
return dir_body.length if directory?
|
27
|
-
File.exist?(@file_path) ? File.stat(@file_path).size : 0
|
28
|
-
end
|
29
|
-
|
30
|
-
def each_header; end
|
31
|
-
|
32
|
-
def [](key)
|
33
|
-
return nil unless key.downcase == 'content-type'
|
34
|
-
return 'text/html' if directory?
|
35
|
-
return 'text/html' if ['.html', '.xhtml'].any? { |extn|
|
36
|
-
@file_path =~ /#{extn}$/
|
37
|
-
}
|
38
|
-
nil
|
39
|
-
end
|
40
|
-
|
41
|
-
def each
|
42
|
-
end
|
43
|
-
|
44
|
-
def get_fields(key)
|
45
|
-
[]
|
46
|
-
end
|
47
|
-
|
48
|
-
def http_version
|
49
|
-
'0'
|
50
|
-
end
|
51
|
-
|
52
|
-
def message
|
53
|
-
File.exist?(@file_path) ? 'OK' : 'Not Found'
|
54
|
-
end
|
55
|
-
|
56
|
-
private
|
57
|
-
|
58
|
-
def dir_body
|
59
|
-
body = %w[<html><body>]
|
60
|
-
body.concat Dir[File.join(@file_path, '*')].map { |f|
|
61
|
-
"<a href=\"file://#{f}\">#{File.basename(f)}</a>"
|
62
|
-
}
|
63
|
-
body << %w[</body></html>]
|
64
|
-
|
65
|
-
body = body.join "\n"
|
66
|
-
body.force_encoding Encoding::BINARY if body.respond_to? :force_encoding
|
67
|
-
body
|
68
|
-
end
|
69
|
-
|
70
|
-
def directory?
|
71
|
-
File.directory?(@file_path)
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
data/lib/mechanize/file_saver.rb
DELETED
@@ -1,39 +0,0 @@
|
|
1
|
-
##
|
2
|
-
# This is a pluggable parser that automatically saves every file it
|
3
|
-
# encounters. It saves the files as a tree, reflecting the host and file
|
4
|
-
# path.
|
5
|
-
#
|
6
|
-
# == Example
|
7
|
-
#
|
8
|
-
# This example saves all .pdf files
|
9
|
-
#
|
10
|
-
# require 'mechanize'
|
11
|
-
#
|
12
|
-
# agent = Mechanize.new
|
13
|
-
# agent.pluggable_parser.pdf = Mechanize::FileSaver
|
14
|
-
# agent.get('http://example.com/foo.pdf')
|
15
|
-
#
|
16
|
-
# Dir['example.com/*'] # => foo.pdf
|
17
|
-
|
18
|
-
class Mechanize::FileSaver < Mechanize::Download
|
19
|
-
|
20
|
-
attr_reader :filename
|
21
|
-
|
22
|
-
def initialize uri = nil, response = nil, body_io = nil, code = nil
|
23
|
-
@full_path = true
|
24
|
-
|
25
|
-
super
|
26
|
-
|
27
|
-
save @filename
|
28
|
-
end
|
29
|
-
|
30
|
-
##
|
31
|
-
# The save_as alias is provided for backwards compatibility with mechanize
|
32
|
-
# 2.0. It will be removed in mechanize 3.
|
33
|
-
#--
|
34
|
-
# TODO remove in mechanize 3
|
35
|
-
|
36
|
-
alias save_as save
|
37
|
-
|
38
|
-
end
|
39
|
-
|
@@ -1,12 +0,0 @@
|
|
1
|
-
##
|
2
|
-
# This class represents a check box found in a Form. To activate the CheckBox
|
3
|
-
# in the Form, set the checked method to true.
|
4
|
-
|
5
|
-
class Mechanize::Form::CheckBox < Mechanize::Form::RadioButton
|
6
|
-
|
7
|
-
def query_value
|
8
|
-
[[@name, @value || "on"]]
|
9
|
-
end
|
10
|
-
|
11
|
-
end
|
12
|
-
|
data/lib/mechanize/form/field.rb
DELETED
@@ -1,54 +0,0 @@
|
|
1
|
-
##
|
2
|
-
# This class represents a field in a form. It handles the following input
|
3
|
-
# tags found in a form:
|
4
|
-
#
|
5
|
-
# * text
|
6
|
-
# * password
|
7
|
-
# * hidden
|
8
|
-
# * int
|
9
|
-
# * textarea
|
10
|
-
# * keygen
|
11
|
-
#
|
12
|
-
# To set the value of a field, just use the value method:
|
13
|
-
#
|
14
|
-
# field.value = "foo"
|
15
|
-
|
16
|
-
class Mechanize::Form::Field
|
17
|
-
attr_accessor :name, :value, :node, :type
|
18
|
-
|
19
|
-
def initialize node, value = node['value']
|
20
|
-
@node = node
|
21
|
-
@name = Mechanize::Util.html_unescape(node['name'])
|
22
|
-
@value = if value.is_a? String
|
23
|
-
Mechanize::Util.html_unescape(value)
|
24
|
-
else
|
25
|
-
value
|
26
|
-
end
|
27
|
-
|
28
|
-
@type = node['type']
|
29
|
-
end
|
30
|
-
|
31
|
-
def query_value
|
32
|
-
[[@name, @value || '']]
|
33
|
-
end
|
34
|
-
|
35
|
-
def <=> other
|
36
|
-
return 0 if self == other
|
37
|
-
return 1 if Hash === node
|
38
|
-
return -1 if Hash === other.node
|
39
|
-
node <=> other.node
|
40
|
-
end
|
41
|
-
|
42
|
-
# This method is a shortcut to get field's DOM id.
|
43
|
-
# Common usage: form.field_with(:dom_id => "foo")
|
44
|
-
def dom_id
|
45
|
-
node['id']
|
46
|
-
end
|
47
|
-
|
48
|
-
# This method is a shortcut to get field's DOM id.
|
49
|
-
# Common usage: form.field_with(:dom_class => "foo")
|
50
|
-
def dom_class
|
51
|
-
node['class']
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
@@ -1,21 +0,0 @@
|
|
1
|
-
# This class represents a file upload field found in a form. To use this
|
2
|
-
# class, set FileUpload#file_data= to the data of the file you want to upload
|
3
|
-
# and FileUpload#mime_type= to the appropriate mime type of the file.
|
4
|
-
#
|
5
|
-
# See the example in EXAMPLES
|
6
|
-
|
7
|
-
class Mechanize::Form::FileUpload < Mechanize::Form::Field
|
8
|
-
attr_accessor :file_name # File name
|
9
|
-
attr_accessor :mime_type # Mime Type (Optional)
|
10
|
-
|
11
|
-
alias :file_data :value
|
12
|
-
alias :file_data= :value=
|
13
|
-
|
14
|
-
def initialize node, file_name
|
15
|
-
@file_name = Mechanize::Util.html_unescape(file_name)
|
16
|
-
@file_data = nil
|
17
|
-
@node = node
|
18
|
-
super(node, @file_data)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
@@ -1,19 +0,0 @@
|
|
1
|
-
##
|
2
|
-
# This class represents an image button in a form. Use the x and y methods to
|
3
|
-
# set the x and y positions for where the mouse "clicked".
|
4
|
-
|
5
|
-
class Mechanize::Form::ImageButton < Mechanize::Form::Button
|
6
|
-
attr_accessor :x, :y
|
7
|
-
|
8
|
-
def initialize *args
|
9
|
-
@x = nil
|
10
|
-
@y = nil
|
11
|
-
super
|
12
|
-
end
|
13
|
-
|
14
|
-
def query_value
|
15
|
-
[["#{@name}.x", (@x || 0).to_s],
|
16
|
-
["#{@name}.y", (@y || 0).to_s]]
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
@@ -1,34 +0,0 @@
|
|
1
|
-
##
|
2
|
-
# This class represents a keygen (public / private key generator) found in a
|
3
|
-
# Form. The field will automatically generate a key pair and compute its own
|
4
|
-
# value to match the challenge. Call key to access the public/private key
|
5
|
-
# pair.
|
6
|
-
|
7
|
-
class Mechanize::Form::Keygen < Mechanize::Form::Field
|
8
|
-
# The challenge for this <keygen>.
|
9
|
-
attr_reader :challenge
|
10
|
-
|
11
|
-
# The key associated with this <keygen> tag.
|
12
|
-
attr_reader :key
|
13
|
-
|
14
|
-
def initialize(node, value = nil)
|
15
|
-
super
|
16
|
-
@challenge = node['challenge']
|
17
|
-
|
18
|
-
@spki = OpenSSL::Netscape::SPKI.new
|
19
|
-
@spki.challenge = @challenge
|
20
|
-
|
21
|
-
@key = nil
|
22
|
-
generate_key if value.nil? || value.empty?
|
23
|
-
end
|
24
|
-
|
25
|
-
# Generates a key pair and sets the field's value.
|
26
|
-
def generate_key(key_size = 2048)
|
27
|
-
# Spec at http://dev.w3.org/html5/spec/Overview.html#the-keygen-element
|
28
|
-
@key = OpenSSL::PKey::RSA.new key_size
|
29
|
-
@spki.public_key = @key.public_key
|
30
|
-
@spki.sign @key, OpenSSL::Digest::MD5.new
|
31
|
-
self.value = @spki.to_pem
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
@@ -1,94 +0,0 @@
|
|
1
|
-
##
|
2
|
-
# This class represents a select list where multiple values can be selected.
|
3
|
-
# MultiSelectList#value= accepts an array, and those values are used as
|
4
|
-
# values for the select list. For example, to select multiple values,
|
5
|
-
# simply do this:
|
6
|
-
#
|
7
|
-
# list.value = ['one', 'two']
|
8
|
-
#
|
9
|
-
# Single values are still supported, so these two are the same:
|
10
|
-
#
|
11
|
-
# list.value = ['one']
|
12
|
-
# list.value = 'one'
|
13
|
-
|
14
|
-
class Mechanize::Form::MultiSelectList < Mechanize::Form::Field
|
15
|
-
|
16
|
-
extend Mechanize::ElementMatcher
|
17
|
-
|
18
|
-
attr_accessor :options
|
19
|
-
|
20
|
-
def initialize node
|
21
|
-
value = []
|
22
|
-
@options = []
|
23
|
-
|
24
|
-
# parse
|
25
|
-
node.search('option').each do |n|
|
26
|
-
@options << Mechanize::Form::Option.new(n, self)
|
27
|
-
end
|
28
|
-
|
29
|
-
super node, value
|
30
|
-
end
|
31
|
-
|
32
|
-
##
|
33
|
-
# :method: option_with
|
34
|
-
#
|
35
|
-
# Find one option on this select list with +criteria+
|
36
|
-
#
|
37
|
-
# Example:
|
38
|
-
#
|
39
|
-
# select_list.option_with(:value => '1').value = 'foo'
|
40
|
-
|
41
|
-
##
|
42
|
-
# :method: options_with
|
43
|
-
#
|
44
|
-
# Find all options on this select list with +criteria+
|
45
|
-
#
|
46
|
-
# Example:
|
47
|
-
#
|
48
|
-
# select_list.options_with(:value => /1|2/).each do |field|
|
49
|
-
# field.value = '20'
|
50
|
-
# end
|
51
|
-
|
52
|
-
elements_with :option
|
53
|
-
|
54
|
-
def query_value
|
55
|
-
value ? value.map { |v| [name, v] } : ''
|
56
|
-
end
|
57
|
-
|
58
|
-
# Select no options
|
59
|
-
def select_none
|
60
|
-
@value = []
|
61
|
-
options.each { |o| o.untick }
|
62
|
-
end
|
63
|
-
|
64
|
-
# Select all options
|
65
|
-
def select_all
|
66
|
-
@value = []
|
67
|
-
options.each { |o| o.tick }
|
68
|
-
end
|
69
|
-
|
70
|
-
# Get a list of all selected options
|
71
|
-
def selected_options
|
72
|
-
@options.find_all { |o| o.selected? }
|
73
|
-
end
|
74
|
-
|
75
|
-
def value=(values)
|
76
|
-
select_none
|
77
|
-
[values].flatten.each do |value|
|
78
|
-
option = options.find { |o| o.value == value }
|
79
|
-
if option.nil?
|
80
|
-
@value.push(value)
|
81
|
-
else
|
82
|
-
option.select
|
83
|
-
end
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
def value
|
88
|
-
value = []
|
89
|
-
value.concat @value
|
90
|
-
value.concat selected_options.map { |o| o.value }
|
91
|
-
value
|
92
|
-
end
|
93
|
-
|
94
|
-
end
|