Skip to content

Commit 7f485d6

Browse files
committed
feat: add HTML5::Sanitizer to provide a vendor interface for Rails
Also add HTML4::Sanitizer for symmetry, which returns the exact same set of sanitizers as HTML::Sanitizer.
1 parent 50644ff commit 7f485d6

File tree

2 files changed

+71
-20
lines changed

2 files changed

+71
-20
lines changed

lib/rails/html/sanitizer.rb

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,6 @@ module Rails
44
module HTML
55
class Sanitizer
66
class << self
7-
def full_sanitizer
8-
Rails::HTML4::FullSanitizer
9-
end
10-
11-
def link_sanitizer
12-
Rails::HTML4::LinkSanitizer
13-
end
14-
15-
def safe_list_sanitizer
16-
Rails::HTML4::SafeListSanitizer
17-
end
18-
19-
def white_list_sanitizer # :nodoc:
20-
safe_list_sanitizer
21-
end
22-
237
def html5_support?
248
return @html5_support if defined?(@html5_support)
259

@@ -209,6 +193,28 @@ def serialize(fragment)
209193
end
210194

211195
module HTML4
196+
module Sanitizer
197+
module VendorMethods
198+
def full_sanitizer
199+
Rails::HTML4::FullSanitizer
200+
end
201+
202+
def link_sanitizer
203+
Rails::HTML4::LinkSanitizer
204+
end
205+
206+
def safe_list_sanitizer
207+
Rails::HTML4::SafeListSanitizer
208+
end
209+
210+
def white_list_sanitizer # :nodoc:
211+
safe_list_sanitizer
212+
end
213+
end
214+
215+
extend VendorMethods
216+
end
217+
212218
# == Rails::HTML4::FullSanitizer
213219
#
214220
# Removes all tags from HTML4 but strips out scripts, forms and comments.
@@ -299,6 +305,26 @@ class SafeListSanitizer < Rails::HTML::Sanitizer
299305
end
300306

301307
module HTML5
308+
class Sanitizer
309+
class << self
310+
def full_sanitizer
311+
Rails::HTML5::FullSanitizer
312+
end
313+
314+
def link_sanitizer
315+
Rails::HTML5::LinkSanitizer
316+
end
317+
318+
def safe_list_sanitizer
319+
Rails::HTML5::SafeListSanitizer
320+
end
321+
322+
def white_list_sanitizer # :nodoc:
323+
safe_list_sanitizer
324+
end
325+
end
326+
end
327+
302328
# == Rails::HTML5::FullSanitizer
303329
#
304330
# Removes all tags from HTML5 but strips out scripts, forms and comments.
@@ -389,6 +415,7 @@ class SafeListSanitizer < Rails::HTML::Sanitizer
389415
end if Rails::HTML::Sanitizer.html5_support?
390416

391417
module HTML
418+
Sanitizer.extend(HTML4::Sanitizer::VendorMethods) # :nodoc:
392419
FullSanitizer = HTML4::FullSanitizer # :nodoc:
393420
LinkSanitizer = HTML4::LinkSanitizer # :nodoc:
394421
SafeListSanitizer = HTML4::SafeListSanitizer # :nodoc:

test/rails_api_test.rb

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,43 @@ def test_html4_sanitizer_alias_safe_list
3232
assert_equal("Rails::HTML4::SafeListSanitizer", Rails::HTML::SafeListSanitizer.name)
3333
end
3434

35-
def test_full_sanitizer_returns_a_full_sanitizer
35+
def test_html4_full_sanitizer
3636
assert_equal(Rails::HTML4::FullSanitizer, Rails::HTML::Sanitizer.full_sanitizer)
37+
assert_equal(Rails::HTML4::FullSanitizer, Rails::HTML4::Sanitizer.full_sanitizer)
3738
end
3839

39-
def test_link_sanitizer_returns_a_link_sanitizer
40+
def test_html4_link_sanitizer
4041
assert_equal(Rails::HTML4::LinkSanitizer, Rails::HTML::Sanitizer.link_sanitizer)
42+
assert_equal(Rails::HTML4::LinkSanitizer, Rails::HTML4::Sanitizer.link_sanitizer)
4143
end
4244

43-
def test_safe_list_sanitizer_returns_a_safe_list_sanitizer
45+
def test_html4_safe_list_sanitizer
4446
assert_equal(Rails::HTML4::SafeListSanitizer, Rails::HTML::Sanitizer.safe_list_sanitizer)
47+
assert_equal(Rails::HTML4::SafeListSanitizer, Rails::HTML4::Sanitizer.safe_list_sanitizer)
4548
end
4649

47-
def test_white_list_sanitizer_returns_a_safe_list_sanitizer
50+
def test_html4_white_list_sanitizer
4851
assert_equal(Rails::HTML4::SafeListSanitizer, Rails::HTML::Sanitizer.white_list_sanitizer)
52+
assert_equal(Rails::HTML4::SafeListSanitizer, Rails::HTML4::Sanitizer.white_list_sanitizer)
53+
end
54+
55+
def test_html5_full_sanitizer
56+
skip("no HTML5 support on this platform") unless Rails::HTML::Sanitizer.html5_support?
57+
assert_equal(Rails::HTML5::FullSanitizer, Rails::HTML5::Sanitizer.full_sanitizer)
58+
end
59+
60+
def test_html5_link_sanitizer
61+
skip("no HTML5 support on this platform") unless Rails::HTML::Sanitizer.html5_support?
62+
assert_equal(Rails::HTML5::LinkSanitizer, Rails::HTML5::Sanitizer.link_sanitizer)
63+
end
64+
65+
def test_html5_safe_list_sanitizer
66+
skip("no HTML5 support on this platform") unless Rails::HTML::Sanitizer.html5_support?
67+
assert_equal(Rails::HTML5::SafeListSanitizer, Rails::HTML5::Sanitizer.safe_list_sanitizer)
68+
end
69+
70+
def test_html5_white_list_sanitizer
71+
skip("no HTML5 support on this platform") unless Rails::HTML::Sanitizer.html5_support?
72+
assert_equal(Rails::HTML5::SafeListSanitizer, Rails::HTML5::Sanitizer.white_list_sanitizer)
4973
end
5074
end

0 commit comments

Comments
 (0)