mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-12 23:40:45 -07:00
Support prop spreading
This commit is contained in:
committed by
Guilherme Carreiro
parent
0ceeefba02
commit
d4d2237b90
@@ -12,6 +12,14 @@ module Liquid
|
||||
@p = point
|
||||
end
|
||||
|
||||
def read(type = nil)
|
||||
token = @tokens[@p]
|
||||
if type && token[0] != type
|
||||
raise SyntaxError, "Expected #{type} but found #{@tokens[@p].first}"
|
||||
end
|
||||
token[1]
|
||||
end
|
||||
|
||||
def consume(type = nil)
|
||||
token = @tokens[@p]
|
||||
if type && token[0] != type
|
||||
|
||||
@@ -76,12 +76,27 @@ module Liquid
|
||||
inner_context['forloop'] = forloop if forloop
|
||||
|
||||
@attributes.each do |key, value|
|
||||
if key == "..." && is_inline
|
||||
if key.start_with?("...") && is_inline
|
||||
if key == "..."
|
||||
context.scopes.each do |scope|
|
||||
scope.each do |k, v|
|
||||
inner_context[k] = v
|
||||
end
|
||||
end
|
||||
else
|
||||
obj = context.evaluate(value)
|
||||
if obj.is_a?(Liquid::Drop)
|
||||
(obj.class.invokable_methods - ['to_liquid']).each do |method_name|
|
||||
inner_context[method_name] = obj.invoke_drop(method_name)
|
||||
end
|
||||
elsif obj.is_a?(Hash)
|
||||
obj.each do |k, v|
|
||||
inner_context[k] = v
|
||||
end
|
||||
else
|
||||
raise ::ArgumentError
|
||||
end
|
||||
end
|
||||
else
|
||||
inner_context[key] = context.evaluate(value)
|
||||
end
|
||||
@@ -117,11 +132,24 @@ module Liquid
|
||||
p.consume?(:comma)
|
||||
|
||||
@attributes = {}
|
||||
while p.look(:id)
|
||||
while p.look(:dotdotdot) || p.look(:id)
|
||||
if p.consume?(:dotdotdot)
|
||||
if p.look(:id)
|
||||
identifier = p.read(:id)
|
||||
key = "...#{identifier}"
|
||||
@attributes.delete(key)
|
||||
@attributes[key] = safe_parse_expression(p)
|
||||
else
|
||||
@attributes.delete("...")
|
||||
@attributes["..."] = true
|
||||
end
|
||||
else
|
||||
key = p.consume
|
||||
p.consume(:colon)
|
||||
@attributes.delete(key)
|
||||
@attributes[key] = safe_parse_expression(p)
|
||||
p.consume?(:comma)
|
||||
end
|
||||
p.consume?(:comma) # optional comma
|
||||
end
|
||||
|
||||
p.consume(:end_of_string)
|
||||
@@ -150,10 +178,16 @@ module Liquid
|
||||
@is_for_loop = (with_or_for == FOR)
|
||||
|
||||
@attributes = {}
|
||||
markup.scan(/(\.\.\.)(?=\s|,|$)|#{TagAttributes.source}/) do |context_marker, key, value|
|
||||
if context_marker
|
||||
markup.scan(/(\.\.\.)(\w+)?(?=\s|,|$)|#{TagAttributes.source}/) do |spread, identifier, key, value|
|
||||
if spread
|
||||
if identifier
|
||||
spread_key = "...#{identifier}"
|
||||
@attributes.delete(spread_key)
|
||||
@attributes[spread_key] = parse_expression(identifier)
|
||||
else
|
||||
@attributes.delete("...")
|
||||
@attributes["..."] = true
|
||||
end
|
||||
elsif key && value
|
||||
@attributes.delete(key)
|
||||
@attributes[key] = parse_expression(value)
|
||||
|
||||
@@ -446,6 +446,154 @@ class SnippetTest < Minitest::Test
|
||||
assert_template_result(expected, template)
|
||||
end
|
||||
|
||||
def test_render_inline_snippet_with_spread_hash
|
||||
template = <<~LIQUID.strip
|
||||
{% snippet header %}
|
||||
<div>
|
||||
{{ word }} {{ number }}
|
||||
</div>
|
||||
{% endsnippet %}
|
||||
|
||||
{% render header, ...details %}
|
||||
LIQUID
|
||||
|
||||
expected = <<~OUTPUT
|
||||
|
||||
|
||||
|
||||
<div>
|
||||
potato 5
|
||||
</div>
|
||||
OUTPUT
|
||||
|
||||
assert_template_result(expected, template, { 'details' => { 'word' => 'potato', 'number' => 5 } })
|
||||
end
|
||||
|
||||
def test_render_inline_snippet_with_spread_drop
|
||||
product_drop = Class.new(Liquid::Drop) do
|
||||
def title
|
||||
'Cool Product'
|
||||
end
|
||||
|
||||
def price
|
||||
99
|
||||
end
|
||||
|
||||
def vendor
|
||||
'Acme'
|
||||
end
|
||||
end
|
||||
|
||||
template = <<~LIQUID.strip
|
||||
{% snippet card %}
|
||||
<div>
|
||||
{{ title }} - ${{ price }} by {{ vendor }}
|
||||
</div>
|
||||
{% endsnippet %}
|
||||
|
||||
{% render card, ...product %}
|
||||
LIQUID
|
||||
|
||||
expected = <<~OUTPUT
|
||||
|
||||
|
||||
|
||||
<div>
|
||||
Cool Product - $99 by Acme
|
||||
</div>
|
||||
OUTPUT
|
||||
|
||||
assert_template_result(expected, template, { 'product' => product_drop.new })
|
||||
end
|
||||
|
||||
def test_render_inline_snippet_with_overwritten_spread_drop
|
||||
product_drop = Class.new(Liquid::Drop) do
|
||||
def title
|
||||
'Cool Product'
|
||||
end
|
||||
|
||||
def price
|
||||
99
|
||||
end
|
||||
|
||||
def vendor
|
||||
'Acme'
|
||||
end
|
||||
end
|
||||
|
||||
template = <<~LIQUID.strip
|
||||
{% snippet card %}
|
||||
<div>
|
||||
{{ title }} - ${{ price }} by {{ vendor }}
|
||||
</div>
|
||||
{% endsnippet %}
|
||||
|
||||
{% render card, ...product, price: 10 %}
|
||||
LIQUID
|
||||
|
||||
expected = <<~OUTPUT
|
||||
|
||||
|
||||
|
||||
<div>
|
||||
Cool Product - $10 by Acme
|
||||
</div>
|
||||
OUTPUT
|
||||
|
||||
assert_template_result(expected, template, { 'product' => product_drop.new })
|
||||
end
|
||||
|
||||
def test_render_inline_snippet_spread_before_explicit_args
|
||||
template = <<~LIQUID.strip
|
||||
{% snippet card %}
|
||||
<div>{{ price }}</div>
|
||||
{% endsnippet %}
|
||||
|
||||
{% render card, ...details, price: 10 %}
|
||||
LIQUID
|
||||
|
||||
expected = <<~OUTPUT
|
||||
|
||||
|
||||
|
||||
<div>10</div>
|
||||
OUTPUT
|
||||
|
||||
assert_template_result(expected, template, { 'details' => { 'price' => 99 } })
|
||||
end
|
||||
|
||||
def test_render_inline_snippet_multiple_spreads
|
||||
product_drop = Class.new(Liquid::Drop) do
|
||||
def title
|
||||
'Cool Product'
|
||||
end
|
||||
end
|
||||
|
||||
template = <<~LIQUID.strip
|
||||
{% snippet card %}
|
||||
<div>{{ title }} - {{ price }} {{ color }}</div>
|
||||
{% endsnippet %}
|
||||
|
||||
{% render card, ...defaults, ...product %}
|
||||
LIQUID
|
||||
|
||||
expected = <<~OUTPUT
|
||||
|
||||
|
||||
|
||||
<div>Cool Product - 10 </div>
|
||||
OUTPUT
|
||||
|
||||
assert_template_result(
|
||||
expected,
|
||||
template,
|
||||
{
|
||||
'defaults' => { 'title' => 'Default', 'price' => 10 },
|
||||
'product' => product_drop.new,
|
||||
},
|
||||
)
|
||||
end
|
||||
|
||||
def test_render_captured_snippet
|
||||
template = <<~LIQUID
|
||||
{% assign color_scheme = 'dark' %}
|
||||
@@ -1063,6 +1211,132 @@ class SnippetTest < Minitest::Test
|
||||
assert_template_result(expected, template, error_mode: :rigid)
|
||||
end
|
||||
|
||||
def test_render_inline_snippet_with_spread_drop
|
||||
product_drop = Class.new(Liquid::Drop) do
|
||||
def title
|
||||
'Cool Product'
|
||||
end
|
||||
|
||||
def price
|
||||
99
|
||||
end
|
||||
|
||||
def vendor
|
||||
'Acme'
|
||||
end
|
||||
end
|
||||
|
||||
template = <<~LIQUID.strip
|
||||
{% snippet card %}
|
||||
<div>
|
||||
{{ title }} - ${{ price }} by {{ vendor }}
|
||||
</div>
|
||||
{% endsnippet %}
|
||||
|
||||
{% render card, ...product %}
|
||||
LIQUID
|
||||
|
||||
expected = <<~OUTPUT
|
||||
|
||||
|
||||
|
||||
<div>
|
||||
Cool Product - $99 by Acme
|
||||
</div>
|
||||
OUTPUT
|
||||
|
||||
assert_template_result(expected, template, { 'product' => product_drop.new }, error_mode: :rigid)
|
||||
end
|
||||
|
||||
def test_render_inline_snippet_with_overwritten_spread_drop
|
||||
product_drop = Class.new(Liquid::Drop) do
|
||||
def title
|
||||
'Cool Product'
|
||||
end
|
||||
|
||||
def price
|
||||
99
|
||||
end
|
||||
|
||||
def vendor
|
||||
'Acme'
|
||||
end
|
||||
end
|
||||
|
||||
template = <<~LIQUID.strip
|
||||
{% snippet card %}
|
||||
<div>
|
||||
{{ title }} - ${{ price }} by {{ vendor }}
|
||||
</div>
|
||||
{% endsnippet %}
|
||||
|
||||
{% render card, ...product, price: 10 %}
|
||||
LIQUID
|
||||
|
||||
expected = <<~OUTPUT
|
||||
|
||||
|
||||
|
||||
<div>
|
||||
Cool Product - $10 by Acme
|
||||
</div>
|
||||
OUTPUT
|
||||
|
||||
assert_template_result(expected, template, { 'product' => product_drop.new }, error_mode: :rigid)
|
||||
end
|
||||
|
||||
def test_render_inline_snippet_spread_before_explicit_args
|
||||
template = <<~LIQUID.strip
|
||||
{% snippet card %}
|
||||
<div>{{ price }}</div>
|
||||
{% endsnippet %}
|
||||
|
||||
{% render card, ...details, price: 10 %}
|
||||
LIQUID
|
||||
|
||||
expected = <<~OUTPUT
|
||||
|
||||
|
||||
|
||||
<div>10</div>
|
||||
OUTPUT
|
||||
|
||||
assert_template_result(expected, template, { 'details' => { 'price' => 99 } }, error_mode: :rigid)
|
||||
end
|
||||
|
||||
def test_render_inline_snippet_multiple_spreads
|
||||
product_drop = Class.new(Liquid::Drop) do
|
||||
def title
|
||||
'Cool Product'
|
||||
end
|
||||
end
|
||||
|
||||
template = <<~LIQUID.strip
|
||||
{% snippet card %}
|
||||
<div>{{ title }} - {{ price }} {{ color }}</div>
|
||||
{% endsnippet %}
|
||||
|
||||
{% render card, ...defaults, ...product %}
|
||||
LIQUID
|
||||
|
||||
expected = <<~OUTPUT
|
||||
|
||||
|
||||
|
||||
<div>Cool Product - 10 </div>
|
||||
OUTPUT
|
||||
|
||||
assert_template_result(
|
||||
expected,
|
||||
template,
|
||||
{
|
||||
'defaults' => { 'title' => 'Default', 'price' => 10 },
|
||||
'product' => product_drop.new,
|
||||
},
|
||||
error_mode: :rigid,
|
||||
)
|
||||
end
|
||||
|
||||
def test_render_captured_snippet
|
||||
template = <<~LIQUID
|
||||
{% assign color_scheme = 'dark' %}
|
||||
|
||||
Reference in New Issue
Block a user