ruby-changes:46711
From: k0kubun <ko1@a...>
Date: Sun, 21 May 2017 02:36:15 +0900 (JST)
Subject: [ruby-changes:46711] k0kubun:r58826 (trunk): erb.rb: Allow trimming CR in all trim_modes
k0kubun 2017-05-21 02:36:09 +0900 (Sun, 21 May 2017) New Revision: 58826 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=58826 Log: erb.rb: Allow trimming CR in all trim_modes to unify a behavior with r58823 and r58825. Modified files: trunk/NEWS trunk/lib/erb.rb trunk/test/erb/test_erb.rb Index: lib/erb.rb =================================================================== --- lib/erb.rb (revision 58825) +++ lib/erb.rb (revision 58826) @@ -389,7 +389,7 @@ class ERB https://github.com/ruby/ruby/blob/trunk/lib/erb.rb#L389 @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 https://github.com/ruby/ruby/blob/trunk/lib/erb.rb#L441 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 Index: NEWS =================================================================== --- NEWS (revision 58825) +++ NEWS (revision 58826) @@ -88,6 +88,11 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L88 * 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 Index: test/erb/test_erb.rb =================================================================== --- test/erb/test_erb.rb (revision 58825) +++ test/erb/test_erb.rb (revision 58826) @@ -197,6 +197,14 @@ EOS https://github.com/ruby/ruby/blob/trunk/test/erb/test_erb.rb#L197 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) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/