Merge pull request #40346 from joelhawksley/annotations-whitespace

Do not add newlines when annotating rendered views
This commit is contained in:
Rafael França 2020-10-06 21:54:04 -04:00 committed by GitHub
commit ee247c850e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 10 additions and 29 deletions

@ -333,10 +333,8 @@ def #{method_name}(local_assigns, output_buffer)
raise WrongEncodingError.new(source, Encoding.default_internal)
end
start_line = @handler.respond_to?(:start_line) ? @handler.start_line(self) : 0
begin
mod.module_eval(source, identifier, start_line)
mod.module_eval(source, identifier, 0)
rescue SyntaxError
# Account for when code in the template is not syntactically valid; e.g. if we're using
# ERB and the user writes <%= foo( %>, attempting to call a helper `foo` and interpolate

@ -40,15 +40,6 @@ def handles_encoding?
true
end
# Line number to pass to #module_eval
#
# If we're annotating the template, we need to offset the starting
# line number passed to #module_eval so that errors in the template
# will be raised on the correct line.
def start_line(template)
annotate?(template) ? -1 : 0
end
def call(template, source)
# First, convert to BINARY, so in case the encoding is
# wrong, we can still find an encoding tag
@ -69,19 +60,15 @@ def call(template, source)
trim: (self.class.erb_trim_mode == "-")
}
if annotate?(template)
options[:preamble] = "@output_buffer.safe_append='<!-- BEGIN #{template.short_identifier} -->\n';"
options[:postamble] = "@output_buffer.safe_append='<!-- END #{template.short_identifier} -->\n';@output_buffer.to_s"
if ActionView::Base.annotate_rendered_view_with_filenames && template.format == :html
options[:preamble] = "@output_buffer.safe_append='<!-- BEGIN #{template.short_identifier} -->';"
options[:postamble] = "@output_buffer.safe_append='<!-- END #{template.short_identifier} -->';@output_buffer.to_s"
end
self.class.erb_implementation.new(erb, options).src
end
private
def annotate?(template)
ActionView::Base.annotate_rendered_view_with_filenames && template.format == :html
end
def valid_encoding(string, encoding)
# If a magic encoding comment was found, tag the
# String with this encoding. This is for a case

@ -217,7 +217,7 @@ def render_xml_hello_as_string_template
end
def render_line_offset
render inline: "<% raise %>", locals: { foo: "bar" }
render template: "test/raise"
end
def heading
@ -1459,15 +1459,10 @@ def test_template_annotations
get :greeting
lines = @response.body.split("\n")
assert_includes lines.first, "<!-- BEGIN"
assert_includes lines.first, "test/fixtures/actionpack/test/greeting.html.erb -->"
assert_includes lines[1], "This is grand!"
assert_includes lines.last, "<!-- END"
assert_includes lines.last, "test/fixtures/actionpack/test/greeting.html.erb -->"
assert_includes @response.body, "<!-- BEGIN"
assert_includes @response.body, "<!-- END"
assert_includes @response.body, "test/fixtures/actionpack/test/greeting.html.erb"
assert_includes @response.body, "This is grand!"
ensure
ActionView::Base.annotate_rendered_view_with_filenames = false
end

@ -0,0 +1 @@
<% raise %>