public
Description: Webrat - Ruby Acceptance Testing for Web applications
Homepage: http://gitrdoc.com/brynary/webrat/tree/master/
Clone URL: git://github.com/brynary/webrat.git
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)
moro (author)
Mon Nov 17 17:43:43 -0800 2008
brynary (committer)
Tue Nov 18 17:37:35 -0800 2008
commit  dc52f07a9f9b8debfd1562c9c119154dd8856e27
tree    35cfeb6ca36de98af0a530d788a7aa3abadc617b
parent  95604d6320fc145584081042b698e07635ae7cb4
...
22
23
24
25
26
27
28
29
30
31
32
33
 
 
34
35
 
36
37
38
...
40
41
42
 
 
 
 
43
44
45
 
46
47
48
...
108
109
110
 
 
 
 
 
 
 
 
111
112
...
22
23
24
 
 
25
26
27
28
29
 
 
30
31
32
 
33
34
35
36
...
38
39
40
41
42
43
44
45
46
 
47
48
49
50
...
110
111
112
113
114
115
116
117
118
119
120
121
122
0
@@ -22,17 +22,15 @@ module Webrat
0
     end
0
     
0
     def matches_text?(link_text)
0
-      html = text.gsub(' ',' ').gsub(' ', ' ')
0
-      
0
       if link_text.is_a?(Regexp)
0
         matcher = link_text
0
       else
0
         matcher = /#{Regexp.escape(link_text.to_s)}/i
0
       end
0
-      
0
-      html =~ matcher || title =~ matcher
0
+
0
+      replace_nbsp(text) =~ matcher || replace_nbsp_ref(inner_html) =~ matcher || title =~ matcher
0
     end
0
-    
0
+
0
     def matches_id?(id_or_regexp)
0
       if id_or_regexp.is_a?(Regexp)
0
         (id =~ id_or_regexp) ? true : false
0
@@ -40,9 +38,13 @@ module Webrat
0
         (id == id_or_regexp) ? true : false
0
       end
0
     end
0
+
0
+    def inner_html
0
+      @element.inner_html
0
+    end
0
     
0
     def text
0
-      @element.inner_html
0
+      @element.text
0
     end
0
     
0
   protected
0
@@ -108,5 +110,13 @@ module Webrat
0
       end
0
     end
0
 
0
+  private
0
+    def replace_nbsp(str)
0
+      str.gsub([0xA0].pack('U'), ' ')
0
+    end
0
+
0
+    def replace_nbsp_ref(str)
0
+      str.gsub(' ',' ').gsub(' ', ' ')
0
+    end
0
   end
0
 end
...
5
6
7
 
8
9
10
...
22
23
24
25
 
26
27
28
29
30
31
 
32
33
34
35
36
37
 
38
39
40
41
42
43
 
 
44
45
46
47
48
 
49
50
51
 
 
52
53
54
55
56
 
 
 
57
58
59
60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
62
63
...
76
77
78
79
80
 
...
5
6
7
8
9
10
11
...
23
24
25
 
26
27
28
29
30
31
 
32
33
34
35
36
37
 
38
39
40
41
42
43
 
44
45
46
47
48
49
 
50
51
 
 
52
53
54
55
56
57
 
58
59
60
61
62
63
 
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
...
97
98
99
 
100
101
0
@@ -5,6 +5,7 @@ describe Webrat::Link do
0
 
0
   before do
0
     @session = mock(Webrat::TestSession)
0
+    @link_text_with_nbsp = 'Link' + [0xA0].pack("U") + 'Text'
0
   end
0
   
0
   it "should pass through relative urls" do
0
@@ -22,42 +23,62 @@ describe Webrat::Link do
0
   
0
   it "should matches_text? on regexp" do
0
     link = Webrat::Link.new(@session, nil)
0
-    link.should_receive(:text).and_return("Link Text")
0
+    link.should_receive(:text).and_return(@link_text_with_nbsp)
0
     link.matches_text?(/link/i).should == 0
0
   end
0
   
0
   it "should matches_text? on link_text" do
0
     link = Webrat::Link.new(@session, nil)
0
-    link.should_receive(:text).and_return("Link Text")
0
+    link.should_receive(:text).and_return(@link_text_with_nbsp)
0
     link.matches_text?("Link Text").should == 0
0
   end
0
   
0
   it "should matches_text? on substring" do
0
     link = Webrat::Link.new(@session, nil)
0
-    link.should_receive(:text).and_return("Link Text")
0
+    link.should_receive(:text).and_return(@link_text_with_nbsp)
0
     link.matches_text?("nk Te").should_not be_nil
0
   end
0
   
0
   it "should not matches_text? on link_text case insensitive" do
0
     link = Webrat::Link.new(@session, nil)
0
-    link.should_receive(:text).and_return("Link Text")
0
+    link.should_receive(:text).and_return(@link_text_with_nbsp)
0
+    link.should_receive(:inner_html).and_return('Link Text')
0
     link.should_receive(:title).and_return(nil)
0
     link.matches_text?("link_text").should == false
0
   end
0
   
0
-  it "should match text including  " do
0
+  it "should match text not include  " do
0
     link = Webrat::Link.new(@session, nil)
0
-    link.should_receive(:text).and_return("Link Text")
0
-    link.matches_text?("Link Text").should == 0
0
+    link.should_receive(:text).and_return('LinkText')
0
+    link.matches_text?("LinkText").should == 0
0
   end
0
   
0
   it "should not matches_text? on wrong text" do 
0
     link = Webrat::Link.new(@session, nil)
0
-    link.should_receive(:text).and_return("Some Other Link")
0
+    nbsp = [0xA0].pack("U")
0
+    link.should_receive(:text).and_return("Some"+nbsp+"Other"+nbsp+"Link")
0
+    link.should_receive(:inner_html).and_return("Some Other Link")
0
     link.should_receive(:title).and_return(nil)
0
     link.matches_text?("Link Text").should == false
0
   end
0
-  
0
+
0
+  it "should match text including character reference" do
0
+    no_ko_gi_ri = [0x30CE,0x30B3,0x30AE,0x30EA]
0
+    nokogiri_ja_kana = no_ko_gi_ri.pack("U*")
0
+    nokogiri_char_ref = no_ko_gi_ri.map{|c| "&#x%X;" % c }.join("")
0
+
0
+    link = Webrat::Link.new(@session, nil)
0
+    link.should_receive(:text).and_return(nokogiri_ja_kana)
0
+    link.matches_text?(nokogiri_ja_kana).should == 0
0
+  end
0
+
0
+  it "should match img link" do
0
+    link = Webrat::Link.new(@session, nil)
0
+    link.should_receive(:text).and_return('')
0
+    link.should_receive(:inner_html).and_return('<img src="logo.png" />')
0
+    link.matches_text?('logo.png').should == 10
0
+  end
0
+
0
   it "should matches_id? on exact matching id" do
0
     link = Webrat::Link.new(@session, nil)
0
     link.should_receive(:id).and_return("some_id")
0
@@ -76,4 +97,4 @@ describe Webrat::Link do
0
     link.matches_id?(/some/).should == true
0
   end
0
   
0
-end
0
\ No newline at end of file
0
+end

Comments