From 6bdf88a32c8d732c6f5a1f2b0dd0576c172591bc Mon Sep 17 00:00:00 2001 From: Matt Rose Date: Thu, 29 Sep 2022 00:32:43 -0400 Subject: [PATCH] Add ability to pass tags to parse_context --- History.md | 3 +++ lib/liquid/block_body.rb | 9 +++++---- lib/liquid/parse_context.rb | 3 ++- test/integration/tag_test.rb | 31 +++++++++++++++++++++++++++++++ 4 files changed, 41 insertions(+), 5 deletions(-) diff --git a/History.md b/History.md index 65e29243..536eed02 100644 --- a/History.md +++ b/History.md @@ -1,5 +1,8 @@ # Liquid Change Log +### Features +* Add ability to pass `tags` to parse context to take precedence over `Liquid::Template.tags` + ## 5.4.0 2022-07-29 ### Breaking Changes diff --git a/lib/liquid/block_body.rb b/lib/liquid/block_body.rb index a0d35a79..6022324c 100644 --- a/lib/liquid/block_body.rb +++ b/lib/liquid/block_body.rb @@ -45,7 +45,7 @@ module Liquid end tag_name = Regexp.last_match(1) markup = Regexp.last_match(2) - unless (tag = registered_tags[tag_name]) + unless (tag = registered_tag(tag_name, parse_context)) # end parsing if we reach an unknown tag and let the caller decide # determine how to proceed return yield tag_name, markup @@ -132,7 +132,7 @@ module Liquid next end - unless (tag = registered_tags[tag_name]) + unless (tag = registered_tag(tag_name, parse_context)) # end parsing if we reach an unknown tag and let the caller decide # determine how to proceed return yield tag_name, markup @@ -248,8 +248,9 @@ module Liquid BlockBody.raise_missing_variable_terminator(token, parse_context) end - def registered_tags - Template.tags + def registered_tag(tag_name, parse_context) + context_tag = parse_context.tags[tag_name] if parse_context.tags.respond_to?(:[]) + context_tag || Template.tags[tag_name] end end end diff --git a/lib/liquid/parse_context.rb b/lib/liquid/parse_context.rb index 87570ad5..afd4ea53 100644 --- a/lib/liquid/parse_context.rb +++ b/lib/liquid/parse_context.rb @@ -2,7 +2,7 @@ module Liquid class ParseContext - attr_accessor :locale, :line_number, :trim_whitespace, :depth + attr_accessor :locale, :line_number, :trim_whitespace, :depth, :tags attr_reader :partial, :warnings, :error_mode def initialize(options = {}) @@ -10,6 +10,7 @@ module Liquid @locale = @template_options[:locale] ||= I18n.new @warnings = [] + @tags = @template_options[:tags] self.depth = 0 self.partial = false diff --git a/test/integration/tag_test.rb b/test/integration/tag_test.rb index 6e28eb52..71d851f2 100644 --- a/test/integration/tag_test.rb +++ b/test/integration/tag_test.rb @@ -42,4 +42,35 @@ class TagTest < Minitest::Test assert_equal(buf.object_id, output.object_id) end end + + def test_tags_can_be_overwritten_using_parse_context + tag_name = 'testtag' + + original_tag = Class.new(Block) do + def render(*) + 'original_tag' + end + end + + new_tag = Class.new(Block) do + def render(*) + 'new_tag' + end + end + + tags_overwrite = Liquid::Template::TagRegistry.new + tags_overwrite[tag_name] = new_tag + + with_custom_tag(tag_name, original_tag) do + liquid = "{% #{tag_name} %} {% end#{tag_name} %}" + + template = Liquid::Template.parse(liquid) + assert_equal('original_tag', template.render) + + unless ENV['LIQUID_C'] == '1' + template_with_overwrite = Liquid::Template.parse(liquid, tags: tags_overwrite) + assert_equal('new_tag', template_with_overwrite.render) + end + end + end end