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 class BlockBody
LiquidTagToken = /\A\s*(#{TagName})\s*(.*?)\z/o LiquidTagToken = /\A\s*(#{TagName})\s*(.*?)\z/o
FullToken = /\A#{TagStart}#{WhitespaceControl}?(\s*)(#{TagName})(\s*)(.*?)#{WhitespaceControl}?#{TagEnd}\z/om 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 ContentOfVariable = /\A#{VariableStart}#{WhitespaceControl}?(.*?)#{WhitespaceControl}?#{VariableEnd}\z/om
WhitespaceOrNothing = /\A\s*\z/ WhitespaceOrNothing = /\A\s*\z/
TAGSTART = "{%" 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 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. # The child comment and raw tag must be closed.
while (token = tokens.send(:shift)) 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? next if tag_name_match.nil?
tag_name = tag_name_match[2] tag_name = tag_name_match[2]
if tag_name == "raw" case tag_name
# raw tags are required to be closed when "raw"
parse_raw_tag_body(tokens) parse_raw_tag_body(tokens)
next when "comment"
end
if tag_name_match[2] == "comment"
comment_tag_depth += 1 comment_tag_depth += 1
next when "endcomment"
elsif tag_name_match[2] == "endcomment"
comment_tag_depth -= 1 comment_tag_depth -= 1
return false if comment_tag_depth.zero? return false if comment_tag_depth.zero?
@ -73,7 +69,7 @@ module Liquid
def parse_raw_tag_body(tokens) def parse_raw_tag_body(tokens)
while (token = tokens.send(:shift)) 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 end
raise_tag_never_closed("raw") 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. # @liquid_syntax_keyword expression The expression to be output without being rendered.
class Raw < Block class Raw < Block
Syntax = /\A\s*\z/ Syntax = /\A\s*\z/
FullTokenPossiblyInvalid = /\A(.*)#{TagStart}#{WhitespaceControl}?\s*(\w+)\s*(.*)?#{WhitespaceControl}?#{TagEnd}\z/om
def initialize(tag_name, markup, parse_context) def initialize(tag_name, markup, parse_context)
super super
@ -25,7 +24,7 @@ module Liquid
def parse(tokens) def parse(tokens)
@body = +'' @body = +''
while (token = tokens.shift) 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) parse_context.trim_whitespace = (token[-3] == WhitespaceControl)
@body << Regexp.last_match(1) if Regexp.last_match(1) != "" @body << Regexp.last_match(1) if Regexp.last_match(1) != ""
return return

View File

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