Skip to content

Commit

Permalink
simple_format returns a safe buffer escaping unsafe input [Santiago P…
Browse files Browse the repository at this point in the history
…astorino] (Closes #3767)

Signed-off-by: David Heinemeier Hansson <david@loudthinking.com>
  • Loading branch information
Santiago Pastorino and José Ignacio Costa authored and dhh committed Feb 13, 2010
1 parent 6451e86 commit 4158282
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions actionpack/lib/action_view/helpers/text_helper.rb
Expand Up @@ -323,12 +323,12 @@ 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
text.insert 0, start_tag
text << "</p>"
text.safe_concat "</p>"
end

# Turns all URLs and e-mail addresses into clickable links. The <tt>:link</tt> option
Expand Down
12 changes: 12 additions & 0 deletions actionpack/test/template/text_helper_test.rb
Expand Up @@ -39,6 +39,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)
Expand Down

0 comments on commit 4158282

Please sign in to comment.