lib/mechanize/form.rb in mechanize-2.7.5 vs lib/mechanize/form.rb in mechanize-2.7.6

- old
+ new

@@ -14,11 +14,11 @@ # # form['name'] = 'Aaron' # puts form['name'] class Mechanize::Form - + extend Forwardable extend Mechanize::ElementMatcher attr_accessor :method, :action, :name attr_reader :fields, :buttons, :file_uploads, :radiobuttons, :checkboxes @@ -33,16 +33,17 @@ # submission. Default is false attr_accessor :ignore_encoding_error alias :elements :fields - attr_reader :form_node + attr_reader :node + alias form_node node # for backward compatibility attr_reader :page def initialize(node, mech = nil, page = nil) @enctype = node['enctype'] || 'application/x-www-form-urlencoded' - @form_node = node + @node = node @action = Mechanize::Util.html_unescape(node['action']) @method = (node['method'] || 'GET').upcase @name = node['name'] @clicked_buttons = [] @page = page @@ -53,18 +54,18 @@ parse end # Returns whether or not the form contains a field with +field_name+ def has_field?(field_name) - fields.find { |f| f.name == field_name } + fields.any? { |f| f.name == field_name } end alias :has_key? :has_field? # Returns whether or not the form contains a field with +value+ def has_value?(value) - fields.find { |f| f.value == value } + fields.any? { |f| f.value == value } end # Returns all field names (keys) for this form def keys fields.map(&:name) @@ -134,22 +135,69 @@ # Common usage: # page.form_with(:dom_id => "foorm") # Note that you can also use +:id+ to get to this method: # page.form_with(:id => "foorm") def dom_id - form_node['id'] + @node['id'] end # This method is a shortcut to get form's DOM class. # Common usage: # page.form_with(:dom_class => "foorm") # Note that you can also use +:class+ to get to this method: # page.form_with(:class => "foorm") + # However, attribute values are compared literally as string, so + # form_with(class: "a") does not match a form with class="a b". + # Use form_with(css: "form.a") instead. def dom_class - form_node['class'] + @node['class'] end + ## + # :method: search + # + # Shorthand for +node.search+. + # + # See Nokogiri::XML::Node#search for details. + + ## + # :method: css + # + # Shorthand for +node.css+. + # + # See also Nokogiri::XML::Node#css for details. + + ## + # :method: xpath + # + # Shorthand for +node.xpath+. + # + # See also Nokogiri::XML::Node#xpath for details. + + ## + # :method: at + # + # Shorthand for +node.at+. + # + # See also Nokogiri::XML::Node#at for details. + + ## + # :method: at_css + # + # Shorthand for +node.at_css+. + # + # See also Nokogiri::XML::Node#at_css for details. + + ## + # :method: at_xpath + # + # Shorthand for +node.at_xpath+. + # + # See also Nokogiri::XML::Node#at_xpath for details. + + def_delegators :node, :search, :css, :xpath, :at, :at_css, :at_xpath + # Add a field with +field_name+ and +value+ def add_field!(field_name, value = nil) fields << Field.new({'name' => field_name}, value) end @@ -205,11 +253,11 @@ end end # Treat form fields like accessors. def method_missing(meth, *args) - method = meth.to_s.gsub(/=$/, '') + (method = meth.to_s).chomp!('=') if field(method) return field(method).value if args.empty? return field(method).value = args[0] end @@ -317,11 +365,11 @@ end # 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) - unless button.node.document == @form_node.document then + unless button.node.document == @node.document then message = "#{button.inspect} does not belong to the same page as " \ "the form #{@name.inspect} in #{@page.uri}" raise ArgumentError, message @@ -533,11 +581,11 @@ @file_uploads = [] @radiobuttons = [] @checkboxes = [] # Find all input tags - form_node.search('input').each do |node| + @node.search('input').each do |node| type = (node['type'] || 'text').downcase name = node['name'] next if name.nil? && !%w[submit button image].include?(type) case type when 'radio' @@ -564,34 +612,34 @@ @fields << Field.new(node, node['value'] || '') end end # Find all textarea tags - form_node.search('textarea').each do |node| + @node.search('textarea').each do |node| next unless node['name'] @fields << Textarea.new(node, node.inner_text) end # Find all select tags - form_node.search('select').each do |node| + @node.search('select').each do |node| next unless node['name'] if node.has_attribute? 'multiple' @fields << MultiSelectList.new(node) else @fields << SelectList.new(node) end end # Find all submit button tags # FIXME: what can I do with the reset buttons? - form_node.search('button').each do |node| + @node.search('button').each do |node| type = (node['type'] || 'submit').downcase next if type == 'reset' @buttons << Button.new(node) end # Find all keygen tags - form_node.search('keygen').each do |node| + @node.search('keygen').each do |node| @fields << Keygen.new(node, node['value'] || '') end end unless ::String.method_defined?(:b)