summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authork0kubun <k0kubun@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-11-12 00:37:15 +0000
committerk0kubun <k0kubun@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-11-12 00:37:15 +0000
commitab5a3db48e446a33f790fa5e8594a6dc24062987 (patch)
treeb8f6a5b6b1b05b585d7d7432b6cc902906478487
parent7e61fdca9a082970e93b0d60ea87572af600b805 (diff)
erb.rb: warn invalid trim_mode [Misc #15294]
From: Justin Collins <justin@presidentbeef.com> git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65671 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--lib/erb.rb11
-rw-r--r--test/erb/test_erb.rb22
2 files changed, 32 insertions, 1 deletions
diff --git a/lib/erb.rb b/lib/erb.rb
index c9b987d..5b725d7 100644
--- a/lib/erb.rb
+++ b/lib/erb.rb
@@ -665,9 +665,13 @@ class ERB
return [false, '>']
when 2
return [false, '<>']
- when 0
+ when 0, nil
return [false, nil]
when String
+ unless mode.match?(/\A(%|-|>|<>){1,2}\z/)
+ warn_invalid_trim_mode(mode, uplevel: 5)
+ end
+
perc = mode.include?('%')
if mode.include?('-')
return [perc, '-']
@@ -679,6 +683,7 @@ class ERB
[perc, nil]
end
else
+ warn_invalid_trim_mode(mode, uplevel: 5)
return [false, nil]
end
end
@@ -730,6 +735,10 @@ class ERB
end
return enc, frozen
end
+
+ def warn_invalid_trim_mode(mode, uplevel:)
+ warn "Invalid ERB trim mode: #{mode.inspect} (trim_mode: nil, 0, 1, 2, or String composed of '%' and/or '-', '>', '<>')", uplevel: uplevel + 1
+ end
end
end
diff --git a/test/erb/test_erb.rb b/test/erb/test_erb.rb
index ffa1fef..1fec46d 100644
--- a/test/erb/test_erb.rb
+++ b/test/erb/test_erb.rb
@@ -235,6 +235,28 @@ EOS
assert_equal("line\r\n" * 3, erb.result)
end
+ def test_invalid_trim_mode
+ assert_warning(/#{__FILE__}:#{__LINE__ + 1}/) do
+ @erb.new("", trim_mode: 'abc-def')
+ end
+
+ assert_warning(/Invalid ERB trim mode/) do
+ @erb.new("", trim_mode: 'abc-def')
+ end
+
+ assert_warning(/Invalid ERB trim mode/) do
+ @erb.new("", trim_mode: '%<')
+ end
+
+ assert_warning(/Invalid ERB trim mode/) do
+ @erb.new("", trim_mode: '%<>-')
+ end
+
+ assert_warning(/Invalid ERB trim mode/) do
+ @erb.new("", trim_mode: 3)
+ end
+ end
+
def test_run
out = StringIO.new
orig, $stdout = $stdout, out