Skip to content

Commit

Permalink
don't parse nodes inside comment and raw tags
Browse files Browse the repository at this point in the history
Co-Authored by Alex Coco <alex.coco@shopify.com>
  • Loading branch information
ggmichaelgo committed Nov 6, 2023
1 parent 0b93182 commit 760961a
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/liquid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ module Liquid
require 'liquid/tag/disabler'
require 'liquid/tag/disableable'
require 'liquid/block'
require 'liquid/non_parsing_block'
require 'liquid/block_body'
require 'liquid/document'
require 'liquid/variable'
Expand Down
26 changes: 26 additions & 0 deletions lib/liquid/non_parsing_block.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

module Liquid
class NonParsingBlock < Block
def parse_body(body, tokens)
if parse_context.depth >= MAX_DEPTH
raise StackLevelError, "Nesting too deep"
end
parse_context.depth += 1

begin
while token = tokens.shift
tag_name_match = BlockBody::FullToken.match(token)

return false if tag_name_match && tag_name_match[2] == block_delimiter
end

raise_tag_never_closed(block_name)
ensure
parse_context.depth -= 1
end

false
end
end
end
2 changes: 1 addition & 1 deletion lib/liquid/tags/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module Liquid
# content
# {% endcomment %}
# @liquid_syntax_keyword content The content of the comment.
class Comment < Block
class Comment < NonParsingBlock
def render_to_output_buffer(_context, output)
output
end
Expand Down
2 changes: 1 addition & 1 deletion lib/liquid/tags/raw.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Liquid
# expression
# {% endraw %}
# @liquid_syntax_keyword expression The expression to be output without being rendered.
class Raw < Block
class Raw < NonParsingBlock
Syntax = /\A\s*\z/
FullTokenPossiblyInvalid = /\A(.*)#{TagStart}#{WhitespaceControl}?\s*(\w+)\s*(.*)?#{WhitespaceControl}?#{TagEnd}\z/om

Expand Down
31 changes: 31 additions & 0 deletions test/integration/non_parsing_block_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

require 'test_helper'

class NonParsingBlockTest < Minitest::Test
include Liquid

def test_comment_tag_does_not_parse_nodes_inside_a_comment
template = Template.parse(<<~LIQUID.chomp, line_numbers: true)
{% comment %}
{% if true %}
{% while true %}
{% endcomment %}
LIQUID
end

def test_error_line_number_is_correct
template = Template.parse(<<~LIQUID.chomp, line_numbers: true)
{% raw %}{% endraw %}
{{ errors.standard_error }}
LIQUID

output = template.render('errors' => ErrorDrop.new)
expected = <<~TEXT.chomp
Liquid error (line 2): standard error
TEXT

assert_equal(expected, output)
end
end

0 comments on commit 760961a

Please sign in to comment.