mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-20 03:10:39 -07:00
Merge pull request #1093 from Shopify/bytesize-not-length
use bytesize, not length
This commit is contained in:
@@ -115,7 +115,7 @@ module Liquid
|
|||||||
end
|
end
|
||||||
|
|
||||||
def check_resources(context, node_output)
|
def check_resources(context, node_output)
|
||||||
context.resource_limits.render_length += node_output.length
|
context.resource_limits.render_length += node_output.bytesize
|
||||||
return unless context.resource_limits.reached?
|
return unless context.resource_limits.reached?
|
||||||
raise MemoryError.new("Memory limits exceeded".freeze)
|
raise MemoryError.new("Memory limits exceeded".freeze)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ module Liquid
|
|||||||
|
|
||||||
def assign_score_of(val)
|
def assign_score_of(val)
|
||||||
if val.instance_of?(String)
|
if val.instance_of?(String)
|
||||||
val.length
|
val.bytesize
|
||||||
elsif val.instance_of?(Array) || val.instance_of?(Hash)
|
elsif val.instance_of?(Array) || val.instance_of?(Hash)
|
||||||
sum = 1
|
sum = 1
|
||||||
# Uses #each to avoid extra allocations.
|
# Uses #each to avoid extra allocations.
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ module Liquid
|
|||||||
def render(context)
|
def render(context)
|
||||||
output = super
|
output = super
|
||||||
context.scopes.last[@to] = output
|
context.scopes.last[@to] = output
|
||||||
context.resource_limits.assign_score += output.length
|
context.resource_limits.assign_score += output.bytesize
|
||||||
''.freeze
|
''.freeze
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -139,6 +139,16 @@ class TemplateTest < Minitest::Test
|
|||||||
refute_nil t.resource_limits.assign_score
|
refute_nil t.resource_limits.assign_score
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_resource_limits_assign_score_counts_bytes_not_characters
|
||||||
|
t = Template.parse("{% assign foo = 'すごい' %}")
|
||||||
|
t.render
|
||||||
|
assert_equal 9, t.resource_limits.assign_score
|
||||||
|
|
||||||
|
t = Template.parse("{% capture foo %}すごい{% endcapture %}")
|
||||||
|
t.render
|
||||||
|
assert_equal 9, t.resource_limits.assign_score
|
||||||
|
end
|
||||||
|
|
||||||
def test_resource_limits_assign_score_nested
|
def test_resource_limits_assign_score_nested
|
||||||
t = Template.parse("{% assign foo = 'aaaa' | reverse %}")
|
t = Template.parse("{% assign foo = 'aaaa' | reverse %}")
|
||||||
|
|
||||||
@@ -187,6 +197,14 @@ class TemplateTest < Minitest::Test
|
|||||||
assert_equal "ababab", t.render
|
assert_equal "ababab", t.render
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_render_length_uses_number_of_bytes_not_characters
|
||||||
|
t = Template.parse("{% if true %}すごい{% endif %}")
|
||||||
|
t.resource_limits.render_length_limit = 10
|
||||||
|
assert_equal "Liquid error: Memory limits exceeded", t.render
|
||||||
|
t.resource_limits.render_length_limit = 18
|
||||||
|
assert_equal "すごい", t.render
|
||||||
|
end
|
||||||
|
|
||||||
def test_default_resource_limits_unaffected_by_render_with_context
|
def test_default_resource_limits_unaffected_by_render_with_context
|
||||||
context = Context.new
|
context = Context.new
|
||||||
t = Template.parse("{% for a in (1..100) %} {% assign foo = 1 %} {% endfor %}")
|
t = Template.parse("{% for a in (1..100) %} {% assign foo = 1 %} {% endfor %}")
|
||||||
|
|||||||
Reference in New Issue
Block a user