From d20c58f74c3ead85a12277aa211ec9be96703fba Mon Sep 17 00:00:00 2001 From: moro Date: Tue, 18 Nov 2008 10:43:43 +0900 Subject: [PATCH] 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) --- lib/webrat/core/link.rb | 22 ++++++++++++++++------ spec/webrat/core/link_spec.rb | 41 +++++++++++++++++++++++++++++++---------- 2 files changed, 47 insertions(+), 16 deletions(-) diff --git a/lib/webrat/core/link.rb b/lib/webrat/core/link.rb index 6980810..23da0f1 100644 --- a/lib/webrat/core/link.rb +++ b/lib/webrat/core/link.rb @@ -22,17 +22,15 @@ 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) if id_or_regexp.is_a?(Regexp) (id =~ id_or_regexp) ? true : false @@ -40,9 +38,13 @@ module Webrat (id == id_or_regexp) ? true : false end end + + def inner_html + @element.inner_html + end def text - @element.inner_html + @element.text end protected @@ -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 diff --git a/spec/webrat/core/link_spec.rb b/spec/webrat/core/link_spec.rb index bf20341..ef6ddf6 100755 --- a/spec/webrat/core/link_spec.rb +++ b/spec/webrat/core/link_spec.rb @@ -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('') + 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") @@ -76,4 +97,4 @@ describe Webrat::Link do link.matches_id?(/some/).should == true end -end \ No newline at end of file +end -- 1.6.0