diff --git a/actionview/lib/action_view/template/handlers/erb.rb b/actionview/lib/action_view/template/handlers/erb.rb index c98685843f96c4891facd5dac9f7ad65c964d7e9..de8cb0509622b3adb22c12aab35e0ac3c72c3563 100644 --- a/actionview/lib/action_view/template/handlers/erb.rb +++ b/actionview/lib/action_view/template/handlers/erb.rb @@ -59,6 +59,7 @@ def call(template, source) 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 b58560063d1a3bada2de3d0b7c93f3c680c96c46..fa9974f54234288ca237f0de1540449b4367fbe4 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 @@ def initialize(input, properties = {}) # 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 707e4b98f4e7e273d5faac7e6aebffcc80e3aab7..ae906e43082a9959cbd2c022855960596d37690a 100644 --- a/actionview/test/actionpack/controller/render_test.rb +++ b/actionview/test/actionpack/controller/render_test.rb @@ -1457,17 +1457,28 @@ def test_render_call_to_partial_with_layout_in_main_layout_and_within_content_fo 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