Skip to content
This repository
Browse code

Fix destructive methods for selecting options

Both `select_option` and `unselect_option` were modifying the
`selected` attribute of their target `<option>` element. This attribute
is meant to be used solely as the marker for which `<option>`(s) is
(are) the default selection(s) in a (multi-)`<select>` element. The
actual _selectedness_ of an `<option>` should be tracked using its
`selected` **property**. Read [the spec][] for more info.

This change removes any code which modified the `selected` attribute of
`<option>` elements, which leaves only the code that modifies the
`selected` property. In addition, `Node#value` needed to be changed to
return `<option>` elements whose selectedness is true rather than just
those with a `selected` attribute.

The tests introduced in the previous commit now pass.

[the spec]: http://dev.w3.org/html5/spec/Overview.html#the-option-element
  • Loading branch information...
commit 2fef84403173c8c789efae911dff380034bbca81 1 parent acd6e47
Jason Petersen authored February 12, 2012
6  lib/capybara/driver/webkit/node.rb
@@ -22,11 +22,7 @@ def [](name)
22 22
 
23 23
     def value
24 24
       if multiple_select?
25  
-        self.find(".//option").select do |option|
26  
-          option["selected"] == "selected"
27  
-        end.map do |option|
28  
-          option.value
29  
-        end
  25
+        self.find(".//option").select(&:selected?).map(&:value)
30 26
       else
31 27
         invoke "value"
32 28
       end
2  src/capybara.js
@@ -196,13 +196,11 @@ Capybara = {
196 196
 
197 197
   selectOption: function(index) {
198 198
     this.nodes[index].selected = true;
199  
-    this.nodes[index].setAttribute("selected", "selected");
200 199
     this.trigger(index, "change");
201 200
   },
202 201
 
203 202
   unselectOption: function(index) {
204 203
     this.nodes[index].selected = false;
205  
-    this.nodes[index].removeAttribute("selected");
206 204
     this.trigger(index, "change");
207 205
   },
208 206
 

0 notes on commit 2fef844

Please sign in to comment.
Something went wrong with that request. Please try again.