public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
auto_link helper: add intelligent ending closing bracket handling. add new tests 
and reorder new ones for readability

Signed-off-by: Michael Koziarski <michael@koziarski.com>
[#1353 state:committed]
Mislav Marohnić (author)
Thu Nov 13 13:39:16 -0800 2008
NZKoz (committer)
Sat Nov 15 09:30:17 -0800 2008
commit  4f984c9d0e66601a81cb5ae6e3b50582e6dc0c2d
tree    eed03362c18cc80ef3b69b1bb1aee78e734b4fc0
parent  c6c5cd554110f6e62290de3e3008076b2f69e7cb
...
549
550
551
 
 
552
553
554
555
556
557
 
558
559
560
561
562
563
564
 
 
 
 
 
 
 
 
 
 
565
566
567
568
569
570
571
572
573
574
575
576
...
549
550
551
552
553
554
555
556
557
558
559
560
561
 
 
 
 
 
 
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
 
 
 
578
579
580
0
@@ -549,28 +549,32 @@ module ActionView
0
             [^\s<]+
0
           }x unless const_defined?(:AUTO_LINK_RE)
0
 
0
+        BRACKETS = { ']' => '[', ')' => '(', '}' => '{' }
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, html_options = {})
0
           link_attributes = html_options.stringify_keys
0
           text.gsub(AUTO_LINK_RE) do
0
             href = $&
0
+            punctuation = ''
0
             # detect already linked URLs
0
-            unless $` =~ /<a\s[^>]*href="$/
0
-              if href =~ /[^\w\/-]$/
0
-                punctuation = href[-1, 1]
0
-                href = href[0, href.length - 1]
0
-              else
0
-                punctuation = ''
0
+            if $` =~ /<a\s[^>]*href="$/
0
+              # do not change string; URL is alreay linked
0
+              href
0
+            else
0
+              # don't include trailing punctuation character as part of the URL
0
+              if href.sub!(/[^\w\/-]$/, '') and punctuation = $& and opening = BRACKETS[punctuation]
0
+                if href.scan(opening).size > href.scan(punctuation).size
0
+                  href << punctuation
0
+                  punctuation = ''
0
+                end
0
               end
0
 
0
               link_text = block_given?? yield(href) : href
0
               href = 'http://' + href unless href.index('http') == 0
0
 
0
               content_tag(:a, h(link_text), link_attributes.merge('href' => href)) + punctuation
0
-            else
0
-              # do not change string; URL is alreay linked
0
-              href
0
             end
0
           end
0
         end
...
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
226
227
228
 
229
230
231
...
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
...
270
271
272
 
 
 
 
 
 
 
273
274
275
276
277
278
 
 
 
279
280
281
282
283
284
 
 
 
 
 
285
286
 
 
 
287
 
 
 
288
 
 
 
289
 
 
 
290
291
292
293
294
295
 
 
 
 
 
296
297
298
299
300
301
 
 
 
 
 
302
303
304
305
306
307
308
309
...
313
314
315
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
316
317
318
...
205
206
207
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
 
231
232
233
234
...
240
241
242
 
 
243
244
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
245
246
247
248
249
250
251
252
...
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
 
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
 
308
309
310
311
312
313
314
315
316
317
 
318
319
320
321
322
323
 
 
 
 
324
325
326
...
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
0
@@ -205,27 +205,30 @@ class TextHelperTest < ActionView::TestCase
0
   end
0
 
0
   def test_auto_link_parsing
0
-    urls = %w(http://www.rubyonrails.com
0
-              http://www.rubyonrails.com:80
0
-              http://www.rubyonrails.com/~minam
0
-              https://www.rubyonrails.com/~minam
0
-              http://www.rubyonrails.com/~minam/url%20with%20spaces
0
-              http://www.rubyonrails.com/foo.cgi?something=here
0
-              http://www.rubyonrails.com/foo.cgi?something=here&and=here
0
-              http://www.rubyonrails.com/contact;new
0
-              http://www.rubyonrails.com/contact;new%20with%20spaces
0
-              http://www.rubyonrails.com/contact;new?with=query&string=params
0
-              http://www.rubyonrails.com/~minam/contact;new?with=query&string=params
0
-              http://en.wikipedia.org/wiki/Wikipedia:Today%27s_featured_picture_%28animation%29/January_20%2C_2007
0
-              http://www.mail-archive.com/rails@lists.rubyonrails.org/
0
-              http://www.amazon.com/Testing-Equal-Sign-In-Path/ref=pd_bbs_sr_1?ie=UTF8&s=books&qid=1198861734&sr=8-1
0
-              http://en.wikipedia.org/wiki/Sprite_(computer_graphics)
0
-              http://en.wikipedia.org/wiki/Texas_hold'em
0
-              https://www.google.com/doku.php?id=gps:resource:scs:start
0
-            )
0
+    urls = %w(
0
+      http://www.rubyonrails.com
0
+      http://www.rubyonrails.com:80
0
+      http://www.rubyonrails.com/~minam
0
+      https://www.rubyonrails.com/~minam
0
+      http://www.rubyonrails.com/~minam/url%20with%20spaces
0
+      http://www.rubyonrails.com/foo.cgi?something=here
0
+      http://www.rubyonrails.com/foo.cgi?something=here&and=here
0
+      http://www.rubyonrails.com/contact;new
0
+      http://www.rubyonrails.com/contact;new%20with%20spaces
0
+      http://www.rubyonrails.com/contact;new?with=query&string=params
0
+      http://www.rubyonrails.com/~minam/contact;new?with=query&string=params
0
+      http://en.wikipedia.org/wiki/Wikipedia:Today%27s_featured_picture_%28animation%29/January_20%2C_2007
0
+      http://www.mail-archive.com/rails@lists.rubyonrails.org/
0
+      http://www.amazon.com/Testing-Equal-Sign-In-Path/ref=pd_bbs_sr_1?ie=UTF8&s=books&qid=1198861734&sr=8-1
0
+      http://en.wikipedia.org/wiki/Texas_hold'em
0
+      https://www.google.com/doku.php?id=gps:resource:scs:start
0
+      http://connect.oraclecorp.com/search?search[q]=green+france&search[type]=Group
0
+      http://of.openfoundry.org/projects/492/download#4th.Release.3
0
+      http://maps.google.co.uk/maps?f=q&q=the+london+eye&ie=UTF8&ll=51.503373,-0.11939&spn=0.007052,0.012767&z=16&iwloc=A
0
+    )
0
 
0
     urls.each do |url|
0
-      assert_equal %(<a href="#{CGI::escapeHTML url}">#{CGI::escapeHTML url}</a>), auto_link(url)
0
+      assert_equal generate_result(url), auto_link(url)
0
     end
0
   end
0
 
0
@@ -237,29 +240,13 @@ class TextHelperTest < ActionView::TestCase
0
   def test_auto_linking
0
     email_raw    = 'david@loudthinking.com'
0
     email_result = %{<a href="mailto:#{email_raw}">#{email_raw}</a>}
0
-    email2_raw    = '+david@loudthinking.com'
0
-    email2_result = %{<a href="mailto:#{email2_raw}">#{email2_raw}</a>}
0
     link_raw     = 'http://www.rubyonrails.com'
0
     link_result  = generate_result(link_raw)
0
-    link_result_with_options  = %{<a href="#{link_raw}" target="_blank">#{link_raw}</a>}
0
-    link2_raw    = 'www.rubyonrails.com'
0
-    link2_result = generate_result(link2_raw, "http://#{link2_raw}")
0
-    link3_raw    = 'http://manuals.ruby-on-rails.com/read/chapter.need_a-period/103#page281'
0
-    link3_result = generate_result(link3_raw)
0
-    link4_raw    = 'http://foo.example.com/controller/action?parm=value&p2=v2#anchor123'
0
-    link4_result = generate_result(link4_raw)
0
-    link5_raw    = 'http://foo.example.com:3000/controller/action'
0
-    link5_result = generate_result(link5_raw)
0
-    link6_raw    = 'http://foo.example.com:3000/controller/action+pack'
0
-    link6_result = generate_result(link6_raw)
0
-    link7_raw    = 'http://foo.example.com/controller/action?parm=value&p2=v2#anchor-123'
0
-    link7_result = generate_result(link7_raw)
0
-    link8_raw    = 'http://foo.example.com:3000/controller/action.html'
0
-    link8_result = generate_result(link8_raw)
0
-    link9_raw    = 'http://business.timesonline.co.uk/article/0,,9065-2473189,00.html'
0
-    link9_result = generate_result(link9_raw)
0
-    link10_raw    = 'http://www.mail-archive.com/ruby-talk@ruby-lang.org/'
0
-    link10_result = generate_result(link10_raw)
0
+    link_result_with_options = %{<a href="#{link_raw}" target="_blank">#{link_raw}</a>}
0
+
0
+    assert_equal '', auto_link(nil)
0
+    assert_equal '', auto_link('')
0
+    assert_equal "#{link_result} #{link_result} #{link_result}", auto_link("#{link_raw} #{link_raw} #{link_raw}")
0
 
0
     assert_equal %(hello #{email_result}), auto_link("hello #{email_raw}", :email_addresses)
0
     assert_equal %(Go to #{link_result}), auto_link("Go to #{link_raw}", :urls)
0
@@ -270,40 +257,70 @@ class TextHelperTest < ActionView::TestCase
0
     assert_equal %(<p>Link #{link_result_with_options}</p>), auto_link("<p>Link #{link_raw}</p>", :all, {:target => "_blank"})
0
     assert_equal %(Go to #{link_result}.), auto_link(%(Go to #{link_raw}.))
0
     assert_equal %(<p>Go to #{link_result}, then say hello to #{email_result}.</p>), auto_link(%(<p>Go to #{link_raw}, then say hello to #{email_raw}.</p>))
0
+
0
+    email2_raw    = '+david@loudthinking.com'
0
+    email2_result = %{<a href="mailto:#{email2_raw}">#{email2_raw}</a>}
0
+    assert_equal email2_result, auto_link(email2_raw)
0
+
0
+    link2_raw    = 'www.rubyonrails.com'
0
+    link2_result = generate_result(link2_raw, "http://#{link2_raw}")
0
     assert_equal %(Go to #{link2_result}), auto_link("Go to #{link2_raw}", :urls)
0
     assert_equal %(Go to #{link2_raw}), auto_link("Go to #{link2_raw}", :email_addresses)
0
     assert_equal %(<p>Link #{link2_result}</p>), auto_link("<p>Link #{link2_raw}</p>")
0
     assert_equal %(<p>#{link2_result} Link</p>), auto_link("<p>#{link2_raw} Link</p>")
0
     assert_equal %(Go to #{link2_result}.), auto_link(%(Go to #{link2_raw}.))
0
     assert_equal %(<p>Say hello to #{email_result}, then go to #{link2_result}.</p>), auto_link(%(<p>Say hello to #{email_raw}, then go to #{link2_raw}.</p>))
0
+
0
+    link3_raw    = 'http://manuals.ruby-on-rails.com/read/chapter.need_a-period/103#page281'
0
+    link3_result = generate_result(link3_raw)
0
     assert_equal %(Go to #{link3_result}), auto_link("Go to #{link3_raw}", :urls)
0
     assert_equal %(Go to #{link3_raw}), auto_link("Go to #{link3_raw}", :email_addresses)
0
     assert_equal %(<p>Link #{link3_result}</p>), auto_link("<p>Link #{link3_raw}</p>")
0
     assert_equal %(<p>#{link3_result} Link</p>), auto_link("<p>#{link3_raw} Link</p>")
0
     assert_equal %(Go to #{link3_result}.), auto_link(%(Go to #{link3_raw}.))
0
-    assert_equal %(<p>Go to #{link3_result}. seriously, #{link3_result}? i think I'll say hello to #{email_result}. instead.</p>), auto_link(%(<p>Go to #{link3_raw}. seriously, #{link3_raw}? i think I'll say hello to #{email_raw}. instead.</p>))
0
+    assert_equal %(<p>Go to #{link3_result}. Seriously, #{link3_result}? I think I'll say hello to #{email_result}. Instead.</p>),
0
+       auto_link(%(<p>Go to #{link3_raw}. Seriously, #{link3_raw}? I think I'll say hello to #{email_raw}. Instead.</p>))
0
+
0
+    link4_raw    = 'http://foo.example.com/controller/action?parm=value&p2=v2#anchor123'
0
+    link4_result = generate_result(link4_raw)
0
     assert_equal %(<p>Link #{link4_result}</p>), auto_link("<p>Link #{link4_raw}</p>")
0
     assert_equal %(<p>#{link4_result} Link</p>), auto_link("<p>#{link4_raw} Link</p>")
0
+
0
+    link5_raw    = 'http://foo.example.com:3000/controller/action'
0
+    link5_result = generate_result(link5_raw)
0
     assert_equal %(<p>#{link5_result} Link</p>), auto_link("<p>#{link5_raw} Link</p>")
0
+
0
+    link6_raw    = 'http://foo.example.com:3000/controller/action+pack'
0
+    link6_result = generate_result(link6_raw)
0
     assert_equal %(<p>#{link6_result} Link</p>), auto_link("<p>#{link6_raw} Link</p>")
0
+
0
+    link7_raw    = 'http://foo.example.com/controller/action?parm=value&p2=v2#anchor-123'
0
+    link7_result = generate_result(link7_raw)
0
     assert_equal %(<p>#{link7_result} Link</p>), auto_link("<p>#{link7_raw} Link</p>")
0
+
0
+    link8_raw    = 'http://foo.example.com:3000/controller/action.html'
0
+    link8_result = generate_result(link8_raw)
0
     assert_equal %(Go to #{link8_result}), auto_link("Go to #{link8_raw}", :urls)
0
     assert_equal %(Go to #{link8_raw}), auto_link("Go to #{link8_raw}", :email_addresses)
0
     assert_equal %(<p>Link #{link8_result}</p>), auto_link("<p>Link #{link8_raw}</p>")
0
     assert_equal %(<p>#{link8_result} Link</p>), auto_link("<p>#{link8_raw} Link</p>")
0
     assert_equal %(Go to #{link8_result}.), auto_link(%(Go to #{link8_raw}.))
0
-    assert_equal %(<p>Go to #{link8_result}. seriously, #{link8_result}? i think I'll say hello to #{email_result}. instead.</p>), auto_link(%(<p>Go to #{link8_raw}. seriously, #{link8_raw}? i think I'll say hello to #{email_raw}. instead.</p>))
0
+    assert_equal %(<p>Go to #{link8_result}. Seriously, #{link8_result}? I think I'll say hello to #{email_result}. Instead.</p>),
0
+       auto_link(%(<p>Go to #{link8_raw}. Seriously, #{link8_raw}? I think I'll say hello to #{email_raw}. Instead.</p>))
0
+
0
+    link9_raw    = 'http://business.timesonline.co.uk/article/0,,9065-2473189,00.html'
0
+    link9_result = generate_result(link9_raw)
0
     assert_equal %(Go to #{link9_result}), auto_link("Go to #{link9_raw}", :urls)
0
     assert_equal %(Go to #{link9_raw}), auto_link("Go to #{link9_raw}", :email_addresses)
0
     assert_equal %(<p>Link #{link9_result}</p>), auto_link("<p>Link #{link9_raw}</p>")
0
     assert_equal %(<p>#{link9_result} Link</p>), auto_link("<p>#{link9_raw} Link</p>")
0
     assert_equal %(Go to #{link9_result}.), auto_link(%(Go to #{link9_raw}.))
0
-    assert_equal %(<p>Go to #{link9_result}. seriously, #{link9_result}? i think I'll say hello to #{email_result}. instead.</p>), auto_link(%(<p>Go to #{link9_raw}. seriously, #{link9_raw}? i think I'll say hello to #{email_raw}. instead.</p>))
0
+    assert_equal %(<p>Go to #{link9_result}. Seriously, #{link9_result}? I think I'll say hello to #{email_result}. Instead.</p>),
0
+       auto_link(%(<p>Go to #{link9_raw}. Seriously, #{link9_raw}? I think I'll say hello to #{email_raw}. Instead.</p>))
0
+
0
+    link10_raw    = 'http://www.mail-archive.com/ruby-talk@ruby-lang.org/'
0
+    link10_result = generate_result(link10_raw)
0
     assert_equal %(<p>#{link10_result} Link</p>), auto_link("<p>#{link10_raw} Link</p>")
0
-    assert_equal email2_result, auto_link(email2_raw)
0
-    assert_equal '', auto_link(nil)
0
-    assert_equal '', auto_link('')
0
-    assert_equal "#{link_result} #{link_result} #{link_result}", auto_link("#{link_raw} #{link_raw} #{link_raw}")
0
   end
0
 
0
   def test_auto_link_already_linked
0
@@ -313,6 +330,23 @@ class TextHelperTest < ActionView::TestCase
0
     assert_equal linked2, auto_link(linked2)
0
   end
0
 
0
+  def test_auto_link_with_brackets
0
+    link1_raw = 'http://en.wikipedia.org/wiki/Sprite_(computer_graphics)'
0
+    link1_result = generate_result(link1_raw)
0
+    assert_equal link1_result, auto_link(link1_raw)
0
+    assert_equal "(link: #{link1_result})", auto_link("(link: #{link1_raw})")
0
+
0
+    link2_raw = 'http://en.wikipedia.org/wiki/Sprite_[computer_graphics]'
0
+    link2_result = generate_result(link2_raw)
0
+    assert_equal link2_result, auto_link(link2_raw)
0
+    assert_equal "[link: #{link2_result}]", auto_link("[link: #{link2_raw}]")
0
+
0
+    link3_raw = 'http://en.wikipedia.org/wiki/Sprite_{computer_graphics}'
0
+    link3_result = generate_result(link3_raw)
0
+    assert_equal link3_result, auto_link(link3_raw)
0
+    assert_equal "{link: #{link3_result}}", auto_link("{link: #{link3_raw}}")
0
+  end
0
+
0
   def test_auto_link_at_eol
0
     url1 = "http://api.rubyonrails.com/Foo.html"
0
     url2 = "http://www.ruby-doc.org/core/Bar.html"

Comments