From 64c914706ebeeda42892c6e0a4082cff75e76a1f Mon Sep 17 00:00:00 2001 From: k0kubun Date: Sat, 20 May 2017 17:36:09 +0000 Subject: [PATCH] erb.rb: Allow trimming CR in all trim_modes to unify a behavior with r58823 and r58825. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58826 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- NEWS | 5 +++++ lib/erb.rb | 4 ++-- test/erb/test_erb.rb | 8 ++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index 2458ddd4f4..7dfcf8da7d 100644 --- a/NEWS +++ b/NEWS @@ -88,6 +88,11 @@ with all sufficient information, see the ChangeLog file or Redmine * New constants: * RbConfig::LIMITS is added to provide the limits of C types. +* ERB + * Carriage returns are changed to be trimmed properly if trim_mode is specified + and used. Duplicated newlines will be removed on Windows. + [Bug #5339] [Bug #11464] + === Compatibility issues (excluding feature bug fixes) * Net::HTTP diff --git a/lib/erb.rb b/lib/erb.rb index 6af69c94a7..182a42437f 100644 --- a/lib/erb.rb +++ b/lib/erb.rb @@ -389,7 +389,7 @@ class ERB @trim_mode = trim_mode @percent = percent if @trim_mode == '>' - @scan_reg = /(.*?)(%>\n|#{(stags + etags).join('|')}|\n|\z)/m + @scan_reg = /(.*?)(%>\r?\n|#{(stags + etags).join('|')}|\n|\z)/m @scan_line = self.method(:trim_line1) elsif @trim_mode == '<>' @scan_reg = /(.*?)(%>\r?\n|#{(stags + etags).join('|')}|\n|\z)/m @@ -441,7 +441,7 @@ class ERB line.scan(@scan_reg) do |tokens| tokens.each do |token| next if token.empty? - if token == "%>\n" + if token == "%>\n" || token == "%>\r\n" yield('%>') yield(:cr) else diff --git a/test/erb/test_erb.rb b/test/erb/test_erb.rb index d2c6c9230f..10fb177f58 100644 --- a/test/erb/test_erb.rb +++ b/test/erb/test_erb.rb @@ -197,6 +197,14 @@ EOS assert_equal(ans, erb.result) end + def test_trim_line1_with_carriage_return + erb = @erb.new("<% 3.times do %>\r\nline\r\n<% end %>\r\n", nil, '>') + assert_equal("line\r\n" * 3, erb.result) + + erb = @erb.new("<% 3.times do %>\r\nline\r\n<% end %>\r\n", nil, '%>') + assert_equal("line\r\n" * 3, erb.result) + end + def test_trim_line2_with_carriage_return erb = @erb.new("<% 3.times do %>\r\nline\r\n<% end %>\r\n", nil, '<>') assert_equal("line\r\n" * 3, erb.result)