diff --git a/actionview/lib/action_view/template/handlers/erb.rb b/actionview/lib/action_view/template/handlers/erb.rb index c98685843f..de8cb05096 100644 --- a/actionview/lib/action_view/template/handlers/erb.rb +++ b/actionview/lib/action_view/template/handlers/erb.rb @@ -59,6 +59,7 @@ module ActionView erb, escape: (self.class.escape_ignore_list.include? template.type), trim: (self.class.erb_trim_mode == "-"), + format: template.format, short_identifier: template.short_identifier ).src end diff --git a/actionview/lib/action_view/template/handlers/erb/erubi.rb b/actionview/lib/action_view/template/handlers/erb/erubi.rb index b58560063d..fa9974f542 100644 --- a/actionview/lib/action_view/template/handlers/erb/erubi.rb +++ b/actionview/lib/action_view/template/handlers/erb/erubi.rb @@ -14,7 +14,8 @@ module ActionView # Dup properties so that we don't modify argument properties = Hash[properties] - if ActionView::Base.annotate_template_file_names + # Annotate output with template file names, if we're rendering HTML + if ActionView::Base.annotate_template_file_names && properties[:format] == :html properties[:preamble] = "@output_buffer.safe_append='\n';" properties[:postamble] = "@output_buffer.safe_append='\n';@output_buffer.to_s" else diff --git a/actionview/test/actionpack/controller/render_test.rb b/actionview/test/actionpack/controller/render_test.rb index 707e4b98f4..ae906e4308 100644 --- a/actionview/test/actionpack/controller/render_test.rb +++ b/actionview/test/actionpack/controller/render_test.rb @@ -1457,17 +1457,28 @@ class RenderTest < ActionController::TestCase def test_template_annotations ActionView::Base.annotate_template_file_names = true - get :render_with_explicit_template_with_locals + get :greeting lines = @response.body.split("\n") assert_includes lines.first, "" + assert_includes lines.first, "test/fixtures/actionpack/test/greeting.html.erb -->" - assert_includes lines[1], "The secret is area51" + assert_includes lines[1], "This is grand!" assert_includes lines.last, "" + assert_includes lines.last, "test/fixtures/actionpack/test/greeting.html.erb -->" + ensure + ActionView::Base.annotate_template_file_names = false + end + + def test_template_annotations_do_not_render_for_non_html_format + ActionView::Base.annotate_template_file_names = true + + get :render_with_explicit_template_with_locals + + assert_not_includes @response.body, "BEGIN" + assert_equal @response.body.split("\n").length, 1 ensure ActionView::Base.annotate_template_file_names = false end