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

Add option to strip trailing newlines from ERB templates

Add failing test for views with trailing newlines

Add and test config option

Move config option to config/application.rb

Move implementation to ERB template handler

Move config option to ActionView::Template::Handlers::ERB
This commit is contained in:
Simon Fish 2021-05-24 10:54:58 +01:00
parent 346ae79d5a
commit fe5ef4281f
No known key found for this signature in database
GPG key ID: B184D12D0AC5C2FA
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