<commit_before>module Cucumber
  module Rails
    module Capybara
      module JavascriptEmulation
        def self.included(base)
          base.class_eval do
            alias_method :click_without_javascript_emulation, :click
            alias_method :click, :click_with_javascript_emulation
          end
        end
  
        def click_with_javascript_emulation
          if link_with_non_get_http_method?
            ::Capybara::Driver::RackTest::Form.new(driver, js_form(element_node.document, self[:href], emulated_method)).submit(self)
          else
            click_without_javascript_emulation
          end
        end

        private

        def js_form(document, action, emulated_method, method = 'POST')
          js_form = document.create_element('form')
          js_form['action'] = action
          js_form['method'] = method

          if emulated_method and emulated_method.downcase != method.downcase
            input = document.create_element('input')
            input['type'] = 'hidden'
            input['name'] = '_method'
            input['value'] = emulated_method
            js_form.add_child(input)
          end
        
          js_form
        end

        def link_with_non_get_http_method?
          if ::Rails.version.to_f >= 3.0
            tag_name == 'a' && element_node['data-method'] && element_node['data-method'] =~ /(?:delete|put|post)/
          else
            tag_name == 'a' && element_node['onclick'] && element_node['onclick'] =~ /var f = document\.createElement\('form'\); f\.style\.display = 'none';/
          end
        end

        def emulated_method
          if ::Rails.version.to_f >= 3.0
            element_node['data-method']
          else
            element_node['onclick'][/m\.setAttribute\('value', '([^']*)'\)/, 1]
          end
        end

        def element_node
          if self.respond_to? :native
            self.native
          else
            warn "DEPRECATED: cucumber-rails loves you, just not your version of Capybara. Please update Capybara to >= 0.4.0"
            self.node
          end
        end
      end
    end
  end
end

class Capybara::Driver::RackTest::Node
  include ::Cucumber::Rails::Capybara::JavascriptEmulation
end

Before('~@no-js-emulation') do
  # Enable javascript emulation
  ::Capybara::Driver::RackTest::Node.class_eval do
    alias_method :click, :click_with_javascript_emulation
  end
end

Before('@no-js-emulation') do
  # Disable javascript emulation
  ::Capybara::Driver::RackTest::Node.class_eval do
    alias_method :click, :click_without_javascript_emulation
  end
end<commit_msg>Fix capybara rack/test<commit_after>module Cucumber
  module Rails
    module Capybara
      module JavascriptEmulation
        def self.included(base)
          base.class_eval do
            alias_method :click_without_javascript_emulation, :click
            alias_method :click, :click_with_javascript_emulation
          end
        end
  
        def click_with_javascript_emulation
          if link_with_non_get_http_method?
            ::Capybara::RackTest::Form.new(driver, js_form(element_node.document, self[:href], emulated_method)).submit(self)
          else
            click_without_javascript_emulation
          end
        end

        private

        def js_form(document, action, emulated_method, method = 'POST')
          js_form = document.create_element('form')
          js_form['action'] = action
          js_form['method'] = method

          if emulated_method and emulated_method.downcase != method.downcase
            input = document.create_element('input')
            input['type'] = 'hidden'
            input['name'] = '_method'
            input['value'] = emulated_method
            js_form.add_child(input)
          end
        
          js_form
        end

        def link_with_non_get_http_method?
          if ::Rails.version.to_f >= 3.0
            tag_name == 'a' && element_node['data-method'] && element_node['data-method'] =~ /(?:delete|put|post)/
          else
            tag_name == 'a' && element_node['onclick'] && element_node['onclick'] =~ /var f = document\.createElement\('form'\); f\.style\.display = 'none';/
          end
        end

        def emulated_method
          if ::Rails.version.to_f >= 3.0
            element_node['data-method']
          else
            element_node['onclick'][/m\.setAttribute\('value', '([^']*)'\)/, 1]
          end
        end

        def element_node
          if self.respond_to? :native
            self.native
          else
            warn "DEPRECATED: cucumber-rails loves you, just not your version of Capybara. Please update Capybara to >= 0.4.0"
            self.node
          end
        end
      end
    end
  end
end

class Capybara::RackTest::Node
  include ::Cucumber::Rails::Capybara::JavascriptEmulation
end

Before('~@no-js-emulation') do
  # Enable javascript emulation
  ::Capybara::RackTest::Node.class_eval do
    alias_method :click, :click_with_javascript_emulation
  end
end

Before('@no-js-emulation') do
  # Disable javascript emulation
  ::Capybara::RackTest::Node.class_eval do
    alias_method :click, :click_without_javascript_emulation
  end
end
{'fraction_non_alphanumeric': 0.10486819318837791, 'fraction_numerical': 0.003078699249567058, 'mean_word_length': 2.2774274905422445, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '7584104', 'n_tokens_mistral': 1567, 'n_tokens_neox': 1518, 'n_words': 309}