Link#matches_text?() use @element.text as text

It decodes character references.
Userful for multibyte languages(eg. Japanese).

And also uses @element.inner_html to match with HTML (eg.image link)
This commit is contained in:
moro 2008-11-18 10:43:43 +09:00 committed by Bryan Helmkamp
parent 95604d6320
commit dc52f07a9f
2 changed files with 47 additions and 16 deletions

View File

@ -22,15 +22,13 @@ module Webrat
end
def matches_text?(link_text)
html = text.gsub(' ',' ').gsub(' ', ' ')
if link_text.is_a?(Regexp)
matcher = link_text
else
matcher = /#{Regexp.escape(link_text.to_s)}/i
end
html =~ matcher || title =~ matcher
replace_nbsp(text) =~ matcher || replace_nbsp_ref(inner_html) =~ matcher || title =~ matcher
end
def matches_id?(id_or_regexp)
@ -41,10 +39,14 @@ module Webrat
end
end
def text
def inner_html
@element.inner_html
end
def text
@element.text
end
protected
def id
@element['id']
@ -108,5 +110,13 @@ module Webrat
end
end
private
def replace_nbsp(str)
str.gsub([0xA0].pack('U'), ' ')
end
def replace_nbsp_ref(str)
str.gsub(' ',' ').gsub(' ', ' ')
end
end
end

View File

@ -5,6 +5,7 @@ describe Webrat::Link do
before do
@session = mock(Webrat::TestSession)
@link_text_with_nbsp = 'Link' + [0xA0].pack("U") + 'Text'
end
it "should pass through relative urls" do
@ -22,42 +23,62 @@ describe Webrat::Link do
it "should matches_text? on regexp" do
link = Webrat::Link.new(@session, nil)
link.should_receive(:text).and_return("Link Text")
link.should_receive(:text).and_return(@link_text_with_nbsp)
link.matches_text?(/link/i).should == 0
end
it "should matches_text? on link_text" do
link = Webrat::Link.new(@session, nil)
link.should_receive(:text).and_return("Link Text")
link.should_receive(:text).and_return(@link_text_with_nbsp)
link.matches_text?("Link Text").should == 0
end
it "should matches_text? on substring" do
link = Webrat::Link.new(@session, nil)
link.should_receive(:text).and_return("Link Text")
link.should_receive(:text).and_return(@link_text_with_nbsp)
link.matches_text?("nk Te").should_not be_nil
end
it "should not matches_text? on link_text case insensitive" do
link = Webrat::Link.new(@session, nil)
link.should_receive(:text).and_return("Link Text")
link.should_receive(:text).and_return(@link_text_with_nbsp)
link.should_receive(:inner_html).and_return('Link Text')
link.should_receive(:title).and_return(nil)
link.matches_text?("link_text").should == false
end
it "should match text including  " do
it "should match text not include  " do
link = Webrat::Link.new(@session, nil)
link.should_receive(:text).and_return("Link Text")
link.matches_text?("Link Text").should == 0
link.should_receive(:text).and_return('LinkText')
link.matches_text?("LinkText").should == 0
end
it "should not matches_text? on wrong text" do
link = Webrat::Link.new(@session, nil)
link.should_receive(:text).and_return("Some Other Link")
nbsp = [0xA0].pack("U")
link.should_receive(:text).and_return("Some"+nbsp+"Other"+nbsp+"Link")
link.should_receive(:inner_html).and_return("Some Other Link")
link.should_receive(:title).and_return(nil)
link.matches_text?("Link Text").should == false
end
it "should match text including character reference" do
no_ko_gi_ri = [0x30CE,0x30B3,0x30AE,0x30EA]
nokogiri_ja_kana = no_ko_gi_ri.pack("U*")
nokogiri_char_ref = no_ko_gi_ri.map{|c| "&#x%X;" % c }.join("")
link = Webrat::Link.new(@session, nil)
link.should_receive(:text).and_return(nokogiri_ja_kana)
link.matches_text?(nokogiri_ja_kana).should == 0
end
it "should match img link" do
link = Webrat::Link.new(@session, nil)
link.should_receive(:text).and_return('')
link.should_receive(:inner_html).and_return('<img src="logo.png" />')
link.matches_text?('logo.png').should == 10
end
it "should matches_id? on exact matching id" do
link = Webrat::Link.new(@session, nil)
link.should_receive(:id).and_return("some_id")