rails_autolink 1.1.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +4 -0
- data/OLDGEMS/{howto_release.txt → how_to_release.txt} +0 -0
- data/OLDGEMS/rails_autolink-1.1.0.gem +0 -0
- data/lib/rails_autolink/helpers.rb +2 -1
- data/rails_autolink.gemspec +2 -2
- data/test/test_rails_autolink.rb +8 -0
- metadata +8 -25
- data/OLDGEMS/rails_autolink-1.0.5.gem +0 -0
- data/OLDGEMS/rails_autolink-1.0.5/CHANGELOG.rdoc +0 -26
- data/OLDGEMS/rails_autolink-1.0.5/Gemfile +0 -8
- data/OLDGEMS/rails_autolink-1.0.5/Manifest.txt +0 -9
- data/OLDGEMS/rails_autolink-1.0.5/README.rdoc +0 -75
- data/OLDGEMS/rails_autolink-1.0.5/Rakefile +0 -21
- data/OLDGEMS/rails_autolink-1.0.5/lib/rails_autolink.rb +0 -11
- data/OLDGEMS/rails_autolink-1.0.5/lib/rails_autolink/helpers.rb +0 -154
- data/OLDGEMS/rails_autolink-1.0.5/test/test_rails_autolink.rb +0 -338
- data/OLDGEMS/rails_autolink-1.0.7.gem +0 -0
- data/OLDGEMS/rails_autolink-1.0.7/CHANGELOG.rdoc +0 -26
- data/OLDGEMS/rails_autolink-1.0.7/Gemfile +0 -8
- data/OLDGEMS/rails_autolink-1.0.7/Manifest.txt +0 -9
- data/OLDGEMS/rails_autolink-1.0.7/README.rdoc +0 -75
- data/OLDGEMS/rails_autolink-1.0.7/Rakefile +0 -21
- data/OLDGEMS/rails_autolink-1.0.7/lib/rails_autolink.rb +0 -11
- data/OLDGEMS/rails_autolink-1.0.7/lib/rails_autolink/helpers.rb +0 -156
- data/OLDGEMS/rails_autolink-1.0.7/test/test_rails_autolink.rb +0 -329
Binary file
|
@@ -1,26 +0,0 @@
|
|
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
|
-
|
@@ -1,75 +0,0 @@
|
|
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.
|
@@ -1,21 +0,0 @@
|
|
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
|
@@ -1,156 +0,0 @@
|
|
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
|
-
|
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
|
-
href = 'http://' + href unless scheme
|
110
|
-
|
111
|
-
unless options[:sanitize] == false
|
112
|
-
link_text = sanitize(link_text)
|
113
|
-
href = sanitize(href)
|
114
|
-
end
|
115
|
-
content_tag(:a, link_text, link_attributes.merge('href' => href), !!options[:sanitize]) + punctuation.reverse.join('')
|
116
|
-
end
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
|
-
# Turns all email addresses into clickable links. If a block is given,
|
121
|
-
# each email is yielded and the result is used as the link text.
|
122
|
-
def auto_link_email_addresses(text, html_options = {}, options = {})
|
123
|
-
text.gsub(AUTO_EMAIL_RE) do
|
124
|
-
text = $&
|
125
|
-
|
126
|
-
if auto_linked?($`, $')
|
127
|
-
text.html_safe
|
128
|
-
else
|
129
|
-
display_text = (block_given?) ? yield(text) : text
|
130
|
-
|
131
|
-
unless options[:sanitize] == false
|
132
|
-
text = sanitize(text)
|
133
|
-
display_text = sanitize(display_text) unless text == display_text
|
134
|
-
end
|
135
|
-
mail_to text, display_text, html_options
|
136
|
-
end
|
137
|
-
end
|
138
|
-
end
|
139
|
-
|
140
|
-
# Detects already linked context or position in the middle of a tag
|
141
|
-
def auto_linked?(left, right)
|
142
|
-
(left =~ AUTO_LINK_CRE[0] and right =~ AUTO_LINK_CRE[1]) or
|
143
|
-
(left.rindex(AUTO_LINK_CRE[2]) and $' !~ AUTO_LINK_CRE[3])
|
144
|
-
end
|
145
|
-
|
146
|
-
def conditional_sanitize(target, condition, sanitize_options = {})
|
147
|
-
condition ? sanitize(target, sanitize_options) : target
|
148
|
-
end
|
149
|
-
|
150
|
-
def conditional_html_safe(target, condition)
|
151
|
-
condition ? target.html_safe : target
|
152
|
-
end
|
153
|
-
end
|
154
|
-
end
|
155
|
-
end
|
156
|
-
end
|
@@ -1,329 +0,0 @@
|
|
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
|
-
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
|
-
|
142
|
-
def test_auto_link_at_eol
|
143
|
-
url1 = "http://api.rubyonrails.com/Foo.html"
|
144
|
-
url2 = "http://www.ruby-doc.org/core/Bar.html"
|
145
|
-
|
146
|
-
assert_equal %(<p><a href="#{url1}">#{url1}</a><br /><a href="#{url2}">#{url2}</a><br /></p>), auto_link("<p>#{url1}<br />#{url2}<br /></p>")
|
147
|
-
end
|
148
|
-
|
149
|
-
def test_auto_link_should_be_html_safe
|
150
|
-
email_raw = 'santiago@wyeworks.com'
|
151
|
-
link_raw = 'http://www.rubyonrails.org'
|
152
|
-
malicious_script = '<script>alert("malicious!")</script>'
|
153
|
-
|
154
|
-
assert auto_link(nil).html_safe?, 'should be html safe'
|
155
|
-
assert auto_link('').html_safe?, 'should be html safe'
|
156
|
-
assert auto_link("#{link_raw} #{link_raw} #{link_raw}").html_safe?, 'should be html safe'
|
157
|
-
assert auto_link("hello #{email_raw}").html_safe?, 'should be html safe'
|
158
|
-
assert auto_link("hello #{email_raw} #{malicious_script}").html_safe?, 'should be html safe'
|
159
|
-
end
|
160
|
-
|
161
|
-
def test_auto_link_should_not_be_html_safe_when_sanitize_option_false
|
162
|
-
email_raw = 'santiago@wyeworks.com'
|
163
|
-
link_raw = 'http://www.rubyonrails.org'
|
164
|
-
|
165
|
-
assert !auto_link("hello", :sanitize => false).html_safe?, 'should not be html safe'
|
166
|
-
assert !auto_link("#{link_raw} #{link_raw} #{link_raw}", :sanitize => false).html_safe?, 'should not be html safe'
|
167
|
-
assert !auto_link("hello #{email_raw}", :sanitize => false).html_safe?, 'should not be html safe'
|
168
|
-
end
|
169
|
-
|
170
|
-
def test_auto_link_email_address
|
171
|
-
email_raw = 'aaron@tenderlovemaking.com'
|
172
|
-
email_result = %{<a href="mailto:#{email_raw}">#{email_raw}</a>}
|
173
|
-
assert !auto_link_email_addresses(email_result).html_safe?, 'should not be html safe'
|
174
|
-
end
|
175
|
-
|
176
|
-
def test_auto_link
|
177
|
-
email_raw = 'david@loudthinking.com'
|
178
|
-
email_result = %{<a href="mailto:#{email_raw}">#{email_raw}</a>}
|
179
|
-
link_raw = 'http://www.rubyonrails.com'
|
180
|
-
link_result = generate_result(link_raw)
|
181
|
-
link_result_with_options = %{<a href="#{link_raw}" target="_blank">#{link_raw}</a>}
|
182
|
-
|
183
|
-
assert_equal '', auto_link(nil)
|
184
|
-
assert_equal '', auto_link('')
|
185
|
-
assert_equal "#{link_result} #{link_result} #{link_result}", auto_link("#{link_raw} #{link_raw} #{link_raw}")
|
186
|
-
|
187
|
-
assert_equal %(hello #{email_result}), auto_link("hello #{email_raw}", :email_addresses)
|
188
|
-
assert_equal %(Go to #{link_result}), auto_link("Go to #{link_raw}", :urls)
|
189
|
-
assert_equal %(Go to #{link_raw}), auto_link("Go to #{link_raw}", :email_addresses)
|
190
|
-
assert_equal %(Go to #{link_result} and say hello to #{email_result}), auto_link("Go to #{link_raw} and say hello to #{email_raw}")
|
191
|
-
assert_equal %(<p>Link #{link_result}</p>), auto_link("<p>Link #{link_raw}</p>")
|
192
|
-
assert_equal %(<p>#{link_result} Link</p>), auto_link("<p>#{link_raw} Link</p>")
|
193
|
-
assert_equal %(<p>Link #{link_result_with_options}</p>), auto_link("<p>Link #{link_raw}</p>", :all, {:target => "_blank"})
|
194
|
-
assert_equal %(Go to #{link_result}.), auto_link(%(Go to #{link_raw}.))
|
195
|
-
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>))
|
196
|
-
assert_equal %(#{link_result} #{link_result}), auto_link(%(#{link_result} #{link_raw}))
|
197
|
-
|
198
|
-
email2_raw = '+david@loudthinking.com'
|
199
|
-
email2_result = %{<a href="mailto:#{email2_raw}">#{email2_raw}</a>}
|
200
|
-
assert_equal email2_result, auto_link(email2_raw)
|
201
|
-
assert_equal email2_result, auto_link(email2_raw, :all)
|
202
|
-
assert_equal email2_result, auto_link(email2_raw, :email_addresses)
|
203
|
-
|
204
|
-
link2_raw = 'www.rubyonrails.com'
|
205
|
-
link2_result = generate_result(link2_raw, "http://#{link2_raw}")
|
206
|
-
assert_equal %(Go to #{link2_result}), auto_link("Go to #{link2_raw}", :urls)
|
207
|
-
assert_equal %(Go to #{link2_raw}), auto_link("Go to #{link2_raw}", :email_addresses)
|
208
|
-
assert_equal %(<p>Link #{link2_result}</p>), auto_link("<p>Link #{link2_raw}</p>")
|
209
|
-
assert_equal %(<p>#{link2_result} Link</p>), auto_link("<p>#{link2_raw} Link</p>")
|
210
|
-
assert_equal %(Go to #{link2_result}.), auto_link(%(Go to #{link2_raw}.))
|
211
|
-
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>))
|
212
|
-
|
213
|
-
link3_raw = 'http://manuals.ruby-on-rails.com/read/chapter.need_a-period/103#page281'
|
214
|
-
link3_result = generate_result(link3_raw)
|
215
|
-
assert_equal %(Go to #{link3_result}), auto_link("Go to #{link3_raw}", :urls)
|
216
|
-
assert_equal %(Go to #{link3_raw}), auto_link("Go to #{link3_raw}", :email_addresses)
|
217
|
-
assert_equal %(<p>Link #{link3_result}</p>), auto_link("<p>Link #{link3_raw}</p>")
|
218
|
-
assert_equal %(<p>#{link3_result} Link</p>), auto_link("<p>#{link3_raw} Link</p>")
|
219
|
-
assert_equal %(Go to #{link3_result}.), auto_link(%(Go to #{link3_raw}.))
|
220
|
-
assert_equal %(<p>Go to #{link3_result}. Seriously, #{link3_result}? I think I'll say hello to #{email_result}. Instead.</p>),
|
221
|
-
auto_link(%(<p>Go to #{link3_raw}. Seriously, #{link3_raw}? I think I'll say hello to #{email_raw}. Instead.</p>))
|
222
|
-
|
223
|
-
link4_raw = 'http://foo.example.com/controller/action?parm=value&p2=v2#anchor123'
|
224
|
-
link4_result = generate_result(link4_raw)
|
225
|
-
assert_equal %(<p>Link #{link4_result}</p>), auto_link("<p>Link #{link4_raw}</p>")
|
226
|
-
assert_equal %(<p>#{link4_result} Link</p>), auto_link("<p>#{link4_raw} Link</p>")
|
227
|
-
|
228
|
-
link5_raw = 'http://foo.example.com:3000/controller/action'
|
229
|
-
link5_result = generate_result(link5_raw)
|
230
|
-
assert_equal %(<p>#{link5_result} Link</p>), auto_link("<p>#{link5_raw} Link</p>")
|
231
|
-
|
232
|
-
link6_raw = 'http://foo.example.com:3000/controller/action+pack'
|
233
|
-
link6_result = generate_result(link6_raw)
|
234
|
-
assert_equal %(<p>#{link6_result} Link</p>), auto_link("<p>#{link6_raw} Link</p>")
|
235
|
-
|
236
|
-
link7_raw = 'http://foo.example.com/controller/action?parm=value&p2=v2#anchor-123'
|
237
|
-
link7_result = generate_result(link7_raw)
|
238
|
-
assert_equal %(<p>#{link7_result} Link</p>), auto_link("<p>#{link7_raw} Link</p>")
|
239
|
-
|
240
|
-
link8_raw = 'http://foo.example.com:3000/controller/action.html'
|
241
|
-
link8_result = generate_result(link8_raw)
|
242
|
-
assert_equal %(Go to #{link8_result}), auto_link("Go to #{link8_raw}", :urls)
|
243
|
-
assert_equal %(Go to #{link8_raw}), auto_link("Go to #{link8_raw}", :email_addresses)
|
244
|
-
assert_equal %(<p>Link #{link8_result}</p>), auto_link("<p>Link #{link8_raw}</p>")
|
245
|
-
assert_equal %(<p>#{link8_result} Link</p>), auto_link("<p>#{link8_raw} Link</p>")
|
246
|
-
assert_equal %(Go to #{link8_result}.), auto_link(%(Go to #{link8_raw}.))
|
247
|
-
assert_equal %(<p>Go to #{link8_result}. Seriously, #{link8_result}? I think I'll say hello to #{email_result}. Instead.</p>),
|
248
|
-
auto_link(%(<p>Go to #{link8_raw}. Seriously, #{link8_raw}? I think I'll say hello to #{email_raw}. Instead.</p>))
|
249
|
-
|
250
|
-
link9_raw = 'http://business.timesonline.co.uk/article/0,,9065-2473189,00.html'
|
251
|
-
link9_result = generate_result(link9_raw)
|
252
|
-
assert_equal %(Go to #{link9_result}), auto_link("Go to #{link9_raw}", :urls)
|
253
|
-
assert_equal %(Go to #{link9_raw}), auto_link("Go to #{link9_raw}", :email_addresses)
|
254
|
-
assert_equal %(<p>Link #{link9_result}</p>), auto_link("<p>Link #{link9_raw}</p>")
|
255
|
-
assert_equal %(<p>#{link9_result} Link</p>), auto_link("<p>#{link9_raw} Link</p>")
|
256
|
-
assert_equal %(Go to #{link9_result}.), auto_link(%(Go to #{link9_raw}.))
|
257
|
-
assert_equal %(<p>Go to #{link9_result}. Seriously, #{link9_result}? I think I'll say hello to #{email_result}. Instead.</p>),
|
258
|
-
auto_link(%(<p>Go to #{link9_raw}. Seriously, #{link9_raw}? I think I'll say hello to #{email_raw}. Instead.</p>))
|
259
|
-
|
260
|
-
link10_raw = 'http://www.mail-archive.com/ruby-talk@ruby-lang.org/'
|
261
|
-
link10_result = generate_result(link10_raw)
|
262
|
-
assert_equal %(<p>#{link10_result} Link</p>), auto_link("<p>#{link10_raw} Link</p>")
|
263
|
-
|
264
|
-
link11_raw = 'http://asakusa.rubyist.net/'
|
265
|
-
link11_result = generate_result(link11_raw)
|
266
|
-
with_kcode 'u' do
|
267
|
-
assert_equal %(浅草.rbの公式サイトはこちら#{link11_result}), auto_link("浅草.rbの公式サイトはこちら#{link11_raw}")
|
268
|
-
end
|
269
|
-
end
|
270
|
-
|
271
|
-
def test_auto_link_parsing
|
272
|
-
urls = %w(
|
273
|
-
http://www.rubyonrails.com
|
274
|
-
http://www.rubyonrails.com:80
|
275
|
-
http://www.rubyonrails.com/~minam
|
276
|
-
https://www.rubyonrails.com/~minam
|
277
|
-
http://www.rubyonrails.com/~minam/url%20with%20spaces
|
278
|
-
http://www.rubyonrails.com/foo.cgi?something=here
|
279
|
-
http://www.rubyonrails.com/foo.cgi?something=here&and=here
|
280
|
-
http://www.rubyonrails.com/contact;new
|
281
|
-
http://www.rubyonrails.com/contact;new%20with%20spaces
|
282
|
-
http://www.rubyonrails.com/contact;new?with=query&string=params
|
283
|
-
http://www.rubyonrails.com/~minam/contact;new?with=query&string=params
|
284
|
-
http://en.wikipedia.org/wiki/Wikipedia:Today%27s_featured_picture_%28animation%29/January_20%2C_2007
|
285
|
-
http://www.mail-archive.com/rails@lists.rubyonrails.org/
|
286
|
-
http://www.amazon.com/Testing-Equal-Sign-In-Path/ref=pd_bbs_sr_1?ie=UTF8&s=books&qid=1198861734&sr=8-1
|
287
|
-
http://en.wikipedia.org/wiki/Texas_hold'em
|
288
|
-
https://www.google.com/doku.php?id=gps:resource:scs:start
|
289
|
-
http://connect.oraclecorp.com/search?search[q]=green+france&search[type]=Group
|
290
|
-
http://of.openfoundry.org/projects/492/download#4th.Release.3
|
291
|
-
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
|
292
|
-
http://около.кола/колокола
|
293
|
-
)
|
294
|
-
|
295
|
-
urls.each do |url|
|
296
|
-
assert_equal generate_result(url), auto_link(url)
|
297
|
-
end
|
298
|
-
end
|
299
|
-
|
300
|
-
private
|
301
|
-
def generate_result(link_text, href = nil, escape = false)
|
302
|
-
href ||= link_text
|
303
|
-
if escape
|
304
|
-
%{<a href="#{CGI::escapeHTML href}">#{CGI::escapeHTML link_text}</a>}
|
305
|
-
else
|
306
|
-
%{<a href="#{href}">#{link_text}</a>}
|
307
|
-
end
|
308
|
-
end
|
309
|
-
|
310
|
-
# from ruby core
|
311
|
-
def build_message(head, template=nil, *arguments)
|
312
|
-
template &&= template.chomp
|
313
|
-
template.gsub(/\?/) { mu_pp(arguments.shift) }
|
314
|
-
end
|
315
|
-
|
316
|
-
# Temporarily replaces KCODE for the block
|
317
|
-
def with_kcode(kcode)
|
318
|
-
if RUBY_VERSION < '1.9'
|
319
|
-
old_kcode, $KCODE = $KCODE, kcode
|
320
|
-
begin
|
321
|
-
yield
|
322
|
-
ensure
|
323
|
-
$KCODE = old_kcode
|
324
|
-
end
|
325
|
-
else
|
326
|
-
yield
|
327
|
-
end
|
328
|
-
end
|
329
|
-
end
|