Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 7516

Show
Ignore:
Timestamp:
09/20/07 20:40:22 (2 years ago)
Author:
bitsweat
Message:

Autolink behaves well with emails embedded in URLs. Closes #7313.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/CHANGELOG

    r7514 r7516  
    11*SVN* 
     2 
     3* Autolink behaves well with emails embedded in URLs.  #7313 [Jeremy McAnally, tarmo] 
    24 
    35* Fixed that default layouts did not take the format into account #9564 [lifofifo] 
  • trunk/actionpack/lib/action_view/helpers/text_helper.rb

    r7515 r7516  
    306306        return '' if text.blank? 
    307307        case link 
    308           when :all             then auto_link_urls(auto_link_email_addresses(text, &block), href_options, &block) 
     308          when :all             then auto_link_email_addresses(auto_link_urls(text, href_options, &block), &block) 
    309309          when :email_addresses then auto_link_email_addresses(text, &block) 
    310310          when :urls            then auto_link_urls(text, href_options, &block) 
     
    535535                          (?:\.[-\w]+)*            # remaining subdomains or domain 
    536536                          (?::\d+)?                # port 
    537                           (?:/(?:(?:[~\w\+%-]|(?:[,.;:][^\s$]))+)?)* # path 
    538                           (?:\?[\w\+%&=.;-]+)?     # query string 
     537                          (?:/(?:(?:[~\w\+@%-]|(?:[,.;:][^\s$]))+)?)* # path 
     538                          (?:\?[\w\+@%&=.;-]+)?     # query string 
    539539                          (?:\#[\w\-]*)?           # trailing anchor 
    540540                        ) 
     
    561561        # each email is yielded and the result is used as the link text. 
    562562        def auto_link_email_addresses(text) 
     563          body = text.dup 
    563564          text.gsub(/([\w\.!#\$%\-+.]+@[A-Za-z0-9\-]+(\.[A-Za-z0-9\-]+)+)/) do 
    564565            text = $1 
    565             text = yield(text) if block_given? 
    566             %{<a href="mailto:#{$1}">#{text}</a>} 
     566             
     567            if body.match(/<a\b[^>]*>(.*)(#{Regexp.escape(text)})(.*)<\/a>/) 
     568              text 
     569            else 
     570              display_text = (block_given?) ? yield(text) : text 
     571              %{<a href="mailto:#{text}">#{display_text}</a>} 
     572            end 
    567573          end 
    568574        end 
  • trunk/actionpack/test/template/text_helper_test.rb

    r7515 r7516  
    158158              http://www.rubyonrails.com/~minam/contact;new?with=query&string=params 
    159159              http://en.wikipedia.org/wiki/Wikipedia:Today%27s_featured_picture_%28animation%29/January_20%2C_2007 
     160              http://www.mail-archive.com/rails@lists.rubyonrails.org/ 
    160161            ) 
    161162 
     
    168169    email_raw    = 'david@loudthinking.com' 
    169170    email_result = %{<a href="mailto:#{email_raw}">#{email_raw}</a>} 
     171    email2_raw    = '+david@loudthinking.com' 
     172    email2_result = %{<a href="mailto:#{email2_raw}">#{email2_raw}</a>} 
    170173    link_raw     = 'http://www.rubyonrails.com' 
    171174    link_result  = %{<a href="#{link_raw}">#{link_raw}</a>} 
     
    187190    link9_raw    = 'http://business.timesonline.co.uk/article/0,,9065-2473189,00.html' 
    188191    link9_result = %{<a href="#{link9_raw}">#{link9_raw}</a>} 
     192    link10_raw    = 'http://www.mail-archive.com/ruby-talk@ruby-lang.org/' 
     193    link10_result = %{<a href="#{link10_raw}">#{link10_raw}</a>} 
    189194 
    190195    assert_equal %(hello #{email_result}), auto_link("hello #{email_raw}", :email_addresses) 
     
    226231    assert_equal %(Go to #{link9_result}.), auto_link(%(Go to #{link9_raw}.)) 
    227232    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>)) 
     233    assert_equal %(<p>#{link10_result} Link</p>), auto_link("<p>#{link10_raw} Link</p>") 
     234    assert_equal email2_result, auto_link(email2_raw) 
    228235    assert_equal '', auto_link(nil) 
    229236    assert_equal '', auto_link('')