Compare commits

...
Author SHA1 Message Date
Matt Rose eb37f21f1b Use hash for mocking tags in tests 2022-09-30 15:05:37 -04:00
Matt Rose ed82805c98 Update context tags to act as a merge rather than a complete overwrite 2022-09-29 14:22:36 -04:00
Matt RoseandDylan Thacker-Smith 3d2aa05d64 Add ability to pass tags to parse_context
Co-authored-by: Dylan Thacker-Smith <[email protected]>
2022-09-29 11:35:46 -04:00
5 changed files with 52 additions and 8 deletions
+1 -1
View File
@@ -22,6 +22,6 @@ group :test do
gem 'rubocop-performance', require: false
platform :mri, :truffleruby do
gem 'liquid-c', github: 'Shopify/liquid-c', ref: 'master'
gem 'liquid-c', github: 'Shopify/liquid-c', ref: 'parse-specific-tags'
end
end
+3
View File
@@ -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
+2 -6
View File
@@ -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 = parse_context.tags[tag_name])
# 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 = parse_context.tags[tag_name])
# end parsing if we reach an unknown tag and let the caller decide
# determine how to proceed
return yield tag_name, markup
@@ -247,9 +247,5 @@ module Liquid
def raise_missing_variable_terminator(token, parse_context)
BlockBody.raise_missing_variable_terminator(token, parse_context)
end
def registered_tags
Template.tags
end
end
end
+12 -1
View File
@@ -3,13 +3,24 @@
module Liquid
class ParseContext
attr_accessor :locale, :line_number, :trim_whitespace, :depth
attr_reader :partial, :warnings, :error_mode
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 = Tags.new(@template_options[:tags])
self.depth = 0
self.partial = false
+34
View File
@@ -42,4 +42,38 @@ class TagTest < Minitest::Test
assert_equal(buf.object_id, output.object_id)
end
end
def test_tags_can_be_overwritten_using_parse_context
static_tag = Class.new(Tag) do
def render(*)
'static_tag'
end
end
original_tag = Class.new(Tag) do
def render(*)
'original_tag'
end
end
new_tag = Class.new(Tag) do
def render(*)
'new_tag'
end
end
tags_overwrite = { 'dynamic_tag' => new_tag }
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('static_tag original_tag', template.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