Skip to content

Commit

Permalink
ensuring that json_escape returns html safe strings when passed an ht…
Browse files Browse the repository at this point in the history
…ml safe string
  • Loading branch information
tenderlove committed Jun 9, 2011
1 parent 8fcdc15 commit cce7085
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
10 changes: 10 additions & 0 deletions actionpack/test/template/erb_util_test.rb
Expand Up @@ -15,6 +15,16 @@ class ErbUtilTest < Test::Unit::TestCase
end
end

def test_json_escape_returns_unsafe_strings_when_passed_unsafe_strings
value = json_escape("asdf")
assert !value.html_safe?
end

def test_json_escape_returns_safe_strings_when_passed_safe_strings
value = json_escape("asdf".html_safe)
assert value.html_safe?
end

def test_html_escape_is_html_safe
escaped = h("<p>")
assert_equal "&lt;p&gt;", escaped
Expand Down
Expand Up @@ -50,7 +50,8 @@ def html_escape(s)
# <%=j @person.to_json %>
#
def json_escape(s)
s.to_s.gsub(/[&"><]/) { |special| JSON_ESCAPE[special] }
result = s.to_s.gsub(/[&"><]/) { |special| JSON_ESCAPE[special] }
s.html_safe? ? result.html_safe : result
end

alias j json_escape
Expand Down

0 comments on commit cce7085

Please sign in to comment.