Update context tags to act as a merge rather than a complete overwrite

This commit is contained in:
Matt Rose
2022-09-29 14:22:36 -04:00
parent 3d2aa05d64
commit ed82805c98
2 changed files with 29 additions and 13 deletions
+13 -3
View File
@@ -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
+16 -10
View File
@@ -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