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