Skip to content

Commit 5836d1d

Browse files
committed
naming: Rails::Html is now Rails::HTML
but Rails::Html is an alias for backwards compatibility
1 parent 2ada04e commit 5836d1d

File tree

8 files changed

+56
-34
lines changed

8 files changed

+56
-34
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99

1010
*Mike Dalessio*
1111

12+
* `Rails::Html` has been renamed to `Rails::HTML`, but this module is aliased to `Rails::Html` for
13+
backwards compatibility.
14+
15+
*Mike Dalessio*
16+
1217

1318
## 1.5.0 / 2023-01-20
1419

lib/rails-html-sanitizer.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
require_relative "rails/html/scrubbers"
88
require_relative "rails/html/sanitizer"
99

10+
module Rails
11+
Html = HTML # :nodoc:
12+
end
13+
1014
module ActionView
1115
module Helpers
1216
module SanitizeHelper

lib/rails/html/sanitizer.rb

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
# frozen_string_literal: true
22

33
module Rails
4-
module Html
4+
module HTML
55
class Sanitizer
66
class << self
77
def full_sanitizer
8-
Rails::Html::FullSanitizer
8+
Rails::HTML::FullSanitizer
99
end
1010

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

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

1919
def white_list_sanitizer # :nodoc:
@@ -47,7 +47,7 @@ def sanitize(html, options = {})
4747
end
4848

4949
module Parser # :nodoc:
50-
module Html4 # :nodoc:
50+
module HTML4 # :nodoc:
5151
def parse_fragment(html)
5252
Loofah.html4_fragment(html)
5353
end
@@ -192,34 +192,34 @@ def serialize(fragment)
192192
end
193193
end
194194

195-
# === Rails::Html::FullSanitizer
195+
# === Rails::HTML::FullSanitizer
196196
# Removes all tags but strips out scripts, forms and comments.
197197
#
198-
# full_sanitizer = Rails::Html::FullSanitizer.new
198+
# full_sanitizer = Rails::HTML::FullSanitizer.new
199199
# full_sanitizer.sanitize("<b>Bold</b> no more! <a href='more.html'>See more here</a>...")
200200
# # => Bold no more! See more here...
201201
class FullSanitizer < Sanitizer
202202
include Concern::ComposedSanitize
203-
include Concern::Parser::Html4
203+
include Concern::Parser::HTML4
204204
include Concern::Scrubber::Full
205205
include Concern::Serializer::UTF8Encode
206206
end
207207

208-
# === Rails::Html::LinkSanitizer
208+
# === Rails::HTML::LinkSanitizer
209209
# Removes +a+ tags and +href+ attributes leaving only the link text.
210210
#
211-
# link_sanitizer = Rails::Html::LinkSanitizer.new
211+
# link_sanitizer = Rails::HTML::LinkSanitizer.new
212212
# link_sanitizer.sanitize('<a href="example.com">Only the link text will be kept.</a>')
213213
#
214214
# => 'Only the link text will be kept.'
215215
class LinkSanitizer < Sanitizer
216216
include Concern::ComposedSanitize
217-
include Concern::Parser::Html4
217+
include Concern::Parser::HTML4
218218
include Concern::Scrubber::Link
219219
include Concern::Serializer::SimpleString
220220
end
221221

222-
# === Rails::Html::SafeListSanitizer
222+
# === Rails::HTML::SafeListSanitizer
223223
# Sanitizes html and css from an extensive safe list (see link further down).
224224
#
225225
# === Whitespace
@@ -240,14 +240,14 @@ class LinkSanitizer < Sanitizer
240240
# SafeListSanitizer also accepts options to configure
241241
# the safe list used when sanitizing html.
242242
# 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)
243+
# Rails::HTML::SafeListSanitizer.allowed_tags = %w(table tr td)
244+
# Rails::HTML::SafeListSanitizer.allowed_attributes = %w(id class style)
245245
#
246246
# Tags and attributes can also be passed to +sanitize+.
247247
# Passed options take precedence over the class level options.
248248
#
249249
# === Examples
250-
# safe_list_sanitizer = Rails::Html::SafeListSanitizer.new
250+
# safe_list_sanitizer = Rails::HTML::SafeListSanitizer.new
251251
#
252252
# Sanitize css doesn't take options
253253
# safe_list_sanitizer.sanitize_css('background-color: #000;')
@@ -263,7 +263,7 @@ class LinkSanitizer < Sanitizer
263263
# safe_list_sanitizer.sanitize(@article.body, scrubber: ArticleScrubber.new)
264264
class SafeListSanitizer < Sanitizer
265265
include Concern::ComposedSanitize
266-
include Concern::Parser::Html4
266+
include Concern::Parser::HTML4
267267
include Concern::Scrubber::SafeList
268268
include Concern::Serializer::UTF8Encode
269269
end

lib/rails/html/sanitizer/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
module Rails
4-
module Html
4+
module HTML
55
class Sanitizer
66
VERSION = "1.6.0.dev"
77
end

lib/rails/html/scrubbers.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# frozen_string_literal: true
22

33
module Rails
4-
module Html
5-
# === Rails::Html::PermitScrubber
4+
module HTML
5+
# === Rails::HTML::PermitScrubber
66
#
7-
# +Rails::Html::PermitScrubber+ allows you to permit only your own tags and/or attributes.
7+
# +Rails::HTML::PermitScrubber+ allows you to permit only your own tags and/or attributes.
88
#
9-
# +Rails::Html::PermitScrubber+ can be subclassed to determine:
9+
# +Rails::HTML::PermitScrubber+ can be subclassed to determine:
1010
# - When a node should be skipped via +skip_node?+.
1111
# - When a node is allowed via +allowed_node?+.
1212
# - When an attribute should be scrubbed via +scrub_attribute?+.
@@ -29,7 +29,7 @@ module Html
2929
# If set, attributes excluded will be removed.
3030
# If not, attributes are removed based on Loofahs +HTML5::Scrub.scrub_attributes+.
3131
#
32-
# class CommentScrubber < Html::PermitScrubber
32+
# class CommentScrubber < Rails::HTML::PermitScrubber
3333
# def initialize
3434
# super
3535
# self.tags = %w(form script comment blockquote)
@@ -158,10 +158,10 @@ def scrub_attribute(node, attr_node)
158158
end
159159
end
160160

161-
# === Rails::Html::TargetScrubber
161+
# === Rails::HTML::TargetScrubber
162162
#
163-
# Where +Rails::Html::PermitScrubber+ picks out tags and attributes to permit in
164-
# sanitization, +Rails::Html::TargetScrubber+ targets them for removal.
163+
# Where +Rails::HTML::PermitScrubber+ picks out tags and attributes to permit in
164+
# sanitization, +Rails::HTML::TargetScrubber+ targets them for removal.
165165
#
166166
# +tags=+
167167
# If set, elements included will be stripped.
@@ -178,9 +178,9 @@ def scrub_attribute?(name)
178178
end
179179
end
180180

181-
# === Rails::Html::TextOnlyScrubber
181+
# === Rails::HTML::TextOnlyScrubber
182182
#
183-
# +Rails::Html::TextOnlyScrubber+ allows you to permit text nodes.
183+
# +Rails::HTML::TextOnlyScrubber+ allows you to permit text nodes.
184184
#
185185
# Unallowed elements will be stripped, i.e. element is removed but its subtree kept.
186186
class TextOnlyScrubber < Loofah::Scrubber

rails-html-sanitizer.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require_relative "lib/rails/html/sanitizer/version"
55

66
Gem::Specification.new do |spec|
77
spec.name = "rails-html-sanitizer"
8-
spec.version = Rails::Html::Sanitizer::VERSION
8+
spec.version = Rails::HTML::Sanitizer::VERSION
99
spec.authors = ["Rafael Mendonça França", "Kasper Timm Hansen"]
1010
spec.email = ["rafaelmfranca@gmail.com", "kaspth@gmail.com"]
1111
spec.description = "HTML sanitization for Rails applications"

test/rails_api_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@
44
require "rails-html-sanitizer"
55

66
class RailsApiTest < Minitest::Test
7+
def test_html_module_name_alias
8+
assert_equal(Rails::Html, Rails::HTML)
9+
assert_equal("Rails::HTML", Rails::Html.name)
10+
assert_equal("Rails::HTML", Rails::HTML.name)
11+
end
12+
13+
def test_html_scrubber_class_names
14+
assert(Rails::Html::PermitScrubber)
15+
assert(Rails::Html::TargetScrubber)
16+
assert(Rails::Html::TextOnlyScrubber)
17+
assert(Rails::Html::Sanitizer)
18+
end
19+
720
def test_full_sanitizer_returns_a_full_sanitizer
821
assert_equal(Rails::Html::FullSanitizer, Rails::Html::Sanitizer.full_sanitizer)
922
end

test/scrubbers_test.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def assert_scrub_returns(return_value, text)
3434

3535
class PermitScrubberTest < ScrubberTest
3636
def setup
37-
@scrubber = Rails::Html::PermitScrubber.new
37+
@scrubber = Rails::HTML::PermitScrubber.new
3838
end
3939

4040
def test_responds_to_scrub
@@ -80,7 +80,7 @@ def test_leaves_only_supplied_tags
8080
end
8181

8282
def test_prunes_tags
83-
@scrubber = Rails::Html::PermitScrubber.new(prune: true)
83+
@scrubber = Rails::HTML::PermitScrubber.new(prune: true)
8484
@scrubber.tags = %w(tag)
8585
html = "<tag>leave me <span>now</span></tag>"
8686
assert_scrubbed html, "<tag>leave me </tag>"
@@ -150,7 +150,7 @@ def test_attributes_accessor_validation
150150

151151
class TargetScrubberTest < ScrubberTest
152152
def setup
153-
@scrubber = Rails::Html::TargetScrubber.new
153+
@scrubber = Rails::HTML::TargetScrubber.new
154154
end
155155

156156
def test_targeting_tags_removes_only_them
@@ -179,7 +179,7 @@ def test_targeting_tags_and_attributes_removes_only_them
179179
end
180180

181181
def test_prunes_tags
182-
@scrubber = Rails::Html::TargetScrubber.new(prune: true)
182+
@scrubber = Rails::HTML::TargetScrubber.new(prune: true)
183183
@scrubber.tags = %w(span)
184184
html = "<tag>leave me <span>now</span></tag>"
185185
assert_scrubbed html, "<tag>leave me </tag>"
@@ -188,7 +188,7 @@ def test_prunes_tags
188188

189189
class TextOnlyScrubberTest < ScrubberTest
190190
def setup
191-
@scrubber = Rails::Html::TextOnlyScrubber.new
191+
@scrubber = Rails::HTML::TextOnlyScrubber.new
192192
end
193193

194194
def test_removes_all_tags_and_keep_the_content
@@ -201,7 +201,7 @@ def test_skips_text_nodes
201201
end
202202

203203
class ReturningStopFromScrubNodeTest < ScrubberTest
204-
class ScrubStopper < Rails::Html::PermitScrubber
204+
class ScrubStopper < Rails::HTML::PermitScrubber
205205
def scrub_node(node)
206206
Loofah::Scrubber::STOP
207207
end

0 commit comments

Comments
 (0)