1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #42279 from boardfish/strip-final-newline

Add option to strip final newline from ERB renders
This commit is contained in:
John Hawthorn 2021-06-04 10:36:58 -07:00 committed by GitHub
commit b167d8e517
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 0 deletions

View file

@ -16,6 +16,9 @@ module ActionView
# Do not escape templates of these mime types.
class_attribute :escape_ignore_list, default: ["text/plain"]
# Strip trailing newlines from rendered output
class_attribute :strip_trailing_newlines, default: false
ENCODING_TAG = Regexp.new("\\A(<%#{ENCODING_FLAG}-?%>)[ \\t]*")
def self.call(template, source)
@ -45,6 +48,9 @@ module ActionView
# Always make sure we return a String in the default_internal
erb.encode!
# Strip trailing newlines from the template if enabled
erb.chomp! if strip_trailing_newlines
options = {
escape: (self.class.escape_ignore_list.include? template.type),
trim: (self.class.erb_trim_mode == "-")

View file

@ -52,6 +52,10 @@ module AbstractController
render "index.erb"
end
def with_final_newline
render "with_final_newline.erb"
end
def index_to_string
self.response_body = render_to_string "index"
end
@ -84,6 +88,14 @@ module AbstractController
assert_equal "Hello from index.erb", @controller.response_body
end
test "stripping final newline works" do
ActionView::Template::Handlers::ERB.strip_trailing_newlines = true
@controller.process(:with_final_newline)
assert_equal "Hello from with_final_newline.erb", @controller.response_body
ensure
ActionView::Template::Handlers::ERB.strip_trailing_newlines = false
end
test "render_to_string works with a String as an argument" do
@controller.process(:index_to_string)
assert_equal "Hello from index.erb", @controller.response_body

View file

@ -0,0 +1 @@
Hello from with_final_newline.erb