rails_autolink 1.0.5 → 1.0.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.
@@ -1,3 +1,7 @@
1
+ === 1.0.6 / 2012-03-12
2
+
3
+ * Added sanitize_options arg
4
+
1
5
  === 1.0.5 / 2012-01-27
2
6
 
3
7
  * Update dependency to include rails 3.2.X
@@ -1,5 +1,5 @@
1
1
  module RailsAutolink
2
- VERSION = '1.0.5'
2
+ VERSION = '1.0.6'
3
3
 
4
4
  class Railtie < ::Rails::Railtie
5
5
  initializer 'rails_autolink' do |app|
@@ -13,7 +13,8 @@ module RailsAutolink
13
13
  # <tt>:email_addresses</tt>, and <tt>:urls</tt>. If a block is given, each URL and
14
14
  # e-mail address is yielded and the result is used as the link text. By default the
15
15
  # text given is sanitized, you can override this behaviour setting the
16
- # <tt>:sanitize</tt> option to false.
16
+ # <tt>:sanitize</tt> option to false, or you can add options to the sanitization of
17
+ # the text using the <tt>:sanitize_options</tt> option hash.
17
18
  #
18
19
  # ==== Examples
19
20
  # auto_link("Go to http://www.rubyonrails.org and say hello to david@loudthinking.com")
@@ -55,8 +56,9 @@ module RailsAutolink
55
56
  options[:html] = args[1] || {}
56
57
  end
57
58
  options.reverse_merge!(:link => :all, :html => {})
58
- sanitize = (options[:sanitize] != false)
59
- text = conditional_sanitize(text, sanitize).to_str
59
+ sanitize = (options[:sanitize] != false)
60
+ sanitize_options = options[:sanitize_options] || {}
61
+ text = conditional_sanitize(text, sanitize, sanitize_options).to_str
60
62
  case options[:link].to_sym
61
63
  when :all then conditional_html_safe(auto_link_email_addresses(auto_link_urls(text, options[:html], options, &block), options[:html], &block), sanitize)
62
64
  when :email_addresses then conditional_html_safe(auto_link_email_addresses(text, options[:html], &block), sanitize)
@@ -137,8 +139,8 @@ module RailsAutolink
137
139
  (left.rindex(AUTO_LINK_CRE[2]) and $' !~ AUTO_LINK_CRE[3])
138
140
  end
139
141
 
140
- def conditional_sanitize(target, condition)
141
- condition ? sanitize(target) : target
142
+ def conditional_sanitize(target, condition, sanitize_options = {})
143
+ condition ? sanitize(target, sanitize_options) : target
142
144
  end
143
145
 
144
146
  def conditional_html_safe(target, condition)
@@ -0,0 +1,64 @@
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
+
30
+ def test_loco
31
+ t = "OOOOOOOOOOOOOOOOOOOOOOO <h1>textile<a href=\"http://ruby-lang.org\" class='asdasd' target='_blank' >Ruby</a>\n</h1> otro link: www.hola.com"
32
+ assert_equal "loco", auto_link(t, :link=> 'urls', :sanitize_options => {:attributes => ["target", "class"], :tags=>[]}, :html=> {:target => '_blank', :mememe=> 'MEM'})
33
+ end
34
+
35
+ private
36
+ def generate_result(link_text, href = nil, escape = false)
37
+ href ||= link_text
38
+ if escape
39
+ %{<a href="#{CGI::escapeHTML href}">#{CGI::escapeHTML link_text}</a>}
40
+ else
41
+ %{<a href="#{href}">#{link_text}</a>}
42
+ end
43
+ end
44
+
45
+ # from ruby core
46
+ def build_message(head, template=nil, *arguments)
47
+ template &&= template.chomp
48
+ template.gsub(/\?/) { mu_pp(arguments.shift) }
49
+ end
50
+
51
+ # Temporarily replaces KCODE for the block
52
+ def with_kcode(kcode)
53
+ if RUBY_VERSION < '1.9'
54
+ old_kcode, $KCODE = $KCODE, kcode
55
+ begin
56
+ yield
57
+ ensure
58
+ $KCODE = old_kcode
59
+ end
60
+ else
61
+ yield
62
+ end
63
+ end
64
+ end
@@ -88,6 +88,21 @@ class TestRailsAutolink < MiniTest::Unit::TestCase
88
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
89
  assert auto_link("#{link_raw}#{malicious_script}").html_safe?
90
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
91
106
 
92
107
  def test_auto_link_should_not_sanitize_input_when_sanitize_option_is_false
93
108
  link_raw = %{http://www.rubyonrails.com?id=1&num=2}
@@ -117,11 +132,13 @@ class TestRailsAutolink < MiniTest::Unit::TestCase
117
132
  linked3 = %('<a href="http://www.example.com" rel="nofollow">www.example.com</a>')
118
133
  linked4 = %('<a href="http://www.example.com"><b>www.example.com</b></a>')
119
134
  linked5 = %('<a href="#close">close</a> <a href="http://www.example.com"><b>www.example.com</b></a>')
135
+ linked6 = %('<a href="#close">close</a> <a href="http://www.example.com" target="_blank" data-ruby="ror"><b>www.example.com</b></a>')
120
136
  assert_equal linked1, auto_link(linked1)
121
137
  assert_equal linked2, auto_link(linked2)
122
138
  assert_equal linked3, auto_link(linked3, :sanitize => false)
123
139
  assert_equal linked4, auto_link(linked4)
124
140
  assert_equal linked5, auto_link(linked5)
141
+ assert_equal linked6, auto_link(linked6, :sanitize_options => {:attributes => ["href", "target", "data-ruby"]})
125
142
 
126
143
  linked_email = %Q(<a href="mailto:david@loudthinking.com">Mail me</a>)
127
144
  assert_equal linked_email, auto_link(linked_email)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_autolink
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.5
4
+ version: 1.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,11 +11,11 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2012-01-27 00:00:00.000000000Z
14
+ date: 2012-03-12 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rails
18
- requirement: &2157959100 !ruby/object:Gem::Requirement
18
+ requirement: &2152957660 !ruby/object:Gem::Requirement
19
19
  none: false
20
20
  requirements:
21
21
  - - ~>
@@ -23,10 +23,10 @@ dependencies:
23
23
  version: '3.1'
24
24
  type: :runtime
25
25
  prerelease: false
26
- version_requirements: *2157959100
26
+ version_requirements: *2152957660
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: minitest
29
- requirement: &2157958620 !ruby/object:Gem::Requirement
29
+ requirement: &2152956620 !ruby/object:Gem::Requirement
30
30
  none: false
31
31
  requirements:
32
32
  - - ~>
@@ -34,10 +34,10 @@ dependencies:
34
34
  version: '2.11'
35
35
  type: :development
36
36
  prerelease: false
37
- version_requirements: *2157958620
37
+ version_requirements: *2152956620
38
38
  - !ruby/object:Gem::Dependency
39
39
  name: rdoc
40
- requirement: &2157958080 !ruby/object:Gem::Requirement
40
+ requirement: &2152955520 !ruby/object:Gem::Requirement
41
41
  none: false
42
42
  requirements:
43
43
  - - ~>
@@ -45,10 +45,10 @@ dependencies:
45
45
  version: '3.10'
46
46
  type: :development
47
47
  prerelease: false
48
- version_requirements: *2157958080
48
+ version_requirements: *2152955520
49
49
  - !ruby/object:Gem::Dependency
50
50
  name: hoe
51
- requirement: &2157957660 !ruby/object:Gem::Requirement
51
+ requirement: &2152954180 !ruby/object:Gem::Requirement
52
52
  none: false
53
53
  requirements:
54
54
  - - ~>
@@ -56,7 +56,7 @@ dependencies:
56
56
  version: '2.13'
57
57
  type: :development
58
58
  prerelease: false
59
- version_requirements: *2157957660
59
+ version_requirements: *2152954180
60
60
  description: ! 'This is an extraction of the `auto_link` method from rails. The `auto_link`
61
61
 
62
62
  method was removed from Rails in version Rails 3.1. This gem is meant to
@@ -82,6 +82,7 @@ files:
82
82
  - lib/rails_autolink.rb
83
83
  - lib/rails_autolink/helpers.rb
84
84
  - test/test_rails_autolink.rb
85
+ - test/test_loco.rb
85
86
  - .gemtest
86
87
  homepage: http://github.com/tenderlove/rails_autolink
87
88
  licenses: []
@@ -105,9 +106,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
105
106
  version: '0'
106
107
  requirements: []
107
108
  rubyforge_project: rails_autolink
108
- rubygems_version: 1.8.12
109
+ rubygems_version: 1.8.15
109
110
  signing_key:
110
111
  specification_version: 3
111
112
  summary: This is an extraction of the `auto_link` method from rails
112
113
  test_files:
114
+ - test/test_loco.rb
113
115
  - test/test_rails_autolink.rb