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:
parent
346ae79d5a
commit
fe5ef4281f
3 changed files with 19 additions and 0 deletions
|
@ -16,6 +16,9 @@ module ActionView
|
||||||
# Do not escape templates of these mime types.
|
# Do not escape templates of these mime types.
|
||||||
class_attribute :escape_ignore_list, default: ["text/plain"]
|
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]*")
|
ENCODING_TAG = Regexp.new("\\A(<%#{ENCODING_FLAG}-?%>)[ \\t]*")
|
||||||
|
|
||||||
def self.call(template, source)
|
def self.call(template, source)
|
||||||
|
@ -45,6 +48,9 @@ module ActionView
|
||||||
# Always make sure we return a String in the default_internal
|
# Always make sure we return a String in the default_internal
|
||||||
erb.encode!
|
erb.encode!
|
||||||
|
|
||||||
|
# Strip trailing newlines from the template if enabled
|
||||||
|
erb.chomp! if strip_trailing_newlines
|
||||||
|
|
||||||
options = {
|
options = {
|
||||||
escape: (self.class.escape_ignore_list.include? template.type),
|
escape: (self.class.escape_ignore_list.include? template.type),
|
||||||
trim: (self.class.erb_trim_mode == "-")
|
trim: (self.class.erb_trim_mode == "-")
|
||||||
|
|
|
@ -52,6 +52,10 @@ module AbstractController
|
||||||
render "index.erb"
|
render "index.erb"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def with_final_newline
|
||||||
|
render "with_final_newline.erb"
|
||||||
|
end
|
||||||
|
|
||||||
def index_to_string
|
def index_to_string
|
||||||
self.response_body = render_to_string "index"
|
self.response_body = render_to_string "index"
|
||||||
end
|
end
|
||||||
|
@ -84,6 +88,14 @@ module AbstractController
|
||||||
assert_equal "Hello from index.erb", @controller.response_body
|
assert_equal "Hello from index.erb", @controller.response_body
|
||||||
end
|
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
|
test "render_to_string works with a String as an argument" do
|
||||||
@controller.process(:index_to_string)
|
@controller.process(:index_to_string)
|
||||||
assert_equal "Hello from index.erb", @controller.response_body
|
assert_equal "Hello from index.erb", @controller.response_body
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Hello from with_final_newline.erb
|
Loading…
Reference in a new issue