Skip to content

Commit

Permalink
Merge pull request #28061 from andrewhood125/strip_tags_update
Browse files Browse the repository at this point in the history
Remove `encode_special_chars` option from `strip_tags`
  • Loading branch information
rafaelfranca committed Feb 27, 2017
2 parents 4cc1c14 + 504a971 commit 93a33ec
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
7 changes: 7 additions & 0 deletions actionview/CHANGELOG.md
@@ -1,3 +1,10 @@
* Remove the option `encode_special_chars` misnomer from `strip_tags`

As of rails-html-sanitizer v1.0.3 sanitizer will ignore the
`encode_special_chars` option. Fixes #28060.

*Andrew Hood*

## Rails 5.1.0.beta1 (February 23, 2017) ##

* Change the ERB handler from Erubis to Erubi.
Expand Down
14 changes: 10 additions & 4 deletions actionview/lib/action_view/helpers/sanitize_helper.rb
Expand Up @@ -13,15 +13,15 @@ module SanitizeHelper
# It also strips href/src attributes with unsafe protocols like
# <tt>javascript:</tt>, while also protecting against attempts to use Unicode,
# ASCII, and hex character references to work around these protocol filters.
# All special characters will be escaped.
#
# The default sanitizer is Rails::Html::WhiteListSanitizer. See {Rails HTML
# Sanitizers}[https://github.com/rails/rails-html-sanitizer] for more information.
#
# Custom sanitization rules can also be provided.
#
# Please note that sanitizing user-provided text does not guarantee that the
# resulting markup is valid or even well-formed. For example, the output may still
# contain unescaped characters like <tt><</tt>, <tt>></tt>, or <tt>&</tt>.
# resulting markup is valid or even well-formed.
#
# ==== Options
#
Expand Down Expand Up @@ -86,7 +86,7 @@ def sanitize_css(style)
self.class.white_list_sanitizer.sanitize_css(style)
end

# Strips all HTML tags from +html+, including comments.
# Strips all HTML tags from +html+, including comments and special characters.
#
# strip_tags("Strip <i>these</i> tags!")
# # => Strip these tags!
Expand All @@ -96,8 +96,11 @@ def sanitize_css(style)
#
# strip_tags("<div id='top-bar'>Welcome to my website!</div>")
# # => Welcome to my website!
#
# strip_tags("> A quote from Smith & Wesson")
# # => &gt; A quote from Smith &amp; Wesson
def strip_tags(html)
self.class.full_sanitizer.sanitize(html, encode_special_chars: false)
self.class.full_sanitizer.sanitize(html)
end

# Strips all link tags from +html+ leaving just the link text.
Expand All @@ -110,6 +113,9 @@ def strip_tags(html)
#
# strip_links('Blog: <a href="http://www.myblog.com/" class="nav" target=\"_blank\">Visit</a>.')
# # => Blog: Visit.
#
# strip_links('<<a href="https://example.org">malformed & link</a>')
# # => &lt;malformed &amp; link
def strip_links(html)
self.class.link_sanitizer.sanitize(html)
end
Expand Down
2 changes: 2 additions & 0 deletions actionview/test/template/sanitize_helper_test.rb
Expand Up @@ -10,6 +10,7 @@ def test_strip_links
assert_equal "on my mind\nall day long", strip_links("<a href='almost'>on my mind</a>\n<A href='almost'>all day long</A>")
assert_equal "Magic", strip_links("<a href='http://www.rubyonrails.com/'>Mag<a href='http://www.ruby-lang.org/'>ic")
assert_equal "My mind\nall <b>day</b> long", strip_links("<a href='almost'>My mind</a>\n<A href='almost'>all <b>day</b> long</A>")
assert_equal "&lt;malformed &amp; link", strip_links('<<a href="https://example.org">malformed & link</a>')
end

def test_sanitize_form
Expand All @@ -26,6 +27,7 @@ def test_strip_tags
assert_equal("Dont touch me", strip_tags("Dont touch me"))
assert_equal("This is a test.", strip_tags("<p>This <u>is<u> a <a href='test.html'><strong>test</strong></a>.</p>"))
assert_equal "This has a here.", strip_tags("This has a <!-- comment --> here.")
assert_equal("Jekyll &amp; Hyde", strip_tags("Jekyll & Hyde"))
assert_equal "", strip_tags("<script>")
end

Expand Down

0 comments on commit 93a33ec

Please sign in to comment.