Skip to content
This repository
Browse code

Ensure auto_link does not ignore multiple trailing punctuations

[#2504 state:resolved]
  • Loading branch information...
commit 9e08e196fa5c7183aec1f6dbeebe6bb7539e3c66 1 parent 17b4fd2
Lance Ivy cainlevy authored mislav committed
13 actionpack/lib/action_view/helpers/text_helper.rb
@@ -549,24 +549,25 @@ def auto_link_urls(text, html_options = {})
549 549 link_attributes = html_options.stringify_keys
550 550 text.gsub(AUTO_LINK_RE) do
551 551 scheme, href = $1, $&
552   - punctuation = ''
  552 + punctuation = []
553 553
554 554 if auto_linked?($`, $')
555 555 # do not change string; URL is already linked
556 556 href
557 557 else
558 558 # don't include trailing punctuation character as part of the URL
559   - if href.sub!(/[^\w\/-]$/, '') and punctuation = $& and opening = BRACKETS[punctuation]
560   - if href.scan(opening).size > href.scan(punctuation).size
561   - href << punctuation
562   - punctuation = ''
  559 + while href.sub!(/[^\w\/-]$/, '')
  560 + punctuation.push $&
  561 + if opening = BRACKETS[punctuation.last] and href.scan(opening).size > href.scan(punctuation.last).size
  562 + href << punctuation.pop
  563 + break
563 564 end
564 565 end
565 566
566 567 link_text = block_given?? yield(href) : href
567 568 href = 'http://' + href unless scheme
568 569
569   - content_tag(:a, h(link_text), link_attributes.merge('href' => href)) + punctuation
  570 + content_tag(:a, h(link_text), link_attributes.merge('href' => href)) + punctuation.reverse.join('')
570 571 end
571 572 end
572 573 end
7 actionpack/test/template/text_helper_test.rb
@@ -452,6 +452,13 @@ def test_auto_link_with_options_hash
452 452 auto_link("Welcome to my new blog at http://www.myblog.com/. Please e-mail me at me@email.com.",
453 453 :link => :all, :html => { :class => "menu", :target => "_blank" })
454 454 end
  455 +
  456 + def test_auto_link_with_multiple_trailing_punctuations
  457 + url = "http://youtube.com"
  458 + url_result = generate_result(url)
  459 + assert_equal url_result, auto_link(url)
  460 + assert_equal "(link: #{url_result}).", auto_link("(link: #{url}).")
  461 + end
455 462
456 463 def test_cycle_class
457 464 value = Cycle.new("one", 2, "3")

0 comments on commit 9e08e19

Please sign in to comment.
Something went wrong with that request. Please try again.