-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rails-html-sanitizer: Add types for rails-html-sanitizer gem
This gem is responsible for sanitizing HTML fragments in Rails applications. Specifically, this is the set of sanitizers used to implement the Action View SanitizerHelper methods sanitize, sanitize_css, strip_tags and strip_links. refs: https://github.com/rails/rails-html-sanitizer
- Loading branch information
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require "rails-html-sanitizer" | ||
|
||
full_sanitizer = Rails::HTML::FullSanitizer.new | ||
full_sanitizer.sanitize("<b>Bold</b> no more! <a href='more.html'>See more here</a>...") | ||
|
||
link_sanitizer = Rails::HTML::LinkSanitizer.new | ||
link_sanitizer.sanitize('<a href="example.com">Only the link text will be kept.</a>') | ||
|
||
safe_list_sanitizer = Rails::HTML::SafeListSanitizer.new | ||
|
||
# sanitize via an extensive safe list of allowed elements | ||
safe_list_sanitizer.sanitize("<b>Bold</b> no more! <a href='more.html'>See more here</a>...") | ||
|
||
# sanitize only the supplied tags and attributes | ||
safe_list_sanitizer.sanitize("<b>Bold</b> no more! <a href='more.html'>See more here</a>...", tags: %w(table tr td), attributes: %w(id class style)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
module Rails | ||
module Html = HTML | ||
end | ||
|
||
module Rails | ||
module HTML | ||
class Sanitizer | ||
def sanitize: (String html, ?Hash[untyped, untyped] options) -> String | ||
end | ||
|
||
module Concern | ||
module ComposedSanitize | ||
def sanitize: (String html, ?Hash[untyped, untyped] options) -> String | ||
end | ||
end | ||
|
||
class FullSanitizer = HTML4::FullSanitizer | ||
class LinkSanitizer = HTML4::LinkSanitizer | ||
class SafeListSanitizer = HTML4::SafeListSanitizer | ||
class WhiteListSanitizer = SafeListSanitizer | ||
end | ||
|
||
module HTML4 | ||
class FullSanitizer < Rails::HTML::Sanitizer | ||
include HTML::Concern::ComposedSanitize | ||
end | ||
|
||
class LinkSanitizer < Rails::HTML::Sanitizer | ||
include HTML::Concern::ComposedSanitize | ||
end | ||
|
||
class SafeListSanitizer < Rails::HTML::Sanitizer | ||
include HTML::Concern::ComposedSanitize | ||
end | ||
end | ||
|
||
module HTML5 | ||
class Sanitizer | ||
end | ||
|
||
class FullSanitizer < Rails::HTML::Sanitizer | ||
include HTML::Concern::ComposedSanitize | ||
end | ||
|
||
class LinkSanitizer < Rails::HTML::Sanitizer | ||
include HTML::Concern::ComposedSanitize | ||
end | ||
|
||
class SafeListSanitizer < Rails::HTML::Sanitizer | ||
include HTML::Concern::ComposedSanitize | ||
end | ||
end | ||
end |