friendlyfashion-rails_autolink 1.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,22 @@
1
+ === 1.0.7 / 2012-04-27
2
+
3
+ * Added support for non-latin characters in autolinked urls
4
+
5
+ === 1.0.6 / 2012-03-12
6
+
7
+ * Added sanitize_options arg
8
+
9
+ === 1.0.5 / 2012-01-27
10
+
11
+ * Update dependency to include rails 3.2.X
12
+
13
+ === 1.0.2 / 2011-06-18
14
+
15
+ * Compatibility with rails 3.1.0.rc4
16
+
17
+ === 1.0.0 / 2011-05-02
18
+
19
+ * 1 major enhancement
20
+
21
+ * Birthday!
22
+
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'rails'
4
+ gem 'arel'
5
+ gem 'rack'
6
+ gem 'minitest'
@@ -0,0 +1,89 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ actionmailer (3.2.7)
5
+ actionpack (= 3.2.7)
6
+ mail (~> 2.4.4)
7
+ actionpack (3.2.7)
8
+ activemodel (= 3.2.7)
9
+ activesupport (= 3.2.7)
10
+ builder (~> 3.0.0)
11
+ erubis (~> 2.7.0)
12
+ journey (~> 1.0.4)
13
+ rack (~> 1.4.0)
14
+ rack-cache (~> 1.2)
15
+ rack-test (~> 0.6.1)
16
+ sprockets (~> 2.1.3)
17
+ activemodel (3.2.7)
18
+ activesupport (= 3.2.7)
19
+ builder (~> 3.0.0)
20
+ activerecord (3.2.7)
21
+ activemodel (= 3.2.7)
22
+ activesupport (= 3.2.7)
23
+ arel (~> 3.0.2)
24
+ tzinfo (~> 0.3.29)
25
+ activeresource (3.2.7)
26
+ activemodel (= 3.2.7)
27
+ activesupport (= 3.2.7)
28
+ activesupport (3.2.7)
29
+ i18n (~> 0.6)
30
+ multi_json (~> 1.0)
31
+ arel (3.0.2)
32
+ builder (3.0.0)
33
+ erubis (2.7.0)
34
+ hike (1.2.1)
35
+ i18n (0.6.0)
36
+ journey (1.0.4)
37
+ json (1.7.4)
38
+ mail (2.4.4)
39
+ i18n (>= 0.4.0)
40
+ mime-types (~> 1.16)
41
+ treetop (~> 1.4.8)
42
+ mime-types (1.19)
43
+ minitest (3.3.0)
44
+ multi_json (1.3.6)
45
+ polyglot (0.3.3)
46
+ rack (1.4.1)
47
+ rack-cache (1.2)
48
+ rack (>= 0.4)
49
+ rack-ssl (1.3.2)
50
+ rack
51
+ rack-test (0.6.1)
52
+ rack (>= 1.0)
53
+ rails (3.2.7)
54
+ actionmailer (= 3.2.7)
55
+ actionpack (= 3.2.7)
56
+ activerecord (= 3.2.7)
57
+ activeresource (= 3.2.7)
58
+ activesupport (= 3.2.7)
59
+ bundler (~> 1.0)
60
+ railties (= 3.2.7)
61
+ railties (3.2.7)
62
+ actionpack (= 3.2.7)
63
+ activesupport (= 3.2.7)
64
+ rack-ssl (~> 1.3.2)
65
+ rake (>= 0.8.7)
66
+ rdoc (~> 3.4)
67
+ thor (>= 0.14.6, < 2.0)
68
+ rake (0.9.2.2)
69
+ rdoc (3.12)
70
+ json (~> 1.4)
71
+ sprockets (2.1.3)
72
+ hike (~> 1.2)
73
+ rack (~> 1.0)
74
+ tilt (~> 1.1, != 1.3.0)
75
+ thor (0.15.4)
76
+ tilt (1.3.3)
77
+ treetop (1.4.10)
78
+ polyglot
79
+ polyglot (>= 0.3.1)
80
+ tzinfo (0.3.33)
81
+
82
+ PLATFORMS
83
+ ruby
84
+
85
+ DEPENDENCIES
86
+ arel
87
+ minitest
88
+ rack
89
+ rails
@@ -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,8 @@
1
+ # Setup some testing
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << 'test'
7
+ t.pattern = 'test/test_*.rb'
8
+ end
@@ -0,0 +1,11 @@
1
+ module RailsAutolink
2
+ VERSION = '1.0.9'
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,161 @@
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
+ (?: ((?:ed2k|ftp|http|https|irc|mailto|news|gopher|nntp|telnet|webcal|xmpp|callto|feed|svn|urn|aim|rsync|tag|ssh|sftp|rtsp|afs):)// | 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
+
85
+ WORD_PATTERN = RUBY_VERSION < '1.9' ? '\w' : '\p{Word}'
86
+
87
+ # Turns all urls into clickable links. If a block is given, each url
88
+ # is yielded and the result is used as the link text.
89
+ def auto_link_urls(text, html_options = {}, options = {})
90
+ link_attributes = html_options.stringify_keys
91
+ text.gsub(AUTO_LINK_RE) do
92
+ scheme, href = $1, $&
93
+ punctuation = []
94
+
95
+ if auto_linked?($`, $')
96
+ # do not change string; URL is already linked
97
+ href
98
+ else
99
+ # don't include trailing punctuation character as part of the URL
100
+ while href.sub!(/[^#{WORD_PATTERN}\/-]$/, '')
101
+ punctuation.push $&
102
+ if opening = BRACKETS[punctuation.last] and href.scan(opening).size > href.scan(punctuation.last).size
103
+ href << punctuation.pop
104
+ break
105
+ end
106
+ end
107
+
108
+ link_text = block_given?? yield(href) : href
109
+ if link_text.is_a?(Hash)
110
+ href = link_text[:href] || href
111
+ link_text = link_text[:text] || href
112
+ else
113
+ href = 'http://' + href unless scheme
114
+ end
115
+
116
+ unless options[:sanitize] == false
117
+ link_text = sanitize(link_text)
118
+ href = sanitize(href)
119
+ end
120
+ content_tag(:a, link_text, link_attributes.merge('href' => href), !!options[:sanitize]) + punctuation.reverse.join('')
121
+ end
122
+ end
123
+ end
124
+
125
+ # Turns all email addresses into clickable links. If a block is given,
126
+ # each email is yielded and the result is used as the link text.
127
+ def auto_link_email_addresses(text, html_options = {}, options = {})
128
+ text.gsub(AUTO_EMAIL_RE) do
129
+ text = $&
130
+
131
+ if auto_linked?($`, $')
132
+ text.html_safe
133
+ else
134
+ display_text = (block_given?) ? yield(text) : text
135
+
136
+ unless options[:sanitize] == false
137
+ text = sanitize(text)
138
+ display_text = sanitize(display_text) unless text == display_text
139
+ end
140
+ mail_to text, display_text, html_options
141
+ end
142
+ end
143
+ end
144
+
145
+ # Detects already linked context or position in the middle of a tag
146
+ def auto_linked?(left, right)
147
+ (left =~ AUTO_LINK_CRE[0] and right =~ AUTO_LINK_CRE[1]) or
148
+ (left.rindex(AUTO_LINK_CRE[2]) and $' !~ AUTO_LINK_CRE[3])
149
+ end
150
+
151
+ def conditional_sanitize(target, condition, sanitize_options = {})
152
+ condition ? sanitize(target, sanitize_options) : target
153
+ end
154
+
155
+ def conditional_html_safe(target, condition)
156
+ condition ? target.html_safe : target
157
+ end
158
+ end
159
+ end
160
+ end
161
+ end
@@ -0,0 +1,11 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'friendlyfashion-rails_autolink'
3
+ s.version = '1.0.11'
4
+ s.authors = ["Laurynas Butkus", "Tomas Didziokas", "Justas Janauskas", "Edvinas Bartkus"]
5
+ s.email = ["laurynas.butkus@gmail.com", "tomas.did@gmail.com", "jjanauskas@gmail.com", "edvinas.bartkus@gmail.com"]
6
+ s.summary = 'This is an extraction of the `auto_link` method from rails. The `auto_link` method was removed from Rails in version Rails 3.1. This gem is meant to bridge the gap for people migrating.'
7
+
8
+ s.add_dependency 'rails', '> 3.1'
9
+
10
+ s.files = Dir["#{File.dirname(__FILE__)}/**/*"]
11
+ end
@@ -0,0 +1,347 @@
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_block_which_returns_hash
53
+ url = 'http://www.google.com/'
54
+ result = auto_link(url) do |it|
55
+ { text: "www.facebook.com", href: "http://www.facebook.com" }
56
+ end
57
+
58
+ assert_equal '<a href="http://www.facebook.com">www.facebook.com</a>', result
59
+ end
60
+
61
+ def test_auto_link_with_options_hash
62
+ 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>.',
63
+ auto_link("Welcome to my new blog at http://www.myblog.com/. Please e-mail me at me@email.com.",
64
+ :link => :all, :html => { :class => "menu", :target => "_blank" })
65
+ end
66
+
67
+ def test_auto_link_with_multiple_trailing_punctuations
68
+ url = "http://youtube.com"
69
+ url_result = generate_result(url)
70
+ assert_equal url_result, auto_link(url)
71
+ assert_equal "(link: #{url_result}).", auto_link("(link: #{url}).")
72
+ end
73
+
74
+ def test_auto_link_with_block
75
+ url = "http://api.rubyonrails.com/Foo.html"
76
+ email = "fantabulous@shiznadel.ic"
77
+
78
+ 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) }
79
+ end
80
+
81
+ def test_auto_link_with_block_with_html
82
+ pic = "http://example.com/pic.png"
83
+ url = "http://example.com/album?a&b=c"
84
+
85
+ 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|
86
+ if link =~ /\.(jpg|gif|png|bmp|tif)$/i
87
+ raw %(<img src="#{link}" width="160px">)
88
+ else
89
+ link
90
+ end
91
+ }
92
+ end
93
+
94
+ def test_auto_link_should_sanitize_input_when_sanitize_option_is_not_false
95
+ link_raw = %{http://www.rubyonrails.com?id=1&num=2}
96
+ malicious_script = '<script>alert("malicious!")</script>'
97
+ 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}")
98
+ assert auto_link("#{link_raw}#{malicious_script}").html_safe?
99
+ end
100
+
101
+ def test_auto_link_should_sanitize_input_with_sanitize_options
102
+ link_raw = %{http://www.rubyonrails.com?id=1&num=2}
103
+ malicious_script = '<script>alert("malicious!")</script>'
104
+ text_with_attributes = %{<a href="http://ruby-lang-org" target="_blank" data-malicious="inject">Ruby</a>}
105
+
106
+ 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>}
107
+ assert_equal text_result, auto_link("#{link_raw}#{malicious_script}#{text_with_attributes}",
108
+ :sanitize_options => {:attributes => ["target", "href"]},
109
+ :html => {:class => 'big'})
110
+
111
+ assert auto_link("#{link_raw}#{malicious_script}#{text_with_attributes}",
112
+ :sanitize_options => {:attributes => ["target", "href"]},
113
+ :html => {:class => 'big'}).html_safe?
114
+ end
115
+
116
+ def test_auto_link_should_not_sanitize_input_when_sanitize_option_is_false
117
+ link_raw = %{http://www.rubyonrails.com?id=1&num=2}
118
+ malicious_script = '<script>alert("malicious!")</script>'
119
+
120
+ 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)
121
+ assert !auto_link("#{link_raw}#{malicious_script}", :sanitize => false).html_safe?
122
+ end
123
+
124
+ def test_auto_link_other_protocols
125
+ ftp_raw = 'ftp://example.com/file.txt'
126
+ assert_equal %(Download #{generate_result(ftp_raw)}), auto_link("Download #{ftp_raw}")
127
+
128
+ file_scheme = 'file:///home/username/RomeoAndJuliet.pdf'
129
+ z39_scheme = 'z39.50r://host:696/db'
130
+ chrome_scheme = 'chrome://package/section/path'
131
+ view_source = 'view-source:http://en.wikipedia.org/wiki/URI_scheme'
132
+ assert_equal generate_result(file_scheme), auto_link(file_scheme)
133
+ assert_equal generate_result(z39_scheme), auto_link(z39_scheme)
134
+ assert_equal generate_result(chrome_scheme), auto_link(chrome_scheme)
135
+ assert_equal generate_result(view_source), auto_link(view_source)
136
+ end
137
+
138
+ def test_auto_link_already_linked
139
+ linked1 = generate_result('Ruby On Rails', 'http://www.rubyonrails.com')
140
+ linked2 = %('<a href="http://www.example.com">www.example.com</a>')
141
+ linked3 = %('<a href="http://www.example.com" rel="nofollow">www.example.com</a>')
142
+ linked4 = %('<a href="http://www.example.com"><b>www.example.com</b></a>')
143
+ linked5 = %('<a href="#close">close</a> <a href="http://www.example.com"><b>www.example.com</b></a>')
144
+ linked6 = %('<a href="#close">close</a> <a href="http://www.example.com" target="_blank" data-ruby="ror"><b>www.example.com</b></a>')
145
+ assert_equal linked1, auto_link(linked1)
146
+ assert_equal linked2, auto_link(linked2)
147
+ assert_equal linked3, auto_link(linked3, :sanitize => false)
148
+ assert_equal linked4, auto_link(linked4)
149
+ assert_equal linked5, auto_link(linked5)
150
+ assert_equal linked6, auto_link(linked6, :sanitize_options => {:attributes => ["href", "target", "data-ruby"]})
151
+
152
+ linked_email = %Q(<a href="mailto:david@loudthinking.com">Mail me</a>)
153
+ assert_equal linked_email, auto_link(linked_email)
154
+ end
155
+
156
+
157
+ def test_auto_link_at_eol
158
+ url1 = "http://api.rubyonrails.com/Foo.html"
159
+ url2 = "http://www.ruby-doc.org/core/Bar.html"
160
+
161
+ assert_equal %(<p><a href="#{url1}">#{url1}</a><br /><a href="#{url2}">#{url2}</a><br /></p>), auto_link("<p>#{url1}<br />#{url2}<br /></p>")
162
+ end
163
+
164
+ def test_auto_link_should_be_html_safe
165
+ email_raw = 'santiago@wyeworks.com'
166
+ link_raw = 'http://www.rubyonrails.org'
167
+ malicious_script = '<script>alert("malicious!")</script>'
168
+
169
+ assert auto_link(nil).html_safe?, 'should be html safe'
170
+ assert auto_link('').html_safe?, 'should be html safe'
171
+ assert auto_link("#{link_raw} #{link_raw} #{link_raw}").html_safe?, 'should be html safe'
172
+ assert auto_link("hello #{email_raw}").html_safe?, 'should be html safe'
173
+ assert auto_link("hello #{email_raw} #{malicious_script}").html_safe?, 'should be html safe'
174
+ end
175
+
176
+ def test_auto_link_should_not_be_html_safe_when_sanitize_option_false
177
+ email_raw = 'santiago@wyeworks.com'
178
+ link_raw = 'http://www.rubyonrails.org'
179
+
180
+ assert !auto_link("hello", :sanitize => false).html_safe?, 'should not be html safe'
181
+ assert !auto_link("#{link_raw} #{link_raw} #{link_raw}", :sanitize => false).html_safe?, 'should not be html safe'
182
+ assert !auto_link("hello #{email_raw}", :sanitize => false).html_safe?, 'should not be html safe'
183
+ end
184
+
185
+ def test_auto_link_email_address
186
+ email_raw = 'aaron@tenderlovemaking.com'
187
+ email_result = %{<a href="mailto:#{email_raw}">#{email_raw}</a>}
188
+ assert !auto_link_email_addresses(email_result).html_safe?, 'should not be html safe'
189
+ end
190
+
191
+ def test_auto_link
192
+ email_raw = 'david@loudthinking.com'
193
+ email_result = %{<a href="mailto:#{email_raw}">#{email_raw}</a>}
194
+ link_raw = 'http://www.rubyonrails.com'
195
+ link_result = generate_result(link_raw)
196
+ link_result_with_options = %{<a href="#{link_raw}" target="_blank">#{link_raw}</a>}
197
+
198
+ assert_equal '', auto_link(nil)
199
+ assert_equal '', auto_link('')
200
+ assert_equal "#{link_result} #{link_result} #{link_result}", auto_link("#{link_raw} #{link_raw} #{link_raw}")
201
+
202
+ assert_equal %(hello #{email_result}), auto_link("hello #{email_raw}", :email_addresses)
203
+ assert_equal %(Go to #{link_result}), auto_link("Go to #{link_raw}", :urls)
204
+ assert_equal %(Go to #{link_raw}), auto_link("Go to #{link_raw}", :email_addresses)
205
+ assert_equal %(Go to #{link_result} and say hello to #{email_result}), auto_link("Go to #{link_raw} and say hello to #{email_raw}")
206
+ assert_equal %(<p>Link #{link_result}</p>), auto_link("<p>Link #{link_raw}</p>")
207
+ assert_equal %(<p>#{link_result} Link</p>), auto_link("<p>#{link_raw} Link</p>")
208
+ assert_equal %(<p>Link #{link_result_with_options}</p>), auto_link("<p>Link #{link_raw}</p>", :all, {:target => "_blank"})
209
+ assert_equal %(Go to #{link_result}.), auto_link(%(Go to #{link_raw}.))
210
+ 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>))
211
+ assert_equal %(#{link_result} #{link_result}), auto_link(%(#{link_result} #{link_raw}))
212
+
213
+ email2_raw = '+david@loudthinking.com'
214
+ email2_result = %{<a href="mailto:#{email2_raw}">#{email2_raw}</a>}
215
+ assert_equal email2_result, auto_link(email2_raw)
216
+
217
+ email3_raw = '+david@loudthinking.com'
218
+ 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>}
219
+ assert_equal email3_result, auto_link(email3_raw, :all, :encode => :hex)
220
+ assert_equal email3_result, auto_link(email3_raw, :email_addresses, :encode => :hex)
221
+
222
+ link2_raw = 'www.rubyonrails.com'
223
+ link2_result = generate_result(link2_raw, "http://#{link2_raw}")
224
+ assert_equal %(Go to #{link2_result}), auto_link("Go to #{link2_raw}", :urls)
225
+ assert_equal %(Go to #{link2_raw}), auto_link("Go to #{link2_raw}", :email_addresses)
226
+ assert_equal %(<p>Link #{link2_result}</p>), auto_link("<p>Link #{link2_raw}</p>")
227
+ assert_equal %(<p>#{link2_result} Link</p>), auto_link("<p>#{link2_raw} Link</p>")
228
+ assert_equal %(Go to #{link2_result}.), auto_link(%(Go to #{link2_raw}.))
229
+ 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>))
230
+
231
+ link3_raw = 'http://manuals.ruby-on-rails.com/read/chapter.need_a-period/103#page281'
232
+ link3_result = generate_result(link3_raw)
233
+ assert_equal %(Go to #{link3_result}), auto_link("Go to #{link3_raw}", :urls)
234
+ assert_equal %(Go to #{link3_raw}), auto_link("Go to #{link3_raw}", :email_addresses)
235
+ assert_equal %(<p>Link #{link3_result}</p>), auto_link("<p>Link #{link3_raw}</p>")
236
+ assert_equal %(<p>#{link3_result} Link</p>), auto_link("<p>#{link3_raw} Link</p>")
237
+ assert_equal %(Go to #{link3_result}.), auto_link(%(Go to #{link3_raw}.))
238
+ assert_equal %(<p>Go to #{link3_result}. Seriously, #{link3_result}? I think I'll say hello to #{email_result}. Instead.</p>),
239
+ auto_link(%(<p>Go to #{link3_raw}. Seriously, #{link3_raw}? I think I'll say hello to #{email_raw}. Instead.</p>))
240
+
241
+ link4_raw = 'http://foo.example.com/controller/action?parm=value&p2=v2#anchor123'
242
+ link4_result = generate_result(link4_raw)
243
+ assert_equal %(<p>Link #{link4_result}</p>), auto_link("<p>Link #{link4_raw}</p>")
244
+ assert_equal %(<p>#{link4_result} Link</p>), auto_link("<p>#{link4_raw} Link</p>")
245
+
246
+ link5_raw = 'http://foo.example.com:3000/controller/action'
247
+ link5_result = generate_result(link5_raw)
248
+ assert_equal %(<p>#{link5_result} Link</p>), auto_link("<p>#{link5_raw} Link</p>")
249
+
250
+ link6_raw = 'http://foo.example.com:3000/controller/action+pack'
251
+ link6_result = generate_result(link6_raw)
252
+ assert_equal %(<p>#{link6_result} Link</p>), auto_link("<p>#{link6_raw} Link</p>")
253
+
254
+ link7_raw = 'http://foo.example.com/controller/action?parm=value&p2=v2#anchor-123'
255
+ link7_result = generate_result(link7_raw)
256
+ assert_equal %(<p>#{link7_result} Link</p>), auto_link("<p>#{link7_raw} Link</p>")
257
+
258
+ link8_raw = 'http://foo.example.com:3000/controller/action.html'
259
+ link8_result = generate_result(link8_raw)
260
+ assert_equal %(Go to #{link8_result}), auto_link("Go to #{link8_raw}", :urls)
261
+ assert_equal %(Go to #{link8_raw}), auto_link("Go to #{link8_raw}", :email_addresses)
262
+ assert_equal %(<p>Link #{link8_result}</p>), auto_link("<p>Link #{link8_raw}</p>")
263
+ assert_equal %(<p>#{link8_result} Link</p>), auto_link("<p>#{link8_raw} Link</p>")
264
+ assert_equal %(Go to #{link8_result}.), auto_link(%(Go to #{link8_raw}.))
265
+ assert_equal %(<p>Go to #{link8_result}. Seriously, #{link8_result}? I think I'll say hello to #{email_result}. Instead.</p>),
266
+ auto_link(%(<p>Go to #{link8_raw}. Seriously, #{link8_raw}? I think I'll say hello to #{email_raw}. Instead.</p>))
267
+
268
+ link9_raw = 'http://business.timesonline.co.uk/article/0,,9065-2473189,00.html'
269
+ link9_result = generate_result(link9_raw)
270
+ assert_equal %(Go to #{link9_result}), auto_link("Go to #{link9_raw}", :urls)
271
+ assert_equal %(Go to #{link9_raw}), auto_link("Go to #{link9_raw}", :email_addresses)
272
+ assert_equal %(<p>Link #{link9_result}</p>), auto_link("<p>Link #{link9_raw}</p>")
273
+ assert_equal %(<p>#{link9_result} Link</p>), auto_link("<p>#{link9_raw} Link</p>")
274
+ assert_equal %(Go to #{link9_result}.), auto_link(%(Go to #{link9_raw}.))
275
+ assert_equal %(<p>Go to #{link9_result}. Seriously, #{link9_result}? I think I'll say hello to #{email_result}. Instead.</p>),
276
+ auto_link(%(<p>Go to #{link9_raw}. Seriously, #{link9_raw}? I think I'll say hello to #{email_raw}. Instead.</p>))
277
+
278
+ link10_raw = 'http://www.mail-archive.com/ruby-talk@ruby-lang.org/'
279
+ link10_result = generate_result(link10_raw)
280
+ assert_equal %(<p>#{link10_result} Link</p>), auto_link("<p>#{link10_raw} Link</p>")
281
+
282
+ link11_raw = 'http://asakusa.rubyist.net/'
283
+ link11_result = generate_result(link11_raw)
284
+ with_kcode 'u' do
285
+ assert_equal %(浅草.rbの公式サイトはこちら#{link11_result}), auto_link("浅草.rbの公式サイトはこちら#{link11_raw}")
286
+ end
287
+ end
288
+
289
+ def test_auto_link_parsing
290
+ urls = %w(
291
+ http://www.rubyonrails.com
292
+ http://www.rubyonrails.com:80
293
+ http://www.rubyonrails.com/~minam
294
+ https://www.rubyonrails.com/~minam
295
+ http://www.rubyonrails.com/~minam/url%20with%20spaces
296
+ http://www.rubyonrails.com/foo.cgi?something=here
297
+ http://www.rubyonrails.com/foo.cgi?something=here&and=here
298
+ http://www.rubyonrails.com/contact;new
299
+ http://www.rubyonrails.com/contact;new%20with%20spaces
300
+ http://www.rubyonrails.com/contact;new?with=query&string=params
301
+ http://www.rubyonrails.com/~minam/contact;new?with=query&string=params
302
+ http://en.wikipedia.org/wiki/Wikipedia:Today%27s_featured_picture_%28animation%29/January_20%2C_2007
303
+ http://www.mail-archive.com/rails@lists.rubyonrails.org/
304
+ http://www.amazon.com/Testing-Equal-Sign-In-Path/ref=pd_bbs_sr_1?ie=UTF8&s=books&qid=1198861734&sr=8-1
305
+ http://en.wikipedia.org/wiki/Texas_hold'em
306
+ https://www.google.com/doku.php?id=gps:resource:scs:start
307
+ http://connect.oraclecorp.com/search?search[q]=green+france&search[type]=Group
308
+ http://of.openfoundry.org/projects/492/download#4th.Release.3
309
+ 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
310
+ http://около.кола/колокола
311
+ )
312
+
313
+ urls.each do |url|
314
+ assert_equal generate_result(url), auto_link(url)
315
+ end
316
+ end
317
+
318
+ private
319
+ def generate_result(link_text, href = nil, escape = false)
320
+ href ||= link_text
321
+ if escape
322
+ %{<a href="#{CGI::escapeHTML href}">#{CGI::escapeHTML link_text}</a>}
323
+ else
324
+ %{<a href="#{href}">#{link_text}</a>}
325
+ end
326
+ end
327
+
328
+ # from ruby core
329
+ def build_message(head, template=nil, *arguments)
330
+ template &&= template.chomp
331
+ template.gsub(/\?/) { mu_pp(arguments.shift) }
332
+ end
333
+
334
+ # Temporarily replaces KCODE for the block
335
+ def with_kcode(kcode)
336
+ if RUBY_VERSION < '1.9'
337
+ old_kcode, $KCODE = $KCODE, kcode
338
+ begin
339
+ yield
340
+ ensure
341
+ $KCODE = old_kcode
342
+ end
343
+ else
344
+ yield
345
+ end
346
+ end
347
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: friendlyfashion-rails_autolink
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.11
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Laurynas Butkus
9
+ - Tomas Didziokas
10
+ - Justas Janauskas
11
+ - Edvinas Bartkus
12
+ autorequire:
13
+ bindir: bin
14
+ cert_chain: []
15
+ date: 2013-02-21 00:00:00.000000000 Z
16
+ dependencies:
17
+ - !ruby/object:Gem::Dependency
18
+ name: rails
19
+ requirement: !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ! '>'
23
+ - !ruby/object:Gem::Version
24
+ version: '3.1'
25
+ type: :runtime
26
+ prerelease: false
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>'
31
+ - !ruby/object:Gem::Version
32
+ version: '3.1'
33
+ description:
34
+ email:
35
+ - laurynas.butkus@gmail.com
36
+ - tomas.did@gmail.com
37
+ - jjanauskas@gmail.com
38
+ - edvinas.bartkus@gmail.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - ./CHANGELOG.rdoc
44
+ - ./Gemfile
45
+ - ./Gemfile.lock
46
+ - ./lib/rails_autolink/helpers.rb
47
+ - ./lib/rails_autolink.rb
48
+ - ./rails_autolink.gemspec
49
+ - ./Rakefile
50
+ - ./README.rdoc
51
+ - ./test/test_rails_autolink.rb
52
+ homepage:
53
+ licenses: []
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubyforge_project:
72
+ rubygems_version: 1.8.24
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: This is an extraction of the `auto_link` method from rails. The `auto_link`
76
+ method was removed from Rails in version Rails 3.1. This gem is meant to bridge
77
+ the gap for people migrating.
78
+ test_files: []