mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-29 07:05:12 -07:00
Add a rigid_parse method to cycle
This commit is contained in:
committed by
Guilherme Carreiro
parent
6d585a24f1
commit
c78bf20010
+41
-12
@@ -22,18 +22,7 @@ module Liquid
|
|||||||
|
|
||||||
def initialize(tag_name, markup, options)
|
def initialize(tag_name, markup, options)
|
||||||
super
|
super
|
||||||
case markup
|
parse_with_selected_parser(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
|
end
|
||||||
|
|
||||||
def named?
|
def named?
|
||||||
@@ -65,6 +54,46 @@ module Liquid
|
|||||||
|
|
||||||
private
|
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)
|
def variables_from_string(markup)
|
||||||
markup.split(',').collect do |var|
|
markup.split(',').collect do |var|
|
||||||
var =~ /\s*(#{QuotedFragment})\s*/o
|
var =~ /\s*(#{QuotedFragment})\s*/o
|
||||||
|
|||||||
@@ -45,4 +45,19 @@ class CycleTagTest < Minitest::Test
|
|||||||
|
|
||||||
assert_template_result("11", template)
|
assert_template_result("11", template)
|
||||||
end
|
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
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user