<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -549,28 +549,32 @@ module ActionView
             [^\s&lt;]+
           }x unless const_defined?(:AUTO_LINK_RE)
 
+        BRACKETS = { ']' =&gt; '[', ')' =&gt; '(', '}' =&gt; '{' }
+
         # Turns all urls into clickable links.  If a block is given, each url
         # is yielded and the result is used as the link text.
         def auto_link_urls(text, html_options = {})
           link_attributes = html_options.stringify_keys
           text.gsub(AUTO_LINK_RE) do
             href = $&amp;
+            punctuation = ''
             # detect already linked URLs
-            unless $` =~ /&lt;a\s[^&gt;]*href=&quot;$/
-              if href =~ /[^\w\/-]$/
-                punctuation = href[-1, 1]
-                href = href[0, href.length - 1]
-              else
-                punctuation = ''
+            if $` =~ /&lt;a\s[^&gt;]*href=&quot;$/
+              # do not change string; URL is alreay linked
+              href
+            else
+              # don't include trailing punctuation character as part of the URL
+              if href.sub!(/[^\w\/-]$/, '') and punctuation = $&amp; and opening = BRACKETS[punctuation]
+                if href.scan(opening).size &gt; href.scan(punctuation).size
+                  href &lt;&lt; punctuation
+                  punctuation = ''
+                end
               end
 
               link_text = block_given?? yield(href) : href
               href = 'http://' + href unless href.index('http') == 0
 
               content_tag(:a, h(link_text), link_attributes.merge('href' =&gt; href)) + punctuation
-            else
-              # do not change string; URL is alreay linked
-              href
             end
           end
         end</diff>
      <filename>actionpack/lib/action_view/helpers/text_helper.rb</filename>
    </modified>
    <modified>
      <diff>@@ -205,27 +205,30 @@ class TextHelperTest &lt; ActionView::TestCase
   end
 
   def test_auto_link_parsing
-    urls = %w(http://www.rubyonrails.com
-              http://www.rubyonrails.com:80
-              http://www.rubyonrails.com/~minam
-              https://www.rubyonrails.com/~minam
-              http://www.rubyonrails.com/~minam/url%20with%20spaces
-              http://www.rubyonrails.com/foo.cgi?something=here
-              http://www.rubyonrails.com/foo.cgi?something=here&amp;and=here
-              http://www.rubyonrails.com/contact;new
-              http://www.rubyonrails.com/contact;new%20with%20spaces
-              http://www.rubyonrails.com/contact;new?with=query&amp;string=params
-              http://www.rubyonrails.com/~minam/contact;new?with=query&amp;string=params
-              http://en.wikipedia.org/wiki/Wikipedia:Today%27s_featured_picture_%28animation%29/January_20%2C_2007
-              http://www.mail-archive.com/rails@lists.rubyonrails.org/
-              http://www.amazon.com/Testing-Equal-Sign-In-Path/ref=pd_bbs_sr_1?ie=UTF8&amp;s=books&amp;qid=1198861734&amp;sr=8-1
-              http://en.wikipedia.org/wiki/Sprite_(computer_graphics)
-              http://en.wikipedia.org/wiki/Texas_hold'em
-              https://www.google.com/doku.php?id=gps:resource:scs:start
-            )
+    urls = %w(
+      http://www.rubyonrails.com
+      http://www.rubyonrails.com:80
+      http://www.rubyonrails.com/~minam
+      https://www.rubyonrails.com/~minam
+      http://www.rubyonrails.com/~minam/url%20with%20spaces
+      http://www.rubyonrails.com/foo.cgi?something=here
+      http://www.rubyonrails.com/foo.cgi?something=here&amp;and=here
+      http://www.rubyonrails.com/contact;new
+      http://www.rubyonrails.com/contact;new%20with%20spaces
+      http://www.rubyonrails.com/contact;new?with=query&amp;string=params
+      http://www.rubyonrails.com/~minam/contact;new?with=query&amp;string=params
+      http://en.wikipedia.org/wiki/Wikipedia:Today%27s_featured_picture_%28animation%29/January_20%2C_2007
+      http://www.mail-archive.com/rails@lists.rubyonrails.org/
+      http://www.amazon.com/Testing-Equal-Sign-In-Path/ref=pd_bbs_sr_1?ie=UTF8&amp;s=books&amp;qid=1198861734&amp;sr=8-1
+      http://en.wikipedia.org/wiki/Texas_hold'em
+      https://www.google.com/doku.php?id=gps:resource:scs:start
+      http://connect.oraclecorp.com/search?search[q]=green+france&amp;search[type]=Group
+      http://of.openfoundry.org/projects/492/download#4th.Release.3
+      http://maps.google.co.uk/maps?f=q&amp;q=the+london+eye&amp;ie=UTF8&amp;ll=51.503373,-0.11939&amp;spn=0.007052,0.012767&amp;z=16&amp;iwloc=A
+    )
 
     urls.each do |url|
-      assert_equal %(&lt;a href=&quot;#{CGI::escapeHTML url}&quot;&gt;#{CGI::escapeHTML url}&lt;/a&gt;), auto_link(url)
+      assert_equal generate_result(url), auto_link(url)
     end
   end
 
@@ -237,29 +240,13 @@ class TextHelperTest &lt; ActionView::TestCase
   def test_auto_linking
     email_raw    = 'david@loudthinking.com'
     email_result = %{&lt;a href=&quot;mailto:#{email_raw}&quot;&gt;#{email_raw}&lt;/a&gt;}
-    email2_raw    = '+david@loudthinking.com'
-    email2_result = %{&lt;a href=&quot;mailto:#{email2_raw}&quot;&gt;#{email2_raw}&lt;/a&gt;}
     link_raw     = 'http://www.rubyonrails.com'
     link_result  = generate_result(link_raw)
-    link_result_with_options  = %{&lt;a href=&quot;#{link_raw}&quot; target=&quot;_blank&quot;&gt;#{link_raw}&lt;/a&gt;}
-    link2_raw    = 'www.rubyonrails.com'
-    link2_result = generate_result(link2_raw, &quot;http://#{link2_raw}&quot;)
-    link3_raw    = 'http://manuals.ruby-on-rails.com/read/chapter.need_a-period/103#page281'
-    link3_result = generate_result(link3_raw)
-    link4_raw    = 'http://foo.example.com/controller/action?parm=value&amp;p2=v2#anchor123'
-    link4_result = generate_result(link4_raw)
-    link5_raw    = 'http://foo.example.com:3000/controller/action'
-    link5_result = generate_result(link5_raw)
-    link6_raw    = 'http://foo.example.com:3000/controller/action+pack'
-    link6_result = generate_result(link6_raw)
-    link7_raw    = 'http://foo.example.com/controller/action?parm=value&amp;p2=v2#anchor-123'
-    link7_result = generate_result(link7_raw)
-    link8_raw    = 'http://foo.example.com:3000/controller/action.html'
-    link8_result = generate_result(link8_raw)
-    link9_raw    = 'http://business.timesonline.co.uk/article/0,,9065-2473189,00.html'
-    link9_result = generate_result(link9_raw)
-    link10_raw    = 'http://www.mail-archive.com/ruby-talk@ruby-lang.org/'
-    link10_result = generate_result(link10_raw)
+    link_result_with_options = %{&lt;a href=&quot;#{link_raw}&quot; target=&quot;_blank&quot;&gt;#{link_raw}&lt;/a&gt;}
+
+    assert_equal '', auto_link(nil)
+    assert_equal '', auto_link('')
+    assert_equal &quot;#{link_result} #{link_result} #{link_result}&quot;, auto_link(&quot;#{link_raw} #{link_raw} #{link_raw}&quot;)
 
     assert_equal %(hello #{email_result}), auto_link(&quot;hello #{email_raw}&quot;, :email_addresses)
     assert_equal %(Go to #{link_result}), auto_link(&quot;Go to #{link_raw}&quot;, :urls)
@@ -270,40 +257,70 @@ class TextHelperTest &lt; ActionView::TestCase
     assert_equal %(&lt;p&gt;Link #{link_result_with_options}&lt;/p&gt;), auto_link(&quot;&lt;p&gt;Link #{link_raw}&lt;/p&gt;&quot;, :all, {:target =&gt; &quot;_blank&quot;})
     assert_equal %(Go to #{link_result}.), auto_link(%(Go to #{link_raw}.))
     assert_equal %(&lt;p&gt;Go to #{link_result}, then say hello to #{email_result}.&lt;/p&gt;), auto_link(%(&lt;p&gt;Go to #{link_raw}, then say hello to #{email_raw}.&lt;/p&gt;))
+
+    email2_raw    = '+david@loudthinking.com'
+    email2_result = %{&lt;a href=&quot;mailto:#{email2_raw}&quot;&gt;#{email2_raw}&lt;/a&gt;}
+    assert_equal email2_result, auto_link(email2_raw)
+
+    link2_raw    = 'www.rubyonrails.com'
+    link2_result = generate_result(link2_raw, &quot;http://#{link2_raw}&quot;)
     assert_equal %(Go to #{link2_result}), auto_link(&quot;Go to #{link2_raw}&quot;, :urls)
     assert_equal %(Go to #{link2_raw}), auto_link(&quot;Go to #{link2_raw}&quot;, :email_addresses)
     assert_equal %(&lt;p&gt;Link #{link2_result}&lt;/p&gt;), auto_link(&quot;&lt;p&gt;Link #{link2_raw}&lt;/p&gt;&quot;)
     assert_equal %(&lt;p&gt;#{link2_result} Link&lt;/p&gt;), auto_link(&quot;&lt;p&gt;#{link2_raw} Link&lt;/p&gt;&quot;)
     assert_equal %(Go to #{link2_result}.), auto_link(%(Go to #{link2_raw}.))
     assert_equal %(&lt;p&gt;Say hello to #{email_result}, then go to #{link2_result}.&lt;/p&gt;), auto_link(%(&lt;p&gt;Say hello to #{email_raw}, then go to #{link2_raw}.&lt;/p&gt;))
+
+    link3_raw    = 'http://manuals.ruby-on-rails.com/read/chapter.need_a-period/103#page281'
+    link3_result = generate_result(link3_raw)
     assert_equal %(Go to #{link3_result}), auto_link(&quot;Go to #{link3_raw}&quot;, :urls)
     assert_equal %(Go to #{link3_raw}), auto_link(&quot;Go to #{link3_raw}&quot;, :email_addresses)
     assert_equal %(&lt;p&gt;Link #{link3_result}&lt;/p&gt;), auto_link(&quot;&lt;p&gt;Link #{link3_raw}&lt;/p&gt;&quot;)
     assert_equal %(&lt;p&gt;#{link3_result} Link&lt;/p&gt;), auto_link(&quot;&lt;p&gt;#{link3_raw} Link&lt;/p&gt;&quot;)
     assert_equal %(Go to #{link3_result}.), auto_link(%(Go to #{link3_raw}.))
-    assert_equal %(&lt;p&gt;Go to #{link3_result}. seriously, #{link3_result}? i think I'll say hello to #{email_result}. instead.&lt;/p&gt;), auto_link(%(&lt;p&gt;Go to #{link3_raw}. seriously, #{link3_raw}? i think I'll say hello to #{email_raw}. instead.&lt;/p&gt;))
+    assert_equal %(&lt;p&gt;Go to #{link3_result}. Seriously, #{link3_result}? I think I'll say hello to #{email_result}. Instead.&lt;/p&gt;),
+       auto_link(%(&lt;p&gt;Go to #{link3_raw}. Seriously, #{link3_raw}? I think I'll say hello to #{email_raw}. Instead.&lt;/p&gt;))
+
+    link4_raw    = 'http://foo.example.com/controller/action?parm=value&amp;p2=v2#anchor123'
+    link4_result = generate_result(link4_raw)
     assert_equal %(&lt;p&gt;Link #{link4_result}&lt;/p&gt;), auto_link(&quot;&lt;p&gt;Link #{link4_raw}&lt;/p&gt;&quot;)
     assert_equal %(&lt;p&gt;#{link4_result} Link&lt;/p&gt;), auto_link(&quot;&lt;p&gt;#{link4_raw} Link&lt;/p&gt;&quot;)
+
+    link5_raw    = 'http://foo.example.com:3000/controller/action'
+    link5_result = generate_result(link5_raw)
     assert_equal %(&lt;p&gt;#{link5_result} Link&lt;/p&gt;), auto_link(&quot;&lt;p&gt;#{link5_raw} Link&lt;/p&gt;&quot;)
+
+    link6_raw    = 'http://foo.example.com:3000/controller/action+pack'
+    link6_result = generate_result(link6_raw)
     assert_equal %(&lt;p&gt;#{link6_result} Link&lt;/p&gt;), auto_link(&quot;&lt;p&gt;#{link6_raw} Link&lt;/p&gt;&quot;)
+
+    link7_raw    = 'http://foo.example.com/controller/action?parm=value&amp;p2=v2#anchor-123'
+    link7_result = generate_result(link7_raw)
     assert_equal %(&lt;p&gt;#{link7_result} Link&lt;/p&gt;), auto_link(&quot;&lt;p&gt;#{link7_raw} Link&lt;/p&gt;&quot;)
+
+    link8_raw    = 'http://foo.example.com:3000/controller/action.html'
+    link8_result = generate_result(link8_raw)
     assert_equal %(Go to #{link8_result}), auto_link(&quot;Go to #{link8_raw}&quot;, :urls)
     assert_equal %(Go to #{link8_raw}), auto_link(&quot;Go to #{link8_raw}&quot;, :email_addresses)
     assert_equal %(&lt;p&gt;Link #{link8_result}&lt;/p&gt;), auto_link(&quot;&lt;p&gt;Link #{link8_raw}&lt;/p&gt;&quot;)
     assert_equal %(&lt;p&gt;#{link8_result} Link&lt;/p&gt;), auto_link(&quot;&lt;p&gt;#{link8_raw} Link&lt;/p&gt;&quot;)
     assert_equal %(Go to #{link8_result}.), auto_link(%(Go to #{link8_raw}.))
-    assert_equal %(&lt;p&gt;Go to #{link8_result}. seriously, #{link8_result}? i think I'll say hello to #{email_result}. instead.&lt;/p&gt;), auto_link(%(&lt;p&gt;Go to #{link8_raw}. seriously, #{link8_raw}? i think I'll say hello to #{email_raw}. instead.&lt;/p&gt;))
+    assert_equal %(&lt;p&gt;Go to #{link8_result}. Seriously, #{link8_result}? I think I'll say hello to #{email_result}. Instead.&lt;/p&gt;),
+       auto_link(%(&lt;p&gt;Go to #{link8_raw}. Seriously, #{link8_raw}? I think I'll say hello to #{email_raw}. Instead.&lt;/p&gt;))
+
+    link9_raw    = 'http://business.timesonline.co.uk/article/0,,9065-2473189,00.html'
+    link9_result = generate_result(link9_raw)
     assert_equal %(Go to #{link9_result}), auto_link(&quot;Go to #{link9_raw}&quot;, :urls)
     assert_equal %(Go to #{link9_raw}), auto_link(&quot;Go to #{link9_raw}&quot;, :email_addresses)
     assert_equal %(&lt;p&gt;Link #{link9_result}&lt;/p&gt;), auto_link(&quot;&lt;p&gt;Link #{link9_raw}&lt;/p&gt;&quot;)
     assert_equal %(&lt;p&gt;#{link9_result} Link&lt;/p&gt;), auto_link(&quot;&lt;p&gt;#{link9_raw} Link&lt;/p&gt;&quot;)
     assert_equal %(Go to #{link9_result}.), auto_link(%(Go to #{link9_raw}.))
-    assert_equal %(&lt;p&gt;Go to #{link9_result}. seriously, #{link9_result}? i think I'll say hello to #{email_result}. instead.&lt;/p&gt;), auto_link(%(&lt;p&gt;Go to #{link9_raw}. seriously, #{link9_raw}? i think I'll say hello to #{email_raw}. instead.&lt;/p&gt;))
+    assert_equal %(&lt;p&gt;Go to #{link9_result}. Seriously, #{link9_result}? I think I'll say hello to #{email_result}. Instead.&lt;/p&gt;),
+       auto_link(%(&lt;p&gt;Go to #{link9_raw}. Seriously, #{link9_raw}? I think I'll say hello to #{email_raw}. Instead.&lt;/p&gt;))
+
+    link10_raw    = 'http://www.mail-archive.com/ruby-talk@ruby-lang.org/'
+    link10_result = generate_result(link10_raw)
     assert_equal %(&lt;p&gt;#{link10_result} Link&lt;/p&gt;), auto_link(&quot;&lt;p&gt;#{link10_raw} Link&lt;/p&gt;&quot;)
-    assert_equal email2_result, auto_link(email2_raw)
-    assert_equal '', auto_link(nil)
-    assert_equal '', auto_link('')
-    assert_equal &quot;#{link_result} #{link_result} #{link_result}&quot;, auto_link(&quot;#{link_raw} #{link_raw} #{link_raw}&quot;)
   end
 
   def test_auto_link_already_linked
@@ -313,6 +330,23 @@ class TextHelperTest &lt; ActionView::TestCase
     assert_equal linked2, auto_link(linked2)
   end
 
+  def test_auto_link_with_brackets
+    link1_raw = 'http://en.wikipedia.org/wiki/Sprite_(computer_graphics)'
+    link1_result = generate_result(link1_raw)
+    assert_equal link1_result, auto_link(link1_raw)
+    assert_equal &quot;(link: #{link1_result})&quot;, auto_link(&quot;(link: #{link1_raw})&quot;)
+
+    link2_raw = 'http://en.wikipedia.org/wiki/Sprite_[computer_graphics]'
+    link2_result = generate_result(link2_raw)
+    assert_equal link2_result, auto_link(link2_raw)
+    assert_equal &quot;[link: #{link2_result}]&quot;, auto_link(&quot;[link: #{link2_raw}]&quot;)
+
+    link3_raw = 'http://en.wikipedia.org/wiki/Sprite_{computer_graphics}'
+    link3_result = generate_result(link3_raw)
+    assert_equal link3_result, auto_link(link3_raw)
+    assert_equal &quot;{link: #{link3_result}}&quot;, auto_link(&quot;{link: #{link3_raw}}&quot;)
+  end
+
   def test_auto_link_at_eol
     url1 = &quot;http://api.rubyonrails.com/Foo.html&quot;
     url2 = &quot;http://www.ruby-doc.org/core/Bar.html&quot;</diff>
      <filename>actionpack/test/template/text_helper_test.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>c6c5cd554110f6e62290de3e3008076b2f69e7cb</id>
    </parent>
  </parents>
  <author>
    <name>Mislav Marohni&#263;</name>
    <email>mislav.marohnic@gmail.com</email>
  </author>
  <url>http://github.com/rails/rails/commit/4f984c9d0e66601a81cb5ae6e3b50582e6dc0c2d</url>
  <id>4f984c9d0e66601a81cb5ae6e3b50582e6dc0c2d</id>
  <committed-date>2008-11-15T09:30:17-08:00</committed-date>
  <authored-date>2008-11-13T13:39:16-08:00</authored-date>
  <message>auto_link helper: add intelligent ending closing bracket handling. add new tests and reorder new ones for readability

Signed-off-by: Michael Koziarski &lt;michael@koziarski.com&gt;
[#1353 state:committed]</message>
  <tree>eed03362c18cc80ef3b69b1bb1aee78e734b4fc0</tree>
  <committer>
    <name>Michael Koziarski</name>
    <email>michael@koziarski.com</email>
  </committer>
</commit>
