Skip to content

Commit b6bff03

Browse files
aswamyclaude
andcommitted
Add strict2_parse to increment and decrement tags
Both tags previously accepted any string as a variable name via `markup.strip`. Now they use `parse_with_selected_parser` and validate the variable name with `p.consume(:id)` in strict2 mode. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 0d5c15a commit b6bff03

3 files changed

Lines changed: 66 additions & 0 deletions

File tree

lib/liquid/tags/decrement.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,29 @@ module Liquid
2323
# {% decrement variable_name %}
2424
# @liquid_syntax_keyword variable_name The name of the variable being decremented.
2525
class Decrement < Tag
26+
include ParserSwitching
27+
2628
attr_reader :variable_name
2729

2830
def initialize(tag_name, markup, options)
2931
super
32+
parse_with_selected_parser(markup)
33+
end
34+
35+
def lax_parse(markup)
3036
@variable_name = markup.strip
3137
end
3238

39+
def strict_parse(markup)
40+
lax_parse(markup)
41+
end
42+
43+
def strict2_parse(markup)
44+
p = @parse_context.new_parser(markup.strip)
45+
@variable_name = p.consume(:id)
46+
p.consume(:end_of_string)
47+
end
48+
3349
def render_to_output_buffer(context, output)
3450
counter_environment = context.environments.first
3551
value = counter_environment[@variable_name] || 0

lib/liquid/tags/increment.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,29 @@ module Liquid
2323
# {% increment variable_name %}
2424
# @liquid_syntax_keyword variable_name The name of the variable being incremented.
2525
class Increment < Tag
26+
include ParserSwitching
27+
2628
attr_reader :variable_name
2729

2830
def initialize(tag_name, markup, options)
2931
super
32+
parse_with_selected_parser(markup)
33+
end
34+
35+
def lax_parse(markup)
3036
@variable_name = markup.strip
3137
end
3238

39+
def strict_parse(markup)
40+
lax_parse(markup)
41+
end
42+
43+
def strict2_parse(markup)
44+
p = @parse_context.new_parser(markup.strip)
45+
@variable_name = p.consume(:id)
46+
p.consume(:end_of_string)
47+
end
48+
3349
def render_to_output_buffer(context, output)
3450
counter_environment = context.environments.first
3551
value = counter_environment[@variable_name] || 0

test/integration/tags/increment_tag_test.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,40 @@ def test_inc
1616
)
1717
end
1818

19+
def test_increment_strict2_rejects_invalid_variable_name
20+
assert_raises(Liquid::SyntaxError) do
21+
Template.parse('{% increment foo bar %}', error_mode: :strict2)
22+
end
23+
end
24+
25+
def test_increment_strict2_rejects_variable_starting_with_number
26+
assert_raises(Liquid::SyntaxError) do
27+
Template.parse('{% increment 11aa %}', error_mode: :strict2)
28+
end
29+
end
30+
31+
def test_increment_strict2_accepts_valid_variable_name
32+
template = Template.parse('{% increment my-var %}', error_mode: :strict2)
33+
assert_equal('0', template.render)
34+
end
35+
36+
def test_decrement_strict2_rejects_invalid_variable_name
37+
assert_raises(Liquid::SyntaxError) do
38+
Template.parse('{% decrement foo bar %}', error_mode: :strict2)
39+
end
40+
end
41+
42+
def test_decrement_strict2_rejects_variable_starting_with_number
43+
assert_raises(Liquid::SyntaxError) do
44+
Template.parse('{% decrement 11aa %}', error_mode: :strict2)
45+
end
46+
end
47+
48+
def test_decrement_strict2_accepts_valid_variable_name
49+
template = Template.parse('{% decrement my-var %}', error_mode: :strict2)
50+
assert_equal('-1', template.render)
51+
end
52+
1953
def test_dec
2054
assert_template_result('-1 -1', '{%decrement port %} {{ port }}', { 'port' => 10 })
2155
assert_template_result(' -1 -2 -2', '{{port}} {%decrement port %} {%decrement port%} {{port}}')

0 commit comments

Comments
 (0)