Skip to content
Newer
Older
100644 83 lines (67 sloc) 1.89 KB
eb95f6c @brynary Refactoring locators
authored
1 require "webrat/core/locators/locator"
2
a472bbf @brynary Namespace locators in Webrat::Locators
authored
3 module Webrat
4 module Locators
03914fd @brynary Whitespace
authored
5
f5ed57e @brynary Docs
authored
6 class LinkLocator < Locator # :nodoc:
03914fd @brynary Whitespace
authored
7
a472bbf @brynary Namespace locators in Webrat::Locators
authored
8 def locate
522bb32 @brynary Initialize locators with a session and a dom instead of a scope
authored
9 Link.load(@session, link_element)
a472bbf @brynary Namespace locators in Webrat::Locators
authored
10 end
03914fd @brynary Whitespace
authored
11
a472bbf @brynary Namespace locators in Webrat::Locators
authored
12 def link_element
87cc458 Consider whether inner text matches when selecting from matching links
Luke Melia authored
13 matching_links.min { |a, b|
14 a_score = a.inner_text =~ matcher ? a.inner_text.length : 100
15 b_score = b.inner_text =~ matcher ? b.inner_text.length : 100
16 a_score <=> b_score
17 }
a472bbf @brynary Namespace locators in Webrat::Locators
authored
18 end
03914fd @brynary Whitespace
authored
19
a472bbf @brynary Namespace locators in Webrat::Locators
authored
20 def matching_links
21 @matching_links ||= link_elements.select do |link_element|
22 matches_text?(link_element) ||
23 matches_id?(link_element)
24 end
25 end
03914fd @brynary Whitespace
authored
26
a472bbf @brynary Namespace locators in Webrat::Locators
authored
27 def matches_text?(link)
11f30d1 @brynary Dropping support for Hpricot and REXML
authored
28 replace_nbsp(link.inner_text) =~ matcher ||
29 replace_nbsp_ref(link.inner_html) =~ matcher ||
30 link["title"] =~ matcher
a472bbf @brynary Namespace locators in Webrat::Locators
authored
31 end
87cc458 Consider whether inner text matches when selecting from matching links
Luke Melia authored
32
33 def matcher
34 @matcher ||= begin
35 if @value.is_a?(Regexp)
36 @value
37 else
38 /#{Regexp.escape(@value.to_s)}/i
39 end
40 end
41 end
5b8a15b @brynary Using elements to locate links
authored
42
a472bbf @brynary Namespace locators in Webrat::Locators
authored
43 def matches_id?(link)
44 if @value.is_a?(Regexp)
11f30d1 @brynary Dropping support for Hpricot and REXML
authored
45 link["id"] =~ @value ? true : false
a472bbf @brynary Namespace locators in Webrat::Locators
authored
46 else
11f30d1 @brynary Dropping support for Hpricot and REXML
authored
47 link["id"] == @value ? true : false
a472bbf @brynary Namespace locators in Webrat::Locators
authored
48 end
49 end
03914fd @brynary Whitespace
authored
50
a472bbf @brynary Namespace locators in Webrat::Locators
authored
51 def link_elements
11f30d1 @brynary Dropping support for Hpricot and REXML
authored
52 @dom.xpath(*Link.xpath_search)
a472bbf @brynary Namespace locators in Webrat::Locators
authored
53 end
03914fd @brynary Whitespace
authored
54
a472bbf @brynary Namespace locators in Webrat::Locators
authored
55 def replace_nbsp(str)
8d2c027 @manveru Fix replacing of &nbsp;, aka &#160; so it works on 1.9
manveru authored
56 if str.respond_to?(:valid_encoding?)
57 if str.valid_encoding?
58 str.gsub(/\xc2\xa0/u, ' ')
59 else
60 str.force_encoding('UTF-8').gsub(/\xc2\xa0/u, ' ')
61 end
520081c @manveru Don't use $LAST_MATCH_INFO since we don't require English.rb, use $~ …
manveru authored
62 else
8d2c027 @manveru Fix replacing of &nbsp;, aka &#160; so it works on 1.9
manveru authored
63 str.gsub(/\xc2\xa0/u, ' ')
520081c @manveru Don't use $LAST_MATCH_INFO since we don't require English.rb, use $~ …
manveru authored
64 end
a472bbf @brynary Namespace locators in Webrat::Locators
authored
65 end
5b8a15b @brynary Using elements to locate links
authored
66
a472bbf @brynary Namespace locators in Webrat::Locators
authored
67 def replace_nbsp_ref(str)
68 str.gsub('&#xA0;',' ').gsub('&nbsp;', ' ')
69 end
03914fd @brynary Whitespace
authored
70
87a4ff9 @brynary Adding Locator#locate! method for error raising
authored
71 def error_message
72 "Could not find link with text or title or id #{@value.inspect}"
73 end
03914fd @brynary Whitespace
authored
74
a472bbf @brynary Namespace locators in Webrat::Locators
authored
75 end
03914fd @brynary Whitespace
authored
76
f843ac2 @brynary Moving locator methods
authored
77 def find_link(text_or_title_or_id) #:nodoc:
522bb32 @brynary Initialize locators with a session and a dom instead of a scope
authored
78 LinkLocator.new(@session, dom, text_or_title_or_id).locate!
f843ac2 @brynary Moving locator methods
authored
79 end
03914fd @brynary Whitespace
authored
80
a472bbf @brynary Namespace locators in Webrat::Locators
authored
81 end
03914fd @brynary Whitespace
authored
82 end
Something went wrong with that request. Please try again.