mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-20 03:10:39 -07:00
Add ability to pass tags to parse_context
Co-authored-by: Dylan Thacker-Smith <[email protected]>
This commit is contained in:
co-authored by
Dylan Thacker-Smith
parent
456be2f75e
commit
3d2aa05d64
@@ -22,6 +22,6 @@ group :test do
|
|||||||
gem 'rubocop-performance', require: false
|
gem 'rubocop-performance', require: false
|
||||||
|
|
||||||
platform :mri, :truffleruby do
|
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
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
# Liquid Change Log
|
# 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
|
## 5.4.0 2022-07-29
|
||||||
|
|
||||||
### Breaking Changes
|
### Breaking Changes
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ module Liquid
|
|||||||
end
|
end
|
||||||
tag_name = Regexp.last_match(1)
|
tag_name = Regexp.last_match(1)
|
||||||
markup = Regexp.last_match(2)
|
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
|
# end parsing if we reach an unknown tag and let the caller decide
|
||||||
# determine how to proceed
|
# determine how to proceed
|
||||||
return yield tag_name, markup
|
return yield tag_name, markup
|
||||||
@@ -132,7 +132,7 @@ module Liquid
|
|||||||
next
|
next
|
||||||
end
|
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
|
# end parsing if we reach an unknown tag and let the caller decide
|
||||||
# determine how to proceed
|
# determine how to proceed
|
||||||
return yield tag_name, markup
|
return yield tag_name, markup
|
||||||
@@ -247,9 +247,5 @@ module Liquid
|
|||||||
def raise_missing_variable_terminator(token, parse_context)
|
def raise_missing_variable_terminator(token, parse_context)
|
||||||
BlockBody.raise_missing_variable_terminator(token, parse_context)
|
BlockBody.raise_missing_variable_terminator(token, parse_context)
|
||||||
end
|
end
|
||||||
|
|
||||||
def registered_tags
|
|
||||||
Template.tags
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
module Liquid
|
module Liquid
|
||||||
class ParseContext
|
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
|
attr_reader :partial, :warnings, :error_mode
|
||||||
|
|
||||||
def initialize(options = {})
|
def initialize(options = {})
|
||||||
@@ -10,6 +10,7 @@ module Liquid
|
|||||||
|
|
||||||
@locale = @template_options[:locale] ||= I18n.new
|
@locale = @template_options[:locale] ||= I18n.new
|
||||||
@warnings = []
|
@warnings = []
|
||||||
|
@tags = @template_options[:tags] || Liquid::Template.tags
|
||||||
|
|
||||||
self.depth = 0
|
self.depth = 0
|
||||||
self.partial = false
|
self.partial = false
|
||||||
|
|||||||
@@ -42,4 +42,33 @@ class TagTest < Minitest::Test
|
|||||||
assert_equal(buf.object_id, output.object_id)
|
assert_equal(buf.object_id, output.object_id)
|
||||||
end
|
end
|
||||||
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)
|
||||||
|
|
||||||
|
template_with_overwrite = Liquid::Template.parse(liquid, tags: tags_overwrite)
|
||||||
|
assert_equal('new_tag', template_with_overwrite.render)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user