fix parsing comment block body inside a liquid tag

This commit is contained in:
Michael Go
2023-11-09 16:27:23 -04:00
parent e180535784
commit 41f65173b0
2 changed files with 31 additions and 8 deletions
+18 -8
View File
@@ -28,7 +28,7 @@ module Liquid
private
def parse_body(body, tokens)
def parse_body(body, tokenizer)
if parse_context.depth >= MAX_DEPTH
raise StackLevelError, "Nesting too deep"
end
@@ -40,16 +40,26 @@ module Liquid
# Consume tokens without creating child nodes.
# The children tag doesn't require to be a valid Liquid except the comment and raw tag.
# The child comment and raw tag must be closed.
while (token = tokens.send(:shift))
tag_name_match = BlockBody::FullTokenPossiblyInvalid.match(token)
while (token = tokenizer.send(:shift))
tag_name = if tokenizer.for_liquid_tag
next if token.empty? || token.match?(BlockBody::WhitespaceOrNothing)
next if tag_name_match.nil?
tag_name_match = BlockBody::LiquidTagToken.match(token)
tag_name = tag_name_match[2]
next if tag_name_match.nil?
tag_name_match[1]
else
tag_name_match = BlockBody::FullTokenPossiblyInvalid.match(token)
next if tag_name_match.nil?
tag_name_match[2]
end
case tag_name
when "raw"
parse_raw_tag_body(tokens)
parse_raw_tag_body(tokenizer)
when "comment"
comment_tag_depth += 1
when "endcomment"
@@ -67,8 +77,8 @@ module Liquid
false
end
def parse_raw_tag_body(tokens)
while (token = tokens.send(:shift))
def parse_raw_tag_body(tokenizer)
while (token = tokenizer.send(:shift))
return if token =~ BlockBody::FullTokenPossiblyInvalid && "endraw" == Regexp.last_match(2)
end
+13
View File
@@ -3,6 +3,19 @@
require 'test_helper'
class CommentTagUnitTest < Minitest::Test
def test_comment_inside_liquid_tag
assert_template_result("", <<~LIQUID.chomp)
{% liquid
if 1 != 1
comment
else
echo 123
endcomment
endif
%}
LIQUID
end
def test_does_not_parse_nodes_inside_a_comment
assert_template_result("", <<~LIQUID.chomp)
{% comment %}