mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-12 23:40:45 -07:00
Implement resource limits and remove leftover string references
This commit is contained in:
committed by
Guilherme Carreiro
parent
65fb80a347
commit
0ceeefba02
@@ -40,7 +40,6 @@ module Liquid
|
||||
QuotedString = /"[^"]*"|'[^']*'/
|
||||
QuotedFragment = /#{QuotedString}|(?:[^\s,\|'"]|#{QuotedString})+/o
|
||||
TagAttributes = /(\w[\w-]*)\s*\:\s*(#{QuotedFragment})/o
|
||||
ContextInheritance = /\.\.\./
|
||||
AnyStartingTag = /#{TagStart}|#{VariableStart}/o
|
||||
PartialTemplateParser = /#{TagStart}.*?#{TagEnd}|#{VariableStart}.*?#{VariableIncompleteEnd}/om
|
||||
TemplateParser = /(#{PartialTemplateParser}|#{AnyStartingTag})/om
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
block_tag_unexpected_args: "Syntax Error in '%{tag}' - Valid syntax: {% %{tag} %}{% end%{tag} %}"
|
||||
assign: "Syntax Error in 'assign' - Valid syntax: assign [var] = [source]"
|
||||
capture: "Syntax Error in 'capture' - Valid syntax: capture [var]"
|
||||
snippet: "Syntax Error in 'snippet' - Valid syntax: snippet [quoted string]"
|
||||
snippet: "Syntax Error in 'snippet' - Valid syntax: snippet [var]"
|
||||
case: "Syntax Error in 'case' - Valid syntax: case [condition]"
|
||||
case_invalid_when: "Syntax Error in tag 'case' - Valid when condition: {% when [condition] [or condition2...] %}"
|
||||
case_invalid_else: "Syntax Error in tag 'case' - Valid else condition: {% else %} (no parameters) "
|
||||
|
||||
@@ -128,12 +128,9 @@ module Liquid
|
||||
end
|
||||
|
||||
def rigid_template_name(p)
|
||||
if p.look(:string)
|
||||
p.consume(:string)
|
||||
# inline snippets use variable identifiers
|
||||
elsif p.look(:id)
|
||||
p.consume(:id)
|
||||
end
|
||||
return p.consume(:string) if p.look(:string)
|
||||
|
||||
p.consume(:id) if p.look(:id)
|
||||
end
|
||||
|
||||
def strict_parse(markup)
|
||||
@@ -153,7 +150,7 @@ module Liquid
|
||||
@is_for_loop = (with_or_for == FOR)
|
||||
|
||||
@attributes = {}
|
||||
markup.scan(/(#{ContextInheritance})|#{TagAttributes.source}/) do |context_marker, key, value|
|
||||
markup.scan(/(\.\.\.)(?=\s|,|$)|#{TagAttributes.source}/) do |context_marker, key, value|
|
||||
if context_marker
|
||||
@attributes.delete("...")
|
||||
@attributes["..."] = true
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
module Liquid
|
||||
# @liquid_type tag
|
||||
# @liquid_category theme
|
||||
# @liquid_category variable
|
||||
# @liquid_name snippet
|
||||
# @liquid_summary
|
||||
# Creates a new inline snippet.
|
||||
@@ -14,8 +14,6 @@ module Liquid
|
||||
# {% endsnippet %}
|
||||
|
||||
class Snippet < Block
|
||||
SYNTAX = /(#{VariableSignature}+)/o
|
||||
|
||||
def initialize(tag_name, markup, options)
|
||||
super
|
||||
p = @parse_context.new_parser(markup)
|
||||
@@ -29,6 +27,10 @@ module Liquid
|
||||
def render_to_output_buffer(context, output)
|
||||
snippet_drop = SnippetDrop.new(@body)
|
||||
context.scopes.last[@to] = snippet_drop
|
||||
|
||||
snippet_size = @body.nodelist.sum { |node| node.to_s.bytesize }
|
||||
context.resource_limits.increment_assign_score(snippet_size)
|
||||
|
||||
output
|
||||
end
|
||||
|
||||
|
||||
@@ -1238,4 +1238,12 @@ class SnippetTest < Minitest::Test
|
||||
assert_template_result(expected, template, error_mode: :rigid)
|
||||
end
|
||||
end
|
||||
|
||||
class ResourceLimits < SnippetTest
|
||||
def test_increment_assign_score_by_bytes_not_characters
|
||||
t = Template.parse("{% snippet foo %}すごい{% endsnippet %}")
|
||||
t.render!
|
||||
assert_equal(9, t.resource_limits.assign_score)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user