Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

simple_format returns a safe buffer escaping unsafe input [Santiago P…

…astorino]
  • Loading branch information...
commit d68f8ba5c303556ecb8625dd146184d68b704e83 1 parent 325fa58
@dhh dhh authored
View
2  actionpack/lib/action_view/helpers/text_helper.rb
@@ -327,7 +327,7 @@ def markdown(text)
# # => "<p class='description'>Look ma! A class!</p>"
def simple_format(text, html_options={})
start_tag = tag('p', html_options, true)
- text = text.to_s.dup
+ text = h(text)
text.gsub!(/\r\n?/, "\n") # \r\n and \r -> \n
text.gsub!(/\n\n+/, "</p>\n\n#{start_tag}") # 2+ newline -> paragraph
text.gsub!(/([^\n]\n)(?=[^\n])/, '\1<br />') # 1 newline -> br
View
12 actionpack/test/template/text_helper_test.rb
@@ -40,6 +40,18 @@ def test_simple_format
assert_equal %Q(<p class="test">para 1</p>\n\n<p class="test">para 2</p>), simple_format("para 1\n\npara 2", :class => 'test')
end
+ def test_simple_format_should_be_html_safe
+ assert simple_format("<b> test with html tags </b>").html_safe?
+ end
+
+ def test_simple_format_should_escape_unsafe_input
+ assert_equal "<p>&lt;b&gt; test with unsafe string &lt;/b&gt;</p>", simple_format("<b> test with unsafe string </b>")
+ end
+
+ def test_simple_format_should_not_escape_safe_input
+ assert_equal "<p><b> test with safe string </b></p>", simple_format("<b> test with safe string </b>".html_safe)
+ end
+
def test_truncate
assert_equal "Hello World!", truncate("Hello World!", :length => 12)
assert_equal "Hello Wor...", truncate("Hello World!!", :length => 12)
Please sign in to comment.
Something went wrong with that request. Please try again.