<commit_before>require "cgi"
require "webrat/core_extensions/blank"
require "webrat/core_extensions/nil_to_param"

require "webrat/core/elements/element"

module Webrat
  # Raised when Webrat is asked to manipulate a disabled form field
  class DisabledFieldError < WebratError
  end
  
  class Field < Element #:nodoc:
    attr_reader :value
    
    def self.xpath_search
      [".//button", ".//input", ".//textarea", ".//select"]
    end
    
    def self.field_classes
      @field_classes || []
    end
    
    def self.inherited(klass)
      @field_classes ||= []
      @field_classes << klass
      # raise args.inspect
    end
    
    def self.load(session, element)
      return nil if element.nil?
      session.elements[Webrat::XML.xpath_to(element)] ||= field_class(element).new(session, element)
    end
    
    def self.field_class(element)
      case element.name
      when "button"   then ButtonField
      when "select"   then SelectField
      when "textarea" then TextareaField
      else
        case Webrat::XML.attribute(element, "type")
        when "checkbox" then CheckboxField
        when "hidden"   then HiddenField
        when "radio"    then RadioField
        when "password" then PasswordField
        when "file"     then FileField
        when "reset"    then ResetField
        when "submit"   then ButtonField
        when "image"    then ButtonField
        else  TextField
        end
      end
    end
    
    def initialize(*args)
      super
      @value = default_value
    end

    def label_text
      return nil if labels.empty?
      labels.first.text
    end
    
    def id
      Webrat::XML.attribute(@element, "id")
    end

    def disabled?
      @element.attributes.has_key?("disabled") && Webrat::XML.attribute(@element, "disabled") != 'false'
    end
    
    def raise_error_if_disabled
      return unless disabled?
      raise DisabledFieldError.new("Cannot interact with disabled form element (#{self})")
    end
        
    def to_param
      return nil if disabled?
      
      case Webrat.configuration.mode
      when :rails
        ActionController::AbstractRequest.parse_query_parameters("#{name}=#{escaped_value}")
      when :merb
        ::Merb::Parse.query("#{name}=#{escaped_value}")
      else
        { name => escaped_value }
      end
    end
    
    def set(value)
      @value = value
    end
    
    def unset
      @value = default_value
    end
    
  protected<commit_msg>In rails-2.3 parse_query_parameters is in RequestParser<commit_after>require "cgi"
require "webrat/core_extensions/blank"
require "webrat/core_extensions/nil_to_param"

require "webrat/core/elements/element"

module Webrat
  # Raised when Webrat is asked to manipulate a disabled form field
  class DisabledFieldError < WebratError
  end
  
  class Field < Element #:nodoc:
    attr_reader :value
    
    def self.xpath_search
      [".//button", ".//input", ".//textarea", ".//select"]
    end
    
    def self.field_classes
      @field_classes || []
    end
    
    def self.inherited(klass)
      @field_classes ||= []
      @field_classes << klass
      # raise args.inspect
    end
    
    def self.load(session, element)
      return nil if element.nil?
      session.elements[Webrat::XML.xpath_to(element)] ||= field_class(element).new(session, element)
    end
    
    def self.field_class(element)
      case element.name
      when "button"   then ButtonField
      when "select"   then SelectField
      when "textarea" then TextareaField
      else
        case Webrat::XML.attribute(element, "type")
        when "checkbox" then CheckboxField
        when "hidden"   then HiddenField
        when "radio"    then RadioField
        when "password" then PasswordField
        when "file"     then FileField
        when "reset"    then ResetField
        when "submit"   then ButtonField
        when "image"    then ButtonField
        else  TextField
        end
      end
    end
    
    def initialize(*args)
      super
      @value = default_value
    end

    def label_text
      return nil if labels.empty?
      labels.first.text
    end
    
    def id
      Webrat::XML.attribute(@element, "id")
    end

    def disabled?
      @element.attributes.has_key?("disabled") && Webrat::XML.attribute(@element, "disabled") != 'false'
    end
    
    def raise_error_if_disabled
      return unless disabled?
      raise DisabledFieldError.new("Cannot interact with disabled form element (#{self})")
    end
        
    def to_param
      return nil if disabled?
      
      case Webrat.configuration.mode
      when :rails
        if defined?(ActionController::RequestParser)
          ActionController::RequestParser.parse_query_parameters("#{name}=#{escaped_value}")
        else
          ActionController::AbstractRequest.parse_query_parameters("#{name}=#{escaped_value}")
        end
      when :merb
        ::Merb::Parse.query("#{name}=#{escaped_value}")
      else
        { name => escaped_value }
      end
    end
    
    def set(value)
      @value = value
    end
    
    def unset
      @value = default_value
    end
    
  protected
{'fraction_non_alphanumeric': 0.09200544641120405, 'fraction_numerical': 0.00038902937171756465, 'mean_word_length': 2.3088803088803087, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '908026', 'n_tokens_mistral': 1519, 'n_tokens_neox': 1509, 'n_words': 385}