Add a rigid_parse method to cycle

This commit is contained in:
Charles-P. Clermont
2025-10-27 16:33:31 +01:00
committed by Guilherme Carreiro
parent 6d585a24f1
commit c78bf20010
2 changed files with 56 additions and 12 deletions
+41 -12
View File
@@ -22,18 +22,7 @@ module Liquid
def initialize(tag_name, markup, options)
super
case markup
when NamedSyntax
@variables = variables_from_string(Regexp.last_match(2))
@name = parse_expression(Regexp.last_match(1))
@is_named = true
when SimpleSyntax
@variables = variables_from_string(markup)
@name = @variables.to_s
@is_named = !@name.match?(/\w+:0x\h{8}/)
else
raise SyntaxError, options[:locale].t("errors.syntax.cycle")
end
parse_with_selected_parser(markup)
end
def named?
@@ -65,6 +54,46 @@ module Liquid
private
# cycle [name:] expression(, expression)*
def rigid_parse(markup)
$stderr.puts "using rigid"
p = @parse_context.new_parser(markup)
if p.look(:id) && p.peek(1) == :colon
@name = p.consume(:id)
@is_named = true
p.consume(:colon)
end
@variables = []
while (var = p.expression)
@variables << var
break unless p.consume?(:comma)
end
raise_syntax_error(options) if @variables.empty?
end
# Temporarily until we migrate
def strict_parse(markup)
lax_parse(markup)
end
def lax_parse(markup)
case markup
when NamedSyntax
@variables = variables_from_string(Regexp.last_match(2))
@name = parse_expression(Regexp.last_match(1))
@is_named = true
when SimpleSyntax
@variables = variables_from_string(markup)
@name = @variables.to_s
@is_named = !@name.match?(/\w+:0x\h{8}/)
else
raise SyntaxError, options[:locale].t("errors.syntax.cycle")
end
end
def variables_from_string(markup)
markup.split(',').collect do |var|
var =~ /\s*(#{QuotedFragment})\s*/o
+15
View File
@@ -45,4 +45,19 @@ class CycleTagTest < Minitest::Test
assert_template_result("11", template)
end
def test_cycle_tag_with_error_mode
# QuotedFragment is more permissive than what Parser#expression allows.
[:lax, :strict].each do |mode|
with_error_mode(mode) do
assert_template_result("a", "{% cycle .5: 'a', 'b' %}")
assert_template_result("b", "{% assign 5 = 'b' %}{% cycle .5, .4 %}")
end
end
with_error_mode(:rigid) do
assert_raises(Liquid::SyntaxError) { Template.parse("{% cycle .5: 'a', 'b' %}") }
assert_raises(Liquid::SyntaxError) { Template.parse("{% cycle .5, .4 %}") }
end
end
end