diff --git a/lib/liquid.rb b/lib/liquid.rb index 5eda04e74..09b0dd808 100644 --- a/lib/liquid.rb +++ b/lib/liquid.rb @@ -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 } diff --git a/test/integration/variable_test.rb b/test/integration/variable_test.rb index c007f1f5e..0cb8bddc1 100644 --- a/test/integration/variable_test.rb +++ b/test/integration/variable_test.rb @@ -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 diff --git a/test/test_helper.rb b/test/test_helper.rb index 49b1cc760..c1514bb35 100755 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -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()