Compare commits

...
Author SHA1 Message Date
Julia Boutin f78c443ccd Update resource limit handling
This commit aligns snippet tag's resource
limit handling with assign tag's `assign_score_of`
method where the snippet variable acts as a pointer
to a block body - increasing assign score by one
2025-11-04 14:53:08 -07:00
Julia Boutin e753b9025d Ensure render tag checks for most common type first
Previously, render tag would check if a template
responded to `:to_partial` before checking if its
name was a string. As `is_a?(String)` instances are
more common that `to_partial`, this commit reorders
the conditional to follow the common path
2025-11-04 13:11:18 -07:00
3 changed files with 7 additions and 13 deletions
+5 -5
View File
@@ -49,14 +49,14 @@ module Liquid
def render_tag(context, output)
template = context.evaluate(@template_name_expr)
if template.respond_to?(:to_partial)
partial = template.to_partial
template_name = template.filename
context_variable_name = @alias_name || template.name
elsif @template_name_expr.is_a?(String)
if @template_name_expr.is_a?(String)
partial = PartialCache.load(template, context: context, parse_context: parse_context)
template_name = partial.name
context_variable_name = @alias_name || template_name.split('/').last
elsif template.respond_to?(:to_partial) && template.respond_to?(:name)
partial = template.to_partial
template_name = template.filename
context_variable_name = @alias_name || template.name
else
raise ::ArgumentError
end
+1 -7
View File
@@ -28,18 +28,12 @@ module Liquid
def render_to_output_buffer(context, output)
snippet_drop = SnippetDrop.new(@body, @to, context.template_name)
context.scopes.last[@to] = snippet_drop
context.resource_limits.increment_assign_score(assign_score_of(snippet_drop))
context.resource_limits.increment_assign_score(1)
output
end
def blank?
true
end
private
def assign_score_of(snippet_drop)
snippet_drop.body.nodelist.sum { |node| node.to_s.bytesize }
end
end
end
+1 -1
View File
@@ -1081,7 +1081,7 @@ class SnippetTest < Minitest::Test
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)
assert_equal(1, t.resource_limits.assign_score)
end
end
end