Skip to content

Commit c5dab86

Browse files
authored
Merge pull request #366 from openstax/remove_rinku
Removed auto-links and the Rinku gem
2 parents 25344e2 + 5b3ae65 commit c5dab86

File tree

4 files changed

+15
-24
lines changed

4 files changed

+15
-24
lines changed

Gemfile

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ gem 'rails-html-sanitizer'
4141
# URI replacement
4242
gem 'addressable'
4343

44-
# Converts links in Strings to HTML anchors
45-
gem 'rinku'
46-
4744
# Sanitizes user content
4845
gem 'sanitize'
4946

Gemfile.lock

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,6 @@ GEM
399399
actionpack (>= 5.0)
400400
railties (>= 5.0)
401401
rexml (3.2.5)
402-
rinku (2.0.6)
403402
roar (1.0.3)
404403
representable (>= 2.0.1, <= 3.0.0)
405404
roar-rails (1.0.1)
@@ -589,7 +588,6 @@ DEPENDENCIES
589588
redis
590589
representable (~> 3.0.0)
591590
request_store
592-
rinku
593591
roar (= 1.0.3)
594592
roo
595593
rspec-instafail

lib/user_html.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
module UserHtml
22
mattr_accessor :sanitize_config
33

4-
def self.link_and_sanitize(content)
5-
linked_content = Rinku.auto_link(content, :urls)
6-
Sanitize.fragment(linked_content, UserHtml.sanitize_config)
4+
def self.sanitize(content)
5+
Sanitize.fragment(content, UserHtml.sanitize_config)
76
end
87

98
module ActiveRecord
109
module Base
1110
def user_html(*attributes)
1211
attributes.each do |attribute|
13-
filter_name = :"link_and_sanitize_#{attribute.to_s}"
12+
filter_name = :"sanitize_#{attribute.to_s}"
1413

1514
class_exec do
1615
before_validation filter_name
@@ -19,7 +18,7 @@ def user_html(*attributes)
1918
content = send(attribute)
2019
return if content.nil?
2120

22-
send("#{attribute}=", UserHtml.link_and_sanitize(content))
21+
send("#{attribute}=", UserHtml.sanitize(content))
2322
end
2423
end
2524
end

spec/lib/user_html_spec.rb

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,22 @@
55
expect(ActiveRecord::Base).to respond_to(:user_html)
66
end
77

8-
it 'converts url\'s to html anchors with rel="nofollow" and target="_blank"' do
8+
it 'does not auto_link urls' do
99
content = 'Here is a cool link: http://www.example.com.'
10-
expect(described_class.link_and_sanitize(content)).to(
11-
eq 'Here is a cool link: <a href="http://www.example.com" ' +
12-
'rel="nofollow" target="_blank">http://www.example.com</a>.'
13-
)
10+
expect(described_class.sanitize(content)).to eq content
1411
end
1512

1613
it 'adds rel="nofollow" and target="_blank" to existing html anchors' do
1714
content = 'Here is a cooler link: <a href="https://www.example.com">Example</a>.'
18-
expect(described_class.link_and_sanitize(content)).to(
15+
expect(described_class.sanitize(content)).to(
1916
eq 'Here is a cooler link: <a href="https://www.example.com" ' +
2017
'rel="nofollow" target="_blank">Example</a>.'
2118
)
2219
end
2320

2421
it 'removes script tags' do
2522
content = 'Have a cup of <script>1337 $cr1pt</script>.'
26-
expect(described_class.link_and_sanitize(content)).to eq 'Have a cup of .'
23+
expect(described_class.sanitize(content)).to eq 'Have a cup of .'
2724
end
2825

2926
it 'allows iframes to whitelisted domains' do
@@ -34,7 +31,7 @@
3431
'src="https://www.youtube.com/embed/Xp6V_lO1ZKA" frameborder="0" ' +
3532
'allowfullscreen=""></iframe>'
3633

37-
expect(described_class.link_and_sanitize(youtube_content)).to eq expected_youtube_content
34+
expect(described_class.sanitize(youtube_content)).to eq expected_youtube_content
3835

3936
khan_content = \
4037
"<a style=\"color: #111; font-family: helvetica;\" target=\"_blank\" " +
@@ -57,7 +54,7 @@
5754
"src=\"https://www.khanacademy.org/embed_video?v=Xp6V_lO1ZKA\" allowfullscreen=\"\" " +
5855
"webkitallowfullscreen=\"\" mozallowfullscreen=\"\"></iframe>"
5956

60-
expect(described_class.link_and_sanitize(khan_content)).to eq expected_khan_content
57+
expect(described_class.sanitize(khan_content)).to eq expected_khan_content
6158

6259
end
6360

@@ -68,33 +65,33 @@
6865
https://server2.cnx.org/content
6966
}
7067
valid_urls.each do | url |
71-
expect(described_class.link_and_sanitize(
68+
expect(described_class.sanitize(
7269
"<iframe src='#{url}' />"
7370
)).to eq "<iframe src=\"#{url}\"></iframe>"
7471
end
7572
end
7673

7774
it 'removes iframes to non-whitelisted domains' do
7875
content = "Funny cat videos: <iframe src=\"http://mal.icio.us\">"
79-
expect(described_class.link_and_sanitize(content)).to eq 'Funny cat videos: '
76+
expect(described_class.sanitize(content)).to eq 'Funny cat videos: '
8077
end
8178

8279
describe 'data-math attribute' do
8380
let (:formula){ %-\lim_{x\to\infty}f(x)=0- }
8481

8582
it 'is allowed on divs' do
8683
content = "as a block: <div data-math='#{formula}'/>"
87-
expect(described_class.link_and_sanitize(content)).to eq "as a block: <div data-math=\"#{formula}\"></div>"
84+
expect(described_class.sanitize(content)).to eq "as a block: <div data-math=\"#{formula}\"></div>"
8885
end
8986

9087
it 'is allowed on spans' do
9188
content = "as inline: <span data-math='#{formula}'/>"
92-
expect(described_class.link_and_sanitize(content)).to eq "as inline: <span data-math=\"#{formula}\"></span>"
89+
expect(described_class.sanitize(content)).to eq "as inline: <span data-math=\"#{formula}\"></span>"
9390
end
9491

9592
it 'is removed from other elements' do
9693
content = "also: <p data-math='#{formula}'/>"
97-
expect(described_class.link_and_sanitize(content)).to eq 'also: <p></p>'
94+
expect(described_class.sanitize(content)).to eq 'also: <p></p>'
9895
end
9996

10097
end

0 commit comments

Comments
 (0)