mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-20 11:20:41 -07:00
Remove ... syntax references
This commit is contained in:
+2
-8
@@ -17,7 +17,6 @@ module Liquid
|
|||||||
DASH = [:dash, "-"].freeze
|
DASH = [:dash, "-"].freeze
|
||||||
DOT = [:dot, "."].freeze
|
DOT = [:dot, "."].freeze
|
||||||
DOTDOT = [:dotdot, ".."].freeze
|
DOTDOT = [:dotdot, ".."].freeze
|
||||||
DOTDOTDOT = [:dotdotdot, "..."].freeze
|
|
||||||
DOT_ORD = ".".ord
|
DOT_ORD = ".".ord
|
||||||
DOUBLE_STRING_LITERAL = /"[^\"]*"/
|
DOUBLE_STRING_LITERAL = /"[^\"]*"/
|
||||||
EOS = [:end_of_string].freeze
|
EOS = [:end_of_string].freeze
|
||||||
@@ -114,15 +113,10 @@ module Liquid
|
|||||||
|
|
||||||
if (special = SPECIAL_TABLE[peeked])
|
if (special = SPECIAL_TABLE[peeked])
|
||||||
ss.scan_byte
|
ss.scan_byte
|
||||||
# Special case for ".." and "..."
|
# Special case for ".."
|
||||||
if special == DOT && ss.peek_byte == DOT_ORD
|
if special == DOT && ss.peek_byte == DOT_ORD
|
||||||
ss.scan_byte
|
ss.scan_byte
|
||||||
if ss.peek_byte == DOT_ORD
|
output << DOTDOT
|
||||||
ss.scan_byte
|
|
||||||
output << DOTDOTDOT
|
|
||||||
else
|
|
||||||
output << DOTDOT
|
|
||||||
end
|
|
||||||
elsif special == DASH
|
elsif special == DASH
|
||||||
# Special case for negative numbers
|
# Special case for negative numbers
|
||||||
if (peeked_byte = ss.peek_byte) && NUMBER_TABLE[peeked_byte]
|
if (peeked_byte = ss.peek_byte) && NUMBER_TABLE[peeked_byte]
|
||||||
|
|||||||
@@ -76,30 +76,7 @@ module Liquid
|
|||||||
inner_context['forloop'] = forloop if forloop
|
inner_context['forloop'] = forloop if forloop
|
||||||
|
|
||||||
@attributes.each do |key, value|
|
@attributes.each do |key, value|
|
||||||
if key.start_with?("...") && is_inline
|
inner_context[key] = context.evaluate(value)
|
||||||
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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
inner_context[context_variable_name] = var unless var.nil?
|
inner_context[context_variable_name] = var unless var.nil?
|
||||||
@@ -132,23 +109,11 @@ module Liquid
|
|||||||
p.consume?(:comma)
|
p.consume?(:comma)
|
||||||
|
|
||||||
@attributes = {}
|
@attributes = {}
|
||||||
while p.look(:dotdotdot) || p.look(:id)
|
while p.look(:id)
|
||||||
if p.consume?(:dotdotdot)
|
key = p.consume
|
||||||
if p.look(:id)
|
p.consume(:colon)
|
||||||
identifier = p.read(:id)
|
@attributes[key] = safe_parse_expression(p)
|
||||||
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)
|
|
||||||
end
|
|
||||||
p.consume?(:comma) # optional comma
|
p.consume?(:comma) # optional comma
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -180,20 +145,8 @@ module Liquid
|
|||||||
@is_for_loop = (with_or_for == FOR)
|
@is_for_loop = (with_or_for == FOR)
|
||||||
|
|
||||||
@attributes = {}
|
@attributes = {}
|
||||||
markup.scan(/(\.\.\.)(\w+)?(?=\s|,|$)|#{TagAttributes.source}/) do |spread, identifier, key, value|
|
markup.scan(TagAttributes) do |key, value|
|
||||||
if spread
|
@attributes[key] = parse_expression(value)
|
||||||
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)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -205,6 +205,49 @@ class SnippetTest < Minitest::Test
|
|||||||
assert_template_result(expected, template)
|
assert_template_result(expected, template)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_render_snippets_as_arguments
|
||||||
|
template = <<~LIQUID.strip
|
||||||
|
{% assign color_scheme = 'dark' %}
|
||||||
|
|
||||||
|
{% snippet header %}
|
||||||
|
<div class="header">
|
||||||
|
{{ message }}
|
||||||
|
</div>
|
||||||
|
{% endsnippet %}
|
||||||
|
|
||||||
|
{% snippet main %}
|
||||||
|
{% assign color_scheme = 'auto' %}
|
||||||
|
|
||||||
|
<div class="main main--{{ color_scheme }}">
|
||||||
|
{% render header, message: 'Welcome!' %}
|
||||||
|
</div>
|
||||||
|
{% endsnippet %}
|
||||||
|
|
||||||
|
{% render main, header: header %}
|
||||||
|
LIQUID
|
||||||
|
|
||||||
|
expected = <<~OUTPUT
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<div class="main main--auto">
|
||||||
|
|
||||||
|
<div class="header">
|
||||||
|
Welcome!
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
OUTPUT
|
||||||
|
|
||||||
|
assert_template_result(expected, template)
|
||||||
|
end
|
||||||
|
|
||||||
def test_render_inline_snippet_shouldnt_leak_context
|
def test_render_inline_snippet_shouldnt_leak_context
|
||||||
template = <<~LIQUID.strip
|
template = <<~LIQUID.strip
|
||||||
{% snippet input %}
|
{% snippet input %}
|
||||||
@@ -263,7 +306,7 @@ class SnippetTest < Minitest::Test
|
|||||||
assert_template_result(expected, template)
|
assert_template_result(expected, template)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_render_inline_snippet_without_outside_context
|
def test_render_inline_snippet_ignores_outside_context
|
||||||
template = <<~LIQUID.strip
|
template = <<~LIQUID.strip
|
||||||
{% assign color_scheme = 'dark' %}
|
{% assign color_scheme = 'dark' %}
|
||||||
|
|
||||||
@@ -291,18 +334,23 @@ class SnippetTest < Minitest::Test
|
|||||||
assert_template_result(expected, template)
|
assert_template_result(expected, template)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_render_inline_snippet_with_outside_context
|
def test_render_captured_snippet
|
||||||
template = <<~LIQUID.strip
|
template = <<~LIQUID
|
||||||
{% assign color_scheme = 'dark' %}
|
|
||||||
|
|
||||||
{% snippet header %}
|
{% snippet header %}
|
||||||
<div class="header header--{{ color_scheme }}">
|
<div class="header">
|
||||||
{{ message }}
|
{{ message }}
|
||||||
</div>
|
</div>
|
||||||
{% endsnippet %}
|
{% endsnippet %}
|
||||||
|
|
||||||
|
{% capture up_header %}
|
||||||
|
{%- render header, message: 'Welcome!' -%}
|
||||||
|
{% endcapture %}
|
||||||
|
|
||||||
{% render header, ..., message: 'Welcome!' %}
|
{{ up_header | upcase }}
|
||||||
|
|
||||||
|
{{ header | upcase }}
|
||||||
|
|
||||||
|
{{ header }}
|
||||||
LIQUID
|
LIQUID
|
||||||
expected = <<~OUTPUT
|
expected = <<~OUTPUT
|
||||||
|
|
||||||
@@ -310,10 +358,14 @@ class SnippetTest < Minitest::Test
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<DIV CLASS="HEADER">
|
||||||
|
WELCOME!
|
||||||
|
</DIV>
|
||||||
|
|
||||||
<div class="header header--dark">
|
|
||||||
Welcome!
|
SNIPPETDROP
|
||||||
</div>
|
|
||||||
|
SnippetDrop
|
||||||
OUTPUT
|
OUTPUT
|
||||||
|
|
||||||
assert_template_result(expected, template)
|
assert_template_result(expected, template)
|
||||||
@@ -332,7 +384,7 @@ class SnippetTest < Minitest::Test
|
|||||||
|
|
||||||
{{ color_scheme }}
|
{{ color_scheme }}
|
||||||
|
|
||||||
{% render header, ..., message: 'Welcome!' %}
|
{% render header, message: 'Welcome!', color_scheme: color_scheme %}
|
||||||
|
|
||||||
{{ color_scheme }}
|
{{ color_scheme }}
|
||||||
LIQUID
|
LIQUID
|
||||||
@@ -356,328 +408,6 @@ class SnippetTest < Minitest::Test
|
|||||||
assert_template_result(expected, template)
|
assert_template_result(expected, template)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_render_inline_snippet_with_correct_argument_precedence
|
|
||||||
template = <<~LIQUID.strip
|
|
||||||
{% assign color_scheme = 'dark' %}
|
|
||||||
{% assign message = 'Goodbye!' %}
|
|
||||||
|
|
||||||
{% snippet header %}
|
|
||||||
<div class="header header--{{ color_scheme }}">
|
|
||||||
{{ message }}
|
|
||||||
</div>
|
|
||||||
{% endsnippet %}
|
|
||||||
|
|
||||||
|
|
||||||
{% render header, message: 'Welcome!', ... %}
|
|
||||||
LIQUID
|
|
||||||
expected = <<~OUTPUT
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div class="header header--dark">
|
|
||||||
Goodbye!
|
|
||||||
</div>
|
|
||||||
OUTPUT
|
|
||||||
|
|
||||||
assert_template_result(expected, template)
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_render_inline_snippet_with_correct_argument_order
|
|
||||||
template = <<~LIQUID.strip
|
|
||||||
{% assign color_scheme = 'dark' %}
|
|
||||||
{% assign message = 'Goodbye!' %}
|
|
||||||
|
|
||||||
{% snippet header %}
|
|
||||||
<div class="header header--{{ color_scheme }}">
|
|
||||||
{{ message }}
|
|
||||||
</div>
|
|
||||||
{% endsnippet %}
|
|
||||||
|
|
||||||
|
|
||||||
{% render header, ..., message: 'Welcome!' %}
|
|
||||||
LIQUID
|
|
||||||
expected = <<~OUTPUT
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div class="header header--dark">
|
|
||||||
Welcome!
|
|
||||||
</div>
|
|
||||||
OUTPUT
|
|
||||||
|
|
||||||
assert_template_result(expected, template)
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_render_inline_snippet_with_correct_duplicate_argument_precedence
|
|
||||||
template = <<~LIQUID.strip
|
|
||||||
{% assign color_scheme = 'dark' %}
|
|
||||||
{% assign message = 'Goodbye!' %}
|
|
||||||
|
|
||||||
{% snippet header %}
|
|
||||||
<div class="header header--{{ color_scheme }}">
|
|
||||||
{{ message }}
|
|
||||||
</div>
|
|
||||||
{% endsnippet %}
|
|
||||||
|
|
||||||
|
|
||||||
{% render header, message: 'Welcome!', ..., message: 'Hi!' %}
|
|
||||||
LIQUID
|
|
||||||
expected = <<~OUTPUT
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div class="header header--dark">
|
|
||||||
Hi!
|
|
||||||
</div>
|
|
||||||
OUTPUT
|
|
||||||
|
|
||||||
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' %}
|
|
||||||
|
|
||||||
{% snippet header %}
|
|
||||||
<div class="header header--{{ color_scheme }}">
|
|
||||||
{{ message }}
|
|
||||||
</div>
|
|
||||||
{% endsnippet %}
|
|
||||||
|
|
||||||
{% capture up_header %}
|
|
||||||
{%- render header, ..., message: 'Welcome!' -%}
|
|
||||||
{% endcapture %}
|
|
||||||
|
|
||||||
{{ up_header | upcase }}
|
|
||||||
|
|
||||||
{{ header | upcase }}
|
|
||||||
|
|
||||||
{{ header }}
|
|
||||||
LIQUID
|
|
||||||
expected = <<~OUTPUT
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<DIV CLASS="HEADER HEADER--DARK">
|
|
||||||
WELCOME!
|
|
||||||
</DIV>
|
|
||||||
|
|
||||||
|
|
||||||
SNIPPETDROP
|
|
||||||
|
|
||||||
SnippetDrop
|
|
||||||
OUTPUT
|
|
||||||
|
|
||||||
assert_template_result(expected, template)
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_render_snippets_as_arguments
|
|
||||||
template = <<~LIQUID.strip
|
|
||||||
{% assign color_scheme = 'dark' %}
|
|
||||||
|
|
||||||
{% snippet header %}
|
|
||||||
<div class="header header--{{ color_scheme }}">
|
|
||||||
{{ message }}
|
|
||||||
</div>
|
|
||||||
{% endsnippet %}
|
|
||||||
|
|
||||||
{% snippet main %}
|
|
||||||
{% assign color_scheme = 'auto' %}
|
|
||||||
|
|
||||||
<div class="main main--{{ color_scheme }}">
|
|
||||||
{% render header, ..., message: 'Welcome!' %}
|
|
||||||
</div>
|
|
||||||
{% endsnippet %}
|
|
||||||
|
|
||||||
{% render main, header: header %}
|
|
||||||
LIQUID
|
|
||||||
|
|
||||||
expected = <<~OUTPUT
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div class="main main--auto">
|
|
||||||
|
|
||||||
<div class="header header--auto">
|
|
||||||
Welcome!
|
|
||||||
</div>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
OUTPUT
|
|
||||||
|
|
||||||
assert_template_result(expected, template)
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_render_inline_snippet_forloop
|
def test_render_inline_snippet_forloop
|
||||||
template = <<~LIQUID.strip
|
template = <<~LIQUID.strip
|
||||||
{% snippet item %}
|
{% snippet item %}
|
||||||
@@ -738,36 +468,6 @@ class SnippetTest < Minitest::Test
|
|||||||
|
|
||||||
assert_template_result(expected, template)
|
assert_template_result(expected, template)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_render_inline_snippet_inside_loop
|
|
||||||
template = <<~LIQUID.strip
|
|
||||||
{% assign color_scheme = 'dark' %}
|
|
||||||
{% assign array = '1,2,3' | split: ',' %}
|
|
||||||
|
|
||||||
{% for i in array %}
|
|
||||||
{% snippet header %}
|
|
||||||
<div class="header header--{{ color_scheme }}">
|
|
||||||
{{ message }} {{ i }}
|
|
||||||
</div>
|
|
||||||
{% endsnippet %}
|
|
||||||
{% endfor %}
|
|
||||||
|
|
||||||
{% render header, ..., message: '👉' %}
|
|
||||||
LIQUID
|
|
||||||
expected = <<~OUTPUT
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div class="header header--dark">
|
|
||||||
👉#{" "}
|
|
||||||
</div>
|
|
||||||
OUTPUT
|
|
||||||
|
|
||||||
assert_template_result(expected, template)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
class RigidMode < SnippetTest
|
class RigidMode < SnippetTest
|
||||||
@@ -970,6 +670,49 @@ class SnippetTest < Minitest::Test
|
|||||||
assert_template_result(expected, template, error_mode: :rigid)
|
assert_template_result(expected, template, error_mode: :rigid)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_render_snippets_as_arguments
|
||||||
|
template = <<~LIQUID.strip
|
||||||
|
{% assign color_scheme = 'dark' %}
|
||||||
|
|
||||||
|
{% snippet header %}
|
||||||
|
<div class="header">
|
||||||
|
{{ message }}
|
||||||
|
</div>
|
||||||
|
{% endsnippet %}
|
||||||
|
|
||||||
|
{% snippet main %}
|
||||||
|
{% assign color_scheme = 'auto' %}
|
||||||
|
|
||||||
|
<div class="main main--{{ color_scheme }}">
|
||||||
|
{% render header, message: 'Welcome!' %}
|
||||||
|
</div>
|
||||||
|
{% endsnippet %}
|
||||||
|
|
||||||
|
{% render main, header: header %}
|
||||||
|
LIQUID
|
||||||
|
|
||||||
|
expected = <<~OUTPUT
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<div class="main main--auto">
|
||||||
|
|
||||||
|
<div class="header">
|
||||||
|
Welcome!
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
OUTPUT
|
||||||
|
|
||||||
|
assert_template_result(expected, template, error_mode: :rigid)
|
||||||
|
end
|
||||||
|
|
||||||
def test_render_inline_snippet_shouldnt_leak_context
|
def test_render_inline_snippet_shouldnt_leak_context
|
||||||
template = <<~LIQUID.strip
|
template = <<~LIQUID.strip
|
||||||
{% snippet input %}
|
{% snippet input %}
|
||||||
@@ -1028,7 +771,7 @@ class SnippetTest < Minitest::Test
|
|||||||
assert_template_result(expected, template, error_mode: :rigid)
|
assert_template_result(expected, template, error_mode: :rigid)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_render_inline_snippet_without_outside_context
|
def test_render_inline_snippet_ignores_outside_context
|
||||||
template = <<~LIQUID.strip
|
template = <<~LIQUID.strip
|
||||||
{% assign color_scheme = 'dark' %}
|
{% assign color_scheme = 'dark' %}
|
||||||
|
|
||||||
@@ -1056,34 +799,6 @@ class SnippetTest < Minitest::Test
|
|||||||
assert_template_result(expected, template, error_mode: :rigid)
|
assert_template_result(expected, template, error_mode: :rigid)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_render_inline_snippet_with_outside_context
|
|
||||||
template = <<~LIQUID.strip
|
|
||||||
{% assign color_scheme = 'dark' %}
|
|
||||||
|
|
||||||
{% snippet header %}
|
|
||||||
<div class="header header--{{ color_scheme }}">
|
|
||||||
{{ message }}
|
|
||||||
</div>
|
|
||||||
{% endsnippet %}
|
|
||||||
|
|
||||||
|
|
||||||
{% render header, ..., message: 'Welcome!' %}
|
|
||||||
LIQUID
|
|
||||||
expected = <<~OUTPUT
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div class="header header--dark">
|
|
||||||
Welcome!
|
|
||||||
</div>
|
|
||||||
OUTPUT
|
|
||||||
|
|
||||||
assert_template_result(expected, template, error_mode: :rigid)
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_inline_snippet_local_scope_takes_precedence
|
def test_inline_snippet_local_scope_takes_precedence
|
||||||
template = <<~LIQUID
|
template = <<~LIQUID
|
||||||
{% assign color_scheme = 'dark' %}
|
{% assign color_scheme = 'dark' %}
|
||||||
@@ -1097,7 +812,7 @@ class SnippetTest < Minitest::Test
|
|||||||
|
|
||||||
{{ color_scheme }}
|
{{ color_scheme }}
|
||||||
|
|
||||||
{% render header, ..., message: 'Welcome!' %}
|
{% render header, message: 'Welcome!', color_scheme: color_scheme %}
|
||||||
|
|
||||||
{{ color_scheme }}
|
{{ color_scheme }}
|
||||||
LIQUID
|
LIQUID
|
||||||
@@ -1121,306 +836,6 @@ class SnippetTest < Minitest::Test
|
|||||||
assert_template_result(expected, template, error_mode: :rigid)
|
assert_template_result(expected, template, error_mode: :rigid)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_render_inline_snippet_with_correct_argument_precedence
|
|
||||||
template = <<~LIQUID.strip
|
|
||||||
{% assign color_scheme = 'dark' %}
|
|
||||||
{% assign message = 'Goodbye!' %}
|
|
||||||
|
|
||||||
{% snippet header %}
|
|
||||||
<div class="header header--{{ color_scheme }}">
|
|
||||||
{{ message }}
|
|
||||||
</div>
|
|
||||||
{% endsnippet %}
|
|
||||||
|
|
||||||
|
|
||||||
{% render header, message: 'Welcome!', ... %}
|
|
||||||
LIQUID
|
|
||||||
expected = <<~OUTPUT
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div class="header header--dark">
|
|
||||||
Goodbye!
|
|
||||||
</div>
|
|
||||||
OUTPUT
|
|
||||||
|
|
||||||
assert_template_result(expected, template, error_mode: :rigid)
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_render_inline_snippet_with_correct_argument_order
|
|
||||||
template = <<~LIQUID.strip
|
|
||||||
{% assign color_scheme = 'dark' %}
|
|
||||||
{% assign message = 'Goodbye!' %}
|
|
||||||
|
|
||||||
{% snippet header %}
|
|
||||||
<div class="header header--{{ color_scheme }}">
|
|
||||||
{{ message }}
|
|
||||||
</div>
|
|
||||||
{% endsnippet %}
|
|
||||||
|
|
||||||
|
|
||||||
{% render header, ..., message: 'Welcome!' %}
|
|
||||||
LIQUID
|
|
||||||
expected = <<~OUTPUT
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div class="header header--dark">
|
|
||||||
Welcome!
|
|
||||||
</div>
|
|
||||||
OUTPUT
|
|
||||||
|
|
||||||
assert_template_result(expected, template, error_mode: :rigid)
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_render_inline_snippet_with_correct_duplicate_argument_precedence
|
|
||||||
template = <<~LIQUID.strip
|
|
||||||
{% assign color_scheme = 'dark' %}
|
|
||||||
{% assign message = 'Goodbye!' %}
|
|
||||||
|
|
||||||
{% snippet header %}
|
|
||||||
<div class="header header--{{ color_scheme }}">
|
|
||||||
{{ message }}
|
|
||||||
</div>
|
|
||||||
{% endsnippet %}
|
|
||||||
|
|
||||||
|
|
||||||
{% render header, message: 'Welcome!', ..., message: 'Hi!' %}
|
|
||||||
LIQUID
|
|
||||||
expected = <<~OUTPUT
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div class="header header--dark">
|
|
||||||
Hi!
|
|
||||||
</div>
|
|
||||||
OUTPUT
|
|
||||||
|
|
||||||
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' %}
|
|
||||||
|
|
||||||
{% snippet header %}
|
|
||||||
<div class="header header--{{ color_scheme }}">
|
|
||||||
{{ message }}
|
|
||||||
</div>
|
|
||||||
{% endsnippet %}
|
|
||||||
|
|
||||||
{% capture up_header %}
|
|
||||||
{%- render header, ..., message: 'Welcome!' -%}
|
|
||||||
{% endcapture %}
|
|
||||||
|
|
||||||
{{ up_header | upcase }}
|
|
||||||
|
|
||||||
{{ header | upcase }}
|
|
||||||
|
|
||||||
{{ header }}
|
|
||||||
LIQUID
|
|
||||||
expected = <<~OUTPUT
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<DIV CLASS="HEADER HEADER--DARK">
|
|
||||||
WELCOME!
|
|
||||||
</DIV>
|
|
||||||
|
|
||||||
|
|
||||||
SNIPPETDROP
|
|
||||||
|
|
||||||
SnippetDrop
|
|
||||||
OUTPUT
|
|
||||||
|
|
||||||
assert_template_result(expected, template, error_mode: :rigid)
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_render_snippets_as_arguments
|
|
||||||
template = <<~LIQUID.strip
|
|
||||||
{% assign color_scheme = 'dark' %}
|
|
||||||
|
|
||||||
{% snippet header %}
|
|
||||||
<div class="header header--{{ color_scheme }}">
|
|
||||||
{{ message }}
|
|
||||||
</div>
|
|
||||||
{% endsnippet %}
|
|
||||||
|
|
||||||
{% snippet main %}
|
|
||||||
{% assign color_scheme = 'auto' %}
|
|
||||||
|
|
||||||
<div class="main main--{{ color_scheme }}">
|
|
||||||
{% render header, ..., message: 'Welcome!' %}
|
|
||||||
</div>
|
|
||||||
{% endsnippet %}
|
|
||||||
|
|
||||||
{% render main, header: header %}
|
|
||||||
LIQUID
|
|
||||||
|
|
||||||
expected = <<~OUTPUT
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div class="main main--auto">
|
|
||||||
|
|
||||||
<div class="header header--auto">
|
|
||||||
Welcome!
|
|
||||||
</div>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
OUTPUT
|
|
||||||
|
|
||||||
assert_template_result(expected, template, error_mode: :rigid)
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_render_inline_snippet_forloop
|
def test_render_inline_snippet_forloop
|
||||||
template = <<~LIQUID.strip
|
template = <<~LIQUID.strip
|
||||||
{% snippet item %}
|
{% snippet item %}
|
||||||
@@ -1482,20 +897,23 @@ class SnippetTest < Minitest::Test
|
|||||||
assert_template_result(expected, template, error_mode: :rigid)
|
assert_template_result(expected, template, error_mode: :rigid)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_render_inline_snippet_inside_loop
|
def test_render_captured_snippet
|
||||||
template = <<~LIQUID.strip
|
template = <<~LIQUID
|
||||||
{% assign color_scheme = 'dark' %}
|
|
||||||
{% assign array = '1,2,3' | split: ',' %}
|
|
||||||
|
|
||||||
{% for i in array %}
|
|
||||||
{% snippet header %}
|
{% snippet header %}
|
||||||
<div class="header header--{{ color_scheme }}">
|
<div class="header">
|
||||||
{{ message }} {{ i }}
|
{{ message }}
|
||||||
</div>
|
</div>
|
||||||
{% endsnippet %}
|
{% endsnippet %}
|
||||||
{% endfor %}
|
|
||||||
|
|
||||||
{% render header, ..., message: '👉' %}
|
{% capture up_header %}
|
||||||
|
{%- render header, message: 'Welcome!' -%}
|
||||||
|
{% endcapture %}
|
||||||
|
|
||||||
|
{{ up_header | upcase }}
|
||||||
|
|
||||||
|
{{ header | upcase }}
|
||||||
|
|
||||||
|
{{ header }}
|
||||||
LIQUID
|
LIQUID
|
||||||
expected = <<~OUTPUT
|
expected = <<~OUTPUT
|
||||||
|
|
||||||
@@ -1503,10 +921,14 @@ class SnippetTest < Minitest::Test
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<DIV CLASS="HEADER">
|
||||||
|
WELCOME!
|
||||||
|
</DIV>
|
||||||
|
|
||||||
<div class="header header--dark">
|
|
||||||
👉#{" "}
|
SNIPPETDROP
|
||||||
</div>
|
|
||||||
|
SnippetDrop
|
||||||
OUTPUT
|
OUTPUT
|
||||||
|
|
||||||
assert_template_result(expected, template, error_mode: :rigid)
|
assert_template_result(expected, template, error_mode: :rigid)
|
||||||
|
|||||||
Reference in New Issue
Block a user