public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Refactored TextHelper#truncate, highlight, excerpt, word_wrap and auto_link to 
accept options hash [#705 state:resolved]
Signed-off-by: Joshua Peek <josh@joshpeek.com>
clemens (author)
Sun Jul 27 14:49:19 -0700 2008
josh (committer)
Sun Jul 27 14:49:19 -0700 2008
commit  10d9fe4bf3110c1d5de0c6b509fe0cbb9d5eda1d
tree    4cefdc1c9d064a529e3acaef9c0c4a4268eead3b
parent  f7abf0c9db61621b3f27061debd7983075cdca61
...
109
110
111
112
 
113
114
115
...
109
110
111
 
112
113
114
115
0
@@ -109,7 +109,7 @@ module ActionView
0
       def error_message_on(object, method, *args)
0
         options = args.extract_options!
0
         unless args.empty?
0
-          ActiveSupport::Deprecation.warn('error_message_on takes an option hash instead of separate' +
0
+          ActiveSupport::Deprecation.warn('error_message_on takes an option hash instead of separate ' +
0
             'prepend_text, append_text, and css_class arguments', caller)
0
 
0
           options[:prepend_text] = args[0] || ''
...
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
...
78
79
80
81
 
82
83
84
85
86
 
 
 
 
 
 
 
 
 
 
 
 
 
87
88
89
90
91
 
92
93
94
95
96
97
98
99
100
 
 
 
 
101
102
103
104
 
 
105
106
107
 
 
108
109
110
 
111
112
113
 
 
114
115
116
117
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
119
120
121
122
123
 
 
124
125
126
 
 
127
128
129
...
132
133
134
135
 
 
 
 
 
 
 
 
136
137
138
139
140
141
 
 
142
143
144
 
 
145
146
147
...
176
177
178
179
180
181
182
183
184
185
186
187
188
 
 
 
 
 
 
 
189
190
 
 
 
 
 
 
 
 
 
 
 
191
192
 
193
194
195
...
336
337
338
339
 
 
 
 
 
 
 
 
 
 
 
 
 
340
341
342
343
344
 
 
 
 
 
 
 
 
 
 
 
 
345
346
347
...
468
469
470
471
 
472
473
474
...
477
478
479
480
481
 
 
482
483
484
...
508
509
510
511
 
512
...
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
85
86
87
88
89
90
91
 
 
92
93
94
95
96
97
98
 
99
100
101
102
...
107
108
109
 
110
111
112
 
 
 
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
 
130
131
132
133
134
135
 
 
 
 
136
137
138
139
140
141
 
 
142
143
144
 
 
145
146
147
148
 
149
150
 
 
151
152
153
 
 
 
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
 
 
174
175
176
 
 
177
178
179
180
181
...
184
185
186
 
187
188
189
190
191
192
193
194
195
196
197
198
 
 
199
200
201
 
 
202
203
204
205
206
...
235
236
237
 
 
 
 
 
238
239
240
241
 
242
243
244
245
246
247
248
249
 
250
251
252
253
254
255
256
257
258
259
260
261
 
262
263
264
265
...
406
407
408
 
409
410
411
412
413
414
415
416
417
418
419
420
421
422
 
 
 
 
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
...
558
559
560
 
561
562
563
564
...
567
568
569
 
 
570
571
572
573
574
...
598
599
600
 
601
602
0
@@ -34,40 +34,69 @@ module ActionView
0
       end
0
 
0
       if RUBY_VERSION < '1.9'
0
-        # If +text+ is longer than +length+, +text+ will be truncated to the length of
0
-        # +length+ (defaults to 30) and the last characters will be replaced with the +truncate_string+
0
-        # (defaults to "...").
0
+        # Truncates a given +text+ after a given <tt>:length</tt> if +text+ is longer than <tt>:length</tt>
0
+        # (defaults to 30). The last characters will be replaced with the <tt>:omission</tt> (defaults to "...").
0
         #
0
         # ==== Examples
0
-        #   truncate("Once upon a time in a world far far away", 14)
0
-        #   # => Once upon a...
0
         #
0
         #   truncate("Once upon a time in a world far far away")
0
         #   # => Once upon a time in a world f...
0
         #
0
-        #   truncate("And they found that many people were sleeping better.", 25, "(clipped)")
0
+        #   truncate("Once upon a time in a world far far away", :length => 14)
0
+        #   # => Once upon a...
0
+        #
0
+        #   truncate("And they found that many people were sleeping better.", :length => 25, "(clipped)")
0
         #   # => And they found that many (clipped)
0
         #
0
+        #   truncate("And they found that many people were sleeping better.", :omission => "... (continued)", :length => 15)
0
+        #   # => And they found... (continued)
0
+        #
0
+        # You can still use <tt>truncate</tt> with the old API that accepts the
0
+        # +length+ as its optional second and the +ellipsis+ as its
0
+        # optional third parameter:
0
+        #   truncate("Once upon a time in a world far far away", 14)
0
+        #   # => Once upon a time in a world f...
0
+        #
0
         #   truncate("And they found that many people were sleeping better.", 15, "... (continued)")
0
         #   # => And they found... (continued)
0
-        def truncate(text, length = 30, truncate_string = "...")
0
+        def truncate(text, *args)
0
+          options = args.extract_options!
0
+          unless args.empty?
0
+            ActiveSupport::Deprecation.warn('truncate takes an option hash instead of separate ' +
0
+              'length and omission arguments', caller)
0
+
0
+            options[:length] = args[0] || 30
0
+            options[:omission] = args[1] || "..."
0
+          end
0
+          options.reverse_merge!(:length => 30, :omission => "...")
0
+
0
           if text
0
-            l = length - truncate_string.chars.length
0
+            l = options[:length] - options[:omission].chars.length
0
             chars = text.chars
0
-            (chars.length > length ? chars[0...l] + truncate_string : text).to_s
0
+            (chars.length > options[:length] ? chars[0...l] + options[:omission] : text).to_s
0
           end
0
         end
0
       else
0
-        def truncate(text, length = 30, truncate_string = "...") #:nodoc:
0
+        def truncate(text, *args) #:nodoc:
0
+          options = args.extract_options!
0
+          unless args.empty?
0
+            ActiveSupport::Deprecation.warn('truncate takes an option hash instead of separate ' +
0
+              'length and omission arguments', caller)
0
+
0
+            options[:length] = args[0] || 30
0
+            options[:omission] = args[1] || "..."
0
+          end
0
+          options.reverse_merge!(:length => 30, :omission => "...")
0
+
0
           if text
0
-            l = length - truncate_string.length
0
-            (text.length > length ? text[0...l] + truncate_string : text).to_s
0
+            l = options[:length].to_i - options[:omission].length
0
+            (text.length > options[:length].to_i ? text[0...l] + options[:omission] : text).to_s
0
           end
0
         end
0
       end
0
 
0
       # Highlights one or more +phrases+ everywhere in +text+ by inserting it into
0
-      # a +highlighter+ string. The highlighter can be specialized by passing +highlighter+
0
+      # a <tt>:highlighter</tt> string. The highlighter can be specialized by passing <tt>:highlighter</tt>
0
       # as a single-quoted string with \1 where the phrase is to be inserted (defaults to
0
       # '<strong class="highlight">\1</strong>')
0
       #
0
@@ -78,52 +107,75 @@ module ActionView
0
       #   highlight('You searched for: ruby, rails, dhh', 'actionpack')
0
       #   # => You searched for: ruby, rails, dhh
0
       #
0
-      #   highlight('You searched for: rails', ['for', 'rails'], '<em>\1</em>')
0
+      #   highlight('You searched for: rails', ['for', 'rails'], :highlighter => '<em>\1</em>')
0
       #   # => You searched <em>for</em>: <em>rails</em>
0
       #
0
-      #   highlight('You searched for: rails', 'rails', "<a href='search?q=\1'>\1</a>")
0
-      #   # => You searched for: <a href='search?q=rails>rails</a>
0
-      def highlight(text, phrases, highlighter = '<strong class="highlight">\1</strong>')
0
+      #   highlight('You searched for: rails', 'rails', :highlighter => '<a href="search?q=\1">\1</a>')
0
+      #   # => You searched for: <a href="search?q=rails">rails</a>
0
+      #
0
+      # You can still use <tt>highlight</tt> with the old API that accepts the
0
+      # +highlighter+ as its optional third parameter:
0
+      #   highlight('You searched for: rails', 'rails', '<a href="search?q=\1">\1</a>')     # => You searched for: <a href="search?q=rails">rails</a>
0
+      def highlight(text, phrases, *args)
0
+        options = args.extract_options!
0
+        unless args.empty?
0
+          options[:highlighter] = args[0] || '<strong class="highlight">\1</strong>'
0
+        end
0
+        options.reverse_merge!(:highlighter => '<strong class="highlight">\1</strong>')
0
+
0
         if text.blank? || phrases.blank?
0
           text
0
         else
0
           match = Array(phrases).map { |p| Regexp.escape(p) }.join('|')
0
-          text.gsub(/(#{match})/i, highlighter)
0
+          text.gsub(/(#{match})/i, options[:highlighter])
0
         end
0
       end
0
 
0
       if RUBY_VERSION < '1.9'
0
         # Extracts an excerpt from +text+ that matches the first instance of +phrase+.
0
-        # The +radius+ expands the excerpt on each side of the first occurrence of +phrase+ by the number of characters
0
-        # defined in +radius+ (which defaults to 100). If the excerpt radius overflows the beginning or end of the +text+,
0
-        # then the +excerpt_string+ will be prepended/appended accordingly. The resulting string will be stripped in any case.
0
-        # If the +phrase+ isn't found, nil is returned.
0
+        # The <tt>:radius</tt> option expands the excerpt on each side of the first occurrence of +phrase+ by the number of characters
0
+        # defined in <tt>:radius</tt> (which defaults to 100). If the excerpt radius overflows the beginning or end of the +text+,
0
+        # then the <tt>:omission</tt> option (which defaults to "...") will be prepended/appended accordingly. The resulting string
0
+        # will be stripped in any case. If the +phrase+ isn't found, nil is returned.
0
         #
0
         # ==== Examples
0
-        #   excerpt('This is an example', 'an', 5)
0
-        #   # => "...s is an exam..."
0
+        #   excerpt('This is an example', 'an', :radius => 5)
0
+        #   # => ...s is an exam...
0
         #
0
-        #   excerpt('This is an example', 'is', 5)
0
-        #   # => "This is a..."
0
+        #   excerpt('This is an example', 'is', :radius => 5)
0
+        #   # => This is a...
0
         #
0
         #   excerpt('This is an example', 'is')
0
-        #   # => "This is an example"
0
+        #   # => This is an example
0
         #
0
-        #   excerpt('This next thing is an example', 'ex', 2)
0
-        #   # => "...next..."
0
+        #   excerpt('This next thing is an example', 'ex', :radius => 2)
0
+        #   # => ...next...
0
         #
0
-        #   excerpt('This is also an example', 'an', 8, '<chop> ')
0
-        #   # => "<chop> is also an example"
0
-        def excerpt(text, phrase, radius = 100, excerpt_string = "...")
0
+        #   excerpt('This is also an example', 'an', :radius => 8, :omission => '<chop> ')
0
+        #   # => <chop> is also an example
0
+        #
0
+        # You can still use <tt>excerpt</tt> with the old API that accepts the
0
+        # +radius+ as its optional third and the +ellipsis+ as its
0
+        # optional forth parameter:
0
+        #   excerpt('This is an example', 'an', 5)                   # => ...s is an exam...
0
+        #   excerpt('This is also an example', 'an', 8, '<chop> ')   # => <chop> is also an example
0
+        def excerpt(text, phrase, *args)
0
+          options = args.extract_options!
0
+          unless args.empty?
0
+            options[:radius] = args[0] || 100
0
+            options[:omission] = args[1] || "..."
0
+          end
0
+          options.reverse_merge!(:radius => 100, :omission => "...")
0
+
0
           if text && phrase
0
             phrase = Regexp.escape(phrase)
0
 
0
             if found_pos = text.chars =~ /(#{phrase})/i
0
-              start_pos = [ found_pos - radius, 0 ].max
0
-              end_pos   = [ [ found_pos + phrase.chars.length + radius - 1, 0].max, text.chars.length ].min
0
+              start_pos = [ found_pos - options[:radius], 0 ].max
0
+              end_pos   = [ [ found_pos + phrase.chars.length + options[:radius] - 1, 0].max, text.chars.length ].min
0
 
0
-              prefix  = start_pos > 0 ? excerpt_string : ""
0
-              postfix = end_pos < text.chars.length - 1 ? excerpt_string : ""
0
+              prefix  = start_pos > 0 ? options[:omission] : ""
0
+              postfix = end_pos < text.chars.length - 1 ? options[:omission] : ""
0
 
0
               prefix + text.chars[start_pos..end_pos].strip + postfix
0
             else
0
@@ -132,16 +184,23 @@ module ActionView
0
           end
0
         end
0
       else
0
-        def excerpt(text, phrase, radius = 100, excerpt_string = "...") #:nodoc:
0
+        def excerpt(text, phrase, *args) #:nodoc:
0
+          options = args.extract_options!
0
+          unless args.empty?
0
+            options[:radius] = args[0] || 100
0
+            options[:omission] = args[1] || "..."
0
+          end
0
+          options.reverse_merge!(:radius => 100, :omission => "...")
0
+
0
           if text && phrase
0
             phrase = Regexp.escape(phrase)
0
 
0
             if found_pos = text =~ /(#{phrase})/i
0
-              start_pos = [ found_pos - radius, 0 ].max
0
-              end_pos   = [ [ found_pos + phrase.length + radius - 1, 0].max, text.length ].min
0
+              start_pos = [ found_pos - options[:radius], 0 ].max
0
+              end_pos   = [ [ found_pos + phrase.length + options[:radius] - 1, 0].max, text.length ].min
0
 
0
-              prefix  = start_pos > 0 ? excerpt_string : ""
0
-              postfix = end_pos < text.length - 1 ? excerpt_string : ""
0
+              prefix  = start_pos > 0 ? options[:omission] : ""
0
+              postfix = end_pos < text.length - 1 ? options[:omission] : ""
0
 
0
               prefix + text[start_pos..end_pos].strip + postfix
0
             else
0
@@ -176,20 +235,31 @@ module ActionView
0
       # (which is 80 by default).
0
       #
0
       # ==== Examples
0
-      #   word_wrap('Once upon a time', 4)
0
-      #   # => Once\nupon\na\ntime
0
-      #
0
-      #   word_wrap('Once upon a time', 8)
0
-      #   # => Once upon\na time
0
       #
0
       #   word_wrap('Once upon a time')
0
       #   # => Once upon a time
0
       #
0
-      #   word_wrap('Once upon a time', 1)
0
+      #   word_wrap('Once upon a time, in a kingdom called Far Far Away, a king fell ill, and finding a successor to the throne turned out to be more trouble than anyone could have imagined...')
0
+      #   # => Once upon a time, in a kingdom called Far Far Away, a king fell ill, and finding\n a successor to the throne turned out to be more trouble than anyone could have\n imagined...
0
+      #
0
+      #   word_wrap('Once upon a time', :line_width => 8)
0
+      #   # => Once upon\na time
0
+      #
0
+      #   word_wrap('Once upon a time', :line_width => 1)
0
       #   # => Once\nupon\na\ntime
0
-      def word_wrap(text, line_width = 80)
0
+      #
0
+      # You can still use <tt>word_wrap</tt> with the old API that accepts the
0
+      # +line_width+ as its optional second parameter:
0
+      #   word_wrap('Once upon a time', 8)     # => Once upon\na time
0
+      def word_wrap(text, *args)
0
+        options = args.extract_options!
0
+        unless args.blank?
0
+          options[:line_width] = args[0] || 80
0
+        end
0
+        options.reverse_merge!(:line_width => 80)
0
+
0
         text.split("\n").collect do |line|
0
-          line.length > line_width ? line.gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1\n").strip : line
0
+          line.length > options[:line_width] ? line.gsub(/(.{1,#{options[:line_width]}})(\s+|$)/, "\\1\n").strip : line
0
         end * "\n"
0
       end
0
 
0
@@ -336,12 +406,32 @@ module ActionView
0
       #   # => "Welcome to my new blog at <a href=\"http://www.myblog.com/\" target=\"_blank\">http://www.m...</a>.
0
       #         Please e-mail me at <a href=\"mailto:me@email.com\">me@email.com</a>."
0
       #
0
-      def auto_link(text, link = :all, href_options = {}, &block)
0
+      #
0
+      # You can still use <tt>auto_link</tt> with the old API that accepts the
0
+      # +link+ as its optional second parameter and the +html_options+ hash
0
+      # as its optional third parameter:
0
+      #   post_body = "Welcome to my new blog at http://www.myblog.com/. Please e-mail me at me@email.com."
0
+      #   auto_link(post_body, :urls)     # => Once upon\na time
0
+      #   # => "Welcome to my new blog at <a href=\"http://www.myblog.com/\">http://www.myblog.com</a>.
0
+      #         Please e-mail me at me@email.com."
0
+      #
0
+      #   auto_link(post_body, :all, :target => "_blank")     # => Once upon\na time
0
+      #   # => "Welcome to my new blog at <a href=\"http://www.myblog.com/\" target=\"_blank\">http://www.myblog.com</a>.
0
+      #         Please e-mail me at <a href=\"mailto:me@email.com\">me@email.com</a>."
0
+      def auto_link(text, *args, &block)#link = :all, href_options = {}, &block)
0
         return '' if text.blank?
0
-        case link
0
-          when :all             then auto_link_email_addresses(auto_link_urls(text, href_options, &block), &block)
0
-          when :email_addresses then auto_link_email_addresses(text, &block)
0
-          when :urls            then auto_link_urls(text, href_options, &block)
0
+
0
+        options = args.size == 2 ? {} : args.extract_options! # this is necessary because the old auto_link API has a Hash as its last parameter
0
+        unless args.empty?
0
+          options[:link] = args[0] || :all
0
+          options[:html] = args[1] || {}
0
+        end
0
+        options.reverse_merge!(:link => :all, :html => {})
0
+
0
+        case options[:link].to_sym
0
+          when :all                         then auto_link_email_addresses(auto_link_urls(text, options[:html], &block), &block)
0
+          when :email_addresses             then auto_link_email_addresses(text, &block)
0
+          when :urls                        then auto_link_urls(text, options[:html], &block)
0
         end
0
       end
0
 
0
@@ -468,7 +558,7 @@ module ActionView
0
                           [-\w]+                   # subdomain or domain
0
                           (?:\.[-\w]+)*            # remaining subdomains or domain
0
                           (?::\d+)?                # port
0
-                          (?:/(?:(?:[~\w\+@%=\(\)-]|(?:[,.;:'][^\s$])))*)* # path
0
+                          (?:/(?:(?:[~\w\+@%=\(\)-]|(?:[,.;:'][^\s$]))+)?)* # path
0
                           (?:\?[\w\+@%&=.;-]+)?     # query string
0
                           (?:\#[\w\-]*)?           # trailing anchor
0
                         )
0
@@ -477,8 +567,8 @@ module ActionView
0
 
0
         # Turns all urls into clickable links.  If a block is given, each url
0
         # is yielded and the result is used as the link text.
0
-        def auto_link_urls(text, href_options = {})
0
-          extra_options = tag_options(href_options.stringify_keys) || ""
0
+        def auto_link_urls(text, html_options = {})
0
+          extra_options = tag_options(html_options.stringify_keys) || ""
0
           text.gsub(AUTO_LINK_RE) do
0
             all, a, b, c, d = $&, $1, $2, $3, $4
0
             if a =~ /<a\s/i # don't replace URL's that are already linked
0
@@ -508,4 +598,4 @@ module ActionView
0
         end
0
     end
0
   end
0
-end
0
+end
0
\ No newline at end of file
...
35
36
37
38
39
 
 
40
41
42
...
44
45
46
 
 
 
 
 
 
47
48
49
50
 
51
52
53
54
 
55
56
57
58
59
60
 
61
62
63
 
64
65
66
...
88
89
90
91
 
92
93
94
...
105
106
107
108
 
109
110
111
 
 
 
 
 
 
 
112
113
114
...
138
139
140
 
 
 
 
 
 
 
 
 
 
141
142
143
...
162
163
164
 
 
 
 
165
166
167
...
285
286
287
288
 
 
 
 
 
 
 
289
290
291
...
35
36
37
 
 
38
39
40
41
42
...
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
...
94
95
96
 
97
98
99
100
...
111
112
113
 
114
115
116
117
118
119
120
121
122
123
124
125
126
127
...
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
...
185
186
187
188
189
190
191
192
193
194
...
312
313
314
 
315
316
317
318
319
320
321
322
323
324
0
@@ -35,8 +35,8 @@ class TextHelperTest < ActionView::TestCase
0
   end
0
 
0
   def test_truncate
0
-    assert_equal "Hello World!", truncate("Hello World!", 12)
0
-    assert_equal "Hello Wor...", truncate("Hello World!!", 12)
0
+    assert_equal "Hello World!", truncate("Hello World!", :length => 12)
0
+    assert_equal "Hello Wor...", truncate("Hello World!!", :length => 12)
0
   end
0
 
0
   def test_truncate_should_use_default_length_of_30
0
@@ -44,23 +44,29 @@ class TextHelperTest < ActionView::TestCase
0
     assert_equal str[0...27] + "...", truncate(str)
0
   end
0
 
0
+  def test_truncate_with_options_hash
0
+    assert_equal "This is a string that wil[...]", truncate("This is a string that will go longer then the default truncate length of 30", :omission => "[...]")
0
+    assert_equal "Hello W...", truncate("Hello World!", :length => 10)
0
+    assert_equal "Hello[...]", truncate("Hello World!", :omission => "[...]", :length => 10)
0
+  end
0
+
0
   if RUBY_VERSION < '1.9.0'
0
     def test_truncate_multibyte
0
       with_kcode 'none' do
0
-        assert_equal "\354\225\210\353\205\225\355...", truncate("\354\225\210\353\205\225\355\225\230\354\204\270\354\232\224", 10)
0
+        assert_equal "\354\225\210\353\205\225\355...", truncate("\354\225\210\353\205\225\355\225\230\354\204\270\354\232\224", :length => 10)
0
       end
0
       with_kcode 'u' do
0
         assert_equal "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 ...",
0
-          truncate("\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 \354\225\204\353\235\274\353\246\254\354\230\244", 10)
0
+          truncate("\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 \354\225\204\353\235\274\353\246\254\354\230\244", :length => 10)
0
       end
0
     end
0
   else
0
     def test_truncate_multibyte
0
       assert_equal "\354\225\210\353\205\225\355...",
0
-        truncate("\354\225\210\353\205\225\355\225\230\354\204\270\354\232\224", 10)
0
+        truncate("\354\225\210\353\205\225\355\225\230\354\204\270\354\232\224", :length => 10)
0
 
0
       assert_equal "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 ...".force_encoding('UTF-8'),
0
-        truncate("\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 \354\225\204\353\235\274\353\246\254\354\230\244".force_encoding('UTF-8'), 10)
0
+        truncate("\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 \354\225\204\353\235\274\353\246\254\354\230\244".force_encoding('UTF-8'), :length => 10)
0
     end
0
   end
0
 
0
@@ -88,7 +94,7 @@ class TextHelperTest < ActionView::TestCase
0
     assert_equal '   ', highlight('   ', 'blank text is returned verbatim')
0
   end
0
 
0
-  def test_highlighter_with_regexp
0
+  def test_highlight_with_regexp
0
     assert_equal(
0
       "This is a <strong class=\"highlight\">beautiful!</strong> morning",
0
       highlight("This is a beautiful! morning", "beautiful!")
0
@@ -105,10 +111,17 @@ class TextHelperTest < ActionView::TestCase
0
     )
0
   end
0
 
0
-  def test_highlighting_multiple_phrases_in_one_pass
0
+  def test_highlight_with_multiple_phrases_in_one_pass
0
     assert_equal %(<em>wow</em> <em>em</em>), highlight('wow em', %w(wow em), '<em>\1</em>')
0
   end
0
 
0
+  def test_highlight_with_options_hash
0
+    assert_equal(
0
+      "This is a <b>beautiful</b> morning, but also a <b>beautiful</b> day",
0
+      highlight("This is a beautiful morning, but also a beautiful day", "beautiful", :highlighter => '<b>\1</b>')
0
+    )
0
+  end
0
+
0
   def test_excerpt
0
     assert_equal("...is a beautiful morn...", excerpt("This is a beautiful morning", "beautiful", 5))
0
     assert_equal("This is a...", excerpt("This is a beautiful morning", "this", 5))
0
@@ -138,6 +151,16 @@ class TextHelperTest < ActionView::TestCase
0
     assert_equal('...is a beautiful? mor...', excerpt('This is a beautiful? morning', 'beautiful', 5))
0
   end
0
 
0
+  def test_excerpt_with_options_hash
0
+    assert_equal("...is a beautiful morn...", excerpt("This is a beautiful morning", "beautiful", :radius => 5))
0
+    assert_equal("[...]is a beautiful morn[...]", excerpt("This is a beautiful morning", "beautiful", :omission => "[...]",:radius => 5))
0
+    assert_equal(
0
+      "This is the ultimate supercalifragilisticexpialidoceous very looooooooooooooooooong looooooooooooong beautiful morning with amazing sunshine and awesome tempera[...]",
0
+      excerpt("This is the ultimate supercalifragilisticexpialidoceous very looooooooooooooooooong looooooooooooong beautiful morning with amazing sunshine and awesome temperatures. So what are you gonna do about it?", "very",
0
+      :omission => "[...]")
0
+    )
0
+  end
0
+
0
   if RUBY_VERSION < '1.9'
0
     def test_excerpt_with_utf8
0
       with_kcode('u') do
0
@@ -162,6 +185,10 @@ class TextHelperTest < ActionView::TestCase
0
     assert_equal("my very very\nvery long\nstring\n\nwith another\nline", word_wrap("my very very very long string\n\nwith another line", 15))
0
   end
0
 
0
+  def test_word_wrap_with_options_hash
0
+    assert_equal("my very very\nvery long\nstring", word_wrap("my very very very long string", :line_width => 15))
0
+  end
0
+
0
   def test_pluralization
0
     assert_equal("1 count", pluralize(1, "count"))
0
     assert_equal("2 counts", pluralize(2, "count"))
0
@@ -285,7 +312,13 @@ class TextHelperTest < ActionView::TestCase
0
     url = "http://api.rubyonrails.com/Foo.html"
0
     email = "fantabulous@shiznadel.ic"
0
 
0
-    assert_equal %(<p><a href="#{url}">#{url[0...7]}...</a><br /><a href="mailto:#{email}">#{email[0...7]}...</a><br /></p>), auto_link("<p>#{url}<br />#{email}<br /></p>") { |url| truncate(url, 10) }
0
+    assert_equal %(<p><a href="#{url}">#{url[0...7]}...</a><br /><a href="mailto:#{email}">#{email[0...7]}...</a><br /></p>), auto_link("<p>#{url}<br />#{email}<br /></p>") { |url| truncate(url, :length => 10) }
0
+  end
0
+
0
+  def test_auto_link_with_options_hash
0
+    assert_equal 'Welcome to my new blog at <a href="http://www.myblog.com/" class="menu" target="_blank">http://www.myblog.com/</a>. Please e-mail me at <a href="mailto:me@email.com">me@email.com</a>.',
0
+      auto_link("Welcome to my new blog at http://www.myblog.com/. Please e-mail me at me@email.com.",
0
+                :link => :all, :html => { :class => "menu", :target => "_blank" })
0
   end
0
 
0
   def test_cycle_class

Comments