Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simple_format - Prevent too much escaped html #1421

Merged
merged 2 commits into from Sep 27, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion padrino-helpers/lib/padrino-helpers/format_helpers.rb
Expand Up @@ -78,7 +78,7 @@ def strip_tags(html)
def simple_format(text, options={})
t = options.delete(:tag) || :p
start_tag = tag(t, options, true)
text = escape_html(text.to_s.dup)
text = escape_html(text.to_s.dup) unless text.html_safe?
text.gsub!(/\r\n?/, "\n") # \r\n and \r -> \n
text.gsub!(/\n\n+/, "</#{t}>\n\n#{start_tag}") # 2+ newline -> paragraph
text.gsub!(/([^\n]\n)(?=[^\n])/, '\1<br />') # 1 newline -> br
Expand Down
10 changes: 10 additions & 0 deletions padrino-helpers/test/test_format_helpers.rb
Expand Up @@ -29,6 +29,16 @@ def setup
assert_equal "<p class=\"description\">Look me! A class!</p>", actual_text
end

should "escape html tags" do
actual_text = simple_format("Will you escape <b>that</b>?")
assert_equal "<p>Will you escape &lt;b&gt;that&lt;&#x2F;b&gt;?</p>", actual_text
end

should "support already sanitized text" do
actual_text = simple_format("Don't try to escape <b>me</b>!".html_safe)
assert_equal "<p>Don't try to escape <b>me</b>!</p>", actual_text
end

context 'wrapped in a custom tag' do
should "format simple text into html format" do
actual_text = simple_format("Here is some basic text...\n...with a line break.", :tag => :div)
Expand Down