Improve disable tag API

This commit is contained in:
Mike Angell
2019-09-25 11:34:44 +10:00
parent 5e9b0bd3e9
commit 9b2d0dc145
4 changed files with 25 additions and 19 deletions

View File

@@ -155,7 +155,9 @@ module Liquid
def render_node(context, output, node)
return if node.is_a?(Tag) && node.disabled?(context, output)
node.render_to_output_buffer(context, output)
disable_tags(context, node.is_a?(Tag) ? node.disabled_tags : nil) do
node.render_to_output_buffer(context, output)
end
rescue UndefinedVariable, UndefinedDropMethod, UndefinedFilter => e
context.handle_error(e, node.line_number)
rescue ::StandardError => e
@@ -163,6 +165,11 @@ module Liquid
output << context.handle_error(e, line_number)
end
def disable_tags(context, tags, &block)
return yield unless tags
context.registers['disabled_tags'].disable(tags, &block)
end
def raise_if_resource_limits_reached(context, length)
context.resource_limits.render_length += length
return unless context.resource_limits.reached?

View File

@@ -13,7 +13,15 @@ module Liquid
tag
end
def disable_nested_tags(*tags)
@disabled_tags = tags
end
private :new
def disabled_tags
@disabled_tags ||= []
end
end
def initialize(tag_name, markup, parse_context)
@@ -48,10 +56,6 @@ module Liquid
"#{tag_name} #{options[:locale].t('errors.disabled.tag')}"
end
def disable_tags(context, tags, &block)
context.registers['disabled_tags'].disable(tags, &block)
end
# For backwards compatibility with custom tags. In a future release, the semantics
# of the `render_to_output_buffer` method will become the default and the `render`
# method will be removed.
@@ -63,5 +67,9 @@ module Liquid
def blank?
false
end
def disabled_tags
self.class.disabled_tags
end
end
end

View File

@@ -3,7 +3,8 @@
module Liquid
class Render < Tag
SYNTAX = /(#{QuotedString})#{QuotedFragment}*/o
DISABLED_TAGS = %w(include).freeze
disable_nested_tags "include"
attr_reader :template_name_expr, :attributes
@@ -23,9 +24,7 @@ module Liquid
end
def render_to_output_buffer(context, output)
disable_tags(context, DISABLED_TAGS) do
render_tag(context, output)
end
render_tag(context, output)
end
def render_tag(context, output)

View File

@@ -6,19 +6,11 @@ class DisabledTagsTest < Minitest::Test
include Liquid
class DisableRaw < Block
def render(context)
disable_tags(context, ["raw"]) do
@body.render(context)
end
end
disable_nested_tags "raw"
end
class DisableRawEcho < Block
def render(context)
disable_tags(context, ["raw", "echo"]) do
@body.render(context)
end
end
disable_nested_tags "raw", "echo"
end
def test_disables_raw