Skip to content

Commit

Permalink
fix parsing comment block body inside a liquid tag
Browse files Browse the repository at this point in the history
  • Loading branch information
ggmichaelgo committed Nov 9, 2023
1 parent e180535 commit 41f6517
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
26 changes: 18 additions & 8 deletions lib/liquid/tags/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def blank?

private

def parse_body(body, tokens)
def parse_body(body, tokenizer)
if parse_context.depth >= MAX_DEPTH
raise StackLevelError, "Nesting too deep"
end
Expand All @@ -40,16 +40,26 @@ def parse_body(body, tokens)
# 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"
Expand All @@ -67,8 +77,8 @@ def parse_body(body, tokens)
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

Expand Down
13 changes: 13 additions & 0 deletions test/unit/tags/comment_tag_unit_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down

0 comments on commit 41f6517

Please sign in to comment.