Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not add newlines when annotating rendered views #40346

Merged
merged 1 commit into from
Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 1 addition & 3 deletions actionview/lib/action_view/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 3 additions & 16 deletions actionview/lib/action_view/template/handlers/erb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
15 changes: 5 additions & 10 deletions actionview/test/actionpack/controller/render_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions actionview/test/fixtures/actionpack/test/raise.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<% raise %>