erb.rb: Allow explicit trimming carriage return

when trim_mode is "-", for Windows environments.

[ruby-core:39625] [Bug #5339]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58825 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
k0kubun 2017-05-20 17:17:22 +00:00
Родитель 37bc70e650
Коммит b82ed2ce61
2 изменённых файлов: 14 добавлений и 3 удалений

Просмотреть файл

@ -395,7 +395,7 @@ class ERB
@scan_reg = /(.*?)(%>\r?\n|#{(stags + etags).join('|')}|\n|\z)/m
@scan_line = self.method(:trim_line2)
elsif @trim_mode == '-'
@scan_reg = /(.*?)(^[ \t]*<%\-|<%\-|-%>\n|-%>|#{(stags + etags).join('|')}|\z)/m
@scan_reg = /(.*?)(^[ \t]*<%\-|<%\-|-%>\r?\n|-%>|#{(stags + etags).join('|')}|\z)/m
@scan_line = self.method(:explicit_trim_line)
else
@scan_reg = /(.*?)(#{(stags + etags).join('|')}|\n|\z)/m
@ -479,7 +479,7 @@ class ERB
next if token.empty?
if @stag.nil? && /[ \t]*<%-/ =~ token
yield('<%')
elsif @stag && token == "-%>\n"
elsif @stag && (token == "-%>\n" || token == "-%>\r\n")
yield('%>')
yield(:cr)
elsif @stag && token == '-%>'
@ -534,7 +534,7 @@ class ERB
yield('<%')
elsif elem == '-%>'
yield('%>')
yield(:cr) if scanner.scan(/(\n|\z)/)
yield(:cr) if scanner.scan(/(\r?\n|\z)/)
else
yield(elem)
end

Просмотреть файл

@ -200,6 +200,17 @@ EOS
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)
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_explicit_trim_line_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
class Foo; end