Skip to content

Commit

Permalink
Merge pull request #1680 from Shopify/recursively-parse-brackets
Browse files Browse the repository at this point in the history
recursively parse brackets on variable lookup
  • Loading branch information
ggmichaelgo committed Jan 31, 2023
2 parents daf93a8 + dd257b3 commit 6a888d4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/liquid.rb
Expand Up @@ -41,7 +41,7 @@ module Liquid
AnyStartingTag = /#{TagStart}|#{VariableStart}/o
PartialTemplateParser = /#{TagStart}.*?#{TagEnd}|#{VariableStart}.*?#{VariableIncompleteEnd}/om
TemplateParser = /(#{PartialTemplateParser}|#{AnyStartingTag})/om
VariableParser = /\[[^\]]+\]|#{VariableSegment}+\??/o
VariableParser = /\[(?:[^\[\]]+|\g<0>)*\]|#{VariableSegment}+\??/o

RAISE_EXCEPTION_LAMBDA = ->(_e) { raise }

Expand Down
34 changes: 34 additions & 0 deletions test/integration/variable_test.rb
Expand Up @@ -135,4 +135,38 @@ def test_dynamic_find_var
def test_raw_value_variable
assert_template_result('bar', '{{ [key] }}', { 'key' => 'foo', 'foo' => 'bar' })
end

def test_dynamic_find_var_with_drop
assert_template_result(
'bar',
'{{ [list[settings.zero]] }}',
{
'list' => ['foo'],
'settings' => SettingsDrop.new("zero" => 0),
'foo' => 'bar',
}
)

assert_template_result(
'foo',
'{{ [list[settings.zero]["foo"]] }}',
{
'list' => [{ 'foo' => 'bar' }],
'settings' => SettingsDrop.new("zero" => 0),
'bar' => 'foo',
}
)
end

def test_double_nested_variable_lookup
assert_template_result(
'bar',
'{{ list[list[settings.zero]]["foo"] }}',
{
'list' => [1, { 'foo' => 'bar' }],
'settings' => SettingsDrop.new("zero" => 0),
'bar' => 'foo',
}
)
end
end
11 changes: 11 additions & 0 deletions test/test_helper.rb
Expand Up @@ -125,6 +125,17 @@ def to_liquid
end
end

class SettingsDrop < Liquid::Drop
def initialize(settings)
super()
@settings = settings
end

def liquid_method_missing(key)
@settings[key]
end
end

class IntegerDrop < Liquid::Drop
def initialize(value)
super()
Expand Down

0 comments on commit 6a888d4

Please sign in to comment.