Skip to content

Commit 2069426

Browse files
committed
move the sanitizers under the Rails::HTML4 namespace
and test that the sanitizer class names are HTML4 variations
1 parent 5836d1d commit 2069426

File tree

3 files changed

+112
-75
lines changed

3 files changed

+112
-75
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,16 @@ safe_list_sanitizer = Rails::Html::SafeListSanitizer.new
7878
# sanitize via an extensive safe list of allowed elements
7979
safe_list_sanitizer.sanitize(@article.body)
8080

81-
# safe list only the supplied tags and attributes
81+
# sanitize only the supplied tags and attributes
8282
safe_list_sanitizer.sanitize(@article.body, tags: %w(table tr td), attributes: %w(id class style))
8383

84-
# safe list via a custom scrubber
84+
# sanitize via a custom scrubber
8585
safe_list_sanitizer.sanitize(@article.body, scrubber: ArticleScrubber.new)
8686

87-
# safe list sanitizer can also sanitize css
87+
# the sanitizer can also sanitize css
8888
safe_list_sanitizer.sanitize_css('background-color: #000;')
8989

90-
# fully prune nodes from the tree instead of stripping tags and leaving inner content
90+
# prune nodes from the tree instead of stripping tags and leaving inner content
9191
safe_list_sanitizer = Rails::Html::SafeListSanitizer.new(prune: true)
9292
```
9393

lib/rails/html/sanitizer.rb

Lines changed: 89 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ module HTML
55
class Sanitizer
66
class << self
77
def full_sanitizer
8-
Rails::HTML::FullSanitizer
8+
Rails::HTML4::FullSanitizer
99
end
1010

1111
def link_sanitizer
12-
Rails::HTML::LinkSanitizer
12+
Rails::HTML4::LinkSanitizer
1313
end
1414

1515
def safe_list_sanitizer
16-
Rails::HTML::SafeListSanitizer
16+
Rails::HTML4::SafeListSanitizer
1717
end
1818

1919
def white_list_sanitizer # :nodoc:
@@ -36,8 +36,8 @@ def properly_encode(fragment, options)
3636
end
3737
end
3838

39-
module Concern # :nodoc:
40-
module ComposedSanitize # :nodoc:
39+
module Concern
40+
module ComposedSanitize
4141
def sanitize(html, options = {})
4242
return unless html
4343
return html if html.empty?
@@ -46,22 +46,22 @@ def sanitize(html, options = {})
4646
end
4747
end
4848

49-
module Parser # :nodoc:
50-
module HTML4 # :nodoc:
49+
module Parser
50+
module HTML4
5151
def parse_fragment(html)
5252
Loofah.html4_fragment(html)
5353
end
5454
end
5555
end
5656

57-
module Scrubber # :nodoc:
58-
module Full # :nodoc:
57+
module Scrubber
58+
module Full
5959
def scrub(fragment, options = {})
6060
fragment.scrub!(TextOnlyScrubber.new)
6161
end
6262
end
6363

64-
module Link # :nodoc:
64+
module Link
6565
def initialize
6666
super
6767
@link_scrubber = TargetScrubber.new
@@ -74,7 +74,8 @@ def scrub(fragment, options = {})
7474
end
7575
end
7676

77-
module SafeList # :nodoc:
77+
module SafeList
78+
# The default safe list for tags
7879
DEFAULT_ALLOWED_TAGS = Set.new([
7980
"a",
8081
"abbr",
@@ -119,6 +120,8 @@ module SafeList # :nodoc:
119120
"ul",
120121
"var",
121122
]).freeze
123+
124+
# The default safe list for attributes
122125
DEFAULT_ALLOWED_ATTRIBUTES = Set.new([
123126
"abbr",
124127
"alt",
@@ -177,97 +180,116 @@ def allowed_attributes(options)
177180
end
178181
end
179182

180-
module Serializer # :nodoc:
181-
module UTF8Encode # :nodoc:
183+
module Serializer
184+
module UTF8Encode
182185
def serialize(fragment)
183186
properly_encode(fragment, encoding: "UTF-8")
184187
end
185188
end
186189

187-
module SimpleString # :nodoc:
190+
module SimpleString
188191
def serialize(fragment)
189192
fragment.to_s
190193
end
191194
end
192195
end
193196
end
197+
end
194198

195-
# === Rails::HTML::FullSanitizer
196-
# Removes all tags but strips out scripts, forms and comments.
199+
module HTML4
200+
# == Rails::HTML4::FullSanitizer
201+
#
202+
# Removes all tags from HTML4 but strips out scripts, forms and comments.
203+
#
204+
# full_sanitizer = Rails::HTML4::FullSanitizer.new
205+
# full_sanitizer.sanitize("<b>Bold</b> no more! <a href='more.html'>See more here</a>...")
206+
# # => "Bold no more! See more here..."
197207
#
198-
# full_sanitizer = Rails::HTML::FullSanitizer.new
199-
# full_sanitizer.sanitize("<b>Bold</b> no more! <a href='more.html'>See more here</a>...")
200-
# # => Bold no more! See more here...
201-
class FullSanitizer < Sanitizer
202-
include Concern::ComposedSanitize
203-
include Concern::Parser::HTML4
204-
include Concern::Scrubber::Full
205-
include Concern::Serializer::UTF8Encode
208+
class FullSanitizer < Rails::HTML::Sanitizer
209+
include HTML::Concern::ComposedSanitize
210+
include HTML::Concern::Parser::HTML4
211+
include HTML::Concern::Scrubber::Full
212+
include HTML::Concern::Serializer::UTF8Encode
206213
end
207214

208-
# === Rails::HTML::LinkSanitizer
209-
# Removes +a+ tags and +href+ attributes leaving only the link text.
215+
# == Rails::HTML4::LinkSanitizer
210216
#
211-
# link_sanitizer = Rails::HTML::LinkSanitizer.new
212-
# link_sanitizer.sanitize('<a href="example.com">Only the link text will be kept.</a>')
217+
# Removes +a+ tags and +href+ attributes from HTML4 leaving only the link text.
213218
#
214-
# => 'Only the link text will be kept.'
215-
class LinkSanitizer < Sanitizer
216-
include Concern::ComposedSanitize
217-
include Concern::Parser::HTML4
218-
include Concern::Scrubber::Link
219-
include Concern::Serializer::SimpleString
219+
# link_sanitizer = Rails::HTML4::LinkSanitizer.new
220+
# link_sanitizer.sanitize('<a href="example.com">Only the link text will be kept.</a>')
221+
# # => "Only the link text will be kept."
222+
#
223+
class LinkSanitizer < Rails::HTML::Sanitizer
224+
include HTML::Concern::ComposedSanitize
225+
include HTML::Concern::Parser::HTML4
226+
include HTML::Concern::Scrubber::Link
227+
include HTML::Concern::Serializer::SimpleString
220228
end
221229

222-
# === Rails::HTML::SafeListSanitizer
223-
# Sanitizes html and css from an extensive safe list (see link further down).
230+
# == Rails::HTML4::SafeListSanitizer
231+
#
232+
# Sanitizes HTML4 and CSS from an extensive safe list.
224233
#
225234
# === Whitespace
226-
# We can't make any guarantees about whitespace being kept or stripped.
227-
# Loofah uses Nokogiri, which wraps either a C or Java parser for the
228-
# respective Ruby implementation.
229-
# Those two parsers determine how whitespace is ultimately handled.
230235
#
231-
# When the stripped markup will be rendered the users browser won't take
232-
# whitespace into account anyway. It might be better to suggest your users
233-
# wrap their whitespace sensitive content in pre tags or that you do
234-
# so automatically.
236+
# We can't make any guarantees about whitespace being kept or stripped. Loofah uses Nokogiri,
237+
# which wraps either a C or Java parser for the respective Ruby implementation. Those two
238+
# parsers determine how whitespace is ultimately handled.
239+
#
240+
# When the stripped markup will be rendered the users browser won't take whitespace into account
241+
# anyway. It might be better to suggest your users wrap their whitespace sensitive content in
242+
# pre tags or that you do so automatically.
235243
#
236244
# === Options
237-
# Sanitizes both html and css via the safe lists found here:
238-
# https://github.com/flavorjones/loofah/blob/master/lib/loofah/html5/safelist.rb
239245
#
240-
# SafeListSanitizer also accepts options to configure
241-
# the safe list used when sanitizing html.
246+
# Sanitizes both html and css via the safe lists found in
247+
# Rails::HTML::Concern::Scrubber::SafeList
248+
#
249+
# SafeListSanitizer also accepts options to configure the safe list used when sanitizing html.
242250
# There's a class level option:
243-
# Rails::HTML::SafeListSanitizer.allowed_tags = %w(table tr td)
244-
# Rails::HTML::SafeListSanitizer.allowed_attributes = %w(id class style)
245251
#
246-
# Tags and attributes can also be passed to +sanitize+.
247-
# Passed options take precedence over the class level options.
252+
# Rails::HTML4::SafeListSanitizer.allowed_tags = %w(table tr td)
253+
# Rails::HTML4::SafeListSanitizer.allowed_attributes = %w(id class style)
254+
#
255+
# Tags and attributes can also be passed to +sanitize+. Passed options take precedence over the
256+
# class level options.
248257
#
249258
# === Examples
250-
# safe_list_sanitizer = Rails::HTML::SafeListSanitizer.new
251259
#
252-
# Sanitize css doesn't take options
253-
# safe_list_sanitizer.sanitize_css('background-color: #000;')
260+
# safe_list_sanitizer = Rails::HTML4::SafeListSanitizer.new
261+
#
262+
# # default: sanitize via a extensive safe list of allowed elements
263+
# safe_list_sanitizer.sanitize(@article.body)
264+
#
265+
# # sanitize via the supplied tags and attributes
266+
# safe_list_sanitizer.sanitize(
267+
# @article.body,
268+
# tags: %w(table tr td),
269+
# attributes: %w(id class style),
270+
# )
254271
#
255-
# Default: sanitize via a extensive safe list of allowed elements
256-
# safe_list_sanitizer.sanitize(@article.body)
272+
# # sanitize via a custom Loofah scrubber
273+
# safe_list_sanitizer.sanitize(@article.body, scrubber: ArticleScrubber.new)
257274
#
258-
# Safe list via the supplied tags and attributes
259-
# safe_list_sanitizer.sanitize(@article.body, tags: %w(table tr td),
260-
# attributes: %w(id class style))
275+
# # prune nodes from the tree instead of stripping tags and leaving inner content
276+
# safe_list_sanitizer = Rails::HTML4::SafeListSanitizer.new(prune: true)
261277
#
262-
# Safe list via a custom scrubber
263-
# safe_list_sanitizer.sanitize(@article.body, scrubber: ArticleScrubber.new)
264-
class SafeListSanitizer < Sanitizer
265-
include Concern::ComposedSanitize
266-
include Concern::Parser::HTML4
267-
include Concern::Scrubber::SafeList
268-
include Concern::Serializer::UTF8Encode
278+
# # the sanitizer can also sanitize CSS
279+
# safe_list_sanitizer.sanitize_css('background-color: #000;')
280+
#
281+
class SafeListSanitizer < Rails::HTML::Sanitizer
282+
include HTML::Concern::ComposedSanitize
283+
include HTML::Concern::Parser::HTML4
284+
include HTML::Concern::Scrubber::SafeList
285+
include HTML::Concern::Serializer::UTF8Encode
269286
end
287+
end
270288

289+
module HTML
290+
FullSanitizer = HTML4::FullSanitizer # :nodoc:
291+
LinkSanitizer = HTML4::LinkSanitizer # :nodoc:
292+
SafeListSanitizer = HTML4::SafeListSanitizer # :nodoc:
271293
WhiteListSanitizer = SafeListSanitizer # :nodoc:
272294
end
273295
end

test/rails_api_test.rb

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,34 @@ def test_html_scrubber_class_names
1717
assert(Rails::Html::Sanitizer)
1818
end
1919

20+
def test_html4_sanitizer_alias_full
21+
assert_equal(Rails::HTML4::FullSanitizer, Rails::HTML::FullSanitizer)
22+
assert_equal("Rails::HTML4::FullSanitizer", Rails::HTML::FullSanitizer.name)
23+
end
24+
25+
def test_html4_sanitizer_alias_link
26+
assert_equal(Rails::HTML4::LinkSanitizer, Rails::HTML::LinkSanitizer)
27+
assert_equal("Rails::HTML4::LinkSanitizer", Rails::HTML::LinkSanitizer.name)
28+
end
29+
30+
def test_html4_sanitizer_alias_safe_list
31+
assert_equal(Rails::HTML4::SafeListSanitizer, Rails::HTML::SafeListSanitizer)
32+
assert_equal("Rails::HTML4::SafeListSanitizer", Rails::HTML::SafeListSanitizer.name)
33+
end
34+
2035
def test_full_sanitizer_returns_a_full_sanitizer
21-
assert_equal(Rails::Html::FullSanitizer, Rails::Html::Sanitizer.full_sanitizer)
36+
assert_equal(Rails::HTML4::FullSanitizer, Rails::HTML::Sanitizer.full_sanitizer)
2237
end
2338

2439
def test_link_sanitizer_returns_a_link_sanitizer
25-
assert_equal(Rails::Html::LinkSanitizer, Rails::Html::Sanitizer.link_sanitizer)
40+
assert_equal(Rails::HTML4::LinkSanitizer, Rails::HTML::Sanitizer.link_sanitizer)
2641
end
2742

2843
def test_safe_list_sanitizer_returns_a_safe_list_sanitizer
29-
assert_equal(Rails::Html::SafeListSanitizer, Rails::Html::Sanitizer.safe_list_sanitizer)
44+
assert_equal(Rails::HTML4::SafeListSanitizer, Rails::HTML::Sanitizer.safe_list_sanitizer)
3045
end
3146

3247
def test_white_list_sanitizer_returns_a_safe_list_sanitizer
33-
assert_equal(Rails::Html::SafeListSanitizer, Rails::Html::Sanitizer.white_list_sanitizer)
48+
assert_equal(Rails::HTML4::SafeListSanitizer, Rails::HTML::Sanitizer.white_list_sanitizer)
3449
end
3550
end

0 commit comments

Comments
 (0)