mirror of
https://github.com/kemko/liquid.git
synced 2026-01-02 16:25:42 +03:00
Compare commits
5 Commits
inline-com
...
refactor-f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9567794a7a | ||
|
|
cddd8ea4ff | ||
|
|
9917924adb | ||
|
|
17c3e9989a | ||
|
|
2777b9d06c |
@@ -71,15 +71,23 @@ module Liquid
|
|||||||
# @api private
|
# @api private
|
||||||
def self.render_node(context, output, node)
|
def self.render_node(context, output, node)
|
||||||
node.render_to_output_buffer(context, output)
|
node.render_to_output_buffer(context, output)
|
||||||
rescue UndefinedVariable, UndefinedDropMethod, UndefinedFilter => e
|
rescue => exc
|
||||||
context.handle_error(e, node.line_number)
|
blank_tag = !node.instance_of?(Variable) && node.blank?
|
||||||
rescue MemoryError
|
rescue_render_node(context, output, node.line_number, exc, blank_tag)
|
||||||
raise
|
end
|
||||||
rescue ::StandardError => e
|
|
||||||
line_number = node.is_a?(String) ? nil : node.line_number
|
# @api private
|
||||||
error_message = context.handle_error(e, line_number)
|
def self.rescue_render_node(context, output, line_number, exc, blank_tag)
|
||||||
if node.instance_of?(Variable) || !node.blank? # conditional for backwards compatibility
|
case exc
|
||||||
output << error_message
|
when MemoryError
|
||||||
|
raise
|
||||||
|
when UndefinedVariable, UndefinedDropMethod, UndefinedFilter
|
||||||
|
context.handle_error(exc, line_number)
|
||||||
|
else
|
||||||
|
error_message = context.handle_error(exc, line_number)
|
||||||
|
unless blank_tag # conditional for backwards compatibility
|
||||||
|
output << error_message
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -27,10 +27,32 @@ module Liquid
|
|||||||
end,
|
end,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class MethodLiteral
|
||||||
|
attr_reader :method_name, :to_s
|
||||||
|
|
||||||
|
def initialize(method_name, to_s)
|
||||||
|
@method_name = method_name
|
||||||
|
@to_s = to_s
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_liquid
|
||||||
|
to_s
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@@method_literals = {
|
||||||
|
'blank' => MethodLiteral.new(:blank?, '').freeze,
|
||||||
|
'empty' => MethodLiteral.new(:empty?, '').freeze,
|
||||||
|
}
|
||||||
|
|
||||||
def self.operators
|
def self.operators
|
||||||
@@operators
|
@@operators
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.parse_expression(markup)
|
||||||
|
@@method_literals[markup] || Expression.parse(markup)
|
||||||
|
end
|
||||||
|
|
||||||
attr_reader :attachment, :child_condition
|
attr_reader :attachment, :child_condition
|
||||||
attr_accessor :left, :operator, :right
|
attr_accessor :left, :operator, :right
|
||||||
|
|
||||||
@@ -91,7 +113,7 @@ module Liquid
|
|||||||
private
|
private
|
||||||
|
|
||||||
def equal_variables(left, right)
|
def equal_variables(left, right)
|
||||||
if left.is_a?(Liquid::Expression::MethodLiteral)
|
if left.is_a?(MethodLiteral)
|
||||||
if right.respond_to?(left.method_name)
|
if right.respond_to?(left.method_name)
|
||||||
return right.send(left.method_name)
|
return right.send(left.method_name)
|
||||||
else
|
else
|
||||||
@@ -99,7 +121,7 @@ module Liquid
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if right.is_a?(Liquid::Expression::MethodLiteral)
|
if right.is_a?(MethodLiteral)
|
||||||
if left.respond_to?(right.method_name)
|
if left.respond_to?(right.method_name)
|
||||||
return left.send(right.method_name)
|
return left.send(right.method_name)
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -2,25 +2,12 @@
|
|||||||
|
|
||||||
module Liquid
|
module Liquid
|
||||||
class Expression
|
class Expression
|
||||||
class MethodLiteral
|
|
||||||
attr_reader :method_name, :to_s
|
|
||||||
|
|
||||||
def initialize(method_name, to_s)
|
|
||||||
@method_name = method_name
|
|
||||||
@to_s = to_s
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_liquid
|
|
||||||
to_s
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
LITERALS = {
|
LITERALS = {
|
||||||
nil => nil, 'nil' => nil, 'null' => nil, '' => nil,
|
nil => nil, 'nil' => nil, 'null' => nil, '' => nil,
|
||||||
'true' => true,
|
'true' => true,
|
||||||
'false' => false,
|
'false' => false,
|
||||||
'blank' => MethodLiteral.new(:blank?, '').freeze,
|
'blank' => '',
|
||||||
'empty' => MethodLiteral.new(:empty?, '').freeze
|
'empty' => ''
|
||||||
}.freeze
|
}.freeze
|
||||||
|
|
||||||
SINGLE_QUOTED_STRING = /\A'(.*)'\z/m
|
SINGLE_QUOTED_STRING = /\A'(.*)'\z/m
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ module Liquid
|
|||||||
|
|
||||||
markup = Regexp.last_match(2)
|
markup = Regexp.last_match(2)
|
||||||
|
|
||||||
block = Condition.new(@left, '==', Expression.parse(Regexp.last_match(1)))
|
block = Condition.new(@left, '==', Condition.parse_expression(Regexp.last_match(1)))
|
||||||
block.attach(body)
|
block.attach(body)
|
||||||
@blocks << block
|
@blocks << block
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -67,18 +67,22 @@ module Liquid
|
|||||||
block.attach(new_body)
|
block.attach(new_body)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def parse_expression(markup)
|
||||||
|
Condition.parse_expression(markup)
|
||||||
|
end
|
||||||
|
|
||||||
def lax_parse(markup)
|
def lax_parse(markup)
|
||||||
expressions = markup.scan(ExpressionsAndOperators)
|
expressions = markup.scan(ExpressionsAndOperators)
|
||||||
raise SyntaxError, options[:locale].t("errors.syntax.if") unless expressions.pop =~ Syntax
|
raise SyntaxError, options[:locale].t("errors.syntax.if") unless expressions.pop =~ Syntax
|
||||||
|
|
||||||
condition = Condition.new(Expression.parse(Regexp.last_match(1)), Regexp.last_match(2), Expression.parse(Regexp.last_match(3)))
|
condition = Condition.new(parse_expression(Regexp.last_match(1)), Regexp.last_match(2), parse_expression(Regexp.last_match(3)))
|
||||||
|
|
||||||
until expressions.empty?
|
until expressions.empty?
|
||||||
operator = expressions.pop.to_s.strip
|
operator = expressions.pop.to_s.strip
|
||||||
|
|
||||||
raise SyntaxError, options[:locale].t("errors.syntax.if") unless expressions.pop.to_s =~ Syntax
|
raise SyntaxError, options[:locale].t("errors.syntax.if") unless expressions.pop.to_s =~ Syntax
|
||||||
|
|
||||||
new_condition = Condition.new(Expression.parse(Regexp.last_match(1)), Regexp.last_match(2), Expression.parse(Regexp.last_match(3)))
|
new_condition = Condition.new(parse_expression(Regexp.last_match(1)), Regexp.last_match(2), parse_expression(Regexp.last_match(3)))
|
||||||
raise SyntaxError, options[:locale].t("errors.syntax.if") unless BOOLEAN_OPERATORS.include?(operator)
|
raise SyntaxError, options[:locale].t("errors.syntax.if") unless BOOLEAN_OPERATORS.include?(operator)
|
||||||
new_condition.send(operator, condition)
|
new_condition.send(operator, condition)
|
||||||
condition = new_condition
|
condition = new_condition
|
||||||
@@ -106,9 +110,9 @@ module Liquid
|
|||||||
end
|
end
|
||||||
|
|
||||||
def parse_comparison(p)
|
def parse_comparison(p)
|
||||||
a = Expression.parse(p.expression)
|
a = parse_expression(p.expression)
|
||||||
if (op = p.consume?(:comparison))
|
if (op = p.consume?(:comparison))
|
||||||
b = Expression.parse(p.expression)
|
b = parse_expression(p.expression)
|
||||||
Condition.new(a, op, b)
|
Condition.new(a, op, b)
|
||||||
else
|
else
|
||||||
Condition.new(a)
|
Condition.new(a)
|
||||||
|
|||||||
@@ -11,4 +11,48 @@ class BlockTest < Minitest::Test
|
|||||||
end
|
end
|
||||||
assert_equal(exc.message, "Liquid syntax error: 'endunless' is not a valid delimiter for if tags. use endif")
|
assert_equal(exc.message, "Liquid syntax error: 'endunless' is not a valid delimiter for if tags. use endif")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_with_custom_tag
|
||||||
|
with_custom_tag('testtag', Block) do
|
||||||
|
assert Liquid::Template.parse("{% testtag %} {% endtesttag %}")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_custom_block_tags_have_a_default_render_to_output_buffer_method_for_backwards_compatibility
|
||||||
|
klass1 = Class.new(Block) do
|
||||||
|
def render(*)
|
||||||
|
'hello'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
with_custom_tag('blabla', klass1) do
|
||||||
|
template = Liquid::Template.parse("{% blabla %} bla {% endblabla %}")
|
||||||
|
|
||||||
|
assert_equal 'hello', template.render
|
||||||
|
|
||||||
|
buf = +''
|
||||||
|
output = template.render({}, output: buf)
|
||||||
|
assert_equal 'hello', output
|
||||||
|
assert_equal 'hello', buf
|
||||||
|
assert_equal buf.object_id, output.object_id
|
||||||
|
end
|
||||||
|
|
||||||
|
klass2 = Class.new(klass1) do
|
||||||
|
def render(*)
|
||||||
|
'foo' + super + 'bar'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
with_custom_tag('blabla', klass2) do
|
||||||
|
template = Liquid::Template.parse("{% blabla %} foo {% endblabla %}")
|
||||||
|
|
||||||
|
assert_equal 'foohellobar', template.render
|
||||||
|
|
||||||
|
buf = +''
|
||||||
|
output = template.render({}, output: buf)
|
||||||
|
assert_equal 'foohellobar', output
|
||||||
|
assert_equal 'foohellobar', buf
|
||||||
|
assert_equal buf.object_id, output.object_id
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ class ArrayLike
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class ContextUnitTest < Minitest::Test
|
class ContextTest < Minitest::Test
|
||||||
include Liquid
|
include Liquid
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
@@ -43,15 +43,22 @@ class SecurityTest < Minitest::Test
|
|||||||
assert_equal(expected, Template.parse(text).render!(@assigns, filters: SecurityFilter))
|
assert_equal(expected, Template.parse(text).render!(@assigns, filters: SecurityFilter))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_does_not_add_filters_to_symbol_table
|
def test_does_not_permanently_add_filters_to_symbol_table
|
||||||
current_symbols = Symbol.all_symbols
|
current_symbols = Symbol.all_symbols
|
||||||
|
|
||||||
test = %( {{ "some_string" | a_bad_filter }} )
|
# MRI imprecisely marks objects found on the C stack, which can result
|
||||||
|
# in uninitialized memory being marked. This can even result in the test failing
|
||||||
|
# deterministically for a given compilation of ruby. Using a separate thread will
|
||||||
|
# keep these writes of the symbol pointer on a separate stack that will be garbage
|
||||||
|
# collected after Thread#join.
|
||||||
|
Thread.new do
|
||||||
|
test = %( {{ "some_string" | a_bad_filter }} )
|
||||||
|
Template.parse(test).render!
|
||||||
|
nil
|
||||||
|
end.join
|
||||||
|
|
||||||
template = Template.parse(test)
|
GC.start
|
||||||
assert_equal([], (Symbol.all_symbols - current_symbols))
|
|
||||||
|
|
||||||
template.render!
|
|
||||||
assert_equal([], (Symbol.all_symbols - current_symbols))
|
assert_equal([], (Symbol.all_symbols - current_symbols))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
45
test/integration/tag_test.rb
Normal file
45
test/integration/tag_test.rb
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
class TagTest < Minitest::Test
|
||||||
|
include Liquid
|
||||||
|
|
||||||
|
def test_custom_tags_have_a_default_render_to_output_buffer_method_for_backwards_compatibility
|
||||||
|
klass1 = Class.new(Tag) do
|
||||||
|
def render(*)
|
||||||
|
'hello'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
with_custom_tag('blabla', klass1) do
|
||||||
|
template = Liquid::Template.parse("{% blabla %}")
|
||||||
|
|
||||||
|
assert_equal 'hello', template.render
|
||||||
|
|
||||||
|
buf = +''
|
||||||
|
output = template.render({}, output: buf)
|
||||||
|
assert_equal 'hello', output
|
||||||
|
assert_equal 'hello', buf
|
||||||
|
assert_equal buf.object_id, output.object_id
|
||||||
|
end
|
||||||
|
|
||||||
|
klass2 = Class.new(klass1) do
|
||||||
|
def render(*)
|
||||||
|
'foo' + super + 'bar'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
with_custom_tag('blabla', klass2) do
|
||||||
|
template = Liquid::Template.parse("{% blabla %}")
|
||||||
|
|
||||||
|
assert_equal 'foohellobar', template.render
|
||||||
|
|
||||||
|
buf = +''
|
||||||
|
output = template.render({}, output: buf)
|
||||||
|
assert_equal 'foohellobar', output
|
||||||
|
assert_equal 'foohellobar', buf
|
||||||
|
assert_equal buf.object_id, output.object_id
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -45,53 +45,9 @@ class BlockUnitTest < Minitest::Test
|
|||||||
assert_equal(3, template.root.nodelist.size)
|
assert_equal(3, template.root.nodelist.size)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_with_custom_tag
|
|
||||||
with_custom_tag('testtag', Block) do
|
|
||||||
assert Liquid::Template.parse("{% testtag %} {% endtesttag %}")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_custom_block_tags_have_a_default_render_to_output_buffer_method_for_backwards_compatibility
|
|
||||||
klass1 = Class.new(Block) do
|
|
||||||
def render(*)
|
|
||||||
'hello'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
with_custom_tag('blabla', klass1) do
|
|
||||||
template = Liquid::Template.parse("{% blabla %} bla {% endblabla %}")
|
|
||||||
|
|
||||||
assert_equal 'hello', template.render
|
|
||||||
|
|
||||||
buf = +''
|
|
||||||
output = template.render({}, output: buf)
|
|
||||||
assert_equal 'hello', output
|
|
||||||
assert_equal 'hello', buf
|
|
||||||
assert_equal buf.object_id, output.object_id
|
|
||||||
end
|
|
||||||
|
|
||||||
klass2 = Class.new(klass1) do
|
|
||||||
def render(*)
|
|
||||||
'foo' + super + 'bar'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
with_custom_tag('blabla', klass2) do
|
|
||||||
template = Liquid::Template.parse("{% blabla %} foo {% endblabla %}")
|
|
||||||
|
|
||||||
assert_equal 'foohellobar', template.render
|
|
||||||
|
|
||||||
buf = +''
|
|
||||||
output = template.render({}, output: buf)
|
|
||||||
assert_equal 'foohellobar', output
|
|
||||||
assert_equal 'foohellobar', buf
|
|
||||||
assert_equal buf.object_id, output.object_id
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def block_types(nodelist)
|
def block_types(nodelist)
|
||||||
nodelist.collect(&:class)
|
nodelist.collect(&:class)
|
||||||
end
|
end
|
||||||
end # VariableTest
|
end
|
||||||
|
|||||||
@@ -20,42 +20,4 @@ class TagUnitTest < Minitest::Test
|
|||||||
tag = Tag.parse("some_tag", "", Tokenizer.new(""), ParseContext.new)
|
tag = Tag.parse("some_tag", "", Tokenizer.new(""), ParseContext.new)
|
||||||
assert_equal('some_tag', tag.tag_name)
|
assert_equal('some_tag', tag.tag_name)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_custom_tags_have_a_default_render_to_output_buffer_method_for_backwards_compatibility
|
|
||||||
klass1 = Class.new(Tag) do
|
|
||||||
def render(*)
|
|
||||||
'hello'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
with_custom_tag('blabla', klass1) do
|
|
||||||
template = Liquid::Template.parse("{% blabla %}")
|
|
||||||
|
|
||||||
assert_equal 'hello', template.render
|
|
||||||
|
|
||||||
buf = +''
|
|
||||||
output = template.render({}, output: buf)
|
|
||||||
assert_equal 'hello', output
|
|
||||||
assert_equal 'hello', buf
|
|
||||||
assert_equal buf.object_id, output.object_id
|
|
||||||
end
|
|
||||||
|
|
||||||
klass2 = Class.new(klass1) do
|
|
||||||
def render(*)
|
|
||||||
'foo' + super + 'bar'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
with_custom_tag('blabla', klass2) do
|
|
||||||
template = Liquid::Template.parse("{% blabla %}")
|
|
||||||
|
|
||||||
assert_equal 'foohellobar', template.render
|
|
||||||
|
|
||||||
buf = +''
|
|
||||||
output = template.render({}, output: buf)
|
|
||||||
assert_equal 'foohellobar', output
|
|
||||||
assert_equal 'foohellobar', buf
|
|
||||||
assert_equal buf.object_id, output.object_id
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user