Skip to content

Commit 27ead51

Browse files
authored
Add end tag support (Shopify#1823)
1 parent 0b93182 commit 27ead51

File tree

10 files changed

+61
-6
lines changed

10 files changed

+61
-6
lines changed

lib/liquid/block.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ def unknown_tag(tag_name, _markup, _tokenizer)
3434
end
3535

3636
# @api private
37-
def self.raise_unknown_tag(tag, block_name, block_delimiter, parse_context)
37+
def self.raise_unknown_tag(tag, block_name, block_delimiter, parse_context, supports_end_tag = true)
3838
if tag == 'else'
3939
raise SyntaxError, parse_context.locale.t(
4040
"errors.syntax.unexpected_else",
4141
block_name: block_name,
4242
)
4343
elsif tag.start_with?('end')
4444
raise SyntaxError, parse_context.locale.t(
45-
"errors.syntax.invalid_delimiter",
45+
supports_end_tag ? "errors.syntax.invalid_delimiter" : "errors.syntax.invalid_delimiter_no_end",
4646
tag: tag,
4747
block_name: block_name,
4848
block_delimiter: block_delimiter,
@@ -64,6 +64,10 @@ def block_delimiter
6464
@block_delimiter ||= "end#{block_name}"
6565
end
6666

67+
def supports_end_tag?
68+
true
69+
end
70+
6771
private
6872

6973
# @api public
@@ -81,7 +85,7 @@ def parse_body(body, tokens)
8185
body.parse(tokens, parse_context) do |end_tag_name, end_tag_params|
8286
@blank &&= body.blank?
8387

84-
return false if end_tag_name == block_delimiter
88+
return false if end_tag_name == block_delimiter || (supports_end_tag? && end_tag_name == 'end')
8589
raise_tag_never_closed(block_name) unless end_tag_name
8690

8791
# this tag is not registered with the system

lib/liquid/block_body.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def freeze
6868

6969
# @api private
7070
def self.unknown_tag_in_liquid_tag(tag, parse_context)
71-
Block.raise_unknown_tag(tag, 'liquid', '%}', parse_context)
71+
Block.raise_unknown_tag(tag, 'liquid', '%}', parse_context, false)
7272
end
7373

7474
# @api private

lib/liquid/locales/en.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
if: "Syntax Error in tag 'if' - Valid syntax: if [expression]"
1515
include: "Error in tag 'include' - Valid syntax: include '[template]' (with|for) [object|collection]"
1616
inline_comment_invalid: "Syntax error in tag '#' - Each line of comments must be prefixed by the '#' character"
17-
invalid_delimiter: "'%{tag}' is not a valid delimiter for %{block_name} tags. use %{block_delimiter}"
17+
invalid_delimiter: "'%{tag}' is not a valid delimiter for %{block_name} tags. use end or %{block_delimiter}"
18+
invalid_delimiter_no_end: "'%{tag}' is not a valid delimiter for %{block_name} tags. use %{block_delimiter}"
1819
render: "Syntax error in tag 'render' - Template name must be a quoted string"
1920
table_row: "Syntax Error in 'table_row loop' - Valid syntax: table_row [item] in [collection] cols=3"
2021
tag_never_closed: "'%{block_name}' tag was never closed"

lib/liquid/tags/comment.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ def render_to_output_buffer(_context, output)
2222
def unknown_tag(_tag, _markup, _tokens)
2323
end
2424

25+
def supports_end_tag?
26+
false
27+
end
28+
2529
def blank?
2630
true
2731
end

lib/liquid/tags/raw.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ def initialize(tag_name, markup, parse_context)
2222
ensure_valid_markup(tag_name, markup, parse_context)
2323
end
2424

25+
def supports_end_tag?
26+
false
27+
end
28+
2529
def parse(tokens)
2630
@body = +''
2731
while (token = tokens.shift)

test/integration/block_test.rb

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,31 @@
55
class BlockTest < Minitest::Test
66
include Liquid
77

8+
def test_simple_end_tag
9+
assert_template_result('you rock', '{% if true %}you rock{% end %}')
10+
assert_template_result('you rock', '{% if true %}{% unless false %}you rock{% end %}{% end %}')
11+
end
12+
813
def test_unexpected_end_tag
914
source = '{% if true %}{% endunless %}'
10-
assert_match_syntax_error("Liquid syntax error (line 1): 'endunless' is not a valid delimiter for if tags. use endif", source)
15+
assert_match_syntax_error("Liquid syntax error (line 1): 'endunless' is not a valid delimiter for if tags. use end or endif", source)
16+
end
17+
18+
def test_end_closes_closest_open_tag
19+
source = '{% if true %}{% unless true %}{% end %}{% endunless %}'
20+
assert_match_syntax_error("Liquid syntax error (line 1): 'endunless' is not a valid delimiter for if tags. use end or endif", source)
21+
end
22+
23+
# comments are special and can't be closed by `end`
24+
def test_unexpected_end_tag_comment
25+
source = '{% comment %}{% end %}'
26+
assert_match_syntax_error("Liquid syntax error (line 1): 'comment' tag was never closed", source)
27+
end
28+
29+
# raw is special and can't be closed by `end`
30+
def test_unexpected_end_tag_raw
31+
source = '{% raw %}{% end %}'
32+
assert_match_syntax_error("Liquid syntax error (line 1): 'raw' tag was never closed", source)
1133
end
1234

1335
def test_with_custom_tag

test/integration/tags/if_else_tag_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def test_if
1212
' {% if true %} this text should go into the output {% endif %} ',
1313
)
1414
assert_template_result(' you rock ?', '{% if false %} you suck {% endif %} {% if true %} you rock {% endif %}?')
15+
assert_template_result(' you rock ?', '{% if false %} you suck {% endif %} {% if true %} you rock {% end %}?')
1516
end
1617

1718
def test_literal_comparisons

test/integration/tags/liquid_tag_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ def test_cannot_close_blocks_created_before_a_liquid_tag
106106
42
107107
{%- liquid endif -%}
108108
LIQUID
109+
assert_match_syntax_error("syntax error (line 3): 'end' is not a valid delimiter for liquid tags. use %}", <<~LIQUID)
110+
{%- if true -%}
111+
42
112+
{%- liquid end -%}
113+
LIQUID
109114
end
110115

111116
def test_liquid_tag_in_raw
@@ -123,6 +128,15 @@ def test_nested_liquid_tags
123128
endif
124129
-%}
125130
LIQUID
131+
132+
assert_template_result('good', <<~LIQUID)
133+
{%- liquid
134+
liquid
135+
if true
136+
echo "good"
137+
end
138+
-%}
139+
LIQUID
126140
end
127141

128142
def test_nested_liquid_tags_on_same_line

test/integration/tags/raw_tag_test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ def test_tag_in_raw
1010
'{% comment %} test {% endcomment %}',
1111
'{% raw %}{% comment %} test {% endcomment %}{% endraw %}',
1212
)
13+
assert_template_result(
14+
'',
15+
'{% comment %} {% if true %}{% end %} {% endcomment %}',
16+
)
1317
end
1418

1519
def test_output_in_raw

test/integration/tags/standard_tag_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def test_has_a_block_which_does_nothing
3838
assert_template_result('', '{%comment%}{%blabla%}{%endcomment%}')
3939
assert_template_result('', '{% comment %}{% blabla %}{% endcomment %}')
4040
assert_template_result('', '{%comment%}{% endif %}{%endcomment%}')
41+
assert_template_result('', '{%comment%}{% end %}{%endcomment%}')
4142
assert_template_result('', '{% comment %}{% endwhatever %}{% endcomment %}')
4243
assert_template_result('', '{% comment %}{% raw %} {{%%%%}} }} { {% endcomment %} {% comment {% endraw %} {% endcomment %}')
4344
assert_template_result('', '{% comment %}{% " %}{% endcomment %}')

0 commit comments

Comments
 (0)