Compare commits

...

4 Commits

Author SHA1 Message Date
Michael Go
e180535784 allow incomplete tags inside a comment tag 2023-11-08 16:50:41 -04:00
Michael Go
a681e73aec refactor comment tag unit test to use test helper 2023-11-08 16:45:11 -04:00
Michael Go
2abf52d546 add a quirky comment tag unit test 2023-11-08 11:23:00 -04:00
Michael Go
eada2b65a2
clean up comment tag body parsing
Co-authored-by: Peter Zhu <peter@peterzhu.ca>
2023-11-08 11:13:13 -04:00
4 changed files with 33 additions and 22 deletions

View File

@ -6,6 +6,7 @@ module Liquid
class BlockBody
LiquidTagToken = /\A\s*(#{TagName})\s*(.*?)\z/o
FullToken = /\A#{TagStart}#{WhitespaceControl}?(\s*)(#{TagName})(\s*)(.*?)#{WhitespaceControl}?#{TagEnd}\z/om
FullTokenPossiblyInvalid = /\A(.*)#{TagStart}#{WhitespaceControl}?\s*(\w+)\s*(.*)?#{WhitespaceControl}?#{TagEnd}\z/om
ContentOfVariable = /\A#{VariableStart}#{WhitespaceControl}?(.*?)#{WhitespaceControl}?#{VariableEnd}\z/om
WhitespaceOrNothing = /\A\s*\z/
TAGSTART = "{%"

View File

@ -41,22 +41,18 @@ module Liquid
# The children tag doesn't require to be a valid Liquid except the comment and raw tag.
# The child comment and raw tag must be closed.
while (token = tokens.send(:shift))
tag_name_match = BlockBody::FullToken.match(token)
tag_name_match = BlockBody::FullTokenPossiblyInvalid.match(token)
next if tag_name_match.nil?
tag_name = tag_name_match[2]
if tag_name == "raw"
# raw tags are required to be closed
case tag_name
when "raw"
parse_raw_tag_body(tokens)
next
end
if tag_name_match[2] == "comment"
when "comment"
comment_tag_depth += 1
next
elsif tag_name_match[2] == "endcomment"
when "endcomment"
comment_tag_depth -= 1
return false if comment_tag_depth.zero?
@ -73,7 +69,7 @@ module Liquid
def parse_raw_tag_body(tokens)
while (token = tokens.send(:shift))
return if token =~ Raw::FullTokenPossiblyInvalid && "endraw" == Regexp.last_match(2)
return if token =~ BlockBody::FullTokenPossiblyInvalid && "endraw" == Regexp.last_match(2)
end
raise_tag_never_closed("raw")

View File

@ -14,7 +14,6 @@ module Liquid
# @liquid_syntax_keyword expression The expression to be output without being rendered.
class Raw < Block
Syntax = /\A\s*\z/
FullTokenPossiblyInvalid = /\A(.*)#{TagStart}#{WhitespaceControl}?\s*(\w+)\s*(.*)?#{WhitespaceControl}?#{TagEnd}\z/om
def initialize(tag_name, markup, parse_context)
super
@ -25,7 +24,7 @@ module Liquid
def parse(tokens)
@body = +''
while (token = tokens.shift)
if token =~ FullTokenPossiblyInvalid && block_delimiter == Regexp.last_match(2)
if token =~ BlockBody::FullTokenPossiblyInvalid && block_delimiter == Regexp.last_match(2)
parse_context.trim_whitespace = (token[-3] == WhitespaceControl)
@body << Regexp.last_match(1) if Regexp.last_match(1) != ""
return

View File

@ -4,7 +4,7 @@ require 'test_helper'
class CommentTagUnitTest < Minitest::Test
def test_does_not_parse_nodes_inside_a_comment
template = Liquid::Template.parse(<<~LIQUID.chomp, line_numbers: true)
assert_template_result("", <<~LIQUID.chomp)
{% comment %}
{% if true %}
{% if ... %}
@ -16,12 +16,31 @@ class CommentTagUnitTest < Minitest::Test
{% endcase %}
{% endcomment %}
LIQUID
end
assert_equal("", template.render)
def test_allows_incomplete_tags_inside_a_comment
assert_template_result("", <<~LIQUID.chomp)
{% comment %}
{% assign foo = "1"
{% endcomment %}
LIQUID
assert_template_result("", <<~LIQUID.chomp)
{% comment %}
{% comment %}
{% invalid
{% endcomment %}
{% endcomment %}
LIQUID
assert_template_result("", <<~LIQUID.chomp)
{% comment %}
{% {{ {%- endcomment %}
LIQUID
end
def test_child_comment_tags_need_to_be_closed
template = Liquid::Template.parse(<<~LIQUID.chomp, line_numbers: true)
assert_template_result("", <<~LIQUID.chomp)
{% comment %}
{% comment %}
{% comment %}{% endcomment %}
@ -29,10 +48,8 @@ class CommentTagUnitTest < Minitest::Test
{% endcomment %}
LIQUID
assert_equal("", template.render)
assert_raises(Liquid::SyntaxError) do
Liquid::Template.parse(<<~LIQUID.chomp, line_numbers: true)
assert_template_result("", <<~LIQUID.chomp)
{% comment %}
{% comment %}
{% comment %}
@ -43,7 +60,7 @@ class CommentTagUnitTest < Minitest::Test
end
def test_child_raw_tags_need_to_be_closed
template = Liquid::Template.parse(<<~LIQUID.chomp, line_numbers: true)
assert_template_result("", <<~LIQUID.chomp)
{% comment %}
{% raw %}
{% endcomment %}
@ -51,10 +68,8 @@ class CommentTagUnitTest < Minitest::Test
{% endcomment %}
LIQUID
assert_equal("", template.render)
assert_raises(Liquid::SyntaxError) do
Liquid::Template.parse(<<~LIQUID.chomp, line_numbers: true)
Liquid::Template.parse(<<~LIQUID.chomp)
{% comment %}
{% raw %}
{% endcomment %}