Skip to content

Commit 1de0df8

Browse files
committed
Change the raw template handler to render html-safe strings
In PR #24929 the changelog was updated to make note that while the new template handler was changed to raw this changed the behavior when outputting plain html or js files. Previously ERB would output the files unescaped. Changing the default handler to RAW meant that these same files would be rendered as escaped rather than as js or html. Because of this change in behavior and after the discussion #24949 in we decided to change the behavior of the Raw handler to output html_safe strings by default. Now files rendered with the default handler (raw) render the file unescaped.
1 parent be8b2dd commit 1de0df8

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

Diff for: actionview/CHANGELOG.md

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
* Raw template handler (which is also the default template handler in Rails 5) now outputs
2+
HTML-safe strings.
3+
4+
In Rails 5 the default template handler was changed to the raw template handler. Because
5+
the ERB template handler escaped strings by default this broke some applications that
6+
expected plain JS or HTML files to be rendered unescaped. This fixes the issue caused
7+
by changing the default handler by changing the Raw template handler to output HTML-safe
8+
strings.
9+
10+
*Eileen M. Uchitelle*
11+
112
* `select_tag`'s `include_blank` option for generation for blank option tag, now adds an empty space label,
213
when the value as well as content for option tag are empty, so that we confirm with html specification.
314
Ref: https://www.w3.org/TR/html5/forms.html#the-option-element.

Diff for: actionview/lib/action_view/template/handlers/raw.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module ActionView
22
module Template::Handlers
33
class Raw
44
def call(template)
5-
"#{template.source.inspect};"
5+
"#{template.source.inspect}.html_safe;"
66
end
77
end
88
end

Diff for: actionview/test/template/render_test.rb

+7
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ def test_render_raw_template_with_quotes
100100
assert_equal %q;Here are some characters: !@#$%^&*()-="'}{`; + "\n", @view.render(:template => "plain_text_with_characters")
101101
end
102102

103+
def test_render_raw_is_html_safe_and_does_not_escape_output
104+
buffer = ActiveSupport::SafeBuffer.new
105+
buffer << @view.render(file: "plain_text")
106+
assert_equal true, buffer.html_safe?
107+
assert_equal buffer, "<%= hello_world %>\n"
108+
end
109+
103110
def test_render_ruby_template_with_handlers
104111
assert_equal "Hello from Ruby code", @view.render(:template => "ruby_template")
105112
end

0 commit comments

Comments
 (0)