From ed82805c98f2a6746e4d3aa0d809355ee26fd42e Mon Sep 17 00:00:00 2001 From: Matt Rose Date: Thu, 29 Sep 2022 14:21:24 -0400 Subject: [PATCH] Update context tags to act as a merge rather than a complete overwrite --- lib/liquid/parse_context.rb | 16 +++++++++++++--- test/integration/tag_test.rb | 26 ++++++++++++++++---------- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/lib/liquid/parse_context.rb b/lib/liquid/parse_context.rb index efb1aa97..a184fa0d 100644 --- a/lib/liquid/parse_context.rb +++ b/lib/liquid/parse_context.rb @@ -2,15 +2,25 @@ module Liquid class ParseContext - attr_accessor :locale, :line_number, :trim_whitespace, :depth, :tags - attr_reader :partial, :warnings, :error_mode + attr_accessor :locale, :line_number, :trim_whitespace, :depth + attr_reader :partial, :warnings, :error_mode, :tags + + class Tags + def initialize(tags) + @tags = tags || {} + end + + def [](tag_name) + @tags[tag_name] || Liquid::Template.tags[tag_name] + end + end def initialize(options = {}) @template_options = options ? options.dup : {} @locale = @template_options[:locale] ||= I18n.new @warnings = [] - @tags = @template_options[:tags] || Liquid::Template.tags + @tags = Tags.new(@template_options[:tags]) self.depth = 0 self.partial = false diff --git a/test/integration/tag_test.rb b/test/integration/tag_test.rb index 4391fb02..73189608 100644 --- a/test/integration/tag_test.rb +++ b/test/integration/tag_test.rb @@ -44,31 +44,37 @@ class TagTest < Minitest::Test end def test_tags_can_be_overwritten_using_parse_context - tag_name = 'testtag' + static_tag = Class.new(Tag) do + def render(*) + 'static_tag' + end + end - original_tag = Class.new(Block) do + original_tag = Class.new(Tag) do def render(*) 'original_tag' end end - new_tag = Class.new(Block) do + new_tag = Class.new(Tag) do def render(*) 'new_tag' end end tags_overwrite = Liquid::Template::TagRegistry.new - tags_overwrite[tag_name] = new_tag + tags_overwrite['dynamic_tag'] = new_tag - with_custom_tag(tag_name, original_tag) do - liquid = "{% #{tag_name} %} {% end#{tag_name} %}" + with_custom_tag('static_tag', static_tag) do + with_custom_tag('dynamic_tag', original_tag) do + liquid = '{% static_tag %} {% dynamic_tag %}' - template = Liquid::Template.parse(liquid) - assert_equal('original_tag', template.render) + template = Liquid::Template.parse(liquid) + assert_equal('static_tag original_tag', template.render) - template_with_overwrite = Liquid::Template.parse(liquid, tags: tags_overwrite) - assert_equal('new_tag', template_with_overwrite.render) + template_with_overwrite = Liquid::Template.parse(liquid, tags: tags_overwrite) + assert_equal('static_tag new_tag', template_with_overwrite.render) + end end end end