Skip to content

Commit 504a971

Browse files
committed
Remove encode_special_chars option from strip_tags
1 parent 4cc1c14 commit 504a971

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

actionview/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
* Remove the option `encode_special_chars` misnomer from `strip_tags`
2+
3+
As of rails-html-sanitizer v1.0.3 sanitizer will ignore the
4+
`encode_special_chars` option. Fixes #28060.
5+
6+
*Andrew Hood*
7+
18
## Rails 5.1.0.beta1 (February 23, 2017) ##
29

310
* Change the ERB handler from Erubis to Erubi.

actionview/lib/action_view/helpers/sanitize_helper.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ module SanitizeHelper
1313
# It also strips href/src attributes with unsafe protocols like
1414
# <tt>javascript:</tt>, while also protecting against attempts to use Unicode,
1515
# ASCII, and hex character references to work around these protocol filters.
16+
# All special characters will be escaped.
1617
#
1718
# The default sanitizer is Rails::Html::WhiteListSanitizer. See {Rails HTML
1819
# Sanitizers}[https://github.com/rails/rails-html-sanitizer] for more information.
1920
#
2021
# Custom sanitization rules can also be provided.
2122
#
2223
# Please note that sanitizing user-provided text does not guarantee that the
23-
# resulting markup is valid or even well-formed. For example, the output may still
24-
# contain unescaped characters like <tt><</tt>, <tt>></tt>, or <tt>&</tt>.
24+
# resulting markup is valid or even well-formed.
2525
#
2626
# ==== Options
2727
#
@@ -86,7 +86,7 @@ def sanitize_css(style)
8686
self.class.white_list_sanitizer.sanitize_css(style)
8787
end
8888

89-
# Strips all HTML tags from +html+, including comments.
89+
# Strips all HTML tags from +html+, including comments and special characters.
9090
#
9191
# strip_tags("Strip <i>these</i> tags!")
9292
# # => Strip these tags!
@@ -96,8 +96,11 @@ def sanitize_css(style)
9696
#
9797
# strip_tags("<div id='top-bar'>Welcome to my website!</div>")
9898
# # => Welcome to my website!
99+
#
100+
# strip_tags("> A quote from Smith & Wesson")
101+
# # => &gt; A quote from Smith &amp; Wesson
99102
def strip_tags(html)
100-
self.class.full_sanitizer.sanitize(html, encode_special_chars: false)
103+
self.class.full_sanitizer.sanitize(html)
101104
end
102105

103106
# Strips all link tags from +html+ leaving just the link text.
@@ -110,6 +113,9 @@ def strip_tags(html)
110113
#
111114
# strip_links('Blog: <a href="http://www.myblog.com/" class="nav" target=\"_blank\">Visit</a>.')
112115
# # => Blog: Visit.
116+
#
117+
# strip_links('<<a href="https://example.org">malformed & link</a>')
118+
# # => &lt;malformed &amp; link
113119
def strip_links(html)
114120
self.class.link_sanitizer.sanitize(html)
115121
end

actionview/test/template/sanitize_helper_test.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def test_strip_links
1010
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>")
1111
assert_equal "Magic", strip_links("<a href='http://www.rubyonrails.com/'>Mag<a href='http://www.ruby-lang.org/'>ic")
1212
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>")
13+
assert_equal "&lt;malformed &amp; link", strip_links('<<a href="https://example.org">malformed & link</a>')
1314
end
1415

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

0 commit comments

Comments
 (0)