mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-15 00:40:40 -07:00
Compare commits
15
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6b3f6c6fb4 | ||
|
|
ea864f1177 | ||
|
|
c34dd812c5 | ||
|
|
cc04892e54 | ||
|
|
f676e699a4 | ||
|
|
fc0f7f44c1 | ||
|
|
b459eb656f | ||
|
|
c6dcf3e714 | ||
|
|
4dae678c63 | ||
|
|
4c07ff920b | ||
|
|
dbe709c3bf | ||
|
|
2b75bfaff4 | ||
|
|
87bc6e7cfa | ||
|
|
7f122aeed2 | ||
|
|
aa1640035f |
@@ -0,0 +1,6 @@
|
|||||||
|
version: 2
|
||||||
|
updates:
|
||||||
|
- package-ecosystem: github-actions
|
||||||
|
directory: "/"
|
||||||
|
schedule:
|
||||||
|
interval: weekly
|
||||||
@@ -31,8 +31,8 @@ jobs:
|
|||||||
- { ruby: ruby-head, allowed-failure: false, rubyopt: "--yjit" }
|
- { ruby: ruby-head, allowed-failure: false, rubyopt: "--yjit" }
|
||||||
name: Test Ruby ${{ matrix.entry.ruby }}
|
name: Test Ruby ${{ matrix.entry.ruby }}
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0
|
||||||
- uses: ruby/setup-ruby@v1
|
- uses: ruby/setup-ruby@dffc446db9ba5a0c4446edb5bca1c5c473a806c5 # v1.235.0
|
||||||
with:
|
with:
|
||||||
ruby-version: ${{ matrix.entry.ruby }}
|
ruby-version: ${{ matrix.entry.ruby }}
|
||||||
bundler-cache: true
|
bundler-cache: true
|
||||||
@@ -45,8 +45,8 @@ jobs:
|
|||||||
memory_profile:
|
memory_profile:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0
|
||||||
- uses: ruby/setup-ruby@v1
|
- uses: ruby/setup-ruby@dffc446db9ba5a0c4446edb5bca1c5c473a806c5 # v1.235.0
|
||||||
with:
|
with:
|
||||||
bundler-cache: true
|
bundler-cache: true
|
||||||
- run: bundle exec rake memory_profile:run
|
- run: bundle exec rake memory_profile:run
|
||||||
|
|||||||
@@ -3,7 +3,6 @@
|
|||||||
require 'cgi'
|
require 'cgi'
|
||||||
require 'base64'
|
require 'base64'
|
||||||
require 'bigdecimal'
|
require 'bigdecimal'
|
||||||
|
|
||||||
module Liquid
|
module Liquid
|
||||||
module StandardFilters
|
module StandardFilters
|
||||||
MAX_I32 = (1 << 31) - 1
|
MAX_I32 = (1 << 31) - 1
|
||||||
@@ -952,13 +951,11 @@ module Liquid
|
|||||||
# @liquid_syntax array | sum
|
# @liquid_syntax array | sum
|
||||||
# @liquid_return [number]
|
# @liquid_return [number]
|
||||||
def sum(input, property = nil)
|
def sum(input, property = nil)
|
||||||
property = Liquid::Utils.to_s(property)
|
|
||||||
|
|
||||||
ary = InputIterator.new(input, context)
|
ary = InputIterator.new(input, context)
|
||||||
return 0 if ary.empty?
|
return 0 if ary.empty?
|
||||||
|
|
||||||
values_for_sum = ary.map do |item|
|
values_for_sum = ary.map do |item|
|
||||||
if property.empty?
|
if property.nil?
|
||||||
item
|
item
|
||||||
elsif item.respond_to?(:[])
|
elsif item.respond_to?(:[])
|
||||||
item[property]
|
item[property]
|
||||||
@@ -981,10 +978,8 @@ module Liquid
|
|||||||
attr_reader :context
|
attr_reader :context
|
||||||
|
|
||||||
def filter_array(input, property, target_value, default_value = [], &block)
|
def filter_array(input, property, target_value, default_value = [], &block)
|
||||||
property = Liquid::Utils.to_s(property)
|
|
||||||
return default_value if property.empty?
|
|
||||||
|
|
||||||
ary = InputIterator.new(input, context)
|
ary = InputIterator.new(input, context)
|
||||||
|
|
||||||
return default_value if ary.empty?
|
return default_value if ary.empty?
|
||||||
|
|
||||||
block.call(ary) do |item|
|
block.call(ary) do |item|
|
||||||
@@ -1002,7 +997,7 @@ module Liquid
|
|||||||
end
|
end
|
||||||
|
|
||||||
def raise_property_error(property)
|
def raise_property_error(property)
|
||||||
raise Liquid::ArgumentError, "cannot select the property '#{property}'"
|
raise Liquid::ArgumentError, "cannot select the property '#{Utils.to_s(property)}'"
|
||||||
end
|
end
|
||||||
|
|
||||||
def apply_operation(input, operand, operation)
|
def apply_operation(input, operand, operation)
|
||||||
@@ -1074,7 +1069,10 @@ module Liquid
|
|||||||
end
|
end
|
||||||
|
|
||||||
def uniq(&block)
|
def uniq(&block)
|
||||||
to_a.uniq(&block)
|
to_a.uniq do |item|
|
||||||
|
item = Utils.to_liquid_value(item)
|
||||||
|
block ? yield(item) : item
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def compact
|
def compact
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ module Liquid
|
|||||||
# @liquid_description
|
# @liquid_description
|
||||||
# Variables that are declared with `decrement` are unique to the [layout](/themes/architecture/layouts), [template](/themes/architecture/templates),
|
# Variables that are declared with `decrement` are unique to the [layout](/themes/architecture/layouts), [template](/themes/architecture/templates),
|
||||||
# or [section](/themes/architecture/sections) file that they're created in. However, the variable is shared across
|
# or [section](/themes/architecture/sections) file that they're created in. However, the variable is shared across
|
||||||
# [snippets](/themes/architecture#snippets) included in the file.
|
# [snippets](/themes/architecture/snippets) included in the file.
|
||||||
#
|
#
|
||||||
# Similarly, variables that are created with `decrement` are independent from those created with [`assign`](/docs/api/liquid/tags/assign)
|
# Similarly, variables that are created with `decrement` are independent from those created with [`assign`](/docs/api/liquid/tags/assign)
|
||||||
# and [`capture`](/docs/api/liquid/tags/capture). However, `decrement` and [`increment`](/docs/api/liquid/tags/increment) share
|
# and [`capture`](/docs/api/liquid/tags/capture). However, `decrement` and [`increment`](/docs/api/liquid/tags/increment) share
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ module Liquid
|
|||||||
# @liquid_category theme
|
# @liquid_category theme
|
||||||
# @liquid_name include
|
# @liquid_name include
|
||||||
# @liquid_summary
|
# @liquid_summary
|
||||||
# Renders a [snippet](/themes/architecture#snippets).
|
# Renders a [snippet](/themes/architecture/snippets).
|
||||||
# @liquid_description
|
# @liquid_description
|
||||||
# Inside the snippet, you can access and alter variables that are [created](/docs/api/liquid/tags/variable-tags) outside of the
|
# Inside the snippet, you can access and alter variables that are [created](/docs/api/liquid/tags/variable-tags) outside of the
|
||||||
# snippet.
|
# snippet.
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ module Liquid
|
|||||||
# @liquid_description
|
# @liquid_description
|
||||||
# Variables that are declared with `increment` are unique to the [layout](/themes/architecture/layouts), [template](/themes/architecture/templates),
|
# Variables that are declared with `increment` are unique to the [layout](/themes/architecture/layouts), [template](/themes/architecture/templates),
|
||||||
# or [section](/themes/architecture/sections) file that they're created in. However, the variable is shared across
|
# or [section](/themes/architecture/sections) file that they're created in. However, the variable is shared across
|
||||||
# [snippets](/themes/architecture#snippets) included in the file.
|
# [snippets](/themes/architecture/snippets) included in the file.
|
||||||
#
|
#
|
||||||
# Similarly, variables that are created with `increment` are independent from those created with [`assign`](/docs/api/liquid/tags/assign)
|
# Similarly, variables that are created with `increment` are independent from those created with [`assign`](/docs/api/liquid/tags/assign)
|
||||||
# and [`capture`](/docs/api/liquid/tags/capture). However, `increment` and [`decrement`](/docs/api/liquid/tags/decrement) share
|
# and [`capture`](/docs/api/liquid/tags/capture). However, `increment` and [`decrement`](/docs/api/liquid/tags/decrement) share
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ module Liquid
|
|||||||
# @liquid_category theme
|
# @liquid_category theme
|
||||||
# @liquid_name render
|
# @liquid_name render
|
||||||
# @liquid_summary
|
# @liquid_summary
|
||||||
# Renders a [snippet](/themes/architecture#snippets) or [app block](/themes/architecture/sections/section-schema#render-app-blocks).
|
# Renders a [snippet](/themes/architecture/snippets) or [app block](/themes/architecture/sections/section-schema#render-app-blocks).
|
||||||
# @liquid_description
|
# @liquid_description
|
||||||
# Inside snippets and app blocks, you can't directly access variables that are [created](/docs/api/liquid/tags/variable-tags) outside
|
# Inside snippets and app blocks, you can't directly access variables that are [created](/docs/api/liquid/tags/variable-tags) outside
|
||||||
# of the snippet or app block. However, you can [specify variables as parameters](/docs/api/liquid/tags/render#render-passing-variables-to-a-snippet)
|
# of the snippet or app block. However, you can [specify variables as parameters](/docs/api/liquid/tags/render#render-passing-variables-to-a-snippet)
|
||||||
|
|||||||
@@ -2,5 +2,5 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
module Liquid
|
module Liquid
|
||||||
VERSION = "5.8.1"
|
VERSION = "5.8.6"
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -560,12 +560,23 @@ class StandardFiltersTest < Minitest::Test
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_map_returns_empty_with_no_property
|
def test_map_with_value_property
|
||||||
|
array = [
|
||||||
|
{ "handle" => "alpha", "value" => "A" },
|
||||||
|
{ "handle" => "beta", "value" => "B" },
|
||||||
|
{ "handle" => "gamma", "value" => "C" }
|
||||||
|
]
|
||||||
|
|
||||||
|
assert_template_result("A B C", "{{ array | map: 'value' | join: ' ' }}", { "array" => array })
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_map_returns_input_with_no_property
|
||||||
foo = [
|
foo = [
|
||||||
[1],
|
[1],
|
||||||
[2],
|
[2],
|
||||||
[3],
|
[3],
|
||||||
]
|
]
|
||||||
|
|
||||||
assert_raises(Liquid::ArgumentError) do
|
assert_raises(Liquid::ArgumentError) do
|
||||||
@filters.map(foo, nil)
|
@filters.map(foo, nil)
|
||||||
end
|
end
|
||||||
@@ -1033,6 +1044,23 @@ class StandardFiltersTest < Minitest::Test
|
|||||||
assert_template_result(expected_output, template, { "array" => array })
|
assert_template_result(expected_output, template, { "array" => array })
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_where_with_empty_string_is_a_no_op
|
||||||
|
environment = { "array" => ["alpha", "beta", "gamma"] }
|
||||||
|
expected_output = "alpha beta gamma"
|
||||||
|
template = "{{ array | where: '' | join: ' ' }}"
|
||||||
|
|
||||||
|
assert_template_result(expected_output, template, environment)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_where_with_nil_is_a_no_op
|
||||||
|
environment = { "array" => ["alpha", "beta", "gamma"] }
|
||||||
|
template = "{{ array | where: nil | join: ' ' }}"
|
||||||
|
|
||||||
|
assert_raises(Liquid::ArgumentError) do
|
||||||
|
assert_template_result("alpha beta gamma", template, environment)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def test_where_with_value
|
def test_where_with_value
|
||||||
array = [
|
array = [
|
||||||
{ "handle" => "alpha", "ok" => true },
|
{ "handle" => "alpha", "ok" => true },
|
||||||
@@ -1061,19 +1089,6 @@ class StandardFiltersTest < Minitest::Test
|
|||||||
assert_template_result(expected_output, template, { "array" => array })
|
assert_template_result(expected_output, template, { "array" => array })
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_where_with_non_string_property
|
|
||||||
array = [
|
|
||||||
{ "handle" => "alpha", "{}" => true },
|
|
||||||
{ "handle" => "beta", "{}" => false },
|
|
||||||
{ "handle" => "gamma", "{}" => false },
|
|
||||||
{ "handle" => "delta", "{}" => true },
|
|
||||||
]
|
|
||||||
template = "{{ array | where: some_property, true | map: 'handle' | join: ' ' }}"
|
|
||||||
expected_output = "alpha delta"
|
|
||||||
|
|
||||||
assert_template_result(expected_output, template, { "array" => array, "some_property" => {} })
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_where_string_keys
|
def test_where_string_keys
|
||||||
input = [
|
input = [
|
||||||
"alpha", "beta", "gamma", "delta"
|
"alpha", "beta", "gamma", "delta"
|
||||||
@@ -1282,7 +1297,7 @@ class StandardFiltersTest < Minitest::Test
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_sum_with_non_string_property
|
def test_sum_with_non_string_property
|
||||||
input = [{ "true" => 1 }, { "1.0" => 0.2, "1" => -0.3 }, { "1..5" => 0.4 }]
|
input = [{ true => 1 }, { 1.0 => 0.2, 1 => -0.3 }, { 1..5 => 0.4 }]
|
||||||
|
|
||||||
assert_equal(1, @filters.sum(input, true))
|
assert_equal(1, @filters.sum(input, true))
|
||||||
assert_equal(0.2, @filters.sum(input, 1.0))
|
assert_equal(0.2, @filters.sum(input, 1.0))
|
||||||
@@ -1292,6 +1307,22 @@ class StandardFiltersTest < Minitest::Test
|
|||||||
assert_equal(0, @filters.sum(input, ""))
|
assert_equal(0, @filters.sum(input, ""))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_uniq_with_to_liquid_value
|
||||||
|
input = [StringDrop.new("foo"), StringDrop.new("bar"), "foo"]
|
||||||
|
expected = [StringDrop.new("foo"), StringDrop.new("bar")]
|
||||||
|
result = @filters.uniq(input)
|
||||||
|
|
||||||
|
assert_equal(expected, result)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_uniq_with_to_liquid_value_pick_correct_classes
|
||||||
|
input = ["foo", StringDrop.new("foo"), StringDrop.new("bar")]
|
||||||
|
expected = [String, StringDrop]
|
||||||
|
result = @filters.uniq(input).map(&:class)
|
||||||
|
|
||||||
|
assert_equal(expected, result)
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def with_timezone(tz)
|
def with_timezone(tz)
|
||||||
|
|||||||
@@ -146,6 +146,35 @@ class BooleanDrop < Liquid::Drop
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class StringDrop < Liquid::Drop
|
||||||
|
include Comparable
|
||||||
|
|
||||||
|
def initialize(value)
|
||||||
|
super()
|
||||||
|
@value = value
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_liquid_value
|
||||||
|
@value
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_s
|
||||||
|
@value
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_str
|
||||||
|
@value
|
||||||
|
end
|
||||||
|
|
||||||
|
def inspect
|
||||||
|
"#<StringDrop @value=#{@value.inspect}>"
|
||||||
|
end
|
||||||
|
|
||||||
|
def <=>(other)
|
||||||
|
to_liquid_value <=> Liquid::Utils.to_liquid_value(other)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class ErrorDrop < Liquid::Drop
|
class ErrorDrop < Liquid::Drop
|
||||||
def standard_error
|
def standard_error
|
||||||
raise Liquid::StandardError, 'standard error'
|
raise Liquid::StandardError, 'standard error'
|
||||||
|
|||||||
Reference in New Issue
Block a user