Update {% doc %} to no longer support nested tags (as {% comment %} does)

This commit is contained in:
Guilherme Carreiro
2025-02-20 12:37:09 +01:00
committed by Guilherme Carreiro
parent 16592cfb8f
commit b439d0da53
4 changed files with 172 additions and 130 deletions
+1
View File
@@ -8,6 +8,7 @@
case_invalid_when: "Syntax Error in tag 'case' - Valid when condition: {% when [condition] [or condition2...] %}"
case_invalid_else: "Syntax Error in tag 'case' - Valid else condition: {% else %} (no parameters) "
cycle: "Syntax Error in 'cycle' - Valid syntax: cycle [name :] var [, var2, var3 ...]"
doc_invalid_nested: "Syntax Error in 'doc' - Nested doc tags are not allowed"
for: "Syntax Error in 'for loop' - Valid syntax: for [item] in [collection]"
for_invalid_in: "For loops require an 'in' clause"
for_invalid_attribute: "Invalid attribute in for loop. Valid attributes are limit and offset"
+6 -11
View File
@@ -15,8 +15,6 @@ module Liquid
# {% endcomment %}
# @liquid_syntax_keyword content The content of the comment.
class Comment < Block
TAG_NAME = "comment"
def render_to_output_buffer(_context, output)
output
end
@@ -36,10 +34,7 @@ module Liquid
end
parse_context.depth += 1
tag_depth = 1
begin_tag = self.class::TAG_NAME
end_tag = "end#{self.class::TAG_NAME}"
comment_tag_depth = 1
begin
# Consume tokens without creating child nodes.
@@ -62,13 +57,13 @@ module Liquid
case tag_name
when "raw"
parse_raw_tag_body(tokenizer)
when begin_tag
tag_depth += 1
when end_tag
tag_depth -= 1
when "comment"
comment_tag_depth += 1
when "endcomment"
comment_tag_depth -= 1
end
if tag_depth.zero?
if comment_tag_depth.zero?
parse_context.trim_whitespace = (token[-3] == WhitespaceControl) unless tokenizer.for_liquid_tag
return false
end
+43 -2
View File
@@ -24,7 +24,48 @@ module Liquid
# {% render 'message', foo: 'Hello', bar: 'World' %}
# {% enddoc %}
# {{ foo }}, {{ bar }}!
class Doc < Comment
TAG_NAME = "doc"
class Doc < Block
def render_to_output_buffer(_context, output)
output
end
def unknown_tag(_tag, _markup, _tokens)
end
def blank?
true
end
def parse_body(body, tokenizer)
while (token = tokenizer.send(:shift))
tag_name = if tokenizer.for_liquid_tag
next if token.empty? || token.match?(BlockBody::WhitespaceOrNothing)
tag_name_match = BlockBody::LiquidTagToken.match(token)
next if tag_name_match.nil?
tag_name_match[1]
else
token =~ BlockBody::FullToken
Regexp.last_match(2)
end
raise_nested_doc_error if tag_name == "doc"
if tag_name == "enddoc"
parse_context.trim_whitespace = (token[-3] == WhitespaceControl) unless tokenizer.for_liquid_tag
return false
end
end
raise_tag_never_closed(block_name)
end
private
def raise_nested_doc_error
raise SyntaxError, parse_context.locale.t("errors.syntax.doc_invalid_nested")
end
end
end