From 6a5ebb0e856cc481ee64f802e4caf940a9d90488 Mon Sep 17 00:00:00 2001 From: Michael Go Date: Sat, 11 Nov 2023 12:19:44 -0400 Subject: [PATCH] fix parsing nested comment tag with extra strings --- lib/liquid/tags/comment.rb | 9 ++++++--- test/unit/tags/comment_tag_unit_test.rb | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/liquid/tags/comment.rb b/lib/liquid/tags/comment.rb index d6f85571..0446685b 100644 --- a/lib/liquid/tags/comment.rb +++ b/lib/liquid/tags/comment.rb @@ -15,7 +15,7 @@ module Liquid # {% endcomment %} # @liquid_syntax_keyword content The content of the comment. class Comment < Block - TagDelimiter = /\A(.*)#{TagStart}#{WhitespaceControl}?\s*(endcomment)\s*(.*)?#{WhitespaceControl}?#{TagEnd}\z/om + TAG_DELIMITER = /\A(.*)#{TagStart}#{WhitespaceControl}?\s*(endcomment)\s*(.*)?#{WhitespaceControl}?#{TagEnd}\z/om def render_to_output_buffer(_context, output) output @@ -51,9 +51,12 @@ module Liquid next if tag_name_match.nil? tag_name_match[1] - elsif TagDelimiter.match?(token) - # aggressively match comment delimiter first + elsif TAG_DELIMITER.match?(token) + # aggressively match comment delimiter "endcomment" + elsif token =~ BlockBody::FullToken && Regexp.last_match(2) == "comment" + # aggressively match comment tag + "comment" else tag_name_match = BlockBody::FullTokenPossiblyInvalid.match(token) diff --git a/test/unit/tags/comment_tag_unit_test.rb b/test/unit/tags/comment_tag_unit_test.rb index 11d2fd15..9f812b71 100644 --- a/test/unit/tags/comment_tag_unit_test.rb +++ b/test/unit/tags/comment_tag_unit_test.rb @@ -121,4 +121,18 @@ class CommentTagUnitTest < Minitest::Test LIQUID ) end + + def test_nested_comment_tag_with_extra_strings + assert_template_result( + '', + <<~LIQUID.chomp, + {% comment %} + {% comment + {% assign foo = 1 %} + {% endcomment + {% assign foo = 1 %} + {% endcomment %} + LIQUID + ) + end end