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:
parent
95604d6320
commit
dc52f07a9f
@ -22,15 +22,13 @@ module Webrat
|
|||||||
end
|
end
|
||||||
|
|
||||||
def matches_text?(link_text)
|
def matches_text?(link_text)
|
||||||
html = text.gsub(' ',' ').gsub(' ', ' ')
|
|
||||||
|
|
||||||
if link_text.is_a?(Regexp)
|
if link_text.is_a?(Regexp)
|
||||||
matcher = link_text
|
matcher = link_text
|
||||||
else
|
else
|
||||||
matcher = /#{Regexp.escape(link_text.to_s)}/i
|
matcher = /#{Regexp.escape(link_text.to_s)}/i
|
||||||
end
|
end
|
||||||
|
|
||||||
html =~ matcher || title =~ matcher
|
replace_nbsp(text) =~ matcher || replace_nbsp_ref(inner_html) =~ matcher || title =~ matcher
|
||||||
end
|
end
|
||||||
|
|
||||||
def matches_id?(id_or_regexp)
|
def matches_id?(id_or_regexp)
|
||||||
@ -41,10 +39,14 @@ module Webrat
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def text
|
def inner_html
|
||||||
@element.inner_html
|
@element.inner_html
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def text
|
||||||
|
@element.text
|
||||||
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
def id
|
def id
|
||||||
@element['id']
|
@element['id']
|
||||||
@ -108,5 +110,13 @@ module Webrat
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def replace_nbsp(str)
|
||||||
|
str.gsub([0xA0].pack('U'), ' ')
|
||||||
|
end
|
||||||
|
|
||||||
|
def replace_nbsp_ref(str)
|
||||||
|
str.gsub(' ',' ').gsub(' ', ' ')
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -5,6 +5,7 @@ describe Webrat::Link do
|
|||||||
|
|
||||||
before do
|
before do
|
||||||
@session = mock(Webrat::TestSession)
|
@session = mock(Webrat::TestSession)
|
||||||
|
@link_text_with_nbsp = 'Link' + [0xA0].pack("U") + 'Text'
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should pass through relative urls" do
|
it "should pass through relative urls" do
|
||||||
@ -22,42 +23,62 @@ describe Webrat::Link do
|
|||||||
|
|
||||||
it "should matches_text? on regexp" do
|
it "should matches_text? on regexp" do
|
||||||
link = Webrat::Link.new(@session, nil)
|
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
|
link.matches_text?(/link/i).should == 0
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should matches_text? on link_text" do
|
it "should matches_text? on link_text" do
|
||||||
link = Webrat::Link.new(@session, nil)
|
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
|
link.matches_text?("Link Text").should == 0
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should matches_text? on substring" do
|
it "should matches_text? on substring" do
|
||||||
link = Webrat::Link.new(@session, nil)
|
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
|
link.matches_text?("nk Te").should_not be_nil
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should not matches_text? on link_text case insensitive" do
|
it "should not matches_text? on link_text case insensitive" do
|
||||||
link = Webrat::Link.new(@session, nil)
|
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.should_receive(:title).and_return(nil)
|
||||||
link.matches_text?("link_text").should == false
|
link.matches_text?("link_text").should == false
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should match text including " do
|
it "should match text not include " do
|
||||||
link = Webrat::Link.new(@session, nil)
|
link = Webrat::Link.new(@session, nil)
|
||||||
link.should_receive(:text).and_return("Link Text")
|
link.should_receive(:text).and_return('LinkText')
|
||||||
link.matches_text?("Link Text").should == 0
|
link.matches_text?("LinkText").should == 0
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should not matches_text? on wrong text" do
|
it "should not matches_text? on wrong text" do
|
||||||
link = Webrat::Link.new(@session, nil)
|
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.should_receive(:title).and_return(nil)
|
||||||
link.matches_text?("Link Text").should == false
|
link.matches_text?("Link Text").should == false
|
||||||
end
|
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
|
it "should matches_id? on exact matching id" do
|
||||||
link = Webrat::Link.new(@session, nil)
|
link = Webrat::Link.new(@session, nil)
|
||||||
link.should_receive(:id).and_return("some_id")
|
link.should_receive(:id).and_return("some_id")
|
||||||
|
Loading…
Reference in New Issue
Block a user