rails_autolink 1.0.9 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc CHANGED
@@ -1,3 +1,7 @@
1
+ === 1.1.0 / 2013-05-02
2
+
3
+ * Ready for Rails 4.
4
+
1
5
  === 1.0.7 / 2012-04-27
2
6
 
3
7
  * Added support for non-latin characters in autolinked urls
data/Gemfile CHANGED
@@ -1,8 +1,6 @@
1
- source 'http://rubygems.org'
1
+ source 'https://rubygems.org'
2
2
 
3
3
  gem 'rails'
4
4
  gem 'arel'
5
5
  gem 'rack'
6
- gem 'hoe'
7
6
  gem 'minitest'
8
-
data/Gemfile.lock ADDED
@@ -0,0 +1,84 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ actionmailer (4.0.0.rc1)
5
+ actionpack (= 4.0.0.rc1)
6
+ mail (~> 2.5.3)
7
+ actionpack (4.0.0.rc1)
8
+ activesupport (= 4.0.0.rc1)
9
+ builder (~> 3.1.0)
10
+ erubis (~> 2.7.0)
11
+ rack (~> 1.5.2)
12
+ rack-test (~> 0.6.2)
13
+ activemodel (4.0.0.rc1)
14
+ activesupport (= 4.0.0.rc1)
15
+ builder (~> 3.1.0)
16
+ activerecord (4.0.0.rc1)
17
+ activemodel (= 4.0.0.rc1)
18
+ activerecord-deprecated_finders (~> 1.0.2)
19
+ activesupport (= 4.0.0.rc1)
20
+ arel (~> 4.0.0)
21
+ activerecord-deprecated_finders (1.0.2)
22
+ activesupport (4.0.0.rc1)
23
+ i18n (~> 0.6, >= 0.6.4)
24
+ minitest (~> 4.2)
25
+ multi_json (~> 1.3)
26
+ thread_safe (~> 0.1)
27
+ tzinfo (~> 0.3.37)
28
+ arel (4.0.0)
29
+ atomic (1.1.8)
30
+ builder (3.1.4)
31
+ erubis (2.7.0)
32
+ hike (1.2.2)
33
+ i18n (0.6.4)
34
+ mail (2.5.3)
35
+ i18n (>= 0.4.0)
36
+ mime-types (~> 1.16)
37
+ treetop (~> 1.4.8)
38
+ mime-types (1.23)
39
+ minitest (4.7.4)
40
+ multi_json (1.7.2)
41
+ polyglot (0.3.3)
42
+ rack (1.5.2)
43
+ rack-test (0.6.2)
44
+ rack (>= 1.0)
45
+ rails (4.0.0.rc1)
46
+ actionmailer (= 4.0.0.rc1)
47
+ actionpack (= 4.0.0.rc1)
48
+ activerecord (= 4.0.0.rc1)
49
+ activesupport (= 4.0.0.rc1)
50
+ bundler (>= 1.3.0, < 2.0)
51
+ railties (= 4.0.0.rc1)
52
+ sprockets-rails (~> 2.0.0.rc4)
53
+ railties (4.0.0.rc1)
54
+ actionpack (= 4.0.0.rc1)
55
+ activesupport (= 4.0.0.rc1)
56
+ rake (>= 0.8.7)
57
+ thor (>= 0.18.1, < 2.0)
58
+ rake (10.0.4)
59
+ sprockets (2.9.3)
60
+ hike (~> 1.2)
61
+ multi_json (~> 1.0)
62
+ rack (~> 1.0)
63
+ tilt (~> 1.1, != 1.3.0)
64
+ sprockets-rails (2.0.0.rc4)
65
+ actionpack (>= 3.0)
66
+ activesupport (>= 3.0)
67
+ sprockets (~> 2.8)
68
+ thor (0.18.1)
69
+ thread_safe (0.1.0)
70
+ atomic
71
+ tilt (1.4.0)
72
+ treetop (1.4.12)
73
+ polyglot
74
+ polyglot (>= 0.3.1)
75
+ tzinfo (0.3.37)
76
+
77
+ PLATFORMS
78
+ ruby
79
+
80
+ DEPENDENCIES
81
+ arel
82
+ minitest
83
+ rack
84
+ rails
@@ -0,0 +1,3 @@
1
+ rake gem
2
+ cd pkg
3
+ gem push rails_autolink-X.gem
Binary file
@@ -0,0 +1,26 @@
1
+ === 1.1.0 / 2013-05-02
2
+
3
+ * Ready for Rails 4.
4
+
5
+ === 1.0.7 / 2012-04-27
6
+
7
+ * Added support for non-latin characters in autolinked urls
8
+
9
+ === 1.0.6 / 2012-03-12
10
+
11
+ * Added sanitize_options arg
12
+
13
+ === 1.0.5 / 2012-01-27
14
+
15
+ * Update dependency to include rails 3.2.X
16
+
17
+ === 1.0.2 / 2011-06-18
18
+
19
+ * Compatibility with rails 3.1.0.rc4
20
+
21
+ === 1.0.0 / 2011-05-02
22
+
23
+ * 1 major enhancement
24
+
25
+ * Birthday!
26
+
@@ -0,0 +1,8 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'rails'
4
+ gem 'arel'
5
+ gem 'rack'
6
+ gem 'hoe'
7
+ gem 'minitest'
8
+
@@ -0,0 +1,75 @@
1
+ = rails_autolink
2
+
3
+ * http://github.com/tenderlove/rails_autolink
4
+
5
+ == DESCRIPTION:
6
+
7
+ This is an extraction of the `auto_link` method from rails. The `auto_link`
8
+ method was removed from Rails in version Rails 3.1. This gem is meant to
9
+ bridge the gap for people migrating.
10
+
11
+ == FEATURES:
12
+
13
+ By default auto_link returns sanitized html_safe strings.
14
+ This behaviour can be overriden setting the <tt>:sanitize</tt> option to false
15
+ (thus making it insecure if you don't have the content under control).
16
+
17
+ == SYNOPSIS:
18
+
19
+ require 'rails_autolink'
20
+
21
+ auto_link("Go to http://www.rubyonrails.org and say hello to david@loudthinking.com")
22
+ # => "Go to <a href=\"http://www.rubyonrails.org\">http://www.rubyonrails.org</a> and
23
+ # say hello to <a href=\"mailto:david@loudthinking.com\">david@loudthinking.com</a>"
24
+
25
+ auto_link("Visit http://www.loudthinking.com/ or e-mail david@loudthinking.com", :link => :urls)
26
+ # => "Visit <a href=\"http://www.loudthinking.com/\">http://www.loudthinking.com/</a>
27
+ # or e-mail david@loudthinking.com"
28
+
29
+ auto_link("Visit http://www.loudthinking.com/ or e-mail david@loudthinking.com", :link => :email_addresses)
30
+ # => "Visit http://www.loudthinking.com/ or e-mail <a href=\"mailto:david@loudthinking.com\">david@loudthinking.com</a>"
31
+
32
+ auto_link("Go to http://www.rubyonrails.org <script>Malicious code!</script>")
33
+ # => "Go to <a href=\"http://www.rubyonrails.org\">http://www.rubyonrails.org</a> "
34
+
35
+ auto_link("Go to http://www.rubyonrails.org <script>alert('Script!')</script>", :sanitize => false)
36
+ # => "Go to <a href=\"http://www.rubyonrails.org\">http://www.rubyonrails.org</a> <script>alert('Script!')</script>"
37
+
38
+ post_body = "Welcome to my new blog at http://www.myblog.com/. Please e-mail me at me@email.com."
39
+ auto_link(post_body, :html => { :target => '_blank' }) do |text|
40
+ truncate(text, :length => 15)
41
+ end
42
+ # => "Welcome to my new blog at <a href=\"http://www.myblog.com/\" target=\"_blank\">http://www.m...</a>.
43
+
44
+ == REQUIREMENTS:
45
+
46
+ * rails > 3.1
47
+
48
+ == INSTALL:
49
+
50
+ * gem install rails_autolink
51
+
52
+ == LICENSE:
53
+
54
+ (The MIT License)
55
+
56
+ Copyright (c) 2011 DHH
57
+
58
+ Permission is hereby granted, free of charge, to any person obtaining
59
+ a copy of this software and associated documentation files (the
60
+ 'Software'), to deal in the Software without restriction, including
61
+ without limitation the rights to use, copy, modify, merge, publish,
62
+ distribute, sublicense, and/or sell copies of the Software, and to
63
+ permit persons to whom the Software is furnished to do so, subject to
64
+ the following conditions:
65
+
66
+ The above copyright notice and this permission notice shall be
67
+ included in all copies or substantial portions of the Software.
68
+
69
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
70
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
71
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
72
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
73
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
74
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
75
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,21 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+
6
+ Hoe.plugins.delete :rubyforge
7
+ Hoe.plugin :minitest
8
+ Hoe.plugin :gemspec # `gem install hoe-gemspec`
9
+ Hoe.plugin :git # `gem install hoe-git`
10
+
11
+ Hoe.spec 'rails_autolink' do
12
+ developer('Aaron Patterson', 'aaron@tenderlovemaking.com')
13
+ developer('Juanjo Bazan', 'jjbazan@gmail.com')
14
+ developer('Akira Matsuda', 'ronnie@dio.jp')
15
+ self.readme_file = 'README.rdoc'
16
+ self.history_file = 'CHANGELOG.rdoc'
17
+ self.extra_rdoc_files = FileList['*.rdoc']
18
+ self.extra_deps << ['rails', '~> 3.1']
19
+ end
20
+
21
+ # vim: syntax=ruby
@@ -0,0 +1,11 @@
1
+ module RailsAutolink
2
+ VERSION = '1.0.7'
3
+
4
+ class Railtie < ::Rails::Railtie
5
+ initializer 'rails_autolink' do |app|
6
+ ActiveSupport.on_load(:action_view) do
7
+ require 'rails_autolink/helpers'
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,154 @@
1
+ # encoding: utf-8
2
+
3
+ module RailsAutolink
4
+ require 'active_support/core_ext/object/blank'
5
+ require 'active_support/core_ext/array/extract_options'
6
+ require 'active_support/core_ext/hash/reverse_merge'
7
+ require 'active_support/core_ext/hash/keys'
8
+
9
+ module ::ActionView
10
+ module Helpers # :nodoc:
11
+ module TextHelper
12
+ # Turns all URLs and e-mail addresses into clickable links. The <tt>:link</tt> option
13
+ # will limit what should be linked. You can add HTML attributes to the links using
14
+ # <tt>:html</tt>. Possible values for <tt>:link</tt> are <tt>:all</tt> (default),
15
+ # <tt>:email_addresses</tt>, and <tt>:urls</tt>. If a block is given, each URL and
16
+ # e-mail address is yielded and the result is used as the link text. By default the
17
+ # text given is sanitized, you can override this behaviour setting the
18
+ # <tt>:sanitize</tt> option to false, or you can add options to the sanitization of
19
+ # the text using the <tt>:sanitize_options</tt> option hash.
20
+ #
21
+ # ==== Examples
22
+ # auto_link("Go to http://www.rubyonrails.org and say hello to david@loudthinking.com")
23
+ # # => "Go to <a href=\"http://www.rubyonrails.org\">http://www.rubyonrails.org</a> and
24
+ # # say hello to <a href=\"mailto:david@loudthinking.com\">david@loudthinking.com</a>"
25
+ #
26
+ # auto_link("Visit http://www.loudthinking.com/ or e-mail david@loudthinking.com", :link => :urls)
27
+ # # => "Visit <a href=\"http://www.loudthinking.com/\">http://www.loudthinking.com/</a>
28
+ # # or e-mail david@loudthinking.com"
29
+ #
30
+ # auto_link("Visit http://www.loudthinking.com/ or e-mail david@loudthinking.com", :link => :email_addresses)
31
+ # # => "Visit http://www.loudthinking.com/ or e-mail <a href=\"mailto:david@loudthinking.com\">david@loudthinking.com</a>"
32
+ #
33
+ # post_body = "Welcome to my new blog at http://www.myblog.com/. Please e-mail me at me@email.com."
34
+ # auto_link(post_body, :html => { :target => '_blank' }) do |text|
35
+ # truncate(text, :length => 15)
36
+ # end
37
+ # # => "Welcome to my new blog at <a href=\"http://www.myblog.com/\" target=\"_blank\">http://www.m...</a>.
38
+ # Please e-mail me at <a href=\"mailto:me@email.com\">me@email.com</a>."
39
+ #
40
+ #
41
+ # You can still use <tt>auto_link</tt> with the old API that accepts the
42
+ # +link+ as its optional second parameter and the +html_options+ hash
43
+ # as its optional third parameter:
44
+ # post_body = "Welcome to my new blog at http://www.myblog.com/. Please e-mail me at me@email.com."
45
+ # auto_link(post_body, :urls)
46
+ # # => "Welcome to my new blog at <a href=\"http://www.myblog.com/\">http://www.myblog.com</a>.
47
+ # Please e-mail me at me@email.com."
48
+ #
49
+ # auto_link(post_body, :all, :target => "_blank")
50
+ # # => "Welcome to my new blog at <a href=\"http://www.myblog.com/\" target=\"_blank\">http://www.myblog.com</a>.
51
+ # Please e-mail me at <a href=\"mailto:me@email.com\">me@email.com</a>."
52
+ def auto_link(text, *args, &block)#link = :all, html = {}, &block)
53
+ return ''.html_safe if text.blank?
54
+
55
+ options = args.size == 2 ? {} : args.extract_options! # this is necessary because the old auto_link API has a Hash as its last parameter
56
+ unless args.empty?
57
+ options[:link] = args[0] || :all
58
+ options[:html] = args[1] || {}
59
+ end
60
+ options.reverse_merge!(:link => :all, :html => {})
61
+ sanitize = (options[:sanitize] != false)
62
+ sanitize_options = options[:sanitize_options] || {}
63
+ text = conditional_sanitize(text, sanitize, sanitize_options).to_str
64
+ case options[:link].to_sym
65
+ when :all then conditional_html_safe(auto_link_email_addresses(auto_link_urls(text, options[:html], options, &block), options[:html], &block), sanitize)
66
+ when :email_addresses then conditional_html_safe(auto_link_email_addresses(text, options[:html], &block), sanitize)
67
+ when :urls then conditional_html_safe(auto_link_urls(text, options[:html], options, &block), sanitize)
68
+ end
69
+ end
70
+
71
+ private
72
+
73
+ AUTO_LINK_RE = %r{
74
+ (?: ([0-9A-Za-z+.:-]+:)// | www\. )
75
+ [^\s<]+
76
+ }x
77
+
78
+ # regexps for determining context, used high-volume
79
+ AUTO_LINK_CRE = [/<[^>]+$/, /^[^>]*>/, /<a\b.*?>/i, /<\/a>/i]
80
+
81
+ AUTO_EMAIL_RE = /[\w.!#\$%+-]+@[\w-]+(?:\.[\w-]+)+/
82
+
83
+ BRACKETS = { ']' => '[', ')' => '(', '}' => '{' }
84
+ WORD_PATTERN = RUBY_VERSION < '1.9' ? '\w' : '\p{Word}'
85
+ # Turns all urls into clickable links. If a block is given, each url
86
+ # is yielded and the result is used as the link text.
87
+ def auto_link_urls(text, html_options = {}, options = {})
88
+ link_attributes = html_options.stringify_keys
89
+ text.gsub(AUTO_LINK_RE) do
90
+ scheme, href = $1, $&
91
+ punctuation = []
92
+
93
+ if auto_linked?($`, $')
94
+ # do not change string; URL is already linked
95
+ href
96
+ else
97
+ # don't include trailing punctuation character as part of the URL
98
+ while href.sub!(/[^#{WORD_PATTERN}\/-]$/, '')
99
+ punctuation.push $&
100
+ if opening = BRACKETS[punctuation.last] and href.scan(opening).size > href.scan(punctuation.last).size
101
+ href << punctuation.pop
102
+ break
103
+ end
104
+ end
105
+
106
+ link_text = block_given?? yield(href) : href
107
+ href = 'http://' + href unless scheme
108
+
109
+ unless options[:sanitize] == false
110
+ link_text = sanitize(link_text)
111
+ href = sanitize(href)
112
+ end
113
+ content_tag(:a, link_text, link_attributes.merge('href' => href), !!options[:sanitize]) + punctuation.reverse.join('')
114
+ end
115
+ end
116
+ end
117
+
118
+ # Turns all email addresses into clickable links. If a block is given,
119
+ # each email is yielded and the result is used as the link text.
120
+ def auto_link_email_addresses(text, html_options = {}, options = {})
121
+ text.gsub(AUTO_EMAIL_RE) do
122
+ text = $&
123
+
124
+ if auto_linked?($`, $')
125
+ text.html_safe
126
+ else
127
+ display_text = (block_given?) ? yield(text) : text
128
+
129
+ unless options[:sanitize] == false
130
+ text = sanitize(text)
131
+ display_text = sanitize(display_text) unless text == display_text
132
+ end
133
+ mail_to text, display_text, html_options
134
+ end
135
+ end
136
+ end
137
+
138
+ # Detects already linked context or position in the middle of a tag
139
+ def auto_linked?(left, right)
140
+ (left =~ AUTO_LINK_CRE[0] and right =~ AUTO_LINK_CRE[1]) or
141
+ (left.rindex(AUTO_LINK_CRE[2]) and $' !~ AUTO_LINK_CRE[3])
142
+ end
143
+
144
+ def conditional_sanitize(target, condition, sanitize_options = {})
145
+ condition ? sanitize(target, sanitize_options) : target
146
+ end
147
+
148
+ def conditional_html_safe(target, condition)
149
+ condition ? target.html_safe : target
150
+ end
151
+ end
152
+ end
153
+ end
154
+ end
@@ -0,0 +1,338 @@
1
+ # encoding: utf-8
2
+
3
+ require "minitest/autorun"
4
+ require "rails"
5
+ require "rails_autolink/helpers"
6
+ require 'erb'
7
+ require 'cgi'
8
+ require 'active_support/core_ext/class/attribute_accessors'
9
+ require 'action_pack'
10
+ require 'action_view/helpers/capture_helper'
11
+ require 'action_view/helpers/sanitize_helper'
12
+ require 'action_view/helpers/url_helper'
13
+ require 'action_view/helpers/tag_helper'
14
+ require 'active_support/core_ext/module/attribute_accessors'
15
+ require 'active_support/core_ext/string/encoding'
16
+ require 'action_dispatch/testing/assertions'
17
+ require 'action_view/helpers/text_helper'
18
+ require 'action_view/helpers/output_safety_helper'
19
+
20
+ class TestRailsAutolink < MiniTest::Unit::TestCase
21
+ include ActionView::Helpers::CaptureHelper
22
+ include ActionView::Helpers::TextHelper
23
+ include ActionView::Helpers::SanitizeHelper
24
+ include ActionView::Helpers::TagHelper
25
+ include ActionView::Helpers::UrlHelper
26
+ include ActionView::Helpers::OutputSafetyHelper
27
+ include ActionDispatch::Assertions::DomAssertions
28
+
29
+ def test_auto_link_within_tags
30
+ link_raw = 'http://www.rubyonrails.org/images/rails.png'
31
+ link_result = %Q(<img src="#{link_raw}" />)
32
+ assert_equal link_result, auto_link(link_result)
33
+ end
34
+
35
+ def test_auto_link_with_brackets
36
+ link1_raw = 'http://en.wikipedia.org/wiki/Sprite_(computer_graphics)'
37
+ link1_result = generate_result(link1_raw)
38
+ assert_equal link1_result, auto_link(link1_raw)
39
+ assert_equal "(link: #{link1_result})", auto_link("(link: #{link1_raw})")
40
+
41
+ link2_raw = 'http://en.wikipedia.org/wiki/Sprite_[computer_graphics]'
42
+ link2_result = generate_result(link2_raw)
43
+ assert_equal link2_result, auto_link(link2_raw)
44
+ assert_equal "[link: #{link2_result}]", auto_link("[link: #{link2_raw}]")
45
+
46
+ link3_raw = 'http://en.wikipedia.org/wiki/Sprite_{computer_graphics}'
47
+ link3_result = generate_result(link3_raw)
48
+ assert_equal link3_result, auto_link(link3_raw)
49
+ assert_equal "{link: #{link3_result}}", auto_link("{link: #{link3_raw}}")
50
+ end
51
+
52
+ def test_auto_link_with_options_hash
53
+ assert_dom_equal 'Welcome to my new blog at <a href="http://www.myblog.com/" class="menu" target="_blank">http://www.myblog.com/</a>. Please e-mail me at <a href="mailto:me@email.com" class="menu" target="_blank">me@email.com</a>.',
54
+ auto_link("Welcome to my new blog at http://www.myblog.com/. Please e-mail me at me@email.com.",
55
+ :link => :all, :html => { :class => "menu", :target => "_blank" })
56
+ end
57
+
58
+ def test_auto_link_with_multiple_trailing_punctuations
59
+ url = "http://youtube.com"
60
+ url_result = generate_result(url)
61
+ assert_equal url_result, auto_link(url)
62
+ assert_equal "(link: #{url_result}).", auto_link("(link: #{url}).")
63
+ end
64
+
65
+ def test_auto_link_with_block
66
+ url = "http://api.rubyonrails.com/Foo.html"
67
+ email = "fantabulous@shiznadel.ic"
68
+
69
+ assert_equal %(<p><a href="#{url}">#{url[0...7]}...</a><br /><a href="mailto:#{email}">#{email[0...7]}...</a><br /></p>), auto_link("<p>#{url}<br />#{email}<br /></p>") { |_url| truncate(_url, :length => 10) }
70
+ end
71
+
72
+ def test_auto_link_with_block_with_html
73
+ pic = "http://example.com/pic.png"
74
+ url = "http://example.com/album?a&b=c"
75
+
76
+ assert_equal %(My pic: <a href="#{pic}"><img src="#{pic}" width="160px"></a> -- full album here #{generate_result(url)}), auto_link("My pic: #{pic} -- full album here #{url}") { |link|
77
+ if link =~ /\.(jpg|gif|png|bmp|tif)$/i
78
+ raw %(<img src="#{link}" width="160px">)
79
+ else
80
+ link
81
+ end
82
+ }
83
+ end
84
+
85
+ def test_auto_link_should_sanitize_input_when_sanitize_option_is_not_false
86
+ link_raw = %{http://www.rubyonrails.com?id=1&num=2}
87
+ malicious_script = '<script>alert("malicious!")</script>'
88
+ assert_equal %{<a href="http://www.rubyonrails.com?id=1&num=2">http://www.rubyonrails.com?id=1&num=2</a>}, auto_link("#{link_raw}#{malicious_script}")
89
+ assert auto_link("#{link_raw}#{malicious_script}").html_safe?
90
+ end
91
+
92
+ def test_auto_link_should_sanitize_input_with_sanitize_options
93
+ link_raw = %{http://www.rubyonrails.com?id=1&num=2}
94
+ malicious_script = '<script>alert("malicious!")</script>'
95
+ text_with_attributes = %{<a href="http://ruby-lang-org" target="_blank" data-malicious="inject">Ruby</a>}
96
+
97
+ text_result = %{<a class="big" href="http://www.rubyonrails.com?id=1&num=2">http://www.rubyonrails.com?id=1&num=2</a><a href="http://ruby-lang-org" target="_blank">Ruby</a>}
98
+ assert_equal text_result, auto_link("#{link_raw}#{malicious_script}#{text_with_attributes}",
99
+ :sanitize_options => {:attributes => ["target", "href"]},
100
+ :html => {:class => 'big'})
101
+
102
+ assert auto_link("#{link_raw}#{malicious_script}#{text_with_attributes}",
103
+ :sanitize_options => {:attributes => ["target", "href"]},
104
+ :html => {:class => 'big'}).html_safe?
105
+ end
106
+
107
+ def test_auto_link_should_not_sanitize_input_when_sanitize_option_is_false
108
+ link_raw = %{http://www.rubyonrails.com?id=1&num=2}
109
+ malicious_script = '<script>alert("malicious!")</script>'
110
+
111
+ assert_equal %{<a href="http://www.rubyonrails.com?id=1&num=2">http://www.rubyonrails.com?id=1&num=2</a><script>alert("malicious!")</script>}, auto_link("#{link_raw}#{malicious_script}", :sanitize => false)
112
+ assert !auto_link("#{link_raw}#{malicious_script}", :sanitize => false).html_safe?
113
+ end
114
+
115
+ def test_auto_link_other_protocols
116
+ ftp_raw = 'ftp://example.com/file.txt'
117
+ assert_equal %(Download #{generate_result(ftp_raw)}), auto_link("Download #{ftp_raw}")
118
+
119
+ file_scheme = 'file:///home/username/RomeoAndJuliet.pdf'
120
+ z39_scheme = 'z39.50r://host:696/db'
121
+ chrome_scheme = 'chrome://package/section/path'
122
+ view_source = 'view-source:http://en.wikipedia.org/wiki/URI_scheme'
123
+ assert_equal generate_result(file_scheme), auto_link(file_scheme)
124
+ assert_equal generate_result(z39_scheme), auto_link(z39_scheme)
125
+ assert_equal generate_result(chrome_scheme), auto_link(chrome_scheme)
126
+ assert_equal generate_result(view_source), auto_link(view_source)
127
+ end
128
+
129
+ def test_auto_link_already_linked
130
+ linked1 = generate_result('Ruby On Rails', 'http://www.rubyonrails.com')
131
+ linked2 = %('<a href="http://www.example.com">www.example.com</a>')
132
+ linked3 = %('<a href="http://www.example.com" rel="nofollow">www.example.com</a>')
133
+ linked4 = %('<a href="http://www.example.com"><b>www.example.com</b></a>')
134
+ linked5 = %('<a href="#close">close</a> <a href="http://www.example.com"><b>www.example.com</b></a>')
135
+ linked6 = %('<a href="#close">close</a> <a href="http://www.example.com" target="_blank" data-ruby="ror"><b>www.example.com</b></a>')
136
+ assert_equal linked1, auto_link(linked1)
137
+ assert_equal linked2, auto_link(linked2)
138
+ assert_equal linked3, auto_link(linked3, :sanitize => false)
139
+ assert_equal linked4, auto_link(linked4)
140
+ assert_equal linked5, auto_link(linked5)
141
+ assert_equal linked6, auto_link(linked6, :sanitize_options => {:attributes => ["href", "target", "data-ruby"]})
142
+
143
+ linked_email = %Q(<a href="mailto:david@loudthinking.com">Mail me</a>)
144
+ assert_equal linked_email, auto_link(linked_email)
145
+ end
146
+
147
+
148
+ def test_auto_link_at_eol
149
+ url1 = "http://api.rubyonrails.com/Foo.html"
150
+ url2 = "http://www.ruby-doc.org/core/Bar.html"
151
+
152
+ assert_equal %(<p><a href="#{url1}">#{url1}</a><br /><a href="#{url2}">#{url2}</a><br /></p>), auto_link("<p>#{url1}<br />#{url2}<br /></p>")
153
+ end
154
+
155
+ def test_auto_link_should_be_html_safe
156
+ email_raw = 'santiago@wyeworks.com'
157
+ link_raw = 'http://www.rubyonrails.org'
158
+ malicious_script = '<script>alert("malicious!")</script>'
159
+
160
+ assert auto_link(nil).html_safe?, 'should be html safe'
161
+ assert auto_link('').html_safe?, 'should be html safe'
162
+ assert auto_link("#{link_raw} #{link_raw} #{link_raw}").html_safe?, 'should be html safe'
163
+ assert auto_link("hello #{email_raw}").html_safe?, 'should be html safe'
164
+ assert auto_link("hello #{email_raw} #{malicious_script}").html_safe?, 'should be html safe'
165
+ end
166
+
167
+ def test_auto_link_should_not_be_html_safe_when_sanitize_option_false
168
+ email_raw = 'santiago@wyeworks.com'
169
+ link_raw = 'http://www.rubyonrails.org'
170
+
171
+ assert !auto_link("hello", :sanitize => false).html_safe?, 'should not be html safe'
172
+ assert !auto_link("#{link_raw} #{link_raw} #{link_raw}", :sanitize => false).html_safe?, 'should not be html safe'
173
+ assert !auto_link("hello #{email_raw}", :sanitize => false).html_safe?, 'should not be html safe'
174
+ end
175
+
176
+ def test_auto_link_email_address
177
+ email_raw = 'aaron@tenderlovemaking.com'
178
+ email_result = %{<a href="mailto:#{email_raw}">#{email_raw}</a>}
179
+ assert !auto_link_email_addresses(email_result).html_safe?, 'should not be html safe'
180
+ end
181
+
182
+ def test_auto_link
183
+ email_raw = 'david@loudthinking.com'
184
+ email_result = %{<a href="mailto:#{email_raw}">#{email_raw}</a>}
185
+ link_raw = 'http://www.rubyonrails.com'
186
+ link_result = generate_result(link_raw)
187
+ link_result_with_options = %{<a href="#{link_raw}" target="_blank">#{link_raw}</a>}
188
+
189
+ assert_equal '', auto_link(nil)
190
+ assert_equal '', auto_link('')
191
+ assert_equal "#{link_result} #{link_result} #{link_result}", auto_link("#{link_raw} #{link_raw} #{link_raw}")
192
+
193
+ assert_equal %(hello #{email_result}), auto_link("hello #{email_raw}", :email_addresses)
194
+ assert_equal %(Go to #{link_result}), auto_link("Go to #{link_raw}", :urls)
195
+ assert_equal %(Go to #{link_raw}), auto_link("Go to #{link_raw}", :email_addresses)
196
+ assert_equal %(Go to #{link_result} and say hello to #{email_result}), auto_link("Go to #{link_raw} and say hello to #{email_raw}")
197
+ assert_equal %(<p>Link #{link_result}</p>), auto_link("<p>Link #{link_raw}</p>")
198
+ assert_equal %(<p>#{link_result} Link</p>), auto_link("<p>#{link_raw} Link</p>")
199
+ assert_equal %(<p>Link #{link_result_with_options}</p>), auto_link("<p>Link #{link_raw}</p>", :all, {:target => "_blank"})
200
+ assert_equal %(Go to #{link_result}.), auto_link(%(Go to #{link_raw}.))
201
+ assert_equal %(<p>Go to #{link_result}, then say hello to #{email_result}.</p>), auto_link(%(<p>Go to #{link_raw}, then say hello to #{email_raw}.</p>))
202
+ assert_equal %(#{link_result} #{link_result}), auto_link(%(#{link_result} #{link_raw}))
203
+
204
+ email2_raw = '+david@loudthinking.com'
205
+ email2_result = %{<a href="mailto:#{email2_raw}">#{email2_raw}</a>}
206
+ assert_equal email2_result, auto_link(email2_raw)
207
+
208
+ email3_raw = '+david@loudthinking.com'
209
+ email3_result = %{<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;+%64%61%76%69%64@%6c%6f%75%64%74%68%69%6e%6b%69%6e%67.%63%6f%6d">#{email3_raw}</a>}
210
+ assert_equal email3_result, auto_link(email3_raw, :all, :encode => :hex)
211
+ assert_equal email3_result, auto_link(email3_raw, :email_addresses, :encode => :hex)
212
+
213
+ link2_raw = 'www.rubyonrails.com'
214
+ link2_result = generate_result(link2_raw, "http://#{link2_raw}")
215
+ assert_equal %(Go to #{link2_result}), auto_link("Go to #{link2_raw}", :urls)
216
+ assert_equal %(Go to #{link2_raw}), auto_link("Go to #{link2_raw}", :email_addresses)
217
+ assert_equal %(<p>Link #{link2_result}</p>), auto_link("<p>Link #{link2_raw}</p>")
218
+ assert_equal %(<p>#{link2_result} Link</p>), auto_link("<p>#{link2_raw} Link</p>")
219
+ assert_equal %(Go to #{link2_result}.), auto_link(%(Go to #{link2_raw}.))
220
+ assert_equal %(<p>Say hello to #{email_result}, then go to #{link2_result}.</p>), auto_link(%(<p>Say hello to #{email_raw}, then go to #{link2_raw}.</p>))
221
+
222
+ link3_raw = 'http://manuals.ruby-on-rails.com/read/chapter.need_a-period/103#page281'
223
+ link3_result = generate_result(link3_raw)
224
+ assert_equal %(Go to #{link3_result}), auto_link("Go to #{link3_raw}", :urls)
225
+ assert_equal %(Go to #{link3_raw}), auto_link("Go to #{link3_raw}", :email_addresses)
226
+ assert_equal %(<p>Link #{link3_result}</p>), auto_link("<p>Link #{link3_raw}</p>")
227
+ assert_equal %(<p>#{link3_result} Link</p>), auto_link("<p>#{link3_raw} Link</p>")
228
+ assert_equal %(Go to #{link3_result}.), auto_link(%(Go to #{link3_raw}.))
229
+ assert_equal %(<p>Go to #{link3_result}. Seriously, #{link3_result}? I think I'll say hello to #{email_result}. Instead.</p>),
230
+ auto_link(%(<p>Go to #{link3_raw}. Seriously, #{link3_raw}? I think I'll say hello to #{email_raw}. Instead.</p>))
231
+
232
+ link4_raw = 'http://foo.example.com/controller/action?parm=value&p2=v2#anchor123'
233
+ link4_result = generate_result(link4_raw)
234
+ assert_equal %(<p>Link #{link4_result}</p>), auto_link("<p>Link #{link4_raw}</p>")
235
+ assert_equal %(<p>#{link4_result} Link</p>), auto_link("<p>#{link4_raw} Link</p>")
236
+
237
+ link5_raw = 'http://foo.example.com:3000/controller/action'
238
+ link5_result = generate_result(link5_raw)
239
+ assert_equal %(<p>#{link5_result} Link</p>), auto_link("<p>#{link5_raw} Link</p>")
240
+
241
+ link6_raw = 'http://foo.example.com:3000/controller/action+pack'
242
+ link6_result = generate_result(link6_raw)
243
+ assert_equal %(<p>#{link6_result} Link</p>), auto_link("<p>#{link6_raw} Link</p>")
244
+
245
+ link7_raw = 'http://foo.example.com/controller/action?parm=value&p2=v2#anchor-123'
246
+ link7_result = generate_result(link7_raw)
247
+ assert_equal %(<p>#{link7_result} Link</p>), auto_link("<p>#{link7_raw} Link</p>")
248
+
249
+ link8_raw = 'http://foo.example.com:3000/controller/action.html'
250
+ link8_result = generate_result(link8_raw)
251
+ assert_equal %(Go to #{link8_result}), auto_link("Go to #{link8_raw}", :urls)
252
+ assert_equal %(Go to #{link8_raw}), auto_link("Go to #{link8_raw}", :email_addresses)
253
+ assert_equal %(<p>Link #{link8_result}</p>), auto_link("<p>Link #{link8_raw}</p>")
254
+ assert_equal %(<p>#{link8_result} Link</p>), auto_link("<p>#{link8_raw} Link</p>")
255
+ assert_equal %(Go to #{link8_result}.), auto_link(%(Go to #{link8_raw}.))
256
+ assert_equal %(<p>Go to #{link8_result}. Seriously, #{link8_result}? I think I'll say hello to #{email_result}. Instead.</p>),
257
+ auto_link(%(<p>Go to #{link8_raw}. Seriously, #{link8_raw}? I think I'll say hello to #{email_raw}. Instead.</p>))
258
+
259
+ link9_raw = 'http://business.timesonline.co.uk/article/0,,9065-2473189,00.html'
260
+ link9_result = generate_result(link9_raw)
261
+ assert_equal %(Go to #{link9_result}), auto_link("Go to #{link9_raw}", :urls)
262
+ assert_equal %(Go to #{link9_raw}), auto_link("Go to #{link9_raw}", :email_addresses)
263
+ assert_equal %(<p>Link #{link9_result}</p>), auto_link("<p>Link #{link9_raw}</p>")
264
+ assert_equal %(<p>#{link9_result} Link</p>), auto_link("<p>#{link9_raw} Link</p>")
265
+ assert_equal %(Go to #{link9_result}.), auto_link(%(Go to #{link9_raw}.))
266
+ assert_equal %(<p>Go to #{link9_result}. Seriously, #{link9_result}? I think I'll say hello to #{email_result}. Instead.</p>),
267
+ auto_link(%(<p>Go to #{link9_raw}. Seriously, #{link9_raw}? I think I'll say hello to #{email_raw}. Instead.</p>))
268
+
269
+ link10_raw = 'http://www.mail-archive.com/ruby-talk@ruby-lang.org/'
270
+ link10_result = generate_result(link10_raw)
271
+ assert_equal %(<p>#{link10_result} Link</p>), auto_link("<p>#{link10_raw} Link</p>")
272
+
273
+ link11_raw = 'http://asakusa.rubyist.net/'
274
+ link11_result = generate_result(link11_raw)
275
+ with_kcode 'u' do
276
+ assert_equal %(浅草.rbの公式サイトはこちら#{link11_result}), auto_link("浅草.rbの公式サイトはこちら#{link11_raw}")
277
+ end
278
+ end
279
+
280
+ def test_auto_link_parsing
281
+ urls = %w(
282
+ http://www.rubyonrails.com
283
+ http://www.rubyonrails.com:80
284
+ http://www.rubyonrails.com/~minam
285
+ https://www.rubyonrails.com/~minam
286
+ http://www.rubyonrails.com/~minam/url%20with%20spaces
287
+ http://www.rubyonrails.com/foo.cgi?something=here
288
+ http://www.rubyonrails.com/foo.cgi?something=here&and=here
289
+ http://www.rubyonrails.com/contact;new
290
+ http://www.rubyonrails.com/contact;new%20with%20spaces
291
+ http://www.rubyonrails.com/contact;new?with=query&string=params
292
+ http://www.rubyonrails.com/~minam/contact;new?with=query&string=params
293
+ http://en.wikipedia.org/wiki/Wikipedia:Today%27s_featured_picture_%28animation%29/January_20%2C_2007
294
+ http://www.mail-archive.com/rails@lists.rubyonrails.org/
295
+ http://www.amazon.com/Testing-Equal-Sign-In-Path/ref=pd_bbs_sr_1?ie=UTF8&s=books&qid=1198861734&sr=8-1
296
+ http://en.wikipedia.org/wiki/Texas_hold'em
297
+ https://www.google.com/doku.php?id=gps:resource:scs:start
298
+ http://connect.oraclecorp.com/search?search[q]=green+france&search[type]=Group
299
+ http://of.openfoundry.org/projects/492/download#4th.Release.3
300
+ 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
301
+ http://около.кола/колокола
302
+ )
303
+
304
+ urls.each do |url|
305
+ assert_equal generate_result(url), auto_link(url)
306
+ end
307
+ end
308
+
309
+ private
310
+ def generate_result(link_text, href = nil, escape = false)
311
+ href ||= link_text
312
+ if escape
313
+ %{<a href="#{CGI::escapeHTML href}">#{CGI::escapeHTML link_text}</a>}
314
+ else
315
+ %{<a href="#{href}">#{link_text}</a>}
316
+ end
317
+ end
318
+
319
+ # from ruby core
320
+ def build_message(head, template=nil, *arguments)
321
+ template &&= template.chomp
322
+ template.gsub(/\?/) { mu_pp(arguments.shift) }
323
+ end
324
+
325
+ # Temporarily replaces KCODE for the block
326
+ def with_kcode(kcode)
327
+ if RUBY_VERSION < '1.9'
328
+ old_kcode, $KCODE = $KCODE, kcode
329
+ begin
330
+ yield
331
+ ensure
332
+ $KCODE = old_kcode
333
+ end
334
+ else
335
+ yield
336
+ end
337
+ end
338
+ end