Skip to content

Commit

Permalink
Consider whether inner text matches when selecting from matching links
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke Melia committed Jul 8, 2010
1 parent 4881ce8 commit 87cc458
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
22 changes: 15 additions & 7 deletions lib/webrat/core/locators/link_locator.rb
Expand Up @@ -10,7 +10,11 @@ def locate
end

def link_element
matching_links.min { |a, b| a.inner_text.length <=> b.inner_text.length }
matching_links.min { |a, b|
a_score = a.inner_text =~ matcher ? a.inner_text.length : 100
b_score = b.inner_text =~ matcher ? b.inner_text.length : 100
a_score <=> b_score
}
end

def matching_links
Expand All @@ -21,16 +25,20 @@ def matching_links
end

def matches_text?(link)
if @value.is_a?(Regexp)
matcher = @value
else
matcher = /#{Regexp.escape(@value.to_s)}/i
end

replace_nbsp(link.inner_text) =~ matcher ||
replace_nbsp_ref(link.inner_html) =~ matcher ||
link["title"] =~ matcher
end

def matcher
@matcher ||= begin
if @value.is_a?(Regexp)
@value
else
/#{Regexp.escape(@value.to_s)}/i
end
end
end

def matches_id?(link)
if @value.is_a?(Regexp)
Expand Down
12 changes: 12 additions & 0 deletions spec/public/click_link_spec.rb
Expand Up @@ -370,6 +370,18 @@
click_link "Link"
end

it "should choose the shortest link text match, preferring text" do
with_html <<-HTML
<html>
<a href="/page1">Linkerama</a>
<a title="Link" href="/page2">Foo</a>
</html>
HTML

webrat_session.should_receive(:get).with("/page1", {})
click_link "Link"
end

it "should treat non-breaking spaces as spaces" do
with_html <<-HTML
<html>
Expand Down

0 comments on commit 87cc458

Please sign in to comment.