From defbb3f0e84c10418809df8b3118c3f6e2b1c956 Mon Sep 17 00:00:00 2001 From: Dylan Thacker-Smith Date: Fri, 19 Feb 2021 10:39:30 -0500 Subject: [PATCH] Implement the inline comment tag --- lib/liquid/block_body.rb | 4 ++-- lib/liquid/tags/inline_comment.rb | 15 +++++++++++++ test/integration/tags/inline_comment_test.rb | 22 ++++++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 lib/liquid/tags/inline_comment.rb create mode 100644 test/integration/tags/inline_comment_test.rb diff --git a/lib/liquid/block_body.rb b/lib/liquid/block_body.rb index 76ab0a857..a9c002c93 100644 --- a/lib/liquid/block_body.rb +++ b/lib/liquid/block_body.rb @@ -4,8 +4,8 @@ module Liquid class BlockBody - LiquidTagToken = /\A\s*(\w+)\s*(.*?)\z/o - FullToken = /\A#{TagStart}#{WhitespaceControl}?(\s*)(\w+)(\s*)(.*?)#{WhitespaceControl}?#{TagEnd}\z/om + LiquidTagToken = /\A\s*(\w+|#)\s*(.*?)\z/o + FullToken = /\A#{TagStart}#{WhitespaceControl}?(\s*)(\w+|#)(\s*)(.*?)#{WhitespaceControl}?#{TagEnd}\z/om ContentOfVariable = /\A#{VariableStart}#{WhitespaceControl}?(.*?)#{WhitespaceControl}?#{VariableEnd}\z/om WhitespaceOrNothing = /\A\s*\z/ TAGSTART = "{%" diff --git a/lib/liquid/tags/inline_comment.rb b/lib/liquid/tags/inline_comment.rb new file mode 100644 index 000000000..f08c3ff73 --- /dev/null +++ b/lib/liquid/tags/inline_comment.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +module Liquid + class InlineComment < Tag + def render_to_output_buffer(_context, output) + output + end + + def blank? + true + end + end + + Template.register_tag('#', InlineComment) +end diff --git a/test/integration/tags/inline_comment_test.rb b/test/integration/tags/inline_comment_test.rb new file mode 100644 index 000000000..9f4859f12 --- /dev/null +++ b/test/integration/tags/inline_comment_test.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +require 'test_helper' + +class InlineCommentTest < Minitest::Test + include Liquid + + def test_tag + assert_template_result('', '{% # This text gets ignored %}') + end + + def test_inside_liquid_tag + source = <<~LIQUID + {%- liquid + echo "before(" + # This text gets ignored + echo ")after" + -%} + LIQUID + assert_template_result('before()after', source) + end +end