From 061d0c34fdf337b079813ccaacca29f1fe8de067 Mon Sep 17 00:00:00 2001 From: =?utf-8?q?Mislav=20Marohni=C4=87?= Date: Wed, 12 Nov 2008 13:15:57 +0100 Subject: [PATCH] refactor autolink helper. change tests to expect HTML-escaped URLs --- actionpack/lib/action_view/helpers/text_helper.rb | 46 +++++++++------------ actionpack/test/template/text_helper_test.rb | 37 +++++++++++------ 2 files changed, 44 insertions(+), 39 deletions(-) diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index 36f7575..d678cb0 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -545,38 +545,32 @@ module ActionView end AUTO_LINK_RE = %r{ - ( # leading text - <\w+.*?>| # leading HTML tag, or - [^=!:'"/]| # 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\+@%&=.;:-]+)? # query string - (?:\#[\w\-]*)? # trailing anchor - ) - ([[:punct:]]|<|$|) # trailing text - }x unless const_defined?(:AUTO_LINK_RE) + ( https?:// | www\. ) + [^\s<]+ + }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) || "" + link_attributes = html_options.stringify_keys text.gsub(AUTO_LINK_RE) do - all, a, b, c, d = $&, $1, $2, $3, $4 - if a =~ /]*href="$/ + 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' => href)) + punctuation else - text = b + c - text = yield(text) if block_given? - %(#{a}#{text}#{d}) + # do not change string; URL is alreay linked + href end end end diff --git a/actionpack/test/template/text_helper_test.rb b/actionpack/test/template/text_helper_test.rb index 095c952..cf41f76 100644 --- a/actionpack/test/template/text_helper_test.rb +++ b/actionpack/test/template/text_helper_test.rb @@ -225,9 +225,14 @@ class TextHelperTest < ActionView::TestCase ) urls.each do |url| - assert_equal %(#{url}), auto_link(url) + assert_equal %(#{CGI::escapeHTML url}), auto_link(url) end end + + def generate_result(link_text, href = nil) + href ||= link_text + %{#{CGI::escapeHTML link_text}} + end def test_auto_linking email_raw = 'david@loudthinking.com' @@ -235,26 +240,26 @@ class TextHelperTest < ActionView::TestCase email2_raw = '+david@loudthinking.com' email2_result = %{#{email2_raw}} link_raw = 'http://www.rubyonrails.com' - link_result = %{#{link_raw}} + link_result = generate_result(link_raw) link_result_with_options = %{#{link_raw}} link2_raw = 'www.rubyonrails.com' - link2_result = %{#{link2_raw}} + link2_result = generate_result(link2_raw, "http://#{link2_raw}") link3_raw = 'http://manuals.ruby-on-rails.com/read/chapter.need_a-period/103#page281' - link3_result = %{#{link3_raw}} + link3_result = generate_result(link3_raw) link4_raw = 'http://foo.example.com/controller/action?parm=value&p2=v2#anchor123' - link4_result = %{#{link4_raw}} + link4_result = generate_result(link4_raw) link5_raw = 'http://foo.example.com:3000/controller/action' - link5_result = %{#{link5_raw}} + link5_result = generate_result(link5_raw) link6_raw = 'http://foo.example.com:3000/controller/action+pack' - link6_result = %{#{link6_raw}} + link6_result = generate_result(link6_raw) link7_raw = 'http://foo.example.com/controller/action?parm=value&p2=v2#anchor-123' - link7_result = %{#{link7_raw}} + link7_result = generate_result(link7_raw) link8_raw = 'http://foo.example.com:3000/controller/action.html' - link8_result = %{#{link8_raw}} + link8_result = generate_result(link8_raw) link9_raw = 'http://business.timesonline.co.uk/article/0,,9065-2473189,00.html' - link9_result = %{#{link9_raw}} + link9_result = generate_result(link9_raw) link10_raw = 'http://www.mail-archive.com/ruby-talk@ruby-lang.org/' - link10_result = %{#{link10_raw}} + link10_result = generate_result(link10_raw) assert_equal %(hello #{email_result}), auto_link("hello #{email_raw}", :email_addresses) assert_equal %(Go to #{link_result}), auto_link("Go to #{link_raw}", :urls) @@ -299,7 +304,13 @@ class TextHelperTest < ActionView::TestCase assert_equal '', auto_link(nil) assert_equal '', auto_link('') assert_equal "#{link_result} #{link_result} #{link_result}", auto_link("#{link_raw} #{link_raw} #{link_raw}") - assert_equal 'Ruby On Rails', auto_link('Ruby On Rails') + 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 < ActionView::TestCase end def test_auto_link_with_options_hash - assert_equal 'Welcome to my new blog at http://www.myblog.com/. Please e-mail me at me@email.com.', + assert_dom_equal 'Welcome to my new blog at http://www.myblog.com/. Please e-mail me at me@email.com.', auto_link("Welcome to my new blog at http://www.myblog.com/. Please e-mail me at me@email.com.", :link => :all, :html => { :class => "menu", :target => "_blank" }) end -- 1.6.0.1 From e3a05066b22667688e3927affccff87b3ea7983e Mon Sep 17 00:00:00 2001 From: =?utf-8?q?Mislav=20Marohni=C4=87?= Date: Thu, 13 Nov 2008 22:39:16 +0100 Subject: [PATCH] auto_link helper: add intelligent ending closing bracket handling. add new tests and reorder new ones for readability --- actionpack/lib/action_view/helpers/text_helper.rb | 22 ++-- actionpack/test/template/text_helper_test.rb | 130 +++++++++++++-------- 2 files changed, 95 insertions(+), 57 deletions(-) diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index d678cb0..e22cdb7 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -548,6 +548,8 @@ module ActionView ( https?:// | www\. ) [^\s<]+ }x unless const_defined?(:AUTO_LINK_RE) + + BRACKETS = { ']' => '[', ')' => '(', '}' => '{' } # Turns all urls into clickable links. If a block is given, each url # is yielded and the result is used as the link text. @@ -555,22 +557,24 @@ module ActionView link_attributes = html_options.stringify_keys text.gsub(AUTO_LINK_RE) do href = $& + punctuation = '' # detect already linked URLs - unless $` =~ /]*href="$/ - if href =~ /[^\w\/-]$/ - punctuation = href[-1, 1] - href = href[0, href.length - 1] - else - punctuation = '' + if $` =~ /]*href="$/ + # 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 = $& and opening = BRACKETS[punctuation] + if href.scan(opening).size > href.scan(punctuation).size + href << 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' => href)) + punctuation - else - # do not change string; URL is alreay linked - href end end end diff --git a/actionpack/test/template/text_helper_test.rb b/actionpack/test/template/text_helper_test.rb index cf41f76..7cf9503 100644 --- a/actionpack/test/template/text_helper_test.rb +++ b/actionpack/test/template/text_helper_test.rb @@ -205,27 +205,30 @@ class TextHelperTest < 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&and=here - http://www.rubyonrails.com/contact;new - http://www.rubyonrails.com/contact;new%20with%20spaces - http://www.rubyonrails.com/contact;new?with=query&string=params - http://www.rubyonrails.com/~minam/contact;new?with=query&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&s=books&qid=1198861734&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&and=here + http://www.rubyonrails.com/contact;new + http://www.rubyonrails.com/contact;new%20with%20spaces + http://www.rubyonrails.com/contact;new?with=query&string=params + http://www.rubyonrails.com/~minam/contact;new?with=query&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&s=books&qid=1198861734&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&search[type]=Group + http://of.openfoundry.org/projects/492/download#4th.Release.3 + 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 + ) urls.each do |url| - assert_equal %(#{CGI::escapeHTML url}), auto_link(url) + assert_equal generate_result(url), auto_link(url) end end @@ -234,32 +237,16 @@ class TextHelperTest < ActionView::TestCase %{#{CGI::escapeHTML link_text}} end - def test_auto_linking + def test_auto_linking email_raw = 'david@loudthinking.com' email_result = %{#{email_raw}} - email2_raw = '+david@loudthinking.com' - email2_result = %{#{email2_raw}} link_raw = 'http://www.rubyonrails.com' link_result = generate_result(link_raw) - link_result_with_options = %{#{link_raw}} - link2_raw = 'www.rubyonrails.com' - link2_result = generate_result(link2_raw, "http://#{link2_raw}") - 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&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&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 = %{#{link_raw}} + + assert_equal '', auto_link(nil) + assert_equal '', auto_link('') + assert_equal "#{link_result} #{link_result} #{link_result}", auto_link("#{link_raw} #{link_raw} #{link_raw}") assert_equal %(hello #{email_result}), auto_link("hello #{email_raw}", :email_addresses) assert_equal %(Go to #{link_result}), auto_link("Go to #{link_raw}", :urls) @@ -270,40 +257,70 @@ class TextHelperTest < ActionView::TestCase assert_equal %(

Link #{link_result_with_options}

), auto_link("

Link #{link_raw}

", :all, {:target => "_blank"}) assert_equal %(Go to #{link_result}.), auto_link(%(Go to #{link_raw}.)) assert_equal %(

Go to #{link_result}, then say hello to #{email_result}.

), auto_link(%(

Go to #{link_raw}, then say hello to #{email_raw}.

)) + + email2_raw = '+david@loudthinking.com' + email2_result = %{#{email2_raw}} + assert_equal email2_result, auto_link(email2_raw) + + link2_raw = 'www.rubyonrails.com' + link2_result = generate_result(link2_raw, "http://#{link2_raw}") assert_equal %(Go to #{link2_result}), auto_link("Go to #{link2_raw}", :urls) assert_equal %(Go to #{link2_raw}), auto_link("Go to #{link2_raw}", :email_addresses) assert_equal %(

Link #{link2_result}

), auto_link("

Link #{link2_raw}

") assert_equal %(

#{link2_result} Link

), auto_link("

#{link2_raw} Link

") assert_equal %(Go to #{link2_result}.), auto_link(%(Go to #{link2_raw}.)) assert_equal %(

Say hello to #{email_result}, then go to #{link2_result}.

), auto_link(%(

Say hello to #{email_raw}, then go to #{link2_raw}.

)) + + 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("Go to #{link3_raw}", :urls) assert_equal %(Go to #{link3_raw}), auto_link("Go to #{link3_raw}", :email_addresses) assert_equal %(

Link #{link3_result}

), auto_link("

Link #{link3_raw}

") assert_equal %(

#{link3_result} Link

), auto_link("

#{link3_raw} Link

") assert_equal %(Go to #{link3_result}.), auto_link(%(Go to #{link3_raw}.)) - assert_equal %(

Go to #{link3_result}. seriously, #{link3_result}? i think I'll say hello to #{email_result}. instead.

), auto_link(%(

Go to #{link3_raw}. seriously, #{link3_raw}? i think I'll say hello to #{email_raw}. instead.

)) + assert_equal %(

Go to #{link3_result}. Seriously, #{link3_result}? I think I'll say hello to #{email_result}. Instead.

), + auto_link(%(

Go to #{link3_raw}. Seriously, #{link3_raw}? I think I'll say hello to #{email_raw}. Instead.

)) + + link4_raw = 'http://foo.example.com/controller/action?parm=value&p2=v2#anchor123' + link4_result = generate_result(link4_raw) assert_equal %(

Link #{link4_result}

), auto_link("

Link #{link4_raw}

") assert_equal %(

#{link4_result} Link

), auto_link("

#{link4_raw} Link

") + + link5_raw = 'http://foo.example.com:3000/controller/action' + link5_result = generate_result(link5_raw) assert_equal %(

#{link5_result} Link

), auto_link("

#{link5_raw} Link

") + + link6_raw = 'http://foo.example.com:3000/controller/action+pack' + link6_result = generate_result(link6_raw) assert_equal %(

#{link6_result} Link

), auto_link("

#{link6_raw} Link

") + + link7_raw = 'http://foo.example.com/controller/action?parm=value&p2=v2#anchor-123' + link7_result = generate_result(link7_raw) assert_equal %(

#{link7_result} Link

), auto_link("

#{link7_raw} Link

") + + link8_raw = 'http://foo.example.com:3000/controller/action.html' + link8_result = generate_result(link8_raw) assert_equal %(Go to #{link8_result}), auto_link("Go to #{link8_raw}", :urls) assert_equal %(Go to #{link8_raw}), auto_link("Go to #{link8_raw}", :email_addresses) assert_equal %(

Link #{link8_result}

), auto_link("

Link #{link8_raw}

") assert_equal %(

#{link8_result} Link

), auto_link("

#{link8_raw} Link

") assert_equal %(Go to #{link8_result}.), auto_link(%(Go to #{link8_raw}.)) - assert_equal %(

Go to #{link8_result}. seriously, #{link8_result}? i think I'll say hello to #{email_result}. instead.

), auto_link(%(

Go to #{link8_raw}. seriously, #{link8_raw}? i think I'll say hello to #{email_raw}. instead.

)) + assert_equal %(

Go to #{link8_result}. Seriously, #{link8_result}? I think I'll say hello to #{email_result}. Instead.

), + auto_link(%(

Go to #{link8_raw}. Seriously, #{link8_raw}? I think I'll say hello to #{email_raw}. Instead.

)) + + 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("Go to #{link9_raw}", :urls) assert_equal %(Go to #{link9_raw}), auto_link("Go to #{link9_raw}", :email_addresses) assert_equal %(

Link #{link9_result}

), auto_link("

Link #{link9_raw}

") assert_equal %(

#{link9_result} Link

), auto_link("

#{link9_raw} Link

") assert_equal %(Go to #{link9_result}.), auto_link(%(Go to #{link9_raw}.)) - assert_equal %(

Go to #{link9_result}. seriously, #{link9_result}? i think I'll say hello to #{email_result}. instead.

), auto_link(%(

Go to #{link9_raw}. seriously, #{link9_raw}? i think I'll say hello to #{email_raw}. instead.

)) + assert_equal %(

Go to #{link9_result}. Seriously, #{link9_result}? I think I'll say hello to #{email_result}. Instead.

), + auto_link(%(

Go to #{link9_raw}. Seriously, #{link9_raw}? I think I'll say hello to #{email_raw}. Instead.

)) + + link10_raw = 'http://www.mail-archive.com/ruby-talk@ruby-lang.org/' + link10_result = generate_result(link10_raw) assert_equal %(

#{link10_result} Link

), auto_link("

#{link10_raw} Link

") - assert_equal email2_result, auto_link(email2_raw) - assert_equal '', auto_link(nil) - assert_equal '', auto_link('') - assert_equal "#{link_result} #{link_result} #{link_result}", auto_link("#{link_raw} #{link_raw} #{link_raw}") end def test_auto_link_already_linked @@ -312,6 +329,23 @@ class TextHelperTest < ActionView::TestCase assert_equal linked1, auto_link(linked1) 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 "(link: #{link1_result})", auto_link("(link: #{link1_raw})") + + 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 "[link: #{link2_result}]", auto_link("[link: #{link2_raw}]") + + 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 "{link: #{link3_result}}", auto_link("{link: #{link3_raw}}") + end def test_auto_link_at_eol url1 = "http://api.rubyonrails.com/Foo.html" -- 1.6.0.1