Permalink
Browse files

simple_format must not modify the source strings passed to it, confus…

…ing and trashes attribute dirty tracking
  • Loading branch information...
1 parent 960882f commit 94ef4dc28f4b8b6e561c6e861045de4af5c0e817 @willbryant committed Oct 16, 2011
Showing with 12 additions and 2 deletions.
  1. +5 −2 actionpack/lib/action_view/helpers/text_helper.rb
  2. +7 −0 actionpack/test/template/text_helper_test.rb
@@ -257,8 +257,11 @@ def word_wrap(text, *args)
def simple_format(text, html_options={}, options={})
text = '' if text.nil?
start_tag = tag('p', html_options, true)
- text = sanitize(text) unless options[:sanitize] == false
- text = text.to_str
+ if options[:sanitize] == false
+ text = text.to_str.dup
+ else
+ text = sanitize(text).to_str
+ end
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
@@ -48,6 +48,13 @@ def test_simple_format_should_not_sanitize_input_when_sanitize_option_is_false
assert_equal "<p><b> test with unsafe string </b><script>code!</script></p>", simple_format("<b> test with unsafe string </b><script>code!</script>", {}, :sanitize => false)
end
+ def test_simple_format_does_not_modify_source
+ str = "This string object should not be modified"
+ assert_equal "<p>This string object should not be modified</p>", simple_format(str)
+ assert_equal "<p>This string object should not be modified</p>", simple_format(str, {}, :sanitize => false)
+ assert_equal "This string object should not be modified", str
+ end
+
def test_truncate_should_not_be_html_safe
assert !truncate("Hello World!", :length => 12).html_safe?
end

0 comments on commit 94ef4dc

Please sign in to comment.