mechanize 0.6.11 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of mechanize might be problematic. Click here for more details.

Files changed (91) hide show
  1. data/CHANGELOG.txt +8 -0
  2. data/Manifest.txt +31 -22
  3. data/lib/mechanize.rb +2 -652
  4. data/lib/www/mechanize.rb +635 -0
  5. data/lib/www/mechanize/content_type_error.rb +16 -0
  6. data/lib/www/mechanize/cookie.rb +64 -0
  7. data/lib/{mechanize/cookie.rb → www/mechanize/cookie_jar.rb} +0 -60
  8. data/lib/www/mechanize/file.rb +73 -0
  9. data/lib/www/mechanize/file_saver.rb +39 -0
  10. data/lib/{mechanize → www/mechanize}/form.rb +119 -137
  11. data/lib/www/mechanize/form/button.rb +8 -0
  12. data/lib/www/mechanize/form/check_box.rb +13 -0
  13. data/lib/www/mechanize/form/field.rb +28 -0
  14. data/lib/www/mechanize/form/file_upload.rb +24 -0
  15. data/lib/www/mechanize/form/image_button.rb +23 -0
  16. data/lib/www/mechanize/form/multi_select_list.rb +69 -0
  17. data/lib/www/mechanize/form/option.rb +51 -0
  18. data/lib/www/mechanize/form/radio_button.rb +38 -0
  19. data/lib/www/mechanize/form/select_list.rb +41 -0
  20. data/lib/www/mechanize/headers.rb +12 -0
  21. data/lib/{mechanize → www/mechanize}/history.rb +0 -0
  22. data/lib/{mechanize → www/mechanize}/inspect.rb +21 -28
  23. data/lib/{mechanize → www/mechanize}/list.rb +0 -0
  24. data/lib/{mechanize → www/mechanize}/monkey_patch.rb +19 -0
  25. data/lib/www/mechanize/page.rb +121 -0
  26. data/lib/www/mechanize/page/base.rb +10 -0
  27. data/lib/www/mechanize/page/frame.rb +22 -0
  28. data/lib/www/mechanize/page/link.rb +50 -0
  29. data/lib/www/mechanize/page/meta.rb +10 -0
  30. data/lib/www/mechanize/pluggable_parsers.rb +93 -0
  31. data/lib/{mechanize/errors.rb → www/mechanize/response_code_error.rb} +1 -13
  32. data/test/{test_includes.rb → helper.rb} +4 -18
  33. data/test/{test_servlets.rb → servlets.rb} +0 -0
  34. data/test/tc_authenticate.rb +1 -8
  35. data/test/tc_bad_links.rb +3 -10
  36. data/test/tc_blank_form.rb +1 -8
  37. data/test/tc_checkboxes.rb +1 -8
  38. data/test/tc_cookie_class.rb +1 -6
  39. data/test/tc_cookie_jar.rb +1 -7
  40. data/test/tc_cookies.rb +10 -17
  41. data/test/tc_encoded_links.rb +5 -12
  42. data/test/tc_errors.rb +4 -11
  43. data/test/tc_follow_meta.rb +1 -8
  44. data/test/tc_form_action.rb +6 -14
  45. data/test/tc_form_as_hash.rb +1 -9
  46. data/test/tc_form_button.rb +5 -8
  47. data/test/tc_form_no_inputname.rb +1 -8
  48. data/test/tc_forms.rb +16 -24
  49. data/test/tc_frames.rb +3 -10
  50. data/test/tc_gzipping.rb +2 -9
  51. data/test/tc_history.rb +5 -12
  52. data/test/tc_html_unscape_forms.rb +8 -15
  53. data/test/tc_if_modified_since.rb +1 -6
  54. data/test/tc_keep_alive.rb +1 -8
  55. data/test/tc_links.rb +12 -19
  56. data/test/tc_mech.rb +26 -34
  57. data/test/{test_mechanize_file.rb → tc_mechanize_file.rb} +1 -6
  58. data/test/tc_multi_select.rb +10 -17
  59. data/test/tc_no_attributes.rb +1 -8
  60. data/test/tc_page.rb +3 -10
  61. data/test/tc_pluggable_parser.rb +8 -15
  62. data/test/tc_post_form.rb +3 -10
  63. data/test/tc_pretty_print.rb +3 -10
  64. data/test/tc_radiobutton.rb +2 -9
  65. data/test/tc_referer.rb +13 -20
  66. data/test/tc_relative_links.rb +1 -8
  67. data/test/tc_response_code.rb +14 -21
  68. data/test/tc_save_file.rb +1 -9
  69. data/test/tc_select.rb +3 -10
  70. data/test/tc_select_all.rb +2 -10
  71. data/test/tc_select_none.rb +2 -10
  72. data/test/tc_select_noopts.rb +2 -9
  73. data/test/tc_set_fields.rb +2 -9
  74. data/test/tc_ssl_server.rb +5 -12
  75. data/test/tc_subclass.rb +2 -9
  76. data/test/tc_textarea.rb +2 -9
  77. data/test/tc_upload.rb +2 -9
  78. data/test/test_all.rb +4 -43
  79. metadata +96 -80
  80. data/lib/mechanize/form_elements.rb +0 -254
  81. data/lib/mechanize/net-overrides/net/http.rb +0 -2107
  82. data/lib/mechanize/net-overrides/net/https.rb +0 -172
  83. data/lib/mechanize/net-overrides/net/protocol.rb +0 -380
  84. data/lib/mechanize/page.rb +0 -138
  85. data/lib/mechanize/page_elements.rb +0 -77
  86. data/lib/mechanize/parsers/rexml_page.rb +0 -35
  87. data/lib/mechanize/pluggable_parsers.rb +0 -204
  88. data/lib/mechanize/rexml.rb +0 -236
  89. data/setup.rb +0 -1585
  90. data/test/tc_proxy.rb +0 -25
  91. data/test/tc_watches.rb +0 -32
@@ -1,16 +1,9 @@
1
- $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
-
3
- require 'test/unit'
4
- require 'rubygems'
5
- require 'mechanize'
6
- require 'test_includes'
1
+ require File.dirname(__FILE__) + "/helper"
7
2
 
8
3
  class TestSetFields < Test::Unit::TestCase
9
- include TestMethods
10
-
11
4
  def setup
12
5
  @agent = WWW::Mechanize.new
13
- @page = @agent.get("http://localhost:#{PORT}/form_set_fields.html")
6
+ @page = @agent.get("http://localhost/form_set_fields.html")
14
7
  @form = @page.forms.first
15
8
  end
16
9
 
@@ -1,27 +1,20 @@
1
- $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
-
3
- require 'test/unit'
4
- require 'rubygems'
5
- require 'mechanize'
6
- require 'test_includes'
1
+ require File.dirname(__FILE__) + "/helper"
7
2
 
8
3
  class SSLServerTest < Test::Unit::TestCase
9
- include TestMethods
10
-
11
4
  def setup
12
5
  @agent = WWW::Mechanize.new
13
6
  end
14
7
 
15
8
  def test_ssl_request
16
- non_ssl_page = @agent.get("http://localhost:#{PORT}/form_test.html")
17
- ssl_page = @agent.get("https://localhost:#{SSLPORT}/form_test.html")
9
+ non_ssl_page = @agent.get("http://localhost/form_test.html")
10
+ ssl_page = @agent.get("https://localhost/form_test.html")
18
11
  assert_equal(non_ssl_page.body.length, ssl_page.body.length)
19
12
  end
20
13
 
21
14
  def test_ssl_request_verify
22
- non_ssl_page = @agent.get("http://localhost:#{PORT}/form_test.html")
15
+ non_ssl_page = @agent.get("http://localhost/form_test.html")
23
16
  @agent.ca_file = 'data/server.crt'
24
- ssl_page = @agent.get("https://localhost:#{SSLPORT}/form_test.html")
17
+ ssl_page = @agent.get("https://localhost/form_test.html")
25
18
  assert_equal(non_ssl_page.body.length, ssl_page.body.length)
26
19
  end
27
20
  end
data/test/tc_subclass.rb CHANGED
@@ -1,9 +1,4 @@
1
- $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
-
3
- require 'test/unit'
4
- require 'rubygems'
5
- require 'mechanize'
6
- require 'test_includes'
1
+ require File.dirname(__FILE__) + "/helper"
7
2
 
8
3
  class MechSubclass < WWW::Mechanize
9
4
  def set_headers(uri, request, cur_page)
@@ -14,14 +9,12 @@ class MechSubclass < WWW::Mechanize
14
9
  end
15
10
 
16
11
  class TestSubclass < Test::Unit::TestCase
17
- include TestMethods
18
-
19
12
  def setup
20
13
  @agent = MechSubclass.new
21
14
  end
22
15
 
23
16
  def test_send_cookie
24
- page = @agent.get("http://localhost:#{PORT}/send_cookies")
17
+ page = @agent.get("http://localhost/send_cookies")
25
18
  assert_equal(1, page.links.length)
26
19
  assert_not_nil(page.links.find { |l| l.text == "name:Aaron" })
27
20
  end
data/test/tc_textarea.rb CHANGED
@@ -1,16 +1,9 @@
1
- $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
-
3
- require 'test/unit'
4
- require 'rubygems'
5
- require 'mechanize'
6
- require 'test_includes'
1
+ require File.dirname(__FILE__) + "/helper"
7
2
 
8
3
  class TestTextArea < Test::Unit::TestCase
9
- include TestMethods
10
-
11
4
  def setup
12
5
  @agent = WWW::Mechanize.new
13
- @page = @agent.get("http://localhost:#{PORT}/tc_textarea.html")
6
+ @page = @agent.get("http://localhost/tc_textarea.html")
14
7
  end
15
8
 
16
9
  def test_empty_text_area
data/test/tc_upload.rb CHANGED
@@ -1,16 +1,9 @@
1
- $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
-
3
- require 'test/unit'
4
- require 'rubygems'
5
- require 'mechanize'
6
- require 'test_includes'
1
+ require File.dirname(__FILE__) + "/helper"
7
2
 
8
3
  class UploadMechTest < Test::Unit::TestCase
9
- include TestMethods
10
-
11
4
  def setup
12
5
  @agent = WWW::Mechanize.new
13
- @page = @agent.get("http://localhost:#{PORT}/file_upload.html")
6
+ @page = @agent.get("http://localhost/file_upload.html")
14
7
  end
15
8
 
16
9
  def test_form_enctype
data/test/test_all.rb CHANGED
@@ -1,44 +1,5 @@
1
- $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
- $:.unshift File.join(File.dirname(__FILE__), "..", "test")
1
+ Dir[File.dirname(__FILE__) + "/tc_*.rb"].
2
+ reject { |f| f == __FILE__ }.
3
+ collect { |f| File.basename(f, ".rb") }.
4
+ each { |f| require f }
3
5
 
4
- require 'tc_authenticate'
5
- require 'tc_bad_links'
6
- require 'tc_blank_form'
7
- require 'tc_checkboxes'
8
- require 'tc_cookie_class'
9
- require 'tc_cookie_jar'
10
- require 'tc_cookies'
11
- require 'tc_errors'
12
- require 'tc_follow_meta'
13
- require 'tc_form_action'
14
- require 'tc_form_as_hash'
15
- require 'tc_form_button'
16
- require 'tc_form_no_inputname'
17
- require 'tc_forms'
18
- require 'tc_gzipping'
19
- require 'tc_history'
20
- require 'tc_html_unscape_forms'
21
- require 'tc_if_modified_since'
22
- require 'tc_links'
23
- require 'tc_mech'
24
- require 'tc_multi_select'
25
- require 'tc_no_attributes'
26
- require 'tc_page'
27
- require 'tc_pluggable_parser'
28
- require 'tc_post_form'
29
- require 'tc_pretty_print'
30
- require 'tc_radiobutton'
31
- require 'tc_referer'
32
- require 'tc_relative_links'
33
- require 'tc_response_code'
34
- require 'tc_save_file'
35
- require 'tc_select'
36
- require 'tc_select_all'
37
- require 'tc_select_none'
38
- require 'tc_select_noopts'
39
- require 'tc_set_fields'
40
- require 'tc_subclass'
41
- require 'tc_textarea'
42
- require 'tc_upload'
43
- require 'tc_watches'
44
- require 'test_mechanize_file'
metadata CHANGED
@@ -1,33 +1,50 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.4
3
- specification_version: 1
4
2
  name: mechanize
5
3
  version: !ruby/object:Gem::Version
6
- version: 0.6.11
7
- date: 2007-12-04 00:00:00 -08:00
8
- summary: Mechanize provides automated web-browsing
9
- require_paths:
10
- - lib
11
- email: aaronp@rubyforge.org
12
- homepage: http://mechanize.rubyforge.org/
13
- rubyforge_project: mechanize
14
- description: The Mechanize library is used for automating interaction with websites. Mechanize automatically stores and sends cookies, follows redirects, can follow links, and submit forms. Form fields can be populated and submitted. Mechanize also keeps track of the sites that you have visited as a history.
15
- autorequire:
16
- default_executable:
17
- bindir: bin
18
- has_rdoc: true
19
- required_ruby_version: !ruby/object:Gem::Version::Requirement
20
- requirements:
21
- - - ">"
22
- - !ruby/object:Gem::Version
23
- version: 0.0.0
24
- version:
4
+ version: 0.7.0
25
5
  platform: ruby
26
- signing_key:
27
- cert_chain:
28
- post_install_message:
29
6
  authors:
30
7
  - Aaron Patterson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-01-14 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hpricot
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.5.0
23
+ version:
24
+ - !ruby/object:Gem::Dependency
25
+ name: hoe
26
+ version_requirement:
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: 1.4.0
32
+ version:
33
+ description: The Mechanize library is used for automating interaction with websites. Mechanize automatically stores and sends cookies, follows redirects, can follow links, and submit forms. Form fields can be populated and submitted. Mechanize also keeps track of the sites that you have visited as a history.
34
+ email: aaronp@rubyforge.org
35
+ executables: []
36
+
37
+ extensions: []
38
+
39
+ extra_rdoc_files:
40
+ - CHANGELOG.txt
41
+ - EXAMPLES.txt
42
+ - FAQ.txt
43
+ - GUIDE.txt
44
+ - LICENSE.txt
45
+ - Manifest.txt
46
+ - NOTES.txt
47
+ - README.txt
31
48
  files:
32
49
  - CHANGELOG.txt
33
50
  - EXAMPLES.txt
@@ -44,28 +61,40 @@ files:
44
61
  - eg/rubyforge.rb
45
62
  - eg/spider.rb
46
63
  - lib/mechanize.rb
47
- - lib/mechanize/cookie.rb
48
- - lib/mechanize/errors.rb
49
- - lib/mechanize/form.rb
50
- - lib/mechanize/form_elements.rb
51
- - lib/mechanize/history.rb
52
- - lib/mechanize/inspect.rb
53
- - lib/mechanize/list.rb
54
- - lib/mechanize/monkey_patch.rb
55
- - lib/mechanize/net-overrides/net/http.rb
56
- - lib/mechanize/net-overrides/net/https.rb
57
- - lib/mechanize/net-overrides/net/protocol.rb
58
- - lib/mechanize/page.rb
59
- - lib/mechanize/page_elements.rb
60
- - lib/mechanize/parsers/rexml_page.rb
61
- - lib/mechanize/pluggable_parsers.rb
62
- - lib/mechanize/rexml.rb
63
- - setup.rb
64
+ - lib/www/mechanize.rb
65
+ - lib/www/mechanize/content_type_error.rb
66
+ - lib/www/mechanize/cookie.rb
67
+ - lib/www/mechanize/cookie_jar.rb
68
+ - lib/www/mechanize/file.rb
69
+ - lib/www/mechanize/file_saver.rb
70
+ - lib/www/mechanize/form.rb
71
+ - lib/www/mechanize/form/button.rb
72
+ - lib/www/mechanize/form/check_box.rb
73
+ - lib/www/mechanize/form/field.rb
74
+ - lib/www/mechanize/form/file_upload.rb
75
+ - lib/www/mechanize/form/image_button.rb
76
+ - lib/www/mechanize/form/multi_select_list.rb
77
+ - lib/www/mechanize/form/option.rb
78
+ - lib/www/mechanize/form/radio_button.rb
79
+ - lib/www/mechanize/form/select_list.rb
80
+ - lib/www/mechanize/headers.rb
81
+ - lib/www/mechanize/history.rb
82
+ - lib/www/mechanize/inspect.rb
83
+ - lib/www/mechanize/list.rb
84
+ - lib/www/mechanize/monkey_patch.rb
85
+ - lib/www/mechanize/page.rb
86
+ - lib/www/mechanize/page/base.rb
87
+ - lib/www/mechanize/page/frame.rb
88
+ - lib/www/mechanize/page/link.rb
89
+ - lib/www/mechanize/page/meta.rb
90
+ - lib/www/mechanize/pluggable_parsers.rb
91
+ - lib/www/mechanize/response_code_error.rb
64
92
  - test/data/htpasswd
65
93
  - test/data/server.crt
66
94
  - test/data/server.csr
67
95
  - test/data/server.key
68
96
  - test/data/server.pem
97
+ - test/helper.rb
69
98
  - test/htdocs/alt_text.html
70
99
  - test/htdocs/bad_form_test.html
71
100
  - test/htdocs/button.jpg
@@ -104,6 +133,7 @@ files:
104
133
  - test/htdocs/tc_relative_links.html
105
134
  - test/htdocs/tc_textarea.html
106
135
  - test/htdocs/unusual______.html
136
+ - test/servlets.rb
107
137
  - test/ssl_server.rb
108
138
  - test/tc_authenticate.rb
109
139
  - test/tc_bad_links.rb
@@ -128,13 +158,13 @@ files:
128
158
  - test/tc_keep_alive.rb
129
159
  - test/tc_links.rb
130
160
  - test/tc_mech.rb
161
+ - test/tc_mechanize_file.rb
131
162
  - test/tc_multi_select.rb
132
163
  - test/tc_no_attributes.rb
133
164
  - test/tc_page.rb
134
165
  - test/tc_pluggable_parser.rb
135
166
  - test/tc_post_form.rb
136
167
  - test/tc_pretty_print.rb
137
- - test/tc_proxy.rb
138
168
  - test/tc_radiobutton.rb
139
169
  - test/tc_referer.rb
140
170
  - test/tc_relative_links.rb
@@ -149,47 +179,33 @@ files:
149
179
  - test/tc_subclass.rb
150
180
  - test/tc_textarea.rb
151
181
  - test/tc_upload.rb
152
- - test/tc_watches.rb
153
- - test/test_all.rb
154
- - test/test_includes.rb
155
- - test/test_mechanize_file.rb
156
- - test/test_servlets.rb
157
- test_files:
158
182
  - test/test_all.rb
183
+ has_rdoc: true
184
+ homepage: http://mechanize.rubyforge.org/
185
+ post_install_message:
159
186
  rdoc_options:
160
187
  - --main
161
188
  - README.txt
162
- extra_rdoc_files:
163
- - CHANGELOG.txt
164
- - EXAMPLES.txt
165
- - FAQ.txt
166
- - GUIDE.txt
167
- - LICENSE.txt
168
- - Manifest.txt
169
- - NOTES.txt
170
- - README.txt
171
- executables: []
172
-
173
- extensions: []
174
-
189
+ require_paths:
190
+ - lib
191
+ required_ruby_version: !ruby/object:Gem::Requirement
192
+ requirements:
193
+ - - ">="
194
+ - !ruby/object:Gem::Version
195
+ version: "0"
196
+ version:
197
+ required_rubygems_version: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - ">="
200
+ - !ruby/object:Gem::Version
201
+ version: "0"
202
+ version:
175
203
  requirements: []
176
204
 
177
- dependencies:
178
- - !ruby/object:Gem::Dependency
179
- name: hpricot
180
- version_requirement:
181
- version_requirements: !ruby/object:Gem::Version::Requirement
182
- requirements:
183
- - - ">="
184
- - !ruby/object:Gem::Version
185
- version: 0.5.0
186
- version:
187
- - !ruby/object:Gem::Dependency
188
- name: hoe
189
- version_requirement:
190
- version_requirements: !ruby/object:Gem::Version::Requirement
191
- requirements:
192
- - - ">="
193
- - !ruby/object:Gem::Version
194
- version: 1.3.0
195
- version:
205
+ rubyforge_project: mechanize
206
+ rubygems_version: 1.0.1
207
+ signing_key:
208
+ specification_version: 2
209
+ summary: Mechanize provides automated web-browsing
210
+ test_files:
211
+ - test/test_all.rb
@@ -1,254 +0,0 @@
1
- module WWW
2
- class Mechanize
3
- # This class represents a field in a form. It handles the following input
4
- # tags found in a form:
5
- # text, password, hidden, int, textarea
6
- #
7
- # To set the value of a field, just use the value method:
8
- # field.value = "foo"
9
- class Field
10
- attr_accessor :name, :value
11
-
12
- def initialize(name, value)
13
- @name = Util.html_unescape(name)
14
- @value = if value.is_a? String
15
- Util.html_unescape(value)
16
- else
17
- value
18
- end
19
- end
20
-
21
- def query_value
22
- [[@name, @value || '']]
23
- end
24
- end
25
-
26
- # This class represents a file upload field found in a form. To use this
27
- # class, set WWW::FileUpload#file_data= to the data of the file you want
28
- # to upload and WWW::FileUpload#mime_type= to the appropriate mime type
29
- # of the file.
30
- # See the example in EXAMPLES[link://files/EXAMPLES_txt.html]
31
- class FileUpload < Field
32
- attr_accessor :file_name # File name
33
- attr_accessor :mime_type # Mime Type (Optional)
34
-
35
- alias :file_data :value
36
- alias :file_data= :value=
37
-
38
- def initialize(name, file_name)
39
- @file_name = Util.html_unescape(file_name)
40
- @file_data = nil
41
- super(name, @file_data)
42
- end
43
- end
44
-
45
- # This class represents a Submit button in a form.
46
- class Button < Field
47
- end
48
-
49
- # This class represents an image button in a form. Use the x and y methods
50
- # to set the x and y positions for where the mouse "clicked".
51
- class ImageButton < Button
52
- attr_accessor :x, :y
53
-
54
- def initialize(name, value)
55
- @x = nil
56
- @y = nil
57
- super(name, value)
58
- end
59
-
60
- def query_value
61
- super <<
62
- [@name + ".x", (@x || 0).to_s] <<
63
- [@name + ".y", (@y || 0).to_s]
64
- end
65
- end
66
-
67
- # This class represents a radio button found in a Form. To activate the
68
- # RadioButton in the Form, set the checked method to true.
69
- class RadioButton < Field
70
- attr_accessor :checked
71
-
72
- def initialize(name, value, checked, form)
73
- @checked = checked
74
- @form = form
75
- super(name, value)
76
- end
77
-
78
- def check
79
- uncheck_peers
80
- @checked = true
81
- end
82
-
83
- def uncheck
84
- @checked = false
85
- end
86
-
87
- def click
88
- @checked = !@checked
89
- end
90
-
91
- private
92
- def uncheck_peers
93
- @form.radiobuttons.name(name).each do |b|
94
- next if b.value == value
95
- b.uncheck
96
- end
97
- end
98
- end
99
-
100
- # This class represents a check box found in a Form. To activate the
101
- # CheckBox in the Form, set the checked method to true.
102
- class CheckBox < RadioButton
103
- def query_value
104
- [[@name, @value || "on"]]
105
- end
106
- end
107
-
108
- # This class represents a select list where multiple values can be selected.
109
- # MultiSelectList#value= accepts an array, and those values are used as
110
- # values for the select list. For example, to select multiple values,
111
- # simply do this:
112
- # list.value = ['one', 'two']
113
- # Single values are still supported, so these two are the same:
114
- # list.value = ['one']
115
- # list.value = 'one'
116
- class MultiSelectList < Field
117
- attr_accessor :options
118
-
119
- def initialize(name, node)
120
- value = []
121
- @options = WWW::Mechanize::List.new
122
-
123
- # parse
124
- (node/'option').each do |n|
125
- option = Option.new(n, self)
126
- @options << option
127
- end
128
- super(name, value)
129
- end
130
-
131
- def query_value
132
- value ? value.collect { |v| [name, v] } : ''
133
- end
134
-
135
- # Select no options
136
- def select_none
137
- @value = []
138
- options.each { |o| o.untick }
139
- end
140
-
141
- # Select all options
142
- def select_all
143
- @value = []
144
- options.each { |o| o.tick }
145
- end
146
-
147
- # Get a list of all selected options
148
- def selected_options
149
- @options.find_all { |o| o.selected? }
150
- end
151
-
152
- def value=(values)
153
- select_none
154
- values.each do |value|
155
- option = options.find { |o| o.value == value }
156
- if option.nil?
157
- @value.push(value)
158
- else
159
- option.select
160
- end
161
- end
162
- end
163
-
164
- def value
165
- value = []
166
- value.push(*@value)
167
- value.push(*selected_options.collect { |o| o.value })
168
- value
169
- end
170
- end
171
-
172
- # This class represents a select list or drop down box in a Form. Set the
173
- # value for the list by calling SelectList#value=. SelectList contains a
174
- # list of Option that were found. After finding the correct option, set
175
- # the select lists value to the option value:
176
- # selectlist.value = selectlist.options.first.value
177
- # Options can also be selected by "clicking" or selecting them. See Option
178
- class SelectList < MultiSelectList
179
- def initialize(name, node)
180
- super(name, node)
181
- if selected_options.length > 1
182
- selected_options.reverse[1..selected_options.length].each do |o|
183
- o.unselect
184
- end
185
- end
186
- end
187
-
188
- def value
189
- value = super
190
- if value.length > 0
191
- value.last
192
- elsif @options.length > 0
193
- @options.first.value
194
- else
195
- nil
196
- end
197
- end
198
-
199
- def value=(new)
200
- if new != new.to_s and new.respond_to? :first
201
- super([new.first])
202
- else
203
- super([new.to_s])
204
- end
205
- end
206
- end
207
-
208
- # This class contains option an option found within SelectList. A
209
- # SelectList can have many Option classes associated with it. An option
210
- # can be selected by calling Option#tick, or Option#click. For example,
211
- # select the first option in a list:
212
- # select_list.first.tick
213
- class Option
214
- attr_reader :value, :selected, :text, :select_list
215
-
216
- alias :to_s :value
217
- alias :selected? :selected
218
-
219
- def initialize(node, select_list)
220
- @text = node.inner_text
221
- @value = Util.html_unescape(node['value'])
222
- @selected = node.has_attribute? 'selected'
223
- @select_list = select_list # The select list this option belongs to
224
- end
225
-
226
- # Select this option
227
- def select
228
- unselect_peers
229
- @selected = true
230
- end
231
-
232
- # Unselect this option
233
- def unselect
234
- @selected = false
235
- end
236
-
237
- alias :tick :select
238
- alias :untick :unselect
239
-
240
- # Toggle the selection value of this option
241
- def click
242
- unselect_peers
243
- @selected = !@selected
244
- end
245
-
246
- private
247
- def unselect_peers
248
- if @select_list.instance_of? WWW::Mechanize::SelectList
249
- @select_list.select_none
250
- end
251
- end
252
- end
253
- end
254
- end