mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-18 18:30:40 -07:00
Merge pull request #2067 from Shopify/strict2-increment-decrement
Add strict2_parse to increment and decrement tags
This commit is contained in:
@@ -23,13 +23,29 @@ module Liquid
|
|||||||
# {% decrement variable_name %}
|
# {% decrement variable_name %}
|
||||||
# @liquid_syntax_keyword variable_name The name of the variable being decremented.
|
# @liquid_syntax_keyword variable_name The name of the variable being decremented.
|
||||||
class Decrement < Tag
|
class Decrement < Tag
|
||||||
|
include ParserSwitching
|
||||||
|
|
||||||
attr_reader :variable_name
|
attr_reader :variable_name
|
||||||
|
|
||||||
def initialize(tag_name, markup, options)
|
def initialize(tag_name, markup, options)
|
||||||
super
|
super
|
||||||
|
parse_with_selected_parser(markup)
|
||||||
|
end
|
||||||
|
|
||||||
|
def lax_parse(markup)
|
||||||
@variable_name = markup.strip
|
@variable_name = markup.strip
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def strict_parse(markup)
|
||||||
|
lax_parse(markup)
|
||||||
|
end
|
||||||
|
|
||||||
|
def strict2_parse(markup)
|
||||||
|
p = @parse_context.new_parser(markup.strip)
|
||||||
|
@variable_name = p.consume(:id)
|
||||||
|
p.consume(:end_of_string)
|
||||||
|
end
|
||||||
|
|
||||||
def render_to_output_buffer(context, output)
|
def render_to_output_buffer(context, output)
|
||||||
counter_environment = context.environments.first
|
counter_environment = context.environments.first
|
||||||
value = counter_environment[@variable_name] || 0
|
value = counter_environment[@variable_name] || 0
|
||||||
|
|||||||
@@ -23,13 +23,29 @@ module Liquid
|
|||||||
# {% increment variable_name %}
|
# {% increment variable_name %}
|
||||||
# @liquid_syntax_keyword variable_name The name of the variable being incremented.
|
# @liquid_syntax_keyword variable_name The name of the variable being incremented.
|
||||||
class Increment < Tag
|
class Increment < Tag
|
||||||
|
include ParserSwitching
|
||||||
|
|
||||||
attr_reader :variable_name
|
attr_reader :variable_name
|
||||||
|
|
||||||
def initialize(tag_name, markup, options)
|
def initialize(tag_name, markup, options)
|
||||||
super
|
super
|
||||||
|
parse_with_selected_parser(markup)
|
||||||
|
end
|
||||||
|
|
||||||
|
def lax_parse(markup)
|
||||||
@variable_name = markup.strip
|
@variable_name = markup.strip
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def strict_parse(markup)
|
||||||
|
lax_parse(markup)
|
||||||
|
end
|
||||||
|
|
||||||
|
def strict2_parse(markup)
|
||||||
|
p = @parse_context.new_parser(markup.strip)
|
||||||
|
@variable_name = p.consume(:id)
|
||||||
|
p.consume(:end_of_string)
|
||||||
|
end
|
||||||
|
|
||||||
def render_to_output_buffer(context, output)
|
def render_to_output_buffer(context, output)
|
||||||
counter_environment = context.environments.first
|
counter_environment = context.environments.first
|
||||||
value = counter_environment[@variable_name] || 0
|
value = counter_environment[@variable_name] || 0
|
||||||
|
|||||||
@@ -27,4 +27,50 @@ class IncrementTagTest < Minitest::Test
|
|||||||
'{%decrement starboard %}',
|
'{%decrement starboard %}',
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_increment_strict2_rejects_invalid_variable_name
|
||||||
|
assert_raises(Liquid::SyntaxError) do
|
||||||
|
Template.parse('{% increment foo bar %}', error_mode: :strict2)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_increment_strict2_rejects_variable_starting_with_number
|
||||||
|
assert_raises(Liquid::SyntaxError) do
|
||||||
|
Template.parse('{% increment 11aa %}', error_mode: :strict2)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_increment_strict2_accepts_valid_variable_name
|
||||||
|
template = Template.parse('{% increment my-var %}', error_mode: :strict2)
|
||||||
|
assert_equal('0', template.render)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_decrement_strict2_rejects_invalid_variable_name
|
||||||
|
assert_raises(Liquid::SyntaxError) do
|
||||||
|
Template.parse('{% decrement foo bar %}', error_mode: :strict2)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_decrement_strict2_rejects_variable_starting_with_number
|
||||||
|
assert_raises(Liquid::SyntaxError) do
|
||||||
|
Template.parse('{% decrement 11aa %}', error_mode: :strict2)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_decrement_strict2_accepts_valid_variable_name
|
||||||
|
template = Template.parse('{% decrement my-var %}', error_mode: :strict2)
|
||||||
|
assert_equal('-1', template.render)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_increment_strict2_rejects_empty_variable_name
|
||||||
|
assert_raises(Liquid::SyntaxError) do
|
||||||
|
Template.parse('{% increment %}', error_mode: :strict2)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_decrement_strict2_rejects_empty_variable_name
|
||||||
|
assert_raises(Liquid::SyntaxError) do
|
||||||
|
Template.parse('{% decrement %}', error_mode: :strict2)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -174,16 +174,16 @@ class RenderTagTest < Minitest::Test
|
|||||||
def test_increment_is_isolated_between_renders
|
def test_increment_is_isolated_between_renders
|
||||||
assert_template_result(
|
assert_template_result(
|
||||||
'010',
|
'010',
|
||||||
'{% increment %}{% increment %}{% render "incr" %}',
|
'{% increment port %}{% increment port %}{% render "incr" %}',
|
||||||
partials: { 'incr' => '{% increment %}' },
|
partials: { 'incr' => '{% increment port %}' },
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_decrement_is_isolated_between_renders
|
def test_decrement_is_isolated_between_renders
|
||||||
assert_template_result(
|
assert_template_result(
|
||||||
'-1-2-1',
|
'-1-2-1',
|
||||||
'{% decrement %}{% decrement %}{% render "decr" %}',
|
'{% decrement port %}{% decrement port %}{% render "decr" %}',
|
||||||
partials: { 'decr' => '{% decrement %}' },
|
partials: { 'decr' => '{% decrement port %}' },
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user