Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
tree: 83c5c25e11
Fetching contributors…

Cannot retrieve contributors at this time

96 lines (77 sloc) 2.322 kB
require "English"
require "webrat/core_extensions/blank"
require "webrat/core/elements/element"
module Webrat
class Link < Element #:nodoc:
def self.xpath_search
[".//a[@href]"]
end
def click(options = {})
method = options[:method] || http_method
return if href =~ /^#/ && method == :get
options[:javascript] = true if options[:javascript].nil?
if options[:javascript]
@session.request_page(absolute_href, method, data)
else
@session.request_page(absolute_href, :get, {})
end
end
protected
def id
@element["id"]
end
def data
authenticity_token.blank? ? {} : {"authenticity_token" => authenticity_token}
end
def title
@element["title"]
end
def href
@element["href"]
end
def absolute_href
if href =~ /^\?/
"#{@session.current_url}#{href}"
elsif href !~ %r{^https?://} && (href !~ /^\//)
"#{@session.current_url}/#{href}"
else
href
end
end
def authenticity_token
return unless onclick && onclick.include?("s.setAttribute('name', 'authenticity_token');") &&
( onclick =~ /s\.setAttribute\('value', '([a-f0-9]{40})'\);/ || onclick =~ /s\.setAttribute\('value', '(.{44})'\);/ )
$LAST_MATCH_INFO.captures.first
end
def onclick
@element["onclick"]
end
def http_method
if @element["data-method"] && %w(get post put delete).include?(@element["data-method"])
@element["data-method"].to_sym
elsif !onclick.blank? && onclick.include?("f.submit()") || @element["data-method"]
http_method_from_js_form
else
:get
end
end
def http_method_from_js_form
if !onclick.blank? && onclick.include?("m.setAttribute('name', '_method')")
http_method_from_fake_method_param
else
:post
end
end
def http_method_from_fake_method_param
if onclick.include?("m.setAttribute('value', 'delete')")
:delete
elsif onclick.include?("m.setAttribute('value', 'put')")
:put
elsif onclick.include?("m.setAttribute('value', 'post')")
:post
else
raise Webrat::WebratError.new("No HTTP method for _method param in #{onclick.inspect}")
end
end
end
end
Jump to Line
Something went wrong with that request. Please try again.