mirror of
https://github.com/Shopify/liquid.git
synced 2025-07-23 00:00:18 -04:00
Compare commits
4 Commits
dbf0aa8cd5
...
e180535784
Author | SHA1 | Date | |
---|---|---|---|
|
e180535784 | ||
|
a681e73aec | ||
|
2abf52d546 | ||
|
eada2b65a2 |
@ -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 = "{%"
|
||||
|
@ -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")
|
||||
|
@ -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
|
||||
|
@ -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 %}
|
||||
|
Loading…
x
Reference in New Issue
Block a user