From 1aaf6ed0198ba18e1d714f521581a9c921c68635 Mon Sep 17 00:00:00 2001 From: Michael Go Date: Mon, 30 Jan 2023 18:32:23 -0400 Subject: [PATCH] recursively parse brackets on variable lookup --- lib/liquid.rb | 2 +- test/integration/variable_test.rb | 22 ++++++++++++++++++++++ test/test_helper.rb | 11 +++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/lib/liquid.rb b/lib/liquid.rb index 5eda04e7..09b0dd80 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 c007f1f5..192a3fe9 100644 --- a/test/integration/variable_test.rb +++ b/test/integration/variable_test.rb @@ -135,4 +135,26 @@ class VariableTest < Minitest::Test 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 end diff --git a/test/test_helper.rb b/test/test_helper.rb index 49b1cc76..c1514bb3 100755 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -125,6 +125,17 @@ class ThingWithToLiquid 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()