mirror of
https://github.com/kemko/liquid.git
synced 2026-01-02 16:25:42 +03:00
Compare commits
2 Commits
sort-numer
...
tokenizer-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
331b2a2e96 | ||
|
|
3ffae9a6e4 |
@@ -78,10 +78,8 @@ require 'liquid/tokenizer'
|
|||||||
require 'liquid/parse_context'
|
require 'liquid/parse_context'
|
||||||
require 'liquid/partial_cache'
|
require 'liquid/partial_cache'
|
||||||
require 'liquid/usage'
|
require 'liquid/usage'
|
||||||
require 'liquid/register'
|
|
||||||
require 'liquid/static_registers'
|
require 'liquid/static_registers'
|
||||||
|
|
||||||
# Load all the tags of the standard library
|
# Load all the tags of the standard library
|
||||||
#
|
#
|
||||||
Dir["#{__dir__}/liquid/tags/*.rb"].each { |f| require f }
|
Dir["#{__dir__}/liquid/tags/*.rb"].each { |f| require f }
|
||||||
Dir["#{__dir__}/liquid/registers/*.rb"].each { |f| require f }
|
|
||||||
|
|||||||
@@ -154,13 +154,7 @@ module Liquid
|
|||||||
private
|
private
|
||||||
|
|
||||||
def render_node(context, output, node)
|
def render_node(context, output, node)
|
||||||
if node.disabled?(context)
|
node.render_to_output_buffer(context, output)
|
||||||
output << node.disabled_error_message
|
|
||||||
return
|
|
||||||
end
|
|
||||||
disable_tags(context, node.disabled_tags) do
|
|
||||||
node.render_to_output_buffer(context, output)
|
|
||||||
end
|
|
||||||
rescue UndefinedVariable, UndefinedDropMethod, UndefinedFilter => e
|
rescue UndefinedVariable, UndefinedDropMethod, UndefinedFilter => e
|
||||||
context.handle_error(e, node.line_number)
|
context.handle_error(e, node.line_number)
|
||||||
rescue ::StandardError => e
|
rescue ::StandardError => e
|
||||||
@@ -168,11 +162,6 @@ module Liquid
|
|||||||
output << context.handle_error(e, line_number)
|
output << context.handle_error(e, line_number)
|
||||||
end
|
end
|
||||||
|
|
||||||
def disable_tags(context, tags, &block)
|
|
||||||
return yield if tags.empty?
|
|
||||||
context.registers[:disabled_tags].disable(tags, &block)
|
|
||||||
end
|
|
||||||
|
|
||||||
def raise_if_resource_limits_reached(context, length)
|
def raise_if_resource_limits_reached(context, length)
|
||||||
context.resource_limits.render_length += length
|
context.resource_limits.render_length += length
|
||||||
return unless context.resource_limits.reached?
|
return unless context.resource_limits.reached?
|
||||||
|
|||||||
@@ -25,5 +25,3 @@
|
|||||||
render: "Syntax error in tag 'render' - Template name must be a quoted string"
|
render: "Syntax error in tag 'render' - Template name must be a quoted string"
|
||||||
argument:
|
argument:
|
||||||
include: "Argument error in tag 'include' - Illegal template name"
|
include: "Argument error in tag 'include' - Illegal template name"
|
||||||
disabled:
|
|
||||||
tag: "usage is not allowed in this context"
|
|
||||||
|
|||||||
@@ -1,6 +0,0 @@
|
|||||||
# frozen_string_literal: true
|
|
||||||
|
|
||||||
module Liquid
|
|
||||||
class Register
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
# frozen_string_literal: true
|
|
||||||
module Liquid
|
|
||||||
class DisabledTags < Register
|
|
||||||
def initialize
|
|
||||||
@disabled_tags = {}
|
|
||||||
end
|
|
||||||
|
|
||||||
def disabled?(tag)
|
|
||||||
@disabled_tags.key?(tag) && @disabled_tags[tag] > 0
|
|
||||||
end
|
|
||||||
|
|
||||||
def disable(tags)
|
|
||||||
tags.each(&method(:increment))
|
|
||||||
yield
|
|
||||||
ensure
|
|
||||||
tags.each(&method(:decrement))
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def increment(tag)
|
|
||||||
@disabled_tags[tag] ||= 0
|
|
||||||
@disabled_tags[tag] += 1
|
|
||||||
end
|
|
||||||
|
|
||||||
def decrement(tag)
|
|
||||||
@disabled_tags[tag] -= 1
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
Template.add_register(:disabled_tags, DisabledTags.new)
|
|
||||||
end
|
|
||||||
@@ -193,23 +193,6 @@ module Liquid
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Sort elements of an array in numeric order
|
|
||||||
# provide optional property with which to sort an array of hashes or drops
|
|
||||||
def sort_numeric(input, property = nil)
|
|
||||||
ary = InputIterator.new(input)
|
|
||||||
if property.nil?
|
|
||||||
ary.sort do |a, b|
|
|
||||||
Utils.to_number(a) <=> Utils.to_number(b)
|
|
||||||
end
|
|
||||||
elsif ary.empty? # The next two cases assume a non-empty array.
|
|
||||||
[]
|
|
||||||
elsif ary.first.respond_to?(:[]) && !ary.first[property].nil?
|
|
||||||
ary.sort do |a, b|
|
|
||||||
Utils.to_number(a[property]) <=> Utils.to_number(b[property])
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Remove duplicate elements from an array
|
# Remove duplicate elements from an array
|
||||||
# provide optional property with which to determine uniqueness
|
# provide optional property with which to determine uniqueness
|
||||||
def uniq(input, property = nil)
|
def uniq(input, property = nil)
|
||||||
|
|||||||
@@ -13,15 +13,7 @@ module Liquid
|
|||||||
tag
|
tag
|
||||||
end
|
end
|
||||||
|
|
||||||
def disable_tags(*tags)
|
|
||||||
disabled_tags.push(*tags)
|
|
||||||
end
|
|
||||||
|
|
||||||
private :new
|
private :new
|
||||||
|
|
||||||
def disabled_tags
|
|
||||||
@disabled_tags ||= []
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(tag_name, markup, parse_context)
|
def initialize(tag_name, markup, parse_context)
|
||||||
@@ -46,14 +38,6 @@ module Liquid
|
|||||||
''
|
''
|
||||||
end
|
end
|
||||||
|
|
||||||
def disabled?(context)
|
|
||||||
context.registers[:disabled_tags].disabled?(tag_name)
|
|
||||||
end
|
|
||||||
|
|
||||||
def disabled_error_message
|
|
||||||
"#{tag_name} #{options[:locale].t('errors.disabled.tag')}"
|
|
||||||
end
|
|
||||||
|
|
||||||
# For backwards compatibility with custom tags. In a future release, the semantics
|
# 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`
|
# of the `render_to_output_buffer` method will become the default and the `render`
|
||||||
# method will be removed.
|
# method will be removed.
|
||||||
@@ -65,9 +49,5 @@ module Liquid
|
|||||||
def blank?
|
def blank?
|
||||||
false
|
false
|
||||||
end
|
end
|
||||||
|
|
||||||
def disabled_tags
|
|
||||||
self.class.disabled_tags
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -4,8 +4,6 @@ module Liquid
|
|||||||
class Render < Tag
|
class Render < Tag
|
||||||
SYNTAX = /(#{QuotedString})#{QuotedFragment}*/o
|
SYNTAX = /(#{QuotedString})#{QuotedFragment}*/o
|
||||||
|
|
||||||
disable_tags "include"
|
|
||||||
|
|
||||||
attr_reader :template_name_expr, :attributes
|
attr_reader :template_name_expr, :attributes
|
||||||
|
|
||||||
def initialize(tag_name, markup, options)
|
def initialize(tag_name, markup, options)
|
||||||
@@ -24,10 +22,6 @@ module Liquid
|
|||||||
end
|
end
|
||||||
|
|
||||||
def render_to_output_buffer(context, output)
|
def render_to_output_buffer(context, output)
|
||||||
render_tag(context, output)
|
|
||||||
end
|
|
||||||
|
|
||||||
def render_tag(context, output)
|
|
||||||
# Though we evaluate this here we will only ever parse it as a string literal.
|
# Though we evaluate this here we will only ever parse it as a string literal.
|
||||||
template_name = context.evaluate(@template_name_expr)
|
template_name = context.evaluate(@template_name_expr)
|
||||||
raise ArgumentError, options[:locale].t("errors.argument.include") unless template_name
|
raise ArgumentError, options[:locale].t("errors.argument.include") unless template_name
|
||||||
|
|||||||
@@ -92,14 +92,6 @@ module Liquid
|
|||||||
@tags ||= TagRegistry.new
|
@tags ||= TagRegistry.new
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_register(name, klass)
|
|
||||||
registers[name.to_sym] = klass
|
|
||||||
end
|
|
||||||
|
|
||||||
def registers
|
|
||||||
@registers ||= {}
|
|
||||||
end
|
|
||||||
|
|
||||||
def error_mode
|
def error_mode
|
||||||
@error_mode ||= :lax
|
@error_mode ||= :lax
|
||||||
end
|
end
|
||||||
@@ -199,26 +191,18 @@ module Liquid
|
|||||||
|
|
||||||
output = nil
|
output = nil
|
||||||
|
|
||||||
context_register = context.registers.is_a?(StaticRegisters) ? context.registers.static : context.registers
|
|
||||||
|
|
||||||
case args.last
|
case args.last
|
||||||
when Hash
|
when Hash
|
||||||
options = args.pop
|
options = args.pop
|
||||||
output = options[:output] if options[:output]
|
output = options[:output] if options[:output]
|
||||||
|
|
||||||
options[:registers]&.each do |key, register|
|
registers.merge!(options[:registers]) if options[:registers].is_a?(Hash)
|
||||||
context_register[key] = register
|
|
||||||
end
|
|
||||||
|
|
||||||
apply_options_to_context(context, options)
|
apply_options_to_context(context, options)
|
||||||
when Module, Array
|
when Module, Array
|
||||||
context.add_filters(args.pop)
|
context.add_filters(args.pop)
|
||||||
end
|
end
|
||||||
|
|
||||||
Template.registers.each do |key, register|
|
|
||||||
context_register[key] = register
|
|
||||||
end
|
|
||||||
|
|
||||||
# Retrying a render resets resource usage
|
# Retrying a render resets resource usage
|
||||||
context.resource_limits.reset
|
context.resource_limits.reset
|
||||||
|
|
||||||
|
|||||||
@@ -28,12 +28,84 @@ module Liquid
|
|||||||
|
|
||||||
return @source.split("\n") if @for_liquid_tag
|
return @source.split("\n") if @for_liquid_tag
|
||||||
|
|
||||||
tokens = @source.split(TemplateParser)
|
tokens = tokenize_new(@source)
|
||||||
|
# tokens = @source.split(TemplateParser)
|
||||||
|
|
||||||
# removes the rogue empty element at the beginning of the array
|
# removes the rogue empty element at the beginning of the array
|
||||||
tokens.shift if tokens[0]&.empty?
|
tokens.shift if tokens[0]&.empty?
|
||||||
|
|
||||||
tokens
|
tokens
|
||||||
end
|
end
|
||||||
|
|
||||||
|
T_TAG_OPEN = "{%"
|
||||||
|
T_VAR_OPEN = "{{"
|
||||||
|
T_SIN_QUOT = "'"
|
||||||
|
T_DOU_QUOT = '"'
|
||||||
|
T_TAG_CLOS = "%}"
|
||||||
|
T_VAR_CLOS = "}}"
|
||||||
|
T_VAR_CLO2 = "}"
|
||||||
|
|
||||||
|
S_NIL = 0
|
||||||
|
S_TAG = 1
|
||||||
|
S_VAR = 2
|
||||||
|
S_TAG_SIN = 3
|
||||||
|
S_TAG_DOU = 4
|
||||||
|
S_VAR_SIN = 5
|
||||||
|
S_VAR_DOU = 6
|
||||||
|
|
||||||
|
def tokenize_new(source)
|
||||||
|
output = []
|
||||||
|
s = S_NIL
|
||||||
|
current = +""
|
||||||
|
source.split(/({%|{{|"|'|}}|%}|})/om).each do |t|
|
||||||
|
if t == T_TAG_OPEN && s <= S_VAR
|
||||||
|
s = S_TAG
|
||||||
|
output << current
|
||||||
|
current = t
|
||||||
|
elsif t == T_VAR_OPEN && s <= S_VAR
|
||||||
|
s = S_VAR
|
||||||
|
output << current
|
||||||
|
current = t
|
||||||
|
elsif t == T_SIN_QUOT && s == S_TAG
|
||||||
|
s = S_TAG_SIN
|
||||||
|
current += t
|
||||||
|
elsif t == T_SIN_QUOT && s == S_TAG_SIN
|
||||||
|
s = S_TAG
|
||||||
|
current += t
|
||||||
|
elsif t == T_DOU_QUOT && s == S_TAG
|
||||||
|
s = S_TAG_DOU
|
||||||
|
current += t
|
||||||
|
elsif t == T_DOU_QUOT && s == S_TAG_DOU
|
||||||
|
s = S_TAG
|
||||||
|
current += t
|
||||||
|
elsif t == T_SIN_QUOT && s == S_VAR
|
||||||
|
s = S_VAR_SIN
|
||||||
|
current += t
|
||||||
|
elsif t == T_SIN_QUOT && s == S_VAR_SIN
|
||||||
|
s = S_VAR
|
||||||
|
current += t
|
||||||
|
elsif t == T_DOU_QUOT && s == S_VAR
|
||||||
|
s = S_VAR_DOU
|
||||||
|
current += t
|
||||||
|
elsif t == T_DOU_QUOT && s == S_VAR_DOU
|
||||||
|
s = S_VAR
|
||||||
|
current += t
|
||||||
|
elsif t == T_TAG_CLOS && s == S_TAG
|
||||||
|
s = S_NIL
|
||||||
|
current += t
|
||||||
|
output << current
|
||||||
|
current = +""
|
||||||
|
elsif (t == T_VAR_CLOS || t == T_VAR_CLO2) && s == S_VAR
|
||||||
|
s = S_NIL
|
||||||
|
current += t
|
||||||
|
output << current
|
||||||
|
current = +""
|
||||||
|
else
|
||||||
|
current += t
|
||||||
|
end
|
||||||
|
end
|
||||||
|
output << current unless current == ""
|
||||||
|
output
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -104,14 +104,6 @@ module Liquid
|
|||||||
output
|
output
|
||||||
end
|
end
|
||||||
|
|
||||||
def disabled?(_context)
|
|
||||||
false
|
|
||||||
end
|
|
||||||
|
|
||||||
def disabled_tags
|
|
||||||
[]
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def parse_filter_expressions(filter_name, unparsed_args)
|
def parse_filter_expressions(filter_name, unparsed_args)
|
||||||
|
|||||||
@@ -1,27 +0,0 @@
|
|||||||
# frozen_string_literal: true
|
|
||||||
|
|
||||||
require 'test_helper'
|
|
||||||
|
|
||||||
class DisabledTagsTest < Minitest::Test
|
|
||||||
include Liquid
|
|
||||||
|
|
||||||
class DisableRaw < Block
|
|
||||||
disable_tags "raw"
|
|
||||||
end
|
|
||||||
|
|
||||||
class DisableRawEcho < Block
|
|
||||||
disable_tags "raw", "echo"
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_disables_raw
|
|
||||||
with_custom_tag('disable', DisableRaw) do
|
|
||||||
assert_template_result 'raw usage is not allowed in this contextfoo', '{% disable %}{% raw %}Foobar{% endraw %}{% echo "foo" %}{% enddisable %}'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_disables_echo_and_raw
|
|
||||||
with_custom_tag('disable', DisableRawEcho) do
|
|
||||||
assert_template_result 'raw usage is not allowed in this contextecho usage is not allowed in this context', '{% disable %}{% raw %}Foobar{% endraw %}{% echo "foo" %}{% enddisable %}'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -198,12 +198,6 @@ class StandardFiltersTest < Minitest::Test
|
|||||||
assert_equal [{ "a" => 1 }, { "a" => 2 }, { "a" => 3 }, { "a" => 4 }], @filters.sort([{ "a" => 4 }, { "a" => 3 }, { "a" => 1 }, { "a" => 2 }], "a")
|
assert_equal [{ "a" => 1 }, { "a" => 2 }, { "a" => 3 }, { "a" => 4 }], @filters.sort([{ "a" => 4 }, { "a" => 3 }, { "a" => 1 }, { "a" => 2 }], "a")
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_sort_numeric
|
|
||||||
assert_equal ['1', '2', '3', '10'], @filters.sort_numeric(['10', '3', '2', '1'])
|
|
||||||
assert_equal [{ "a" => '1' }, { "a" => '2' }, { "a" => '3' }, { "a" => '10' }],
|
|
||||||
@filters.sort_numeric([{ "a" => '10' }, { "a" => '3' }, { "a" => '1' }, { "a" => '2' }], "a")
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_sort_with_nils
|
def test_sort_with_nils
|
||||||
assert_equal [1, 2, 3, 4, nil], @filters.sort([nil, 4, 3, 2, 1])
|
assert_equal [1, 2, 3, 4, nil], @filters.sort([nil, 4, 3, 2, 1])
|
||||||
assert_equal [{ "a" => 1 }, { "a" => 2 }, { "a" => 3 }, { "a" => 4 }, {}], @filters.sort([{ "a" => 4 }, { "a" => 3 }, {}, { "a" => 1 }, { "a" => 2 }], "a")
|
assert_equal [{ "a" => 1 }, { "a" => 2 }, { "a" => 3 }, { "a" => 4 }, {}], @filters.sort([{ "a" => 4 }, { "a" => 3 }, {}, { "a" => 1 }, { "a" => 2 }], "a")
|
||||||
@@ -298,10 +292,6 @@ class StandardFiltersTest < Minitest::Test
|
|||||||
assert_equal [], @filters.sort_natural([], "a")
|
assert_equal [], @filters.sort_natural([], "a")
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_sort_numeric_empty_array
|
|
||||||
assert_equal [], @filters.sort_numeric([], "a")
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_sort_natural_invalid_property
|
def test_sort_natural_invalid_property
|
||||||
foo = [
|
foo = [
|
||||||
[1],
|
[1],
|
||||||
|
|||||||
19
test/integration/tags/assign_tag_test.rb
Normal file
19
test/integration/tags/assign_tag_test.rb
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
class AssignTagTest < Minitest::Test
|
||||||
|
include Liquid
|
||||||
|
|
||||||
|
def test_assign
|
||||||
|
assert_template_result('monkey', "{% assign foo = 'monkey' %}{{ foo }}")
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_string_with_end_tag
|
||||||
|
assert_template_result("{% quoted %}", "{% assign string = '{% quoted %}' %}{{ string }}")
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_liquid_issue_701
|
||||||
|
assert_template_result(" contents: _{% endraw %}_", "{% assign endraw = '{% endraw %}' %} contents: _{{endraw}}_")
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -23,11 +23,17 @@ class RawTagTest < Minitest::Test
|
|||||||
assert_template_result ' Foobar {% {% {% ', '{% raw %} Foobar {% {% {% {% endraw %}'
|
assert_template_result ' Foobar {% {% {% ', '{% raw %} Foobar {% {% {% {% endraw %}'
|
||||||
assert_template_result ' test {% raw %} {% endraw %}', '{% raw %} test {% raw %} {% {% endraw %}endraw %}'
|
assert_template_result ' test {% raw %} {% endraw %}', '{% raw %} test {% raw %} {% {% endraw %}endraw %}'
|
||||||
assert_template_result ' Foobar {{ invalid 1', '{% raw %} Foobar {{ invalid {% endraw %}{{ 1 }}'
|
assert_template_result ' Foobar {{ invalid 1', '{% raw %} Foobar {{ invalid {% endraw %}{{ 1 }}'
|
||||||
|
assert_template_result ' Foobar {{ invalid 12', '{% raw %} Foobar {{ invalid {% endraw %}{{ 1 }}{{ 2 }}'
|
||||||
|
assert_template_result ' Foobar {{ invalid 1', '{% raw %} Foobar {{ invalid {% endraw %}{{ 1 }}'
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_nested_tag_in_raw
|
||||||
|
assert_template_result '{{ {% test %} }}', '{% raw %}{{ {% test %} }}{% endraw %}'
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_invalid_raw
|
def test_invalid_raw
|
||||||
assert_match_syntax_error(/tag was never closed/, '{% raw %} foo')
|
assert_match_syntax_error(/tag was never closed/, '{% raw %} foo')
|
||||||
assert_match_syntax_error(/Valid syntax/, '{% raw } foo {% endraw %}')
|
assert_match_syntax_error(/was not properly terminated/, '{% raw } foo {% endraw %}')
|
||||||
assert_match_syntax_error(/Valid syntax/, '{% raw } foo %}{% endraw %}')
|
assert_match_syntax_error(/Valid syntax/, '{% raw } foo %}{% endraw %}')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -89,12 +89,14 @@ class RenderTagTest < Minitest::Test
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_sub_contexts_count_towards_the_same_recursion_limit
|
def test_includes_and_renders_count_towards_the_same_recursion_limit
|
||||||
Liquid::Template.file_system = StubFileSystem.new(
|
Liquid::Template.file_system = StubFileSystem.new(
|
||||||
'loop_render' => '{% render "loop_render" %}',
|
'loop_render' => '{% render "loop_include" %}',
|
||||||
|
'loop_include' => '{% include "loop_render" %}'
|
||||||
)
|
)
|
||||||
assert_raises Liquid::StackLevelError do
|
|
||||||
Template.parse('{% render "loop_render" %}').render!
|
assert_raises Liquid::StackLevelError do
|
||||||
|
Template.parse('{% render "loop_include" %}').render!
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -146,23 +148,4 @@ class RenderTagTest < Minitest::Test
|
|||||||
Liquid::Template.file_system = StubFileSystem.new('decr' => '{% decrement %}')
|
Liquid::Template.file_system = StubFileSystem.new('decr' => '{% decrement %}')
|
||||||
assert_template_result '-1-2-1', '{% decrement %}{% decrement %}{% render "decr" %}'
|
assert_template_result '-1-2-1', '{% decrement %}{% decrement %}{% render "decr" %}'
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_includes_will_not_render_inside_render_tag
|
|
||||||
Liquid::Template.file_system = StubFileSystem.new(
|
|
||||||
'foo' => 'bar',
|
|
||||||
'test_include' => '{% include "foo" %}'
|
|
||||||
)
|
|
||||||
|
|
||||||
assert_template_result 'include usage is not allowed in this context', '{% render "test_include" %}'
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_includes_will_not_render_inside_nested_sibling_tags
|
|
||||||
Liquid::Template.file_system = StubFileSystem.new(
|
|
||||||
'foo' => 'bar',
|
|
||||||
'nested_render_with_sibling_include' => '{% render "test_include" %}{% include "foo" %}',
|
|
||||||
'test_include' => '{% include "foo" %}'
|
|
||||||
)
|
|
||||||
|
|
||||||
assert_template_result 'include usage is not allowed in this contextinclude usage is not allowed in this context', '{% render "nested_render_with_sibling_include" %}'
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -361,4 +361,9 @@ class TemplateTest < Minitest::Test
|
|||||||
result = t.render('x' => 1, 'y' => 5)
|
result = t.render('x' => 1, 'y' => 5)
|
||||||
assert_equal '12345', result
|
assert_equal '12345', result
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_curly_braces
|
||||||
|
assert_template_result "{}", "{{ '{}' }}"
|
||||||
|
assert_template_result "{}", "{% assign test = '{}' %}{{ test }}"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -95,4 +95,21 @@ class VariableTest < Minitest::Test
|
|||||||
def test_render_symbol
|
def test_render_symbol
|
||||||
assert_template_result 'bar', '{{ foo }}', 'foo' => :bar
|
assert_template_result 'bar', '{{ foo }}', 'foo' => :bar
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_quoted_single_curly_braces
|
||||||
|
assert_template_result "{user}", "{{ variable | prepend: '{' | append: '}' }}", 'variable' => 'user'
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_string_with_curly_brackets
|
||||||
|
json = '{ "key": { "nested": "value" }}'
|
||||||
|
assert_template_result(json, "{{ '#{json}' }}")
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_liquid_issue_344
|
||||||
|
assert_template_result "blah xx yy }}", "{{ 'blah {{ yy }}' | replace: '{{', 'xx' }}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_liquid_issue_213
|
||||||
|
assert_template_result "blah", "{{ 'blah}' | remove: '}' }}"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,36 +0,0 @@
|
|||||||
# frozen_string_literal: true
|
|
||||||
|
|
||||||
require 'test_helper'
|
|
||||||
|
|
||||||
class DisabledTagsUnitTest < Minitest::Test
|
|
||||||
include Liquid
|
|
||||||
|
|
||||||
def test_disables_tag_specified
|
|
||||||
register = DisabledTags.new
|
|
||||||
register.disable(%w(foo bar)) do
|
|
||||||
assert_equal true, register.disabled?("foo")
|
|
||||||
assert_equal true, register.disabled?("bar")
|
|
||||||
assert_equal false, register.disabled?("unknown")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_disables_nested_tags
|
|
||||||
register = DisabledTags.new
|
|
||||||
register.disable(["foo"]) do
|
|
||||||
register.disable(["foo"]) do
|
|
||||||
assert_equal true, register.disabled?("foo")
|
|
||||||
assert_equal false, register.disabled?("bar")
|
|
||||||
end
|
|
||||||
register.disable(["bar"]) do
|
|
||||||
assert_equal true, register.disabled?("foo")
|
|
||||||
assert_equal true, register.disabled?("bar")
|
|
||||||
register.disable(["foo"]) do
|
|
||||||
assert_equal true, register.disabled?("foo")
|
|
||||||
assert_equal true, register.disabled?("bar")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
assert_equal true, register.disabled?("foo")
|
|
||||||
assert_equal false, register.disabled?("bar")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -30,6 +30,67 @@ class TokenizerTest < Minitest::Test
|
|||||||
assert_equal [1, 1, 3], tokenize_line_numbers(" {{\n funk \n}} ")
|
assert_equal [1, 1, 3], tokenize_line_numbers(" {{\n funk \n}} ")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_tokenize_quirks
|
||||||
|
assert_equal ['{%comment%}'], tokenize('{%comment%}')
|
||||||
|
assert_equal [' ', '{%comment%}', ' '], tokenize(' {%comment%} ')
|
||||||
|
|
||||||
|
assert_equal [' ', '{%comment%}', ' ', '{%endcomment%}', ' '], tokenize(' {%comment%} {%endcomment%} ')
|
||||||
|
assert_equal [' ', '{% "{% comment" %}', ' ', '{% endcomment %}', ' '], tokenize(' {% "{% comment" %} {% endcomment %} ')
|
||||||
|
assert_equal [' ', '{% "{% comment %}" %}', ' ', '{% endcomment %}', ' '], tokenize(' {% "{% comment %}" %} {% endcomment %} ')
|
||||||
|
assert_equal [' ', '{% "comment %}" %}', ' ', '{% endcomment %}', ' '], tokenize(' {% "comment %}" %} {% endcomment %} ')
|
||||||
|
assert_equal [' ', '{% "{{ comment" %}', ' ', '{% endcomment %}', ' '], tokenize(' {% "{{ comment" %} {% endcomment %} ')
|
||||||
|
assert_equal [' ', '{% "{{ comment }}" %}', ' ', '{% endcomment %}', ' '], tokenize(' {% "{{ comment }}" %} {% endcomment %} ')
|
||||||
|
assert_equal [' ', '{% "comment }}" %}', ' ', '{% endcomment %}', ' '], tokenize(' {% "comment }}" %} {% endcomment %} ')
|
||||||
|
assert_equal [' ', '{% "comment }" %}', ' ', '{% endcomment %}', ' '], tokenize(' {% "comment }" %} {% endcomment %} ')
|
||||||
|
|
||||||
|
assert_equal [" ", "{%comment%}", " ", "{%endcomment%}", " "], tokenize(" {%comment%} {%endcomment%} ")
|
||||||
|
assert_equal [" ", "{% '{% comment' %}", " ", "{% endcomment %}", " "], tokenize(" {% '{% comment' %} {% endcomment %} ")
|
||||||
|
assert_equal [" ", "{% '{% comment %}' %}", " ", "{% endcomment %}", " "], tokenize(" {% '{% comment %}' %} {% endcomment %} ")
|
||||||
|
assert_equal [" ", "{% 'comment %}' %}", " ", "{% endcomment %}", " "], tokenize(" {% 'comment %}' %} {% endcomment %} ")
|
||||||
|
assert_equal [" ", "{% '{{ comment' %}", " ", "{% endcomment %}", " "], tokenize(" {% '{{ comment' %} {% endcomment %} ")
|
||||||
|
assert_equal [" ", "{% '{{ comment }}' %}", " ", "{% endcomment %}", " "], tokenize(" {% '{{ comment }}' %} {% endcomment %} ")
|
||||||
|
assert_equal [" ", "{% 'comment }}' %}", " ", "{% endcomment %}", " "], tokenize(" {% 'comment }}' %} {% endcomment %} ")
|
||||||
|
assert_equal [" ", "{% 'comment }' %}", " ", "{% endcomment %}", " "], tokenize(" {% 'comment }' %} {% endcomment %} ")
|
||||||
|
|
||||||
|
assert_equal [' ', '{{comment}}', ' ', '{{endcomment}}', ' '], tokenize(' {{comment}} {{endcomment}} ')
|
||||||
|
assert_equal [' ', '{{ "{{ comment" }}', ' ', '{{ endcomment }}', ' '], tokenize(' {{ "{{ comment" }} {{ endcomment }} ')
|
||||||
|
assert_equal [' ', '{{ "{{ comment }}" }}', ' ', '{{ endcomment }}', ' '], tokenize(' {{ "{{ comment }}" }} {{ endcomment }} ')
|
||||||
|
assert_equal [' ', '{{ "comment }}" }}', ' ', '{{ endcomment }}', ' '], tokenize(' {{ "comment }}" }} {{ endcomment }} ')
|
||||||
|
assert_equal [' ', '{{ "{{ comment" }}', ' ', '{{ endcomment }}', ' '], tokenize(' {{ "{{ comment" }} {{ endcomment }} ')
|
||||||
|
assert_equal [' ', '{{ "{{ comment }}" }}', ' ', '{{ endcomment }}', ' '], tokenize(' {{ "{{ comment }}" }} {{ endcomment }} ')
|
||||||
|
assert_equal [' ', '{{ "comment }}" }}', ' ', '{{ endcomment }}', ' '], tokenize(' {{ "comment }}" }} {{ endcomment }} ')
|
||||||
|
assert_equal [' ', '{{ "comment }" }}', ' ', '{{ endcomment }}', ' '], tokenize(' {{ "comment }" }} {{ endcomment }} ')
|
||||||
|
|
||||||
|
assert_equal [" ", "{{comment}}", " ", "{{endcomment}}", " "], tokenize(" {{comment}} {{endcomment}} ")
|
||||||
|
assert_equal [" ", "{{ '{% comment' }}", " ", "{{ endcomment }}", " "], tokenize(" {{ '{% comment' }} {{ endcomment }} ")
|
||||||
|
assert_equal [" ", "{{ '{% comment }}' }}", " ", "{{ endcomment }}", " "], tokenize(" {{ '{% comment }}' }} {{ endcomment }} ")
|
||||||
|
assert_equal [" ", "{{ 'comment }}' }}", " ", "{{ endcomment }}", " "], tokenize(" {{ 'comment }}' }} {{ endcomment }} ")
|
||||||
|
assert_equal [" ", "{{ '{{ comment' }}", " ", "{{ endcomment }}", " "], tokenize(" {{ '{{ comment' }} {{ endcomment }} ")
|
||||||
|
assert_equal [" ", "{{ '{{ comment }}' }}", " ", "{{ endcomment }}", " "], tokenize(" {{ '{{ comment }}' }} {{ endcomment }} ")
|
||||||
|
assert_equal [" ", "{{ 'comment }}' }}", " ", "{{ endcomment }}", " "], tokenize(" {{ 'comment }}' }} {{ endcomment }} ")
|
||||||
|
assert_equal [" ", "{{ 'comment }' }}", " ", "{{ endcomment }}", " "], tokenize(" {{ 'comment }' }} {{ endcomment }} ")
|
||||||
|
|
||||||
|
assert_equal [' ', '{{comment}', ' ', '{{endcomment}', ' '], tokenize(' {{comment} {{endcomment} ')
|
||||||
|
assert_equal [' ', '{{ "{% comment" }', ' ', '{{ endcomment }', ' '], tokenize(' {{ "{% comment" } {{ endcomment } ')
|
||||||
|
assert_equal [' ', '{{ "{% comment }" }', ' ', '{{ endcomment }', ' '], tokenize(' {{ "{% comment }" } {{ endcomment } ')
|
||||||
|
assert_equal [' ', '{{ "comment }" }', ' ', '{{ endcomment }', ' '], tokenize(' {{ "comment }" } {{ endcomment } ')
|
||||||
|
assert_equal [' ', '{{ "{{ comment" }', ' ', '{{ endcomment }', ' '], tokenize(' {{ "{{ comment" } {{ endcomment } ')
|
||||||
|
assert_equal [' ', '{{ "{{ comment }}" }', ' ', '{{ endcomment }', ' '], tokenize(' {{ "{{ comment }}" } {{ endcomment } ')
|
||||||
|
assert_equal [' ', '{{ "comment }}" }', ' ', '{{ endcomment }', ' '], tokenize(' {{ "comment }}" } {{ endcomment } ')
|
||||||
|
assert_equal [' ', '{{ "comment }" }', ' ', '{{ endcomment }', ' '], tokenize(' {{ "comment }" } {{ endcomment } ')
|
||||||
|
|
||||||
|
assert_equal [" ", "{{comment}", " ", "{{endcomment}", " "], tokenize(" {{comment} {{endcomment} ")
|
||||||
|
assert_equal [" ", "{{ '{{ comment' }", " ", "{{ endcomment }", " "], tokenize(" {{ '{{ comment' } {{ endcomment } ")
|
||||||
|
assert_equal [" ", "{{ '{{ comment }' }", " ", "{{ endcomment }", " "], tokenize(" {{ '{{ comment }' } {{ endcomment } ")
|
||||||
|
assert_equal [" ", "{{ 'comment }' }", " ", "{{ endcomment }", " "], tokenize(" {{ 'comment }' } {{ endcomment } ")
|
||||||
|
assert_equal [" ", "{{ '{{ comment' }", " ", "{{ endcomment }", " "], tokenize(" {{ '{{ comment' } {{ endcomment } ")
|
||||||
|
assert_equal [" ", "{{ '{{ comment }}' }", " ", "{{ endcomment }", " "], tokenize(" {{ '{{ comment }}' } {{ endcomment } ")
|
||||||
|
assert_equal [" ", "{{ 'comment }}' }", " ", "{{ endcomment }", " "], tokenize(" {{ 'comment }}' } {{ endcomment } ")
|
||||||
|
assert_equal [" ", "{{ 'comment }' }", " ", "{{ endcomment }", " "], tokenize(" {{ 'comment }' } {{ endcomment } ")
|
||||||
|
|
||||||
|
assert_equal ['{{funk | replace: "}", \'}}\' }}'], tokenize('{{funk | replace: "}", \'}}\' }}')
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def tokenize(source)
|
def tokenize(source)
|
||||||
|
|||||||
@@ -156,6 +156,12 @@ class VariableUnitTest < Minitest::Test
|
|||||||
assert_equal ['b', 'c'], lookup.lookups
|
assert_equal ['b', 'c'], lookup.lookups
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_filters_with_properly_quoted_curlies
|
||||||
|
var = create_variable("hello | replace: \"}\", '}}'")
|
||||||
|
assert_equal VariableLookup.new('hello'), var.name
|
||||||
|
assert_equal [['replace', ['}', '}}']]], var.filters
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def create_variable(markup, options = {})
|
def create_variable(markup, options = {})
|
||||||
|
|||||||
Reference in New Issue
Block a user