Skip to content

Commit

Permalink
fix encoding of the escaped html safe string
Browse files Browse the repository at this point in the history
`escape_html_as_html_safe` was returning binary strings which will
infect all the UTF-8 strings in our view layer.  This patch tags the
return string with the encoding of the source string so that we're not
eventually forcing everything to be binary.
  • Loading branch information
tenderlove committed Apr 13, 2016
1 parent cee664d commit 10098de
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions ext/escape_utils/escape_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ static VALUE rb_eu_escape_html_as_html_safe(VALUE self, VALUE str)
}

rb_ivar_set(result, ID_at_html_safe, Qtrue);
rb_enc_associate(result, rb_enc_get(str));

return result;
}
Expand Down
24 changes: 24 additions & 0 deletions test/html/escape_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,30 @@ class MyCustomHtmlSafeString < String
end

class HtmlEscapeTest < Minitest::Test
def test_escape_source_encoding_is_maintained
source = 'foobar'
str = EscapeUtils.escape_html_as_html_safe(source)
assert_equal source.encoding, str.encoding
end

def test_escape_binary_encoding_is_maintained
source = 'foobar'.b
str = EscapeUtils.escape_html_as_html_safe(source)
assert_equal source.encoding, str.encoding
end

def test_escape_uft8_encoding_is_maintained
source = 'foobar'.encode 'UTF-8'
str = EscapeUtils.escape_html_as_html_safe(source)
assert_equal source.encoding, str.encoding
end

def test_escape_us_ascii_encoding_is_maintained
source = 'foobar'.encode 'US-ASCII'
str = EscapeUtils.escape_html_as_html_safe(source)
assert_equal source.encoding, str.encoding
end

def test_escape_basic_html_with_secure
assert_equal "&lt;some_tag&#47;&gt;", EscapeUtils.escape_html("<some_tag/>")

Expand Down

0 comments on commit 10098de

Please sign in to comment.