From 31f7be8a6d50da7fba3489d3794e3aea89d35155 Mon Sep 17 00:00:00 2001 From: Dylan Thacker-Smith Date: Fri, 19 Feb 2021 10:55:08 -0500 Subject: [PATCH 1/6] Use liquid-c inline-comment branch until it is merged --- Gemfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index ed0678f80..25ad5b868 100644 --- a/Gemfile +++ b/Gemfile @@ -22,6 +22,6 @@ group :test do gem 'rubocop-performance', require: false platform :mri, :truffleruby do - gem 'liquid-c', github: 'Shopify/liquid-c', ref: 'master' + gem 'liquid-c', github: 'Shopify/liquid-c', ref: 'inline-comment' end end From fff6c565c13af8a750ec58c2a802b412a318b5e9 Mon Sep 17 00:00:00 2001 From: Dylan Thacker-Smith Date: Fri, 19 Feb 2021 10:39:30 -0500 Subject: [PATCH 2/6] 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 From 6ddfaec3f9f8ae5a6340b5e3da7127a50da8ff8c Mon Sep 17 00:00:00 2001 From: Dylan Thacker-Smith Date: Fri, 19 Feb 2021 13:49:39 -0500 Subject: [PATCH 3/6] Add tests for text immediately following liquid tag --- test/integration/tags/inline_comment_test.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/integration/tags/inline_comment_test.rb b/test/integration/tags/inline_comment_test.rb index 9f4859f12..55db2273d 100644 --- a/test/integration/tags/inline_comment_test.rb +++ b/test/integration/tags/inline_comment_test.rb @@ -19,4 +19,9 @@ def test_inside_liquid_tag LIQUID assert_template_result('before()after', source) end + + def test_no_space_after_hash_symbol + assert_template_result('', '{% #immediate text %}') + assert_template_result('', '{% liquid #immediate text %}') + end end From 5e8e5e89b194a0f16ea2c03b0574fabd927d9af1 Mon Sep 17 00:00:00 2001 From: Dylan Thacker-Smith Date: Fri, 19 Feb 2021 13:50:29 -0500 Subject: [PATCH 4/6] Add changelog entry --- History.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/History.md b/History.md index 4280a7298..968e27408 100644 --- a/History.md +++ b/History.md @@ -1,5 +1,10 @@ # Liquid Change Log +## Unreleased + +### Features +* Allow `#` to be used as an inline comment tag (#1401) [Dylan Thacker-Smith] + ## 5.0.0 / 2021-01-06 ### Features From ab455480facb1c4ad19cb0cbee8b9419cb926591 Mon Sep 17 00:00:00 2001 From: Tobias Lutke Date: Thu, 25 Feb 2021 14:35:31 -0400 Subject: [PATCH 5/6] exploring the comment syntax a bit --- test/integration/tags/inline_comment_test.rb | 38 +++++++++++++++++--- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/test/integration/tags/inline_comment_test.rb b/test/integration/tags/inline_comment_test.rb index 55db2273d..f345b74fa 100644 --- a/test/integration/tags/inline_comment_test.rb +++ b/test/integration/tags/inline_comment_test.rb @@ -5,8 +5,35 @@ class InlineCommentTest < Minitest::Test include Liquid - def test_tag + def test_tag_in_different_styles assert_template_result('', '{% # This text gets ignored %}') + assert_template_result('', '{%# This text gets ignored #%}') + assert_template_result('', '{%# This text gets ignored %}') + assert_template_result('', '{%#- This text gets ignored -#%}') + end + + def test_test_syntax_error + assert_template_result('fail', '{% #This doesnt work %}') + + assert false + rescue + # ok good + end + + def test_tag_ws_stripping + assert_template_result('', ' {%#- This text gets ignored -#%} ') + end + + def test_comment_inline_tag + assert_template_result('ok', '{% echo "ok" # output something from a tag %}') + + assert_template_result('ok', '{% # this sort of comment also + echo "ok" %}') + end + + def test_comment_inline_variable + assert_template_result('ok', '{{ "ok" # output something from a variable }}') + assert_template_result('ok', '{{ "OK" | downcase # output something from a variable }}') end def test_inside_liquid_tag @@ -20,8 +47,11 @@ def test_inside_liquid_tag assert_template_result('before()after', source) end - def test_no_space_after_hash_symbol - assert_template_result('', '{% #immediate text %}') - assert_template_result('', '{% liquid #immediate text %}') + def test_multiline + assert_template_result('', '{% # this sort of comment also + # will just work, because it parses + # as a single call to the "#" tag %}') + end + end From 0fc45ca3afae795fc2e0f690e855eebb64ff5853 Mon Sep 17 00:00:00 2001 From: Dylan Thacker-Smith Date: Fri, 26 Feb 2021 06:52:11 -0500 Subject: [PATCH 6/6] Reserve future support for comment line before a tag name --- lib/liquid/locales/en.yml | 1 + lib/liquid/tags/inline_comment.rb | 10 ++++++++++ test/integration/tags/inline_comment_test.rb | 2 ++ 3 files changed, 13 insertions(+) diff --git a/lib/liquid/locales/en.yml b/lib/liquid/locales/en.yml index eb35d8689..090451832 100644 --- a/lib/liquid/locales/en.yml +++ b/lib/liquid/locales/en.yml @@ -22,6 +22,7 @@ tag_never_closed: "'%{block_name}' tag was never closed" table_row: "Syntax Error in 'table_row loop' - Valid syntax: table_row [item] in [collection] cols=3" render: "Syntax error in tag 'render' - Template name must be a quoted string" + inline_comment_invalid: "Syntax error in tag '#' - Each line of comments must be prefixed by the '#' character" argument: include: "Argument error in tag 'include' - Illegal template name" disabled: diff --git a/lib/liquid/tags/inline_comment.rb b/lib/liquid/tags/inline_comment.rb index f08c3ff73..d2d493542 100644 --- a/lib/liquid/tags/inline_comment.rb +++ b/lib/liquid/tags/inline_comment.rb @@ -2,6 +2,16 @@ module Liquid class InlineComment < Tag + def initialize(tag_name, markup, options) + super + # Semantically, a comment should only ignore everything after it on the line. + # Currently, this implementation doesn't support mixing a comment with another tag + # but we need to reserve future support for this. + if markup.match?(/\n\s*[^#]/) + raise SyntaxError, options[:locale].t("errors.syntax.inline_comment_invalid") + end + end + def render_to_output_buffer(_context, output) output end diff --git a/test/integration/tags/inline_comment_test.rb b/test/integration/tags/inline_comment_test.rb index f345b74fa..a880b5451 100644 --- a/test/integration/tags/inline_comment_test.rb +++ b/test/integration/tags/inline_comment_test.rb @@ -26,7 +26,9 @@ def test_tag_ws_stripping def test_comment_inline_tag assert_template_result('ok', '{% echo "ok" # output something from a tag %}') + end + def test_comment_line_before_tag assert_template_result('ok', '{% # this sort of comment also echo "ok" %}') end