favordo_autolink 1.1.6
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.
- checksums.yaml +7 -0
- data/.autotest +8 -0
- data/.gitignore +13 -0
- data/CHANGELOG.rdoc +40 -0
- data/Gemfile +6 -0
- data/README.rdoc +38 -0
- data/Rakefile +8 -0
- data/favordo_autolink.gemspec +18 -0
- data/lib/favordo_autolink.rb +9 -0
- data/lib/favordo_autolink/helpers.rb +164 -0
- data/lib/favordo_autolink/version.rb +3 -0
- data/test/test_favordo_autolink.rb +357 -0
- metadata +68 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1227789082570cd761f17f614303db6cb1e4a74f
|
4
|
+
data.tar.gz: ae8e0cfb3e51fcf51f80af5e5aec22b36ed70160
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3da836f6ceb38785ea4a969144dab785876ebac34c94a339bb667eedbe48bf3bd52bd71c1117a2bf3a6537d0895475330ff30eacc2190909dd6e1eb187655045
|
7
|
+
data.tar.gz: adf726adc5a7d2f88f00eb8b20f069783bf8773baf9c4f293493f5f9472dacf0012809af03e4846ed247eefecbeeeb1e7c3cef93088fbf5043dd78731ed9a8f1
|
data/.autotest
ADDED
data/.gitignore
ADDED
data/CHANGELOG.rdoc
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
=== 1.1.5 / 2013-10-23
|
2
|
+
|
3
|
+
* Improved performance of email regex
|
4
|
+
|
5
|
+
* Protocol regex is case insensitive
|
6
|
+
|
7
|
+
=== 1.1.3 / 2013-09-12
|
8
|
+
|
9
|
+
* Updates gemspec to work with latest rubygems
|
10
|
+
|
11
|
+
=== 1.1.1 / 2013-09-12
|
12
|
+
|
13
|
+
* Improved email regex to allow especial chars: !#$%&'*+-/=?^_`{|}~
|
14
|
+
|
15
|
+
=== 1.1.0 / 2013-05-02
|
16
|
+
|
17
|
+
* Ready for Rails 4.
|
18
|
+
|
19
|
+
=== 1.0.7 / 2012-04-27
|
20
|
+
|
21
|
+
* Added support for non-latin characters in autolinked urls
|
22
|
+
|
23
|
+
=== 1.0.6 / 2012-03-12
|
24
|
+
|
25
|
+
* Added sanitize_options arg
|
26
|
+
|
27
|
+
=== 1.0.5 / 2012-01-27
|
28
|
+
|
29
|
+
* Update dependency to include rails 3.2.X
|
30
|
+
|
31
|
+
=== 1.0.2 / 2011-06-18
|
32
|
+
|
33
|
+
* Compatibility with rails 3.1.0.rc4
|
34
|
+
|
35
|
+
=== 1.0.0 / 2011-05-02
|
36
|
+
|
37
|
+
* 1 major enhancement
|
38
|
+
|
39
|
+
* Birthday!
|
40
|
+
|
data/Gemfile
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
= favordo_autolink
|
2
|
+
|
3
|
+
* http://github.com/jesse-wang/favordo_autolink
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
This is an extraction of the `rails_autolink` method from rails, to also auto link @usernames.
|
8
|
+
|
9
|
+
* rails > 3.1
|
10
|
+
|
11
|
+
== INSTALL:
|
12
|
+
|
13
|
+
* gem install favordo_autolink
|
14
|
+
|
15
|
+
== LICENSE:
|
16
|
+
|
17
|
+
(The MIT License)
|
18
|
+
|
19
|
+
Copyright (c) 2014 DHH
|
20
|
+
|
21
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
22
|
+
a copy of this software and associated documentation files (the
|
23
|
+
'Software'), to deal in the Software without restriction, including
|
24
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
25
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
26
|
+
permit persons to whom the Software is furnished to do so, subject to
|
27
|
+
the following conditions:
|
28
|
+
|
29
|
+
The above copyright notice and this permission notice shall be
|
30
|
+
included in all copies or substantial portions of the Software.
|
31
|
+
|
32
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
33
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
34
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
35
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
36
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
37
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
38
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require File.expand_path "#{File.dirname(__FILE__)}/lib/favordo_autolink/version"
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = 'favordo_autolink'
|
5
|
+
s.version = FavordoAutolink::VERSION
|
6
|
+
s.date = Time.now.strftime('%Y-%m-%d')
|
7
|
+
s.authors = ['Jesse Wang']
|
8
|
+
s.email = 'jesse@jeswang.com'
|
9
|
+
s.homepage = 'https://github.com/jesse-wang/favordo_autolink'
|
10
|
+
s.summary = 'Automatic generation of url, emails and @usernames links in texts'
|
11
|
+
s.description = 'This is an extension of the `rails_autolink` gem from Aaron Patterson.'
|
12
|
+
|
13
|
+
s.add_dependency 'rails', '> 3.1'
|
14
|
+
s.required_ruby_version = '>= 1.9.3'
|
15
|
+
s.license = 'MIT'
|
16
|
+
|
17
|
+
s.files = Dir.glob("{test,lib/**/*}") + `git ls-files -z`.split("\0")
|
18
|
+
end
|
@@ -0,0 +1,164 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module FavordoAutolink
|
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_usernames(auto_link_urls(auto_link_email_addresses(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
|
+
when :usernames then conditional_html_safe(auto_link_usernames(text), sanitize)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
private
|
73
|
+
|
74
|
+
AUTO_LINK_RE = %r{
|
75
|
+
(?: ((?:ed2k|ftp|http|https|irc|mailto|news|gopher|nntp|telnet|webcal|xmpp|callto|feed|svn|urn|aim|rsync|tag|ssh|sftp|rtsp|afs|file):)// | [\w.]+\.[a-z]{2,3}(?:\:[0-9]{1,5})?(?:\/.*)?)
|
76
|
+
[^\s<\u00A0]+
|
77
|
+
}ix
|
78
|
+
|
79
|
+
# regexps for determining context, used high-volume
|
80
|
+
AUTO_LINK_CRE = [/<[^>]+$/, /^[^>]*>/, /<a\b.*?>/i, /<\/a>/i]
|
81
|
+
|
82
|
+
AUTO_EMAIL_LOCAL_RE = /[\w.!#\$%&'*\/=?^`{|}~+-]/
|
83
|
+
AUTO_EMAIL_RE = /[\w.!#\$%+-]\.?#{AUTO_EMAIL_LOCAL_RE}*@[\w-]+(?:\.[\w-]+)+/
|
84
|
+
|
85
|
+
BRACKETS = { ']' => '[', ')' => '(', '}' => '{' }
|
86
|
+
|
87
|
+
WORD_PATTERN = RUBY_VERSION < '1.9' ? '\w' : '\p{Word}'
|
88
|
+
|
89
|
+
# Turns all urls into clickable links. If a block is given, each url
|
90
|
+
# is yielded and the result is used as the link text.
|
91
|
+
def auto_link_urls(text, html_options = {}, options = {})
|
92
|
+
link_attributes = html_options.stringify_keys
|
93
|
+
text.gsub(AUTO_LINK_RE) do
|
94
|
+
scheme, href = $1, $&
|
95
|
+
punctuation = []
|
96
|
+
|
97
|
+
if auto_linked?($`, $')
|
98
|
+
# do not change string; URL is already linked
|
99
|
+
href
|
100
|
+
else
|
101
|
+
# don't include trailing punctuation character as part of the URL
|
102
|
+
while href.sub!(/[^#{WORD_PATTERN}\/-]$/, '')
|
103
|
+
punctuation.push $&
|
104
|
+
if opening = BRACKETS[punctuation.last] and href.scan(opening).size > href.scan(punctuation.last).size
|
105
|
+
href << punctuation.pop
|
106
|
+
break
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
link_text = block_given?? yield(href) : href
|
111
|
+
href = 'http://' + href unless scheme
|
112
|
+
|
113
|
+
unless options[:sanitize] == false
|
114
|
+
link_text = sanitize(link_text)
|
115
|
+
href = sanitize(href)
|
116
|
+
end
|
117
|
+
content_tag(:a, link_text, link_attributes.merge('href' => href), !!options[:sanitize]) + punctuation.reverse.join('')
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
# Turns all email addresses into clickable links. If a block is given,
|
123
|
+
# each email is yielded and the result is used as the link text.
|
124
|
+
def auto_link_email_addresses(text, html_options = {}, options = {})
|
125
|
+
text.gsub(AUTO_EMAIL_RE) do
|
126
|
+
text = $&
|
127
|
+
|
128
|
+
if auto_linked?($`, $')
|
129
|
+
text.html_safe
|
130
|
+
else
|
131
|
+
display_text = (block_given?) ? yield(text) : text
|
132
|
+
|
133
|
+
unless options[:sanitize] == false
|
134
|
+
text = sanitize(text)
|
135
|
+
display_text = sanitize(display_text) unless text == display_text
|
136
|
+
end
|
137
|
+
mail_to text, display_text, html_options
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
def auto_link_usernames(text)
|
143
|
+
text.gsub /(?<=\s|^)@[A-Za-z0-9_]+(?=\b)/ do |username|
|
144
|
+
link_to(username, user_path(username.gsub('@', '')))
|
145
|
+
end.html_safe
|
146
|
+
end
|
147
|
+
|
148
|
+
# Detects already linked context or position in the middle of a tag
|
149
|
+
def auto_linked?(left, right)
|
150
|
+
(left =~ AUTO_LINK_CRE[0] and right =~ AUTO_LINK_CRE[1]) or
|
151
|
+
(left.rindex(AUTO_LINK_CRE[2]) and $' !~ AUTO_LINK_CRE[3])
|
152
|
+
end
|
153
|
+
|
154
|
+
def conditional_sanitize(target, condition, sanitize_options = {})
|
155
|
+
condition ? sanitize(target, sanitize_options) : target
|
156
|
+
end
|
157
|
+
|
158
|
+
def conditional_html_safe(target, condition)
|
159
|
+
condition ? target.html_safe : target
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
@@ -0,0 +1,357 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require "minitest/autorun"
|
4
|
+
require "rails"
|
5
|
+
require "favordo_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 TestFavordoAutolink < 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
|
+
assert_equal generate_result(file_scheme), auto_link(file_scheme)
|
121
|
+
end
|
122
|
+
|
123
|
+
def test_auto_link_already_linked
|
124
|
+
linked1 = generate_result('Ruby On Rails', 'http://www.rubyonrails.com')
|
125
|
+
linked2 = %('<a href="http://www.example.com">www.example.com</a>')
|
126
|
+
linked3 = %('<a href="http://www.example.com" rel="nofollow">www.example.com</a>')
|
127
|
+
linked4 = %('<a href="http://www.example.com"><b>www.example.com</b></a>')
|
128
|
+
linked5 = %('<a href="#close">close</a> <a href="http://www.example.com"><b>www.example.com</b></a>')
|
129
|
+
linked6 = %('<a href="#close">close</a> <a href="http://www.example.com" target="_blank" data-ruby="ror"><b>www.example.com</b></a>')
|
130
|
+
assert_equal linked1, auto_link(linked1)
|
131
|
+
assert_equal linked2, auto_link(linked2)
|
132
|
+
assert_equal linked3, auto_link(linked3, :sanitize => false)
|
133
|
+
assert_equal linked4, auto_link(linked4)
|
134
|
+
assert_equal linked5, auto_link(linked5)
|
135
|
+
assert_equal linked6, auto_link(linked6, :sanitize_options => {:attributes => ["href", "target", "data-ruby"]})
|
136
|
+
|
137
|
+
linked_email = %Q(<a href="mailto:david@loudthinking.com">Mail me</a>)
|
138
|
+
assert_equal linked_email, auto_link(linked_email)
|
139
|
+
end
|
140
|
+
|
141
|
+
def test_auto_link_at_eol
|
142
|
+
url1 = "http://api.rubyonrails.com/Foo.html"
|
143
|
+
url2 = "http://www.ruby-doc.org/core/Bar.html"
|
144
|
+
|
145
|
+
assert_equal %(<p><a href="#{url1}">#{url1}</a><br /><a href="#{url2}">#{url2}</a><br /></p>), auto_link("<p>#{url1}<br />#{url2}<br /></p>")
|
146
|
+
end
|
147
|
+
|
148
|
+
def test_auto_link_should_be_html_safe
|
149
|
+
email_raw = 'santiago@wyeworks.com'
|
150
|
+
link_raw = 'http://www.rubyonrails.org'
|
151
|
+
malicious_script = '<script>alert("malicious!")</script>'
|
152
|
+
|
153
|
+
assert auto_link(nil).html_safe?, 'should be html safe'
|
154
|
+
assert auto_link('').html_safe?, 'should be html safe'
|
155
|
+
assert auto_link("#{link_raw} #{link_raw} #{link_raw}").html_safe?, 'should be html safe'
|
156
|
+
assert auto_link("hello #{email_raw}").html_safe?, 'should be html safe'
|
157
|
+
assert auto_link("hello #{email_raw} #{malicious_script}").html_safe?, 'should be html safe'
|
158
|
+
end
|
159
|
+
|
160
|
+
def test_auto_link_should_not_be_html_safe_when_sanitize_option_false
|
161
|
+
email_raw = 'santiago@wyeworks.com'
|
162
|
+
link_raw = 'http://www.rubyonrails.org'
|
163
|
+
|
164
|
+
assert !auto_link("hello", :sanitize => false).html_safe?, 'should not be html safe'
|
165
|
+
assert !auto_link("#{link_raw} #{link_raw} #{link_raw}", :sanitize => false).html_safe?, 'should not be html safe'
|
166
|
+
assert !auto_link("hello #{email_raw}", :sanitize => false).html_safe?, 'should not be html safe'
|
167
|
+
end
|
168
|
+
|
169
|
+
def test_auto_link_email_address
|
170
|
+
email_raw = 'aaron@tenderlovemaking.com'
|
171
|
+
email_result = %{<a href="mailto:#{email_raw}">#{email_raw}</a>}
|
172
|
+
assert !auto_link_email_addresses(email_result).html_safe?, 'should not be html safe'
|
173
|
+
end
|
174
|
+
|
175
|
+
def test_auto_link_email_addres_with_especial_chars
|
176
|
+
email_raw = "and&re$la*+r-a.o'rea=l~ly@tenderlovemaking.com"
|
177
|
+
email_sanitized = "and&re$la*+r-a.o'rea=l~ly@tenderlovemaking.com"
|
178
|
+
email_result = %{<a href="mailto:#{email_raw}">#{email_sanitized}</a>}
|
179
|
+
assert_equal email_result, auto_link(email_raw)
|
180
|
+
assert !auto_link_email_addresses(email_result).html_safe?, 'should not be html safe'
|
181
|
+
end
|
182
|
+
|
183
|
+
def test_auto_link
|
184
|
+
email_raw = 'david@loudthinking.com'
|
185
|
+
email_result = %{<a href="mailto:#{email_raw}">#{email_raw}</a>}
|
186
|
+
link_raw = 'http://www.rubyonrails.com'
|
187
|
+
link_result = generate_result(link_raw)
|
188
|
+
link_result_with_options = %{<a href="#{link_raw}" target="_blank">#{link_raw}</a>}
|
189
|
+
|
190
|
+
assert_equal '', auto_link(nil)
|
191
|
+
assert_equal '', auto_link('')
|
192
|
+
assert_equal "#{link_result} #{link_result} #{link_result}", auto_link("#{link_raw} #{link_raw} #{link_raw}")
|
193
|
+
|
194
|
+
assert_equal %(hello #{email_result}), auto_link("hello #{email_raw}", :email_addresses)
|
195
|
+
assert_equal %(Go to #{link_result}), auto_link("Go to #{link_raw}", :urls)
|
196
|
+
assert_equal %(Go to #{link_raw}), auto_link("Go to #{link_raw}", :email_addresses)
|
197
|
+
assert_equal %(Go to #{link_result} and say hello to #{email_result}), auto_link("Go to #{link_raw} and say hello to #{email_raw}")
|
198
|
+
assert_equal %(<p>Link #{link_result}</p>), auto_link("<p>Link #{link_raw}</p>")
|
199
|
+
assert_equal %(<p>#{link_result} Link</p>), auto_link("<p>#{link_raw} Link</p>")
|
200
|
+
assert_equal %(<p>Link #{link_result_with_options}</p>), auto_link("<p>Link #{link_raw}</p>", :all, {:target => "_blank"})
|
201
|
+
assert_equal %(Go to #{link_result}.), auto_link(%(Go to #{link_raw}.))
|
202
|
+
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>))
|
203
|
+
assert_equal %(#{link_result} #{link_result}), auto_link(%(#{link_result} #{link_raw}))
|
204
|
+
|
205
|
+
email2_raw = '+david@loudthinking.com'
|
206
|
+
email2_result = %{<a href="mailto:#{email2_raw}">#{email2_raw}</a>}
|
207
|
+
assert_equal email2_result, auto_link(email2_raw)
|
208
|
+
assert_equal email2_result, auto_link(email2_raw, :all)
|
209
|
+
assert_equal email2_result, auto_link(email2_raw, :email_addresses)
|
210
|
+
|
211
|
+
link2_raw = 'www.rubyonrails.com'
|
212
|
+
link2_result = generate_result(link2_raw, "http://#{link2_raw}")
|
213
|
+
assert_equal %(Go to #{link2_result}), auto_link("Go to #{link2_raw}", :urls)
|
214
|
+
assert_equal %(Go to #{link2_raw}), auto_link("Go to #{link2_raw}", :email_addresses)
|
215
|
+
assert_equal %(<p>Link #{link2_result}</p>), auto_link("<p>Link #{link2_raw}</p>")
|
216
|
+
assert_equal %(<p>#{link2_result} Link</p>), auto_link("<p>#{link2_raw} Link</p>")
|
217
|
+
assert_equal %(Go to #{link2_result}.), auto_link(%(Go to #{link2_raw}.))
|
218
|
+
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>))
|
219
|
+
|
220
|
+
link3_raw = 'http://manuals.ruby-on-rails.com/read/chapter.need_a-period/103#page281'
|
221
|
+
link3_result = generate_result(link3_raw)
|
222
|
+
assert_equal %(Go to #{link3_result}), auto_link("Go to #{link3_raw}", :urls)
|
223
|
+
assert_equal %(Go to #{link3_raw}), auto_link("Go to #{link3_raw}", :email_addresses)
|
224
|
+
assert_equal %(<p>Link #{link3_result}</p>), auto_link("<p>Link #{link3_raw}</p>")
|
225
|
+
assert_equal %(<p>#{link3_result} Link</p>), auto_link("<p>#{link3_raw} Link</p>")
|
226
|
+
assert_equal %(Go to #{link3_result}.), auto_link(%(Go to #{link3_raw}.))
|
227
|
+
assert_equal %(<p>Go to #{link3_result}. Seriously, #{link3_result}? I think I'll say hello to #{email_result}. Instead.</p>),
|
228
|
+
auto_link(%(<p>Go to #{link3_raw}. Seriously, #{link3_raw}? I think I'll say hello to #{email_raw}. Instead.</p>))
|
229
|
+
|
230
|
+
link4_raw = 'http://foo.example.com/controller/action?parm=value&p2=v2#anchor123'
|
231
|
+
link4_result = generate_result(link4_raw)
|
232
|
+
assert_equal %(<p>Link #{link4_result}</p>), auto_link("<p>Link #{link4_raw}</p>")
|
233
|
+
assert_equal %(<p>#{link4_result} Link</p>), auto_link("<p>#{link4_raw} Link</p>")
|
234
|
+
|
235
|
+
link5_raw = 'http://foo.example.com:3000/controller/action'
|
236
|
+
link5_result = generate_result(link5_raw)
|
237
|
+
assert_equal %(<p>#{link5_result} Link</p>), auto_link("<p>#{link5_raw} Link</p>")
|
238
|
+
|
239
|
+
link6_raw = 'http://foo.example.com:3000/controller/action+pack'
|
240
|
+
link6_result = generate_result(link6_raw)
|
241
|
+
assert_equal %(<p>#{link6_result} Link</p>), auto_link("<p>#{link6_raw} Link</p>")
|
242
|
+
|
243
|
+
link7_raw = 'http://foo.example.com/controller/action?parm=value&p2=v2#anchor-123'
|
244
|
+
link7_result = generate_result(link7_raw)
|
245
|
+
assert_equal %(<p>#{link7_result} Link</p>), auto_link("<p>#{link7_raw} Link</p>")
|
246
|
+
|
247
|
+
link8_raw = 'http://foo.example.com:3000/controller/action.html'
|
248
|
+
link8_result = generate_result(link8_raw)
|
249
|
+
assert_equal %(Go to #{link8_result}), auto_link("Go to #{link8_raw}", :urls)
|
250
|
+
assert_equal %(Go to #{link8_raw}), auto_link("Go to #{link8_raw}", :email_addresses)
|
251
|
+
assert_equal %(<p>Link #{link8_result}</p>), auto_link("<p>Link #{link8_raw}</p>")
|
252
|
+
assert_equal %(<p>#{link8_result} Link</p>), auto_link("<p>#{link8_raw} Link</p>")
|
253
|
+
assert_equal %(Go to #{link8_result}.), auto_link(%(Go to #{link8_raw}.))
|
254
|
+
assert_equal %(<p>Go to #{link8_result}. Seriously, #{link8_result}? I think I'll say hello to #{email_result}. Instead.</p>),
|
255
|
+
auto_link(%(<p>Go to #{link8_raw}. Seriously, #{link8_raw}? I think I'll say hello to #{email_raw}. Instead.</p>))
|
256
|
+
|
257
|
+
link9_raw = 'http://business.timesonline.co.uk/article/0,,9065-2473189,00.html'
|
258
|
+
link9_result = generate_result(link9_raw)
|
259
|
+
assert_equal %(Go to #{link9_result}), auto_link("Go to #{link9_raw}", :urls)
|
260
|
+
assert_equal %(Go to #{link9_raw}), auto_link("Go to #{link9_raw}", :email_addresses)
|
261
|
+
assert_equal %(<p>Link #{link9_result}</p>), auto_link("<p>Link #{link9_raw}</p>")
|
262
|
+
assert_equal %(<p>#{link9_result} Link</p>), auto_link("<p>#{link9_raw} Link</p>")
|
263
|
+
assert_equal %(Go to #{link9_result}.), auto_link(%(Go to #{link9_raw}.))
|
264
|
+
assert_equal %(<p>Go to #{link9_result}. Seriously, #{link9_result}? I think I'll say hello to #{email_result}. Instead.</p>),
|
265
|
+
auto_link(%(<p>Go to #{link9_raw}. Seriously, #{link9_raw}? I think I'll say hello to #{email_raw}. Instead.</p>))
|
266
|
+
|
267
|
+
link10_raw = 'http://www.mail-archive.com/ruby-talk@ruby-lang.org/'
|
268
|
+
link10_result = generate_result(link10_raw)
|
269
|
+
assert_equal %(<p>#{link10_result} Link</p>), auto_link("<p>#{link10_raw} Link</p>")
|
270
|
+
|
271
|
+
link11_raw = 'http://asakusa.rubyist.net/'
|
272
|
+
link11_result = generate_result(link11_raw)
|
273
|
+
with_kcode 'u' do
|
274
|
+
assert_equal %(浅草.rbの公式サイトはこちら#{link11_result}), auto_link("浅草.rbの公式サイトはこちら#{link11_raw}")
|
275
|
+
end
|
276
|
+
|
277
|
+
link12_raw = 'http://tools.ietf.org/html/rfc3986'
|
278
|
+
link12_result = generate_result(link12_raw)
|
279
|
+
assert_equal %(<p>#{link12_result} text-after-nonbreaking-space</p>), auto_link("<p>#{link12_raw} text-after-nonbreaking-space</p>")
|
280
|
+
|
281
|
+
link13_raw = 'HTtP://www.rubyonrails.com'
|
282
|
+
assert_equal generate_result(link13_raw), auto_link(link13_raw)
|
283
|
+
end
|
284
|
+
|
285
|
+
def test_auto_link_parsing
|
286
|
+
urls = %w(
|
287
|
+
http://www.rubyonrails.com
|
288
|
+
http://www.rubyonrails.com:80
|
289
|
+
http://www.rubyonrails.com/~minam
|
290
|
+
https://www.rubyonrails.com/~minam
|
291
|
+
http://www.rubyonrails.com/~minam/url%20with%20spaces
|
292
|
+
http://www.rubyonrails.com/foo.cgi?something=here
|
293
|
+
http://www.rubyonrails.com/foo.cgi?something=here&and=here
|
294
|
+
http://www.rubyonrails.com/contact;new
|
295
|
+
http://www.rubyonrails.com/contact;new%20with%20spaces
|
296
|
+
http://www.rubyonrails.com/contact;new?with=query&string=params
|
297
|
+
http://www.rubyonrails.com/~minam/contact;new?with=query&string=params
|
298
|
+
http://en.wikipedia.org/wiki/Wikipedia:Today%27s_featured_picture_%28animation%29/January_20%2C_2007
|
299
|
+
http://www.mail-archive.com/rails@lists.rubyonrails.org/
|
300
|
+
http://www.amazon.com/Testing-Equal-Sign-In-Path/ref=pd_bbs_sr_1?ie=UTF8&s=books&qid=1198861734&sr=8-1
|
301
|
+
http://en.wikipedia.org/wiki/Texas_hold'em
|
302
|
+
https://www.google.com/doku.php?id=gps:resource:scs:start
|
303
|
+
http://connect.oraclecorp.com/search?search[q]=green+france&search[type]=Group
|
304
|
+
http://of.openfoundry.org/projects/492/download#4th.Release.3
|
305
|
+
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
|
306
|
+
http://около.кола/колокола
|
307
|
+
)
|
308
|
+
|
309
|
+
urls.each do |url|
|
310
|
+
assert_equal generate_result(url), auto_link(url)
|
311
|
+
end
|
312
|
+
end
|
313
|
+
|
314
|
+
def test_auto_link_does_not_timeout_when_parsing_odd_email_input
|
315
|
+
inputs = %w(
|
316
|
+
foo@...................................
|
317
|
+
foo@........................................
|
318
|
+
foo@.............................................
|
319
|
+
)
|
320
|
+
|
321
|
+
inputs.each do |input|
|
322
|
+
Timeout.timeout(0.2) do
|
323
|
+
assert_equal input, auto_link(input)
|
324
|
+
end
|
325
|
+
end
|
326
|
+
end
|
327
|
+
|
328
|
+
private
|
329
|
+
def generate_result(link_text, href = nil, escape = false)
|
330
|
+
href ||= link_text
|
331
|
+
if escape
|
332
|
+
%{<a href="#{CGI::escapeHTML href}">#{CGI::escapeHTML link_text}</a>}
|
333
|
+
else
|
334
|
+
%{<a href="#{href}">#{link_text}</a>}
|
335
|
+
end
|
336
|
+
end
|
337
|
+
|
338
|
+
# from ruby core
|
339
|
+
def build_message(head, template=nil, *arguments)
|
340
|
+
template &&= template.chomp
|
341
|
+
template.gsub(/\?/) { mu_pp(arguments.shift) }
|
342
|
+
end
|
343
|
+
|
344
|
+
# Temporarily replaces KCODE for the block
|
345
|
+
def with_kcode(kcode)
|
346
|
+
if RUBY_VERSION < '1.9'
|
347
|
+
old_kcode, $KCODE = $KCODE, kcode
|
348
|
+
begin
|
349
|
+
yield
|
350
|
+
ensure
|
351
|
+
$KCODE = old_kcode
|
352
|
+
end
|
353
|
+
else
|
354
|
+
yield
|
355
|
+
end
|
356
|
+
end
|
357
|
+
end
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: favordo_autolink
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.6
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jesse Wang
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-05-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>'
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>'
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.1'
|
27
|
+
description: This is an extension of the `rails_autolink` gem from Aaron Patterson.
|
28
|
+
email: jesse@jeswang.com
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- lib/favordo_autolink/helpers.rb
|
34
|
+
- lib/favordo_autolink/version.rb
|
35
|
+
- lib/favordo_autolink.rb
|
36
|
+
- .autotest
|
37
|
+
- .gitignore
|
38
|
+
- CHANGELOG.rdoc
|
39
|
+
- Gemfile
|
40
|
+
- README.rdoc
|
41
|
+
- Rakefile
|
42
|
+
- favordo_autolink.gemspec
|
43
|
+
- test/test_favordo_autolink.rb
|
44
|
+
homepage: https://github.com/jesse-wang/favordo_autolink
|
45
|
+
licenses:
|
46
|
+
- MIT
|
47
|
+
metadata: {}
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: 1.9.3
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
requirements: []
|
63
|
+
rubyforge_project:
|
64
|
+
rubygems_version: 2.0.3
|
65
|
+
signing_key:
|
66
|
+
specification_version: 4
|
67
|
+
summary: Automatic generation of url, emails and @usernames links in texts
|
68
|
+
test_files: []
|