From 24d461a9e34ac36b0e629af36d5e33e0be52d428 Mon Sep 17 00:00:00 2001 From: Michael Go Date: Fri, 1 Dec 2023 16:04:03 -0400 Subject: [PATCH] implement whitespace control to comment tag --- lib/liquid/tags/comment.rb | 5 ++++- test/unit/tags/comment_tag_unit_test.rb | 11 +++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/liquid/tags/comment.rb b/lib/liquid/tags/comment.rb index 465a363f..15370510 100644 --- a/lib/liquid/tags/comment.rb +++ b/lib/liquid/tags/comment.rb @@ -73,7 +73,10 @@ module Liquid when "endcomment" comment_tag_depth -= 1 - return false if comment_tag_depth.zero? + if comment_tag_depth.zero? + parse_context.trim_whitespace = (token[-3] == WhitespaceControl) + return false + end end end diff --git a/test/unit/tags/comment_tag_unit_test.rb b/test/unit/tags/comment_tag_unit_test.rb index 09da018e..a8bfa4e8 100644 --- a/test/unit/tags/comment_tag_unit_test.rb +++ b/test/unit/tags/comment_tag_unit_test.rb @@ -155,4 +155,15 @@ class CommentTagUnitTest < Minitest::Test assert_template_result('', "{% comment %}123{% endcomment\nxyz %}") assert_template_result('', "{% comment %}123{% endcomment\n xyz endcomment %}") end + + def test_with_whitespace_control + assert_template_result("Hello!", " {%- comment -%}123{%- endcomment -%}Hello!") + assert_template_result("Hello!", "{%- comment -%}123{%- endcomment -%} Hello!") + assert_template_result("Hello!", " {%- comment -%}123{%- endcomment -%} Hello!") + + assert_template_result("Hello!", <<~LIQUID.chomp) + {%- comment %}Whitespace control!{% endcomment -%} + Hello! + LIQUID + end end