<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -545,38 +545,32 @@ module ActionView
         end
 
         AUTO_LINK_RE = %r{
-                        (                          # leading text
-                          &lt;\w+.*?&gt;|                # leading HTML tag, or
-                          [^=!:'&quot;/]|               # leading punctuation, or
-                          ^                        # beginning of line
-                        )
-                        (
-                          (?:https?://)|           # protocol spec, or
-                          (?:www\.)                # www.*
-                        )
-                        (
-                          [-\w]+                   # subdomain or domain
-                          (?:\.[-\w]+)*            # remaining subdomains or domain
-                          (?::\d+)?                # port
-                          (?:/(?:[~\w\+@%=\(\)-]|(?:[,.;:'][^\s$]))*)* # path
-                          (?:\?[\w\+@%&amp;=.;:-]+)?     # query string
-                          (?:\#[\w\-]*)?           # trailing anchor
-                        )
-                        ([[:punct:]]|&lt;|$|)       # trailing text
-                       }x unless const_defined?(:AUTO_LINK_RE)
+            ( https?:// | www\. )
+            [^\s&lt;]+
+          }x unless const_defined?(:AUTO_LINK_RE)
 
         # 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 = {})
-          extra_options = tag_options(html_options.stringify_keys) || &quot;&quot;
+          link_attributes = html_options.stringify_keys
           text.gsub(AUTO_LINK_RE) do
-            all, a, b, c, d = $&amp;, $1, $2, $3, $4
-            if a =~ /&lt;a\s/i # don't replace URL's that are already linked
-              all
+            href = $&amp;
+            # 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 = ''
+              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
-              text = b + c
-              text = yield(text) if block_given?
-              %(#{a}&lt;a href=&quot;#{b==&quot;www.&quot;?&quot;http://www.&quot;:b}#{c}&quot;#{extra_options}&gt;#{text}&lt;/a&gt;#{d})
+              # 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>@@ -225,36 +225,41 @@ class TextHelperTest &lt; ActionView::TestCase
             )
 
     urls.each do |url|
-      assert_equal %(&lt;a href=&quot;#{url}&quot;&gt;#{url}&lt;/a&gt;), auto_link(url)
+      assert_equal %(&lt;a href=&quot;#{CGI::escapeHTML url}&quot;&gt;#{CGI::escapeHTML url}&lt;/a&gt;), auto_link(url)
     end
   end
 
+  def generate_result(link_text, href = nil)
+    href ||= link_text
+    %{&lt;a href=&quot;#{CGI::escapeHTML href}&quot;&gt;#{CGI::escapeHTML link_text}&lt;/a&gt;}
+  end
+
   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  = %{&lt;a href=&quot;#{link_raw}&quot;&gt;#{link_raw}&lt;/a&gt;}
+    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 = %{&lt;a href=&quot;http://#{link2_raw}&quot;&gt;#{link2_raw}&lt;/a&gt;}
+    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 = %{&lt;a href=&quot;#{link3_raw}&quot;&gt;#{link3_raw}&lt;/a&gt;}
+    link3_result = generate_result(link3_raw)
     link4_raw    = 'http://foo.example.com/controller/action?parm=value&amp;p2=v2#anchor123'
-    link4_result = %{&lt;a href=&quot;#{link4_raw}&quot;&gt;#{link4_raw}&lt;/a&gt;}
+    link4_result = generate_result(link4_raw)
     link5_raw    = 'http://foo.example.com:3000/controller/action'
-    link5_result = %{&lt;a href=&quot;#{link5_raw}&quot;&gt;#{link5_raw}&lt;/a&gt;}
+    link5_result = generate_result(link5_raw)
     link6_raw    = 'http://foo.example.com:3000/controller/action+pack'
-    link6_result = %{&lt;a href=&quot;#{link6_raw}&quot;&gt;#{link6_raw}&lt;/a&gt;}
+    link6_result = generate_result(link6_raw)
     link7_raw    = 'http://foo.example.com/controller/action?parm=value&amp;p2=v2#anchor-123'
-    link7_result = %{&lt;a href=&quot;#{link7_raw}&quot;&gt;#{link7_raw}&lt;/a&gt;}
+    link7_result = generate_result(link7_raw)
     link8_raw    = 'http://foo.example.com:3000/controller/action.html'
-    link8_result = %{&lt;a href=&quot;#{link8_raw}&quot;&gt;#{link8_raw}&lt;/a&gt;}
+    link8_result = generate_result(link8_raw)
     link9_raw    = 'http://business.timesonline.co.uk/article/0,,9065-2473189,00.html'
-    link9_result = %{&lt;a href=&quot;#{link9_raw}&quot;&gt;#{link9_raw}&lt;/a&gt;}
+    link9_result = generate_result(link9_raw)
     link10_raw    = 'http://www.mail-archive.com/ruby-talk@ruby-lang.org/'
-    link10_result = %{&lt;a href=&quot;#{link10_raw}&quot;&gt;#{link10_raw}&lt;/a&gt;}
+    link10_result = generate_result(link10_raw)
 
     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)
@@ -299,7 +304,13 @@ class TextHelperTest &lt; ActionView::TestCase
     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 '&lt;a href=&quot;http://www.rubyonrails.com&quot;&gt;Ruby On Rails&lt;/a&gt;', auto_link('&lt;a href=&quot;http://www.rubyonrails.com&quot;&gt;Ruby On Rails&lt;/a&gt;')
+  end
+
+  def test_auto_link_already_linked
+    linked1 = generate_result('Ruby On Rails', 'http://www.rubyonrails.com')
+    linked2 = generate_result('www.rubyonrails.com', 'http://www.rubyonrails.com')
+    assert_equal linked1, auto_link(linked1)
+    assert_equal linked2, auto_link(linked2)
   end
 
   def test_auto_link_at_eol
@@ -317,7 +328,7 @@ class TextHelperTest &lt; ActionView::TestCase
   end
 
   def test_auto_link_with_options_hash
-    assert_equal 'Welcome to my new blog at &lt;a href=&quot;http://www.myblog.com/&quot; class=&quot;menu&quot; target=&quot;_blank&quot;&gt;http://www.myblog.com/&lt;/a&gt;. Please e-mail me at &lt;a href=&quot;mailto:me@email.com&quot;&gt;me@email.com&lt;/a&gt;.',
+    assert_dom_equal 'Welcome to my new blog at &lt;a href=&quot;http://www.myblog.com/&quot; class=&quot;menu&quot; target=&quot;_blank&quot;&gt;http://www.myblog.com/&lt;/a&gt;. Please e-mail me at &lt;a href=&quot;mailto:me@email.com&quot;&gt;me@email.com&lt;/a&gt;.',
       auto_link(&quot;Welcome to my new blog at http://www.myblog.com/. Please e-mail me at me@email.com.&quot;,
                 :link =&gt; :all, :html =&gt; { :class =&gt; &quot;menu&quot;, :target =&gt; &quot;_blank&quot; })
   end</diff>
      <filename>actionpack/test/template/text_helper_test.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>789a3f5b035fd293a9e235672a97b683a56ba0c3</id>
    </parent>
  </parents>
  <author>
    <name>Mislav Marohni&#263;</name>
    <login>mislav</login>
    <email>mislav.marohnic@gmail.com</email>
  </author>
  <url>http://github.com/rails/rails/commit/c6c5cd554110f6e62290de3e3008076b2f69e7cb</url>
  <id>c6c5cd554110f6e62290de3e3008076b2f69e7cb</id>
  <committed-date>2008-11-15T09:30:01-08:00</committed-date>
  <authored-date>2008-11-12T04:15:57-08:00</authored-date>
  <message>refactor autolink helper. change tests to expect HTML-escaped URLs

Signed-off-by: Michael Koziarski &lt;michael@koziarski.com&gt;</message>
  <tree>8acdf33374ce90a659cd20b75825fcde9e99a4fc</tree>
  <committer>
    <name>Michael Koziarski</name>
    <login>NZKoz</login>
    <email>michael@koziarski.com</email>
  </committer>
</commit>
