mirror of
https://github.com/kemko/liquid.git
synced 2026-01-02 00:05:42 +03:00
Compare commits
81 Commits
3-0-stable
...
formatter
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f5ba7ae4b2 | ||
|
|
9e666d7528 | ||
|
|
5ee8a32cb8 | ||
|
|
d2cbc5610e | ||
|
|
4a17d1a4ad | ||
|
|
8a8996387b | ||
|
|
9310640bdd | ||
|
|
4c3381a523 | ||
|
|
261aa2e726 | ||
|
|
247c51ac70 | ||
|
|
37dbec3610 | ||
|
|
ff253a04c6 | ||
|
|
25ef0df671 | ||
|
|
32460c255b | ||
|
|
724d625f47 | ||
|
|
f658dcee8b | ||
|
|
fa6cd6287e | ||
|
|
76c24db039 | ||
|
|
068791d698 | ||
|
|
3a082ddbbd | ||
|
|
03b3446119 | ||
|
|
251ce7483c | ||
|
|
4592afcc8b | ||
|
|
448766b0c4 | ||
|
|
6390652c3f | ||
|
|
f266aee2e5 | ||
|
|
df0649a031 | ||
|
|
78a5972487 | ||
|
|
298ae3357c | ||
|
|
f1f3f57647 | ||
|
|
e5dd63e1fc | ||
|
|
881f86d698 | ||
|
|
a1b209d212 | ||
|
|
8e5926669b | ||
|
|
8736b602ea | ||
|
|
b8365af07d | ||
|
|
53842a471e | ||
|
|
86a82d3039 | ||
|
|
2b78e74b4e | ||
|
|
db396dd739 | ||
|
|
3213db54d6 | ||
|
|
97a3f145a1 | ||
|
|
2fbe813770 | ||
|
|
23a23c6419 | ||
|
|
63eb1aac69 | ||
|
|
205bd19d3f | ||
|
|
950f062041 | ||
|
|
3476a556dd | ||
|
|
d2ef9cef10 | ||
|
|
0021c93fef | ||
|
|
dcf7064460 | ||
|
|
bebd3570ee | ||
|
|
7cfee1616a | ||
|
|
4b0a7c5d1d | ||
|
|
5df1a262ad | ||
|
|
84fddba2e1 | ||
|
|
8b0774b519 | ||
|
|
e2f8b28f56 | ||
|
|
3080f95a4f | ||
|
|
cc57908c03 | ||
|
|
4df4f218cf | ||
|
|
c2f71ee86b | ||
|
|
9f7e601110 | ||
|
|
3755031c18 | ||
|
|
b628477af1 | ||
|
|
dd455a6361 | ||
|
|
8c70682d6b | ||
|
|
742b3c69bb | ||
|
|
1593b784a7 | ||
|
|
db00ec8b32 | ||
|
|
3ca40b5dea | ||
|
|
378775992f | ||
|
|
319400ea23 | ||
|
|
289a03f9d7 | ||
|
|
a0710f4c70 | ||
|
|
737be1a0c1 | ||
|
|
1673098126 | ||
|
|
422bafd66a | ||
|
|
c0aab820ed | ||
|
|
3321cffe08 | ||
|
|
f2772518b0 |
10
.travis.yml
10
.travis.yml
@@ -1,13 +1,17 @@
|
||||
language: ruby
|
||||
|
||||
rvm:
|
||||
- 1.9
|
||||
- 2.0
|
||||
- 2.1
|
||||
- jruby-19mode
|
||||
- 2.2
|
||||
- ruby-head
|
||||
- jruby-head
|
||||
- rbx-2
|
||||
|
||||
sudo: false
|
||||
|
||||
matrix:
|
||||
allow_failures:
|
||||
- rvm: rbx-2
|
||||
- rvm: jruby-head
|
||||
|
||||
script: "rake test"
|
||||
|
||||
87
History.md
87
History.md
@@ -1,47 +1,72 @@
|
||||
# Liquid Version History
|
||||
# Liquid Change Log
|
||||
|
||||
## 3.0.0 / not yet released / branch "master"
|
||||
## 4.0.0 / not yet released / branch "master"
|
||||
### Changed
|
||||
* Add sort_natural filter (#554) [Martin Hanzel, arthanzel]
|
||||
* Add forloop.parentloop as a reference to the parent loop (#520) [Justin Li, pushrax]
|
||||
* Block parsing moved to BlockBody class (#458) [Dylan Thacker-Smith, dylanahsmith]
|
||||
* Add concat filter to concatenate arrays (#429) [Diogo Beato, dvbeato]
|
||||
* Ruby 1.9 support dropped (#491) [Justin Li, pushrax]
|
||||
* Liquid::Template.file_system's read_template_file method is no longer passed the context. (#441) [James Reid-Smith, sunblaze]
|
||||
|
||||
### Fixed
|
||||
* Fix naming of the "context variable" when dynamically including a template (#559) [Justin Li, pushrax]
|
||||
* Gracefully accept empty strings in the date filter (#555) [Loren Hale, boobooninja]
|
||||
* Fix capturing into variables with a hyphen in the name (#505) [Florian Weingarten, fw42]
|
||||
* Fix case sensitivity regression in date standard filter (#499) [Kelley Reynolds, kreynolds]
|
||||
* Disallow filters with no variable in strict mode (#475) [Justin Li, pushrax]
|
||||
* Disallow variable names in the strict parser that are not valid in the lax parser (#463) [Justin Li, pushrax]
|
||||
* Fix BlockBody#warnings taking exponential time to compute (#486) [Justin Li, pushrax]
|
||||
|
||||
## 3.0.2 / 2015-04-24 / branch "3-0-stable"
|
||||
|
||||
* Expose VariableLookup private members (#551) [Justin Li, pushrax]
|
||||
* Documentation fixes
|
||||
|
||||
## 3.0.1 / 2015-01-23
|
||||
|
||||
* Remove duplicate `index0` key in TableRow tag (#502) [Alfred Xing]
|
||||
|
||||
## 3.0.0 / 2014-11-12
|
||||
|
||||
* ...
|
||||
* Block parsing moved to BlockBody class, see #458 [Dylan Thacker-Smith, dylanahsmith]
|
||||
* Removed Block#end_tag. Instead, override parse with `super` followed by your code. See #446 [Dylan Thacker-Smith, dylanahsmith]
|
||||
* Fixed condition with wrong data types, see #423 [Bogdan Gusiev]
|
||||
* Add url_encode to standard filters, see #421 [Derrick Reimer, djreimer]
|
||||
* Fixed condition with wrong data types (#423) [Bogdan Gusiev]
|
||||
* Add url_encode to standard filters (#421) [Derrick Reimer, djreimer]
|
||||
* Add uniq to standard filters [Florian Weingarten, fw42]
|
||||
* Add exception_handler feature, see #397 and #254 [Bogdan Gusiev, bogdan and Florian Weingarten, fw42]
|
||||
* Add exception_handler feature (#397) and #254 [Bogdan Gusiev, bogdan and Florian Weingarten, fw42]
|
||||
* Optimize variable parsing to avoid repeated regex evaluation during template rendering #383 [Jason Hiltz-Laforge, jasonhl]
|
||||
* Optimize checking for block interrupts to reduce object allocation #380 [Jason Hiltz-Laforge, jasonhl]
|
||||
* Properly set context rethrow_errors on render! #349 [Thierry Joyal, tjoyal]
|
||||
* Fix broken rendering of variables which are equal to false, see #345 [Florian Weingarten, fw42]
|
||||
* Fix broken rendering of variables which are equal to false (#345) [Florian Weingarten, fw42]
|
||||
* Remove ActionView template handler [Dylan Thacker-Smith, dylanahsmith]
|
||||
* Freeze lots of string literals for new Ruby 2.1 optimization, see #297 [Florian Weingarten, fw42]
|
||||
* Allow newlines in tags and variables, see #324 [Dylan Thacker-Smith, dylanahsmith]
|
||||
* Freeze lots of string literals for new Ruby 2.1 optimization (#297) [Florian Weingarten, fw42]
|
||||
* Allow newlines in tags and variables (#324) [Dylan Thacker-Smith, dylanahsmith]
|
||||
* Tag#parse is called after initialize, which now takes options instead of tokens as the 3rd argument. See #321 [Dylan Thacker-Smith, dylanahsmith]
|
||||
* Raise `Liquid::ArgumentError` instead of `::ArgumentError` when filter has wrong number of arguments #309 [Bogdan Gusiev, bogdan]
|
||||
* Add a to_s default for liquid drops, see #306 [Adam Doeler, releod]
|
||||
* Add a to_s default for liquid drops (#306) [Adam Doeler, releod]
|
||||
* Add strip, lstrip, and rstrip to standard filters [Florian Weingarten, fw42]
|
||||
* Make if, for & case tags return complete and consistent nodelists, see #250 [Nick Jones, dntj]
|
||||
* Prevent arbitrary method invocation on condition objects, see #274 [Dylan Thacker-Smith, dylanahsmith]
|
||||
* Don't call to_sym when creating conditions for security reasons, see #273 [Bouke van der Bijl, bouk]
|
||||
* Fix resource counting bug with respond_to?(:length), see #263 [Florian Weingarten, fw42]
|
||||
* Allow specifying custom patterns for template filenames, see #284 [Andrei Gladkyi, agladkyi]
|
||||
* Allow drops to optimize loading a slice of elements, see #282 [Tom Burns, boourns]
|
||||
* Support for passing variables to snippets in subdirs, see #271 [Joost Hietbrink, joost]
|
||||
* Add a class cache to avoid runtime extend calls, see #249 [James Tucker, raggi]
|
||||
* Remove some legacy Ruby 1.8 compatibility code, see #276 [Florian Weingarten, fw42]
|
||||
* Add default filter to standard filters, see #267 [Derrick Reimer, djreimer]
|
||||
* Add optional strict parsing and warn parsing, see #235 [Tristan Hume, trishume]
|
||||
* Add I18n syntax error translation, see #241 [Simon Hørup Eskildsen, Sirupsen]
|
||||
* Make sort filter work on enumerable drops, see #239 [Florian Weingarten, fw42]
|
||||
* Fix clashing method names in enumerable drops, see #238 [Florian Weingarten, fw42]
|
||||
* Make map filter work on enumerable drops, see #233 [Florian Weingarten, fw42]
|
||||
* Make if, for & case tags return complete and consistent nodelists (#250) [Nick Jones, dntj]
|
||||
* Prevent arbitrary method invocation on condition objects (#274) [Dylan Thacker-Smith, dylanahsmith]
|
||||
* Don't call to_sym when creating conditions for security reasons (#273) [Bouke van der Bijl, bouk]
|
||||
* Fix resource counting bug with respond_to?(:length) (#263) [Florian Weingarten, fw42]
|
||||
* Allow specifying custom patterns for template filenames (#284) [Andrei Gladkyi, agladkyi]
|
||||
* Allow drops to optimize loading a slice of elements (#282) [Tom Burns, boourns]
|
||||
* Support for passing variables to snippets in subdirs (#271) [Joost Hietbrink, joost]
|
||||
* Add a class cache to avoid runtime extend calls (#249) [James Tucker, raggi]
|
||||
* Remove some legacy Ruby 1.8 compatibility code (#276) [Florian Weingarten, fw42]
|
||||
* Add default filter to standard filters (#267) [Derrick Reimer, djreimer]
|
||||
* Add optional strict parsing and warn parsing (#235) [Tristan Hume, trishume]
|
||||
* Add I18n syntax error translation (#241) [Simon Hørup Eskildsen, Sirupsen]
|
||||
* Make sort filter work on enumerable drops (#239) [Florian Weingarten, fw42]
|
||||
* Fix clashing method names in enumerable drops (#238) [Florian Weingarten, fw42]
|
||||
* Make map filter work on enumerable drops (#233) [Florian Weingarten, fw42]
|
||||
* Improved whitespace stripping for blank blocks, related to #216 [Florian Weingarten, fw42]
|
||||
|
||||
## 2.6.1 / 2014-01-10 / branch "2-6-stable"
|
||||
|
||||
Security fix, cherry-picked from master (4e14a65):
|
||||
* Don't call to_sym when creating conditions for security reasons, see #273 [Bouke van der Bijl, bouk]
|
||||
* Prevent arbitrary method invocation on condition objects, see #274 [Dylan Thacker-Smith, dylanahsmith]
|
||||
* Don't call to_sym when creating conditions for security reasons (#273) [Bouke van der Bijl, bouk]
|
||||
* Prevent arbitrary method invocation on condition objects (#274) [Dylan Thacker-Smith, dylanahsmith]
|
||||
|
||||
## 2.6.0 / 2013-11-25
|
||||
|
||||
@@ -70,12 +95,12 @@ The following releases will only be tested against Ruby 1.9 and Ruby 2.0 and are
|
||||
## 2.5.5 / 2014-01-10 / branch "2-5-stable"
|
||||
|
||||
Security fix, cherry-picked from master (4e14a65):
|
||||
* Don't call to_sym when creating conditions for security reasons, see #273 [Bouke van der Bijl, bouk]
|
||||
* Prevent arbitrary method invocation on condition objects, see #274 [Dylan Thacker-Smith, dylanahsmith]
|
||||
* Don't call to_sym when creating conditions for security reasons (#273) [Bouke van der Bijl, bouk]
|
||||
* Prevent arbitrary method invocation on condition objects (#274) [Dylan Thacker-Smith, dylanahsmith]
|
||||
|
||||
## 2.5.4 / 2013-11-11
|
||||
|
||||
* Fix "can't convert Fixnum into String" for "replace", see #173, [wǒ_is神仙, jsw0528]
|
||||
* Fix "can't convert Fixnum into String" for "replace" (#173), [wǒ_is神仙, jsw0528]
|
||||
|
||||
## 2.5.3 / 2013-10-09
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ class Servlet < LiquidServlet
|
||||
end
|
||||
|
||||
def products
|
||||
{ 'products' => products_list, 'description' => description, 'section' => 'Snowboards', 'cool_products' => true}
|
||||
{ 'products' => products_list, 'more_products' => more_products_list, 'description' => description, 'section' => 'Snowboards', 'cool_products' => true}
|
||||
end
|
||||
|
||||
private
|
||||
@@ -34,6 +34,11 @@ class Servlet < LiquidServlet
|
||||
{'name' => 'Arbor Diamond', 'price' => 59900, 'description' => 'the *arbor diamond* is a made up product because im obsessed with arbor and have no creativity'}]
|
||||
end
|
||||
|
||||
def more_products_list
|
||||
[{'name' => 'Arbor Catalyst', 'price' => 39900, 'description' => 'the *arbor catalyst* is an advanced drop-through for freestyle and flatground performance and versatility' },
|
||||
{'name' => 'Arbor Fish', 'price' => 40000, 'description' => 'the *arbor fish* is a compact pin that features an extended wheelbase and time-honored teardrop shape'}]
|
||||
end
|
||||
|
||||
def description
|
||||
"List of Products ~ This is a list of products with price and description."
|
||||
end
|
||||
|
||||
@@ -16,12 +16,12 @@
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
{% assign all_products = products | concat: more_products %}
|
||||
<h1>{{ description | split: '~' | first }}</h1>
|
||||
|
||||
<h2>{{ description | split: '~' | last }}</h2>
|
||||
|
||||
<h2>There are currently {{products | count}} products in the {{section}} catalog</h2>
|
||||
<h2>There are currently {{all_products | count}} products in the {{section}} catalog</h2>
|
||||
|
||||
{% if cool_products %}
|
||||
Cool products :)
|
||||
@@ -31,7 +31,7 @@
|
||||
|
||||
<ul id="products">
|
||||
|
||||
{% for product in products %}
|
||||
{% for product in all_products %}
|
||||
<li>
|
||||
<h2>{{product.name}}</h2>
|
||||
Only {{product.price | price }}
|
||||
|
||||
@@ -63,6 +63,7 @@ require 'liquid/variable'
|
||||
require 'liquid/variable_lookup'
|
||||
require 'liquid/range_lookup'
|
||||
require 'liquid/file_system'
|
||||
require 'liquid/resource_limits'
|
||||
require 'liquid/template'
|
||||
require 'liquid/standardfilters'
|
||||
require 'liquid/condition'
|
||||
@@ -73,6 +74,3 @@ require 'liquid/token'
|
||||
# Load all the tags of the standard library
|
||||
#
|
||||
Dir[File.dirname(__FILE__) + '/liquid/tags/*.rb'].each { |f| require f }
|
||||
|
||||
require 'liquid/profiler'
|
||||
require 'liquid/profiler/hooks'
|
||||
|
||||
@@ -57,6 +57,10 @@ module Liquid
|
||||
@block_delimiter ||= "end#{block_name}"
|
||||
end
|
||||
|
||||
def format
|
||||
"{% #{raw} %}#{@body.format}{% #{block_delimiter} %}"
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def parse_body(body, tokens)
|
||||
|
||||
@@ -62,15 +62,14 @@ module Liquid
|
||||
def warnings
|
||||
all_warnings = []
|
||||
nodelist.each do |node|
|
||||
all_warnings.concat(node.warnings) if node.respond_to?(:warnings) && node.warnings
|
||||
all_warnings.concat(node.warnings || []) if node.respond_to?(:warnings)
|
||||
end
|
||||
all_warnings
|
||||
end
|
||||
|
||||
def render(context)
|
||||
output = []
|
||||
context.resource_limits[:render_length_current] = 0
|
||||
context.resource_limits[:render_score_current] += @nodelist.length
|
||||
context.resource_limits.render_score += @nodelist.length
|
||||
|
||||
@nodelist.each do |token|
|
||||
# Break out if we have any unhanded interrupts.
|
||||
@@ -100,16 +99,29 @@ module Liquid
|
||||
output.join
|
||||
end
|
||||
|
||||
def format
|
||||
@nodelist.map do |node|
|
||||
if node.is_a?(Variable)
|
||||
"{{ #{node.format} }}"
|
||||
elsif node.is_a?(Tag)
|
||||
node.format
|
||||
else
|
||||
node
|
||||
end
|
||||
end.join
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def render_token(token, context)
|
||||
token_output = (token.respond_to?(:render) ? token.render(context) : token)
|
||||
context.increment_used_resources(:render_length_current, token_output)
|
||||
if context.resource_limits_reached?
|
||||
context.resource_limits[:reached] = true
|
||||
token_str = token_output.is_a?(Array) ? token_output.join : token_output.to_s
|
||||
|
||||
context.resource_limits.render_length += token_str.length
|
||||
if context.resource_limits.reached?
|
||||
raise MemoryError.new("Memory limits exceeded".freeze)
|
||||
end
|
||||
token_output
|
||||
token_str
|
||||
end
|
||||
|
||||
def create_variable(token, options)
|
||||
|
||||
@@ -33,6 +33,12 @@ module Liquid
|
||||
@right = right
|
||||
@child_relation = nil
|
||||
@child_condition = nil
|
||||
|
||||
if right && right.is_a?(VariableLookup) && right.name.nil?
|
||||
# Strip blank variable lookups resulting from cases like:
|
||||
# {% if foo && bar %}
|
||||
@right = nil
|
||||
end
|
||||
end
|
||||
|
||||
def evaluate(context = Context.new)
|
||||
@@ -70,6 +76,20 @@ module Liquid
|
||||
"#<Condition #{[@left, @operator, @right].compact.join(' '.freeze)}>"
|
||||
end
|
||||
|
||||
def format
|
||||
out = if operator.nil?
|
||||
Expression.format(left)
|
||||
else
|
||||
"#{Expression.format(left)} #{operator} #{Expression.format(right)}"
|
||||
end
|
||||
|
||||
if @child_relation
|
||||
"#{out} #{@child_relation.to_s} #{@child_condition.format}"
|
||||
else
|
||||
out
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def equal_variables(left, right)
|
||||
|
||||
@@ -21,9 +21,7 @@ module Liquid
|
||||
@scopes = [(outer_scope || {})]
|
||||
@registers = registers
|
||||
@errors = []
|
||||
@resource_limits = resource_limits || Template.default_resource_limits.dup
|
||||
@resource_limits[:render_score_current] = 0
|
||||
@resource_limits[:assign_score_current] = 0
|
||||
@resource_limits = resource_limits || ResourceLimits.new(Template.default_resource_limits)
|
||||
squash_instance_assigns_with_environments
|
||||
|
||||
@this_stack_used = false
|
||||
@@ -36,20 +34,6 @@ module Liquid
|
||||
@filters = []
|
||||
end
|
||||
|
||||
def increment_used_resources(key, obj)
|
||||
@resource_limits[key] += if obj.kind_of?(String) || obj.kind_of?(Array) || obj.kind_of?(Hash)
|
||||
obj.length
|
||||
else
|
||||
1
|
||||
end
|
||||
end
|
||||
|
||||
def resource_limits_reached?
|
||||
(@resource_limits[:render_length_limit] && @resource_limits[:render_length_current] > @resource_limits[:render_length_limit]) ||
|
||||
(@resource_limits[:render_score_limit] && @resource_limits[:render_score_current] > @resource_limits[:render_score_limit] ) ||
|
||||
(@resource_limits[:assign_score_limit] && @resource_limits[:assign_score_current] > @resource_limits[:assign_score_limit] )
|
||||
end
|
||||
|
||||
def strainer
|
||||
@strainer ||= Strainer.create(self, @filters)
|
||||
end
|
||||
@@ -60,21 +44,8 @@ module Liquid
|
||||
# for that
|
||||
def add_filters(filters)
|
||||
filters = [filters].flatten.compact
|
||||
filters.each do |f|
|
||||
raise ArgumentError, "Expected module but got: #{f.class}" unless f.is_a?(Module)
|
||||
Strainer.add_known_filter(f)
|
||||
end
|
||||
|
||||
# If strainer is already setup then there's no choice but to use a runtime
|
||||
# extend call. If strainer is not yet created, we can utilize strainers
|
||||
# cached class based API, which avoids busting the method cache.
|
||||
if @strainer
|
||||
filters.each do |f|
|
||||
strainer.extend(f)
|
||||
end
|
||||
else
|
||||
@filters.concat filters
|
||||
end
|
||||
@filters += filters
|
||||
@strainer = nil
|
||||
end
|
||||
|
||||
# are there any not handled interrupts?
|
||||
|
||||
@@ -62,6 +62,10 @@ module Liquid
|
||||
|
||||
# Check for method existence without invoking respond_to?, which creates symbols
|
||||
def self.invokable?(method_name)
|
||||
self.invokable_methods.include?(method_name.to_s)
|
||||
end
|
||||
|
||||
def self.invokable_methods
|
||||
unless @invokable_methods
|
||||
blacklist = Liquid::Drop.public_instance_methods + [:each]
|
||||
if include?(Enumerable)
|
||||
@@ -71,7 +75,7 @@ module Liquid
|
||||
whitelist = [:to_liquid] + (public_instance_methods - blacklist)
|
||||
@invokable_methods = Set.new(whitelist.map(&:to_s))
|
||||
end
|
||||
@invokable_methods.include?(method_name.to_s)
|
||||
@invokable_methods
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -8,6 +8,8 @@ module Liquid
|
||||
'empty'.freeze => :empty?
|
||||
}
|
||||
|
||||
INVERTED_LITERALS = LITERALS.invert
|
||||
|
||||
def self.parse(markup)
|
||||
if LITERALS.key?(markup)
|
||||
LITERALS[markup]
|
||||
@@ -29,5 +31,18 @@ module Liquid
|
||||
end
|
||||
end
|
||||
|
||||
def self.format(value)
|
||||
if INVERTED_LITERALS.key?(value)
|
||||
INVERTED_LITERALS[value].dup
|
||||
elsif value.is_a?(VariableLookup) || value.is_a?(RangeLookup)
|
||||
value.format
|
||||
elsif value.is_a?(String)
|
||||
"\"#{value}\""
|
||||
elsif value.is_a?(Range)
|
||||
"(#{value.to_s})"
|
||||
else
|
||||
value.to_s
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -14,7 +14,7 @@ module Liquid
|
||||
# This will parse the template with a LocalFileSystem implementation rooted at 'template_path'.
|
||||
class BlankFileSystem
|
||||
# Called by Liquid to retrieve a template file
|
||||
def read_template_file(template_path, context)
|
||||
def read_template_file(template_path)
|
||||
raise FileSystemError, "This liquid context does not allow includes."
|
||||
end
|
||||
end
|
||||
@@ -49,7 +49,7 @@ module Liquid
|
||||
@pattern = pattern
|
||||
end
|
||||
|
||||
def read_template_file(template_path, context)
|
||||
def read_template_file(template_path)
|
||||
full_path = full_path(template_path)
|
||||
raise FileSystemError, "No such template '#{template_path}'" unless File.exists?(full_path)
|
||||
|
||||
|
||||
@@ -7,16 +7,17 @@
|
||||
# to the allowed method passed with the liquid_methods call
|
||||
# Example:
|
||||
#
|
||||
# class SomeClass
|
||||
# liquid_methods :an_allowed_method
|
||||
# class SomeClass
|
||||
# liquid_methods :an_allowed_method
|
||||
#
|
||||
# def an_allowed_method
|
||||
# 'this comes from an allowed method'
|
||||
# def an_allowed_method
|
||||
# 'this comes from an allowed method'
|
||||
# end
|
||||
#
|
||||
# def unallowed_method
|
||||
# 'this will never be an output'
|
||||
# end
|
||||
# end
|
||||
# def unallowed_method
|
||||
# 'this will never be an output'
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# if you want to extend the drop to other methods you can defines more methods
|
||||
# in the class <YourClass>::LiquidDropClass
|
||||
@@ -26,31 +27,33 @@
|
||||
# 'and this from another allowed method'
|
||||
# end
|
||||
# end
|
||||
# end
|
||||
#
|
||||
#
|
||||
# usage:
|
||||
# @something = SomeClass.new
|
||||
# @something = SomeClass.new
|
||||
#
|
||||
# template:
|
||||
# {{something.an_allowed_method}}{{something.unallowed_method}} {{something.another_allowed_method}}
|
||||
# {{something.an_allowed_method}}{{something.unallowed_method}} {{something.another_allowed_method}}
|
||||
#
|
||||
# output:
|
||||
# 'this comes from an allowed method and this from another allowed method'
|
||||
# 'this comes from an allowed method and this from another allowed method'
|
||||
#
|
||||
# You can also chain associations, by adding the liquid_method call in the
|
||||
# association models.
|
||||
#
|
||||
class Module
|
||||
|
||||
def liquid_methods(*allowed_methods)
|
||||
drop_class = eval "class #{self.to_s}::LiquidDropClass < Liquid::Drop; self; end"
|
||||
|
||||
define_method :to_liquid do
|
||||
drop_class.new(self)
|
||||
end
|
||||
|
||||
drop_class.class_eval do
|
||||
def initialize(object)
|
||||
@object = object
|
||||
end
|
||||
|
||||
allowed_methods.each do |sym|
|
||||
define_method sym do
|
||||
@object.send sym
|
||||
@@ -58,5 +61,4 @@ class Module
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
require 'liquid/profiler/hooks'
|
||||
|
||||
module Liquid
|
||||
|
||||
# Profiler enables support for profiling template rendering to help track down performance issues.
|
||||
#
|
||||
# To enable profiling, pass the <tt>profile: true</tt> option to <tt>Liquid::Template.parse</tt>. Then, after
|
||||
# <tt>Liquid::Template#render</tt> is called, the template object makes available an instance of this
|
||||
# To enable profiling, first require 'liquid/profiler'.
|
||||
# Then, to profile a parse/render cycle, pass the <tt>profile: true</tt> option to <tt>Liquid::Template.parse</tt>.
|
||||
# After <tt>Liquid::Template#render</tt> is called, the template object makes available an instance of this
|
||||
# class via the <tt>Liquid::Template#profiler</tt> method.
|
||||
#
|
||||
# template = Liquid::Template.parse(template_content, profile: true)
|
||||
|
||||
@@ -12,7 +12,7 @@ module Liquid
|
||||
|
||||
class Include < Tag
|
||||
def render_with_profiling(context)
|
||||
Profiler.profile_children(context.evaluate(@template_name).to_s) do
|
||||
Profiler.profile_children(context.evaluate(@template_name_expr).to_s) do
|
||||
render_without_profiling(context)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -18,5 +18,9 @@ module Liquid
|
||||
def evaluate(context)
|
||||
context.evaluate(@start_obj).to_i..context.evaluate(@end_obj).to_i
|
||||
end
|
||||
|
||||
def format
|
||||
"(#{Expression.format(@start_obj)}..#{Expression.format(@end_obj)})"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
23
lib/liquid/resource_limits.rb
Normal file
23
lib/liquid/resource_limits.rb
Normal file
@@ -0,0 +1,23 @@
|
||||
module Liquid
|
||||
class ResourceLimits
|
||||
attr_accessor :render_length, :render_score, :assign_score,
|
||||
:render_length_limit, :render_score_limit, :assign_score_limit
|
||||
|
||||
def initialize(limits)
|
||||
@render_length_limit = limits[:render_length_limit]
|
||||
@render_score_limit = limits[:render_score_limit]
|
||||
@assign_score_limit = limits[:assign_score_limit]
|
||||
reset
|
||||
end
|
||||
|
||||
def reached?
|
||||
(@render_length_limit && @render_length > @render_length_limit) ||
|
||||
(@render_score_limit && @render_score > @render_score_limit ) ||
|
||||
(@assign_score_limit && @assign_score > @assign_score_limit )
|
||||
end
|
||||
|
||||
def reset
|
||||
@render_length = @render_score = @assign_score = 0
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -122,6 +122,20 @@ module Liquid
|
||||
end
|
||||
end
|
||||
|
||||
# Sort elements of an array ignoring case if strings
|
||||
# provide optional property with which to sort an array of hashes or drops
|
||||
def sort_natural(input, property = nil)
|
||||
ary = InputIterator.new(input)
|
||||
|
||||
if property.nil?
|
||||
ary.sort {|a,b| a.casecmp(b) }
|
||||
elsif ary.first.respond_to?(:[]) && !ary.first[property].nil?
|
||||
ary.sort {|a,b| a[property].casecmp(b[property]) }
|
||||
elsif ary.first.respond_to?(property)
|
||||
ary.sort {|a,b| a.send(property).casecmp(b.send(property)) }
|
||||
end
|
||||
end
|
||||
|
||||
# Remove duplicate elements from an array
|
||||
# provide optional property with which to determine uniqueness
|
||||
def uniq(input, property = nil)
|
||||
@@ -177,6 +191,10 @@ module Liquid
|
||||
input.to_s + string.to_s
|
||||
end
|
||||
|
||||
def concat(input, array)
|
||||
InputIterator.new(input).concat(array)
|
||||
end
|
||||
|
||||
# prepend a string to another
|
||||
def prepend(input, string)
|
||||
string.to_s + input.to_s
|
||||
@@ -306,6 +324,11 @@ module Liquid
|
||||
def to_date(obj)
|
||||
return obj if obj.respond_to?(:strftime)
|
||||
|
||||
if obj.is_a?(String)
|
||||
return nil if obj.empty?
|
||||
obj = obj.downcase
|
||||
end
|
||||
|
||||
case obj
|
||||
when 'now'.freeze, 'today'.freeze
|
||||
Time.now
|
||||
@@ -344,6 +367,10 @@ module Liquid
|
||||
to_a.join(glue)
|
||||
end
|
||||
|
||||
def concat(args)
|
||||
to_a.concat args
|
||||
end
|
||||
|
||||
def reverse
|
||||
reverse_each.to_a
|
||||
end
|
||||
|
||||
@@ -8,12 +8,13 @@ module Liquid
|
||||
# The Strainer only allows method calls defined in filters given to it via Strainer.global_filter,
|
||||
# Context#add_filters or Template.register_filter
|
||||
class Strainer #:nodoc:
|
||||
@@filters = []
|
||||
@@known_filters = Set.new
|
||||
@@known_methods = Set.new
|
||||
@@global_strainer = Class.new(Strainer) do
|
||||
@filter_methods = Set.new
|
||||
end
|
||||
@@strainer_class_cache = Hash.new do |hash, filters|
|
||||
hash[filters] = Class.new(Strainer) do
|
||||
filters.each { |f| include f }
|
||||
hash[filters] = Class.new(@@global_strainer) do
|
||||
@filter_methods = @@global_strainer.filter_methods.dup
|
||||
filters.each { |f| add_filter(f) }
|
||||
end
|
||||
end
|
||||
|
||||
@@ -21,33 +22,32 @@ module Liquid
|
||||
@context = context
|
||||
end
|
||||
|
||||
def self.global_filter(filter)
|
||||
raise ArgumentError, "Passed filter is not a module" unless filter.is_a?(Module)
|
||||
add_known_filter(filter)
|
||||
@@filters << filter unless @@filters.include?(filter)
|
||||
def self.filter_methods
|
||||
@filter_methods
|
||||
end
|
||||
|
||||
def self.add_known_filter(filter)
|
||||
unless @@known_filters.include?(filter)
|
||||
@@method_blacklist ||= Set.new(Strainer.instance_methods.map(&:to_s))
|
||||
new_methods = filter.instance_methods.map(&:to_s)
|
||||
new_methods.reject!{ |m| @@method_blacklist.include?(m) }
|
||||
@@known_methods.merge(new_methods)
|
||||
@@known_filters.add(filter)
|
||||
def self.add_filter(filter)
|
||||
raise ArgumentError, "Expected module but got: #{f.class}" unless filter.is_a?(Module)
|
||||
unless self.class.include?(filter)
|
||||
self.send(:include, filter)
|
||||
@filter_methods.merge(filter.public_instance_methods.map(&:to_s))
|
||||
end
|
||||
end
|
||||
|
||||
def self.strainer_class_cache
|
||||
@@strainer_class_cache
|
||||
def self.global_filter(filter)
|
||||
@@global_strainer.add_filter(filter)
|
||||
end
|
||||
|
||||
def self.invokable?(method)
|
||||
@filter_methods.include?(method.to_s)
|
||||
end
|
||||
|
||||
def self.create(context, filters = [])
|
||||
filters = @@filters + filters
|
||||
strainer_class_cache[filters].new(context)
|
||||
@@strainer_class_cache[filters].new(context)
|
||||
end
|
||||
|
||||
def invoke(method, *args)
|
||||
if invokable?(method)
|
||||
if self.class.invokable?(method)
|
||||
send(method, *args)
|
||||
else
|
||||
args.first
|
||||
@@ -55,9 +55,5 @@ module Liquid
|
||||
rescue ::ArgumentError => e
|
||||
raise Liquid::ArgumentError.new(e.message)
|
||||
end
|
||||
|
||||
def invokable?(method)
|
||||
@@known_methods.include?(method.to_s) && respond_to?(method)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -24,7 +24,10 @@ module Liquid
|
||||
end
|
||||
|
||||
def raw
|
||||
"#{@tag_name} #{@markup}"
|
||||
tag = @tag_name.strip
|
||||
markup = @markup.strip
|
||||
tag << " #{markup}" unless markup.empty?
|
||||
tag
|
||||
end
|
||||
|
||||
def name
|
||||
@@ -38,5 +41,9 @@ module Liquid
|
||||
def blank?
|
||||
false
|
||||
end
|
||||
|
||||
def format
|
||||
"{% #{raw} %}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -25,13 +25,20 @@ module Liquid
|
||||
def render(context)
|
||||
val = @from.render(context)
|
||||
context.scopes.last[@to] = val
|
||||
context.increment_used_resources(:assign_score_current, val)
|
||||
|
||||
inc = val.instance_of?(String) || val.instance_of?(Array) || val.instance_of?(Hash) ? val.length : 1
|
||||
context.resource_limits.assign_score += inc
|
||||
|
||||
''.freeze
|
||||
end
|
||||
|
||||
def blank?
|
||||
true
|
||||
end
|
||||
|
||||
def format
|
||||
"{% assign #{@to} = #{@from.format} %}"
|
||||
end
|
||||
end
|
||||
|
||||
Template.register_tag('assign'.freeze, Assign)
|
||||
|
||||
@@ -11,7 +11,7 @@ module Liquid
|
||||
# in a sidebar or footer.
|
||||
#
|
||||
class Capture < Block
|
||||
Syntax = /(\w+)/
|
||||
Syntax = /(#{VariableSignature}+)/o
|
||||
|
||||
def initialize(tag_name, markup, options)
|
||||
super
|
||||
@@ -25,13 +25,17 @@ module Liquid
|
||||
def render(context)
|
||||
output = super
|
||||
context.scopes.last[@to] = output
|
||||
context.increment_used_resources(:assign_score_current, output)
|
||||
context.resource_limits.assign_score += output.length
|
||||
''.freeze
|
||||
end
|
||||
|
||||
def blank?
|
||||
true
|
||||
end
|
||||
|
||||
def format
|
||||
"{% #{block_name} #{@to} %}#{@body.format}{% #{block_delimiter} %}"
|
||||
end
|
||||
end
|
||||
|
||||
Template.register_tag('capture'.freeze, Capture)
|
||||
|
||||
@@ -15,7 +15,7 @@ module Liquid
|
||||
end
|
||||
|
||||
def parse(tokens)
|
||||
body = BlockBody.new
|
||||
@initial_body = body = BlockBody.new
|
||||
while more = parse_body(body, tokens)
|
||||
body = @blocks.last.attachment
|
||||
end
|
||||
@@ -53,6 +53,21 @@ module Liquid
|
||||
end
|
||||
end
|
||||
|
||||
def format
|
||||
out = "{% case #{Expression.format(@left)} %}#{@initial_body.format}"
|
||||
|
||||
@blocks.each do |block|
|
||||
if block.else?
|
||||
out << "{% else %}"
|
||||
else
|
||||
out << "{% when #{Expression.format(block.right)} %}"
|
||||
end
|
||||
out << block.attachment.format
|
||||
end
|
||||
|
||||
out + "{% endcase %}"
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def record_when_condition(markup)
|
||||
|
||||
@@ -21,6 +21,7 @@ module Liquid
|
||||
when NamedSyntax
|
||||
@variables = variables_from_string($2)
|
||||
@name = Expression.parse($1)
|
||||
@named = true
|
||||
when SimpleSyntax
|
||||
@variables = variables_from_string(markup)
|
||||
@name = @variables.to_s
|
||||
@@ -43,6 +44,15 @@ module Liquid
|
||||
end
|
||||
end
|
||||
|
||||
def format
|
||||
formatted_vars = @variables.map { |v| Expression.format(v) }.join(', ')
|
||||
if @named
|
||||
"{% cycle #{Expression.format(@name)}: #{formatted_vars} %}"
|
||||
else
|
||||
"{% cycle #{formatted_vars} %}"
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def variables_from_string(markup)
|
||||
|
||||
@@ -42,6 +42,7 @@ module Liquid
|
||||
# where 0 is the last item.
|
||||
# forloop.first:: Returns true if the item is the first item.
|
||||
# forloop.last:: Returns true if the item is the last item.
|
||||
# forloop.parentloop:: Provides access to the parent loop, if present.
|
||||
#
|
||||
class For < Block
|
||||
Syntax = /\A(#{VariableSegment}+)\s+in\s+(#{QuotedFragment}+)\s*(reversed)?/o
|
||||
@@ -98,18 +99,21 @@ module Liquid
|
||||
# Store our progress through the collection for the continue flag
|
||||
context.registers[:for][@name] = from + segment.length
|
||||
|
||||
parent_loop = context['forloop'.freeze]
|
||||
|
||||
context.stack do
|
||||
segment.each_with_index do |item, index|
|
||||
context[@variable_name] = item
|
||||
context['forloop'.freeze] = {
|
||||
'name'.freeze => @name,
|
||||
'length'.freeze => length,
|
||||
'index'.freeze => index + 1,
|
||||
'index0'.freeze => index,
|
||||
'rindex'.freeze => length - index,
|
||||
'rindex0'.freeze => length - index - 1,
|
||||
'first'.freeze => (index == 0),
|
||||
'last'.freeze => (index == length - 1)
|
||||
'name'.freeze => @name,
|
||||
'length'.freeze => length,
|
||||
'index'.freeze => index + 1,
|
||||
'index0'.freeze => index,
|
||||
'rindex'.freeze => length - index,
|
||||
'rindex0'.freeze => length - index - 1,
|
||||
'first'.freeze => (index == 0),
|
||||
'last'.freeze => (index == length - 1),
|
||||
'parentloop'.freeze => parent_loop
|
||||
}
|
||||
|
||||
result << @for_block.render(context)
|
||||
@@ -125,6 +129,21 @@ module Liquid
|
||||
result
|
||||
end
|
||||
|
||||
def format
|
||||
args = [Expression.format(@collection_name)]
|
||||
args << "reversed" if @reversed
|
||||
args << "limit: #{Expression.format(@limit)}" if @limit
|
||||
args << "offset: " << (@from == :continue ? 'continue' : Expression.format(@from)) if @from
|
||||
|
||||
out = "{% for #{@variable_name} in #{args.join(' ')} %}"
|
||||
out << @for_block.format
|
||||
if @else_block
|
||||
out << "{% else %}"
|
||||
out << @else_block.format
|
||||
end
|
||||
out + "{% endfor %}"
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def lax_parse(markup)
|
||||
|
||||
@@ -48,6 +48,25 @@ module Liquid
|
||||
end
|
||||
end
|
||||
|
||||
def format
|
||||
out = ""
|
||||
first_condition = true
|
||||
|
||||
@blocks.each do |block|
|
||||
if block.else?
|
||||
out << "{% else %}"
|
||||
elsif first_condition
|
||||
out << "{% #{block_name} #{block.format} %}"
|
||||
first_condition = false
|
||||
else
|
||||
out << "{% elsif #{block.format} %}"
|
||||
end
|
||||
out << block.attachment.format
|
||||
end
|
||||
|
||||
out + "{% #{block_delimiter} %}"
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def push_block(tag, markup)
|
||||
|
||||
@@ -25,10 +25,9 @@ module Liquid
|
||||
template_name = $1
|
||||
variable_name = $3
|
||||
|
||||
@variable_name = Expression.parse(variable_name || template_name[1..-2])
|
||||
@context_variable_name = template_name[1..-2].split('/'.freeze).last
|
||||
@template_name = Expression.parse(template_name)
|
||||
@attributes = {}
|
||||
@variable_name_expr = variable_name ? Expression.parse(variable_name) : nil
|
||||
@template_name_expr = Expression.parse(template_name)
|
||||
@attributes = {}
|
||||
|
||||
markup.scan(TagAttributes) do |key, value|
|
||||
@attributes[key] = Expression.parse(value)
|
||||
@@ -44,7 +43,15 @@ module Liquid
|
||||
|
||||
def render(context)
|
||||
partial = load_cached_partial(context)
|
||||
variable = context.evaluate(@variable_name)
|
||||
|
||||
template_name = context.evaluate(@template_name_expr)
|
||||
context_variable_name = template_name.split('/'.freeze).last
|
||||
|
||||
variable = if @variable_name_expr
|
||||
context.evaluate(@variable_name_expr)
|
||||
else
|
||||
context.find_variable(template_name)
|
||||
end
|
||||
|
||||
context.stack do
|
||||
@attributes.each do |key, value|
|
||||
@@ -53,20 +60,35 @@ module Liquid
|
||||
|
||||
if variable.is_a?(Array)
|
||||
variable.collect do |var|
|
||||
context[@context_variable_name] = var
|
||||
context[context_variable_name] = var
|
||||
partial.render(context)
|
||||
end
|
||||
else
|
||||
context[@context_variable_name] = variable
|
||||
context[context_variable_name] = variable
|
||||
partial.render(context)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def format
|
||||
segments = [@tag_name, Expression.format(@template_name_expr)]
|
||||
|
||||
if @variable_name_expr
|
||||
segments << "with"
|
||||
segments << Expression.format(@variable_name_expr)
|
||||
end
|
||||
|
||||
unless @attributes.empty?
|
||||
segments << @attributes.map { |k, v| "#{k}: #{Expression.format(v)}" }.join(", ")
|
||||
end
|
||||
|
||||
"{% #{segments.join(' ')} %}"
|
||||
end
|
||||
|
||||
private
|
||||
def load_cached_partial(context)
|
||||
cached_partials = context.registers[:cached_partials] || {}
|
||||
template_name = context.evaluate(@template_name)
|
||||
template_name = context.evaluate(@template_name_expr)
|
||||
|
||||
if cached = cached_partials[template_name]
|
||||
return cached
|
||||
@@ -81,15 +103,7 @@ module Liquid
|
||||
def read_template_from_file_system(context)
|
||||
file_system = context.registers[:file_system] || Liquid::Template.file_system
|
||||
|
||||
# make read_template_file call backwards-compatible.
|
||||
case file_system.method(:read_template_file).arity
|
||||
when 1
|
||||
file_system.read_template_file(context.evaluate(@template_name))
|
||||
when 2
|
||||
file_system.read_template_file(context.evaluate(@template_name), context)
|
||||
else
|
||||
raise ArgumentError, "file_system.read_template_file expects two parameters: (template_name, context)"
|
||||
end
|
||||
file_system.read_template_file(context.evaluate(@template_name_expr))
|
||||
end
|
||||
|
||||
def pass_options
|
||||
|
||||
@@ -24,6 +24,10 @@ module Liquid
|
||||
def blank?
|
||||
@body.empty?
|
||||
end
|
||||
|
||||
def format
|
||||
"{% #{block_name} %}#{@body}{% #{block_delimiter} %}"
|
||||
end
|
||||
end
|
||||
|
||||
Template.register_tag('raw'.freeze, Raw)
|
||||
|
||||
@@ -42,7 +42,6 @@ module Liquid
|
||||
'index0'.freeze => index,
|
||||
'col'.freeze => col + 1,
|
||||
'col0'.freeze => col,
|
||||
'index0'.freeze => index,
|
||||
'rindex'.freeze => length - index,
|
||||
'rindex0'.freeze => length - index - 1,
|
||||
'first'.freeze => (index == 0),
|
||||
@@ -67,6 +66,12 @@ module Liquid
|
||||
result << "</tr>\n"
|
||||
result
|
||||
end
|
||||
|
||||
def format
|
||||
tag_main = "#{block_name} #{@variable_name} in #{Expression.format(@collection_name)}"
|
||||
args = @attributes.map { |k, v| "#{k}: #{Expression.format(v)}" }.join(", ")
|
||||
"{% #{tag_main} #{args} %}#{@body.format}{% #{block_delimiter} %}"
|
||||
end
|
||||
end
|
||||
|
||||
Template.register_tag('tablerow'.freeze, TableRow)
|
||||
|
||||
@@ -3,7 +3,7 @@ require File.dirname(__FILE__) + '/if'
|
||||
module Liquid
|
||||
# Unless is a conditional just like 'if' but works on the inverse logic.
|
||||
#
|
||||
# {% unless x < 0 %} x is greater than zero {% end %}
|
||||
# {% unless x < 0 %} x is greater than zero {% endunless %}
|
||||
#
|
||||
class Unless < If
|
||||
def render(context)
|
||||
|
||||
@@ -18,7 +18,9 @@ module Liquid
|
||||
:locale => I18n.new
|
||||
}
|
||||
|
||||
attr_accessor :root, :resource_limits
|
||||
attr_accessor :root
|
||||
attr_reader :resource_limits
|
||||
|
||||
@@file_system = BlankFileSystem.new
|
||||
|
||||
class TagRegistry
|
||||
@@ -110,7 +112,7 @@ module Liquid
|
||||
end
|
||||
|
||||
def initialize
|
||||
@resource_limits = self.class.default_resource_limits.dup
|
||||
@resource_limits = ResourceLimits.new(self.class.default_resource_limits)
|
||||
end
|
||||
|
||||
# Parse source code.
|
||||
@@ -203,6 +205,9 @@ module Liquid
|
||||
context.add_filters(args.pop)
|
||||
end
|
||||
|
||||
# Retrying a render resets resource usage
|
||||
context.resource_limits.reset
|
||||
|
||||
begin
|
||||
# render the nodelist.
|
||||
# for performance reasons we get an array back here. join will make a string out of it.
|
||||
@@ -250,6 +255,8 @@ module Liquid
|
||||
|
||||
def with_profiling
|
||||
if @profiling && !@options[:included]
|
||||
raise "Profiler not loaded, require 'liquid/profiler' first" unless defined?(Liquid::Profiler)
|
||||
|
||||
@profiler = Profiler.new
|
||||
@profiler.start
|
||||
|
||||
|
||||
@@ -28,6 +28,28 @@ module Liquid
|
||||
@markup
|
||||
end
|
||||
|
||||
def format
|
||||
out = Expression.format(@name)
|
||||
|
||||
@filters.each do |filter|
|
||||
out << " | "
|
||||
out << filter[0]
|
||||
|
||||
args = filter[1].map { |arg| Expression.format(arg) }
|
||||
|
||||
if filter.size > 2
|
||||
args += filter[2].map { |key, arg| "#{key}: #{Expression.format(arg)}" }
|
||||
end
|
||||
|
||||
unless args.empty?
|
||||
out << ": "
|
||||
out << args.join(', ')
|
||||
end
|
||||
end
|
||||
|
||||
out
|
||||
end
|
||||
|
||||
def markup_context(markup)
|
||||
"in \"{{#{markup}}}\""
|
||||
end
|
||||
|
||||
@@ -3,6 +3,8 @@ module Liquid
|
||||
SQUARE_BRACKETED = /\A\[(.*)\]\z/m
|
||||
COMMAND_METHODS = ['size'.freeze, 'first'.freeze, 'last'.freeze]
|
||||
|
||||
attr_reader :name, :lookups
|
||||
|
||||
def self.parse(markup)
|
||||
new(markup)
|
||||
end
|
||||
@@ -12,6 +14,7 @@ module Liquid
|
||||
|
||||
name = lookups.shift
|
||||
if name =~ SQUARE_BRACKETED
|
||||
@name_expression = true
|
||||
name = Expression.parse($1)
|
||||
end
|
||||
@name = name
|
||||
@@ -69,6 +72,20 @@ module Liquid
|
||||
self.class == other.class && self.state == other.state
|
||||
end
|
||||
|
||||
def format
|
||||
out = @name_expression ? Expression.format(@name) : @name.dup
|
||||
|
||||
@lookups.each do |lookup|
|
||||
if lookup.is_a?(String) && lookup =~ /^#{VariableSegment}+$/
|
||||
out << ".#{lookup}"
|
||||
else
|
||||
out << "[#{Expression.format(lookup)}]"
|
||||
end
|
||||
end
|
||||
|
||||
out
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def state
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# encoding: utf-8
|
||||
module Liquid
|
||||
VERSION = "3.0.0"
|
||||
VERSION = "4.0.0.alpha"
|
||||
end
|
||||
|
||||
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
|
||||
s.version = Liquid::VERSION
|
||||
s.platform = Gem::Platform::RUBY
|
||||
s.summary = "A secure, non-evaling end user template engine with aesthetic markup."
|
||||
s.authors = ["Tobias Luetke"]
|
||||
s.authors = ["Tobias Lütke"]
|
||||
s.email = ["tobi@leetsoft.com"]
|
||||
s.homepage = "http://www.liquidmarkup.org"
|
||||
s.license = "MIT"
|
||||
|
||||
@@ -17,7 +17,7 @@ class ThemeRunner
|
||||
end
|
||||
|
||||
# Called by Liquid to retrieve a template file
|
||||
def read_template_file(template_path, context)
|
||||
def read_template_file(template_path)
|
||||
File.read(@path + '/' + template_path + '.liquid')
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,6 +3,16 @@ require 'test_helper'
|
||||
class AssignTest < Minitest::Test
|
||||
include Liquid
|
||||
|
||||
def test_assign_with_hyphen_in_variable_name
|
||||
template_source = <<-END_TEMPLATE
|
||||
{% assign this-thing = 'Print this-thing' %}
|
||||
{{ this-thing }}
|
||||
END_TEMPLATE
|
||||
template = Template.parse(template_source)
|
||||
rendered = template.render!
|
||||
assert_equal "Print this-thing", rendered.strip
|
||||
end
|
||||
|
||||
def test_assigned_variable
|
||||
assert_template_result('.foo.',
|
||||
'{% assign foo = values %}.{{ foo[0] }}.',
|
||||
|
||||
@@ -9,7 +9,7 @@ class FoobarTag < Liquid::Tag
|
||||
end
|
||||
|
||||
class BlankTestFileSystem
|
||||
def read_template_file(template_path, context)
|
||||
def read_template_file(template_path)
|
||||
template_path
|
||||
end
|
||||
end
|
||||
|
||||
@@ -7,6 +7,16 @@ class CaptureTest < Minitest::Test
|
||||
assert_template_result("test string", "{% capture 'var' %}test string{% endcapture %}{{var}}", {})
|
||||
end
|
||||
|
||||
def test_capture_with_hyphen_in_variable_name
|
||||
template_source = <<-END_TEMPLATE
|
||||
{% capture this-thing %}Print this-thing{% endcapture %}
|
||||
{{ this-thing }}
|
||||
END_TEMPLATE
|
||||
template = Template.parse(template_source)
|
||||
rendered = template.render!
|
||||
assert_equal "Print this-thing", rendered.strip
|
||||
end
|
||||
|
||||
def test_capture_to_variable_from_outer_scope_if_existing
|
||||
template_source = <<-END_TEMPLATE
|
||||
{% assign var = '' %}
|
||||
|
||||
@@ -25,6 +25,12 @@ end
|
||||
class FiltersTest < Minitest::Test
|
||||
include Liquid
|
||||
|
||||
module OverrideObjectMethodFilter
|
||||
def tap(input)
|
||||
"tap overridden"
|
||||
end
|
||||
end
|
||||
|
||||
def setup
|
||||
@context = Context.new
|
||||
end
|
||||
@@ -68,11 +74,34 @@ class FiltersTest < Minitest::Test
|
||||
@context['numbers'] = [2,1,4,3]
|
||||
@context['words'] = ['expected', 'as', 'alphabetic']
|
||||
@context['arrays'] = ['flower', 'are']
|
||||
@context['case_sensitive'] = ['sensitive', 'Expected', 'case']
|
||||
|
||||
assert_equal [1,2,3,4], Variable.new("numbers | sort").render(@context)
|
||||
assert_equal ['alphabetic', 'as', 'expected'], Variable.new("words | sort").render(@context)
|
||||
assert_equal [3], Variable.new("value | sort").render(@context)
|
||||
assert_equal ['are', 'flower'], Variable.new("arrays | sort").render(@context)
|
||||
assert_equal ['Expected', 'case', 'sensitive'], Variable.new("case_sensitive | sort").render(@context)
|
||||
end
|
||||
|
||||
def test_sort_natural
|
||||
@context['words'] = ['case', 'Assert', 'Insensitive']
|
||||
@context['hashes'] = [{ 'a' => 'A'}, { 'a' => 'b'}, { 'a' => 'C' }]
|
||||
@context['objects'] = [TestObject.new('A'), TestObject.new('b'), TestObject.new('C')]
|
||||
|
||||
# Test strings
|
||||
assert_equal ['Assert', 'case', 'Insensitive'], Variable.new("words | sort_natural").render(@context)
|
||||
|
||||
# Test hashes
|
||||
sorted = Variable.new("hashes | sort_natural: 'a'").render(@context)
|
||||
assert_equal sorted[0]['a'], 'A'
|
||||
assert_equal sorted[1]['a'], 'b'
|
||||
assert_equal sorted[2]['a'], 'C'
|
||||
|
||||
# Test objects
|
||||
sorted = Variable.new("objects | sort_natural: 'a'").render(@context)
|
||||
assert_equal sorted[0].a, 'A'
|
||||
assert_equal sorted[1].a, 'b'
|
||||
assert_equal sorted[2].a, 'C'
|
||||
end
|
||||
|
||||
def test_strip_html
|
||||
@@ -105,6 +134,13 @@ class FiltersTest < Minitest::Test
|
||||
output = Variable.new(%! 'hello %{first_name}, %{last_name}' | substitute: first_name: surname, last_name: 'doe' !).render(@context)
|
||||
assert_equal 'hello john, doe', output
|
||||
end
|
||||
|
||||
def test_override_object_method_in_filter
|
||||
assert_equal "tap overridden", Template.parse("{{var | tap}}").render!({ 'var' => 1000 }, :filters => [OverrideObjectMethodFilter])
|
||||
|
||||
# tap still treated as a non-existent filter
|
||||
assert_equal "1000", Template.parse("{{var | tap}}").render!({ 'var' => 1000 })
|
||||
end
|
||||
end
|
||||
|
||||
class FiltersInTemplate < Minitest::Test
|
||||
@@ -123,3 +159,10 @@ class FiltersInTemplate < Minitest::Test
|
||||
assert_equal " 1000$ CAD ", Template.parse("{{1000 | money}}").render!(nil, [CanadianMoneyFilter])
|
||||
end
|
||||
end # FiltersTest
|
||||
|
||||
class TestObject
|
||||
attr_accessor :a
|
||||
def initialize(a)
|
||||
@a = a
|
||||
end
|
||||
end
|
||||
|
||||
172
test/integration/formatter_test.rb
Normal file
172
test/integration/formatter_test.rb
Normal file
@@ -0,0 +1,172 @@
|
||||
require 'test_helper'
|
||||
|
||||
class FormatterTest < Minitest::Test
|
||||
def assert_format(expected, src)
|
||||
with_error_mode(:lax) do
|
||||
src_ast = Liquid::Template.parse(src).root
|
||||
assert_equal expected, src_ast.format
|
||||
|
||||
fmt_ast = Liquid::Template.parse(src_ast.format).root
|
||||
assert_equal expected, fmt_ast.format
|
||||
end
|
||||
end
|
||||
|
||||
def test_filters
|
||||
assert_format '{{ a | b: foo, c: "foo" }}', '{{a|b:foo,c:"foo"}}'
|
||||
assert_format '{{ page.attribs.title | downcase }}', "{{page.attribs['title' ]| downcase}}"
|
||||
assert_format '{{ page.attribs["t.i.t.l.e"] | downcase }}', "{{page.attribs['t.i.t.l.e'] | downcase }}"
|
||||
assert_format '{{ page.attribs["t&tle"] | downcase }}', "{{page.attribs['t&tle'] | downcase }}"
|
||||
end
|
||||
|
||||
def test_conditionals
|
||||
src = <<-eof
|
||||
{% if true && !!%}
|
||||
cats
|
||||
{% elsif a or (b and c) && d%}
|
||||
dogs
|
||||
{% endif %}
|
||||
|
||||
{%unless something%}
|
||||
cats
|
||||
{% endunless%}
|
||||
eof
|
||||
|
||||
expected = <<-eof
|
||||
{% if true %}
|
||||
cats
|
||||
{% elsif a or b and c %}
|
||||
dogs
|
||||
{% endif %}
|
||||
|
||||
{% unless something %}
|
||||
cats
|
||||
{% endunless %}
|
||||
eof
|
||||
|
||||
assert_format expected, src
|
||||
|
||||
src = <<-eof
|
||||
{%case var asdf $$^$ %}
|
||||
{% when true%}
|
||||
w
|
||||
{% else%}
|
||||
e
|
||||
{%endcase %}
|
||||
eof
|
||||
|
||||
expected = <<-eof
|
||||
{% case var %}
|
||||
{% when true %}
|
||||
w
|
||||
{% else %}
|
||||
e
|
||||
{% endcase %}
|
||||
eof
|
||||
|
||||
assert_format expected, src
|
||||
end
|
||||
|
||||
def test_comments
|
||||
assert_format "{% comment %} hunter2 {% endcomment %}", "{%comment %} hunter2 {% endcomment ^ %}"
|
||||
end
|
||||
|
||||
def test_assigns
|
||||
assert_format '{% assign foo = "monkey" %}', "{%assign foo ='monkey' ^ %}"
|
||||
end
|
||||
|
||||
def test_looping
|
||||
src = <<-eof
|
||||
{% for i in (1..10) %}
|
||||
cat
|
||||
{%ifchanged%}{{i}}{% endifchanged %}
|
||||
{% continue%}
|
||||
{% else %}
|
||||
dog
|
||||
{%break %}
|
||||
{% endfor %}
|
||||
eof
|
||||
|
||||
expected = <<-eof
|
||||
{% for i in (1..10) %}
|
||||
cat
|
||||
{% ifchanged %}{{ i }}{% endifchanged %}
|
||||
{% continue %}
|
||||
{% else %}
|
||||
dog
|
||||
{% break %}
|
||||
{% endfor %}
|
||||
eof
|
||||
|
||||
assert_format expected, src
|
||||
|
||||
src = "{% tablerow n in numbers cols:3 offset : 1 limit:6%} {{n}} {% endtablerow %}"
|
||||
expected = "{% tablerow n in numbers cols: 3, offset: 1, limit: 6 %} {{ n }} {% endtablerow %}"
|
||||
assert_format expected, src
|
||||
end
|
||||
|
||||
def test_capture
|
||||
assert_format "{% capture foo %} foo {% endcapture %}", "{%capture foo %} foo {%endcapture%}"
|
||||
end
|
||||
|
||||
def test_cycle
|
||||
assert_format '{% cycle "red", 2.8, "green", 1 %}', "{% cycle 'red',2.8,'green',1 %}"
|
||||
end
|
||||
|
||||
def test_augment
|
||||
assert_format "{% decrement foo %}{% increment foo %}", "{% decrement foo%}{%increment foo %}"
|
||||
end
|
||||
|
||||
def test_raw
|
||||
assert_format "{% raw %} foo {% endraw %}", "{%raw !!%} foo {%endraw foo%}"
|
||||
end
|
||||
|
||||
def test_include
|
||||
src = <<-eof
|
||||
{% include 'foo' %}
|
||||
{% include 'foo' !!! why! %}
|
||||
{% include 'foo' with bar %}
|
||||
{% include 'foo' with bar baz: z qux:f %}
|
||||
{% include 'foo' baz: z qux:f %}
|
||||
eof
|
||||
|
||||
expected = <<-eof
|
||||
{% include "foo" %}
|
||||
{% include "foo" %}
|
||||
{% include "foo" with bar %}
|
||||
{% include "foo" with bar baz: z, qux: f %}
|
||||
{% include "foo" baz: z, qux: f %}
|
||||
eof
|
||||
|
||||
assert_format expected, src
|
||||
end
|
||||
|
||||
def test_quirks
|
||||
src = <<-eof
|
||||
{% if a == 'foo' or (b == 'bar' and c == 'baz') or false %} YES {% endif %}
|
||||
{% if true && false %} YES {% endif %}
|
||||
{% if false || true %} YES {% endif %}
|
||||
{{ 'hi there' | split$$$:' ' | first }}""
|
||||
{{ 'X' | downcase) }}
|
||||
{{ 'hi there' | split:"t"" | reverse | first}}
|
||||
{{ 'hi there' | split:"t"" | remove:"i" | first}}
|
||||
{% for i in (1...5) %}{{ i }}{% endfor %}
|
||||
{{test |a|b|}}
|
||||
{{|test|}}
|
||||
eof
|
||||
|
||||
expected = <<-eof
|
||||
{% if a == "foo" or b == "bar" and c == "baz" or false %} YES {% endif %}
|
||||
{% if true %} YES {% endif %}
|
||||
{% if false %} YES {% endif %}
|
||||
{{ "hi there" | split: " " | first }}""
|
||||
{{ "X" | downcase }}
|
||||
{{ "hi there" | split: "t" | reverse | first }}
|
||||
{{ "hi there" | split: "t" | first }}
|
||||
{% for i in (1..5) %}{{ i }}{% endfor %}
|
||||
{{ test | a | b }}
|
||||
{{ test }}
|
||||
eof
|
||||
|
||||
assert_format expected, src
|
||||
end
|
||||
end
|
||||
@@ -4,7 +4,7 @@ class RenderProfilingTest < Minitest::Test
|
||||
include Liquid
|
||||
|
||||
class ProfilingFileSystem
|
||||
def read_template_file(template_path, context)
|
||||
def read_template_file(template_path)
|
||||
"Rendering template {% assign template_name = '#{template_path}'%}\n{{ template_name }}"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -249,9 +249,12 @@ class StandardFiltersTest < Minitest::Test
|
||||
assert_equal "07/16/2004", @filters.date("Fri Jul 16 01:00:00 2004", "%m/%d/%Y")
|
||||
assert_equal "#{Date.today.year}", @filters.date('now', '%Y')
|
||||
assert_equal "#{Date.today.year}", @filters.date('today', '%Y')
|
||||
assert_equal "#{Date.today.year}", @filters.date('Today', '%Y')
|
||||
|
||||
assert_equal nil, @filters.date(nil, "%B")
|
||||
|
||||
assert_equal '', @filters.date('', "%B")
|
||||
|
||||
assert_equal "07/05/2006", @filters.date(1152098955, "%m/%d/%Y")
|
||||
assert_equal "07/05/2006", @filters.date("1152098955", "%m/%d/%Y")
|
||||
end
|
||||
@@ -358,6 +361,17 @@ class StandardFiltersTest < Minitest::Test
|
||||
assert_template_result('bcd',"{{ a | append: b}}",assigns)
|
||||
end
|
||||
|
||||
def test_concat
|
||||
assert_equal [1, 2, 3, 4], @filters.concat([1, 2], [3, 4])
|
||||
assert_equal [1, 2, 'a'], @filters.concat([1, 2], ['a'])
|
||||
assert_equal [1, 2, 10], @filters.concat([1, 2], [10])
|
||||
|
||||
assert_raises(TypeError) do
|
||||
# no implicit conversion of Fixnum into Array
|
||||
@filters.concat([1, 2], 10)
|
||||
end
|
||||
end
|
||||
|
||||
def test_prepend
|
||||
assigns = {'a' => 'bc', 'b' => 'a' }
|
||||
assert_template_result('abc',"{{ a | prepend: 'a'}}",assigns)
|
||||
|
||||
@@ -298,6 +298,22 @@ HERE
|
||||
'string' => "test string")
|
||||
end
|
||||
|
||||
def test_for_parentloop_references_parent_loop
|
||||
assert_template_result('1.1 1.2 1.3 2.1 2.2 2.3 ',
|
||||
'{% for inner in outer %}{% for k in inner %}' +
|
||||
'{{ forloop.parentloop.index }}.{{ forloop.index }} ' +
|
||||
'{% endfor %}{% endfor %}',
|
||||
'outer' => [[1, 1, 1], [1, 1, 1]])
|
||||
end
|
||||
|
||||
def test_for_parentloop_nil_when_not_present
|
||||
assert_template_result('.1 .2 ',
|
||||
'{% for inner in outer %}' +
|
||||
'{{ forloop.parentloop.index }}.{{ forloop.index }} ' +
|
||||
'{% endfor %}',
|
||||
'outer' => [[1, 1, 1], [1, 1, 1]])
|
||||
end
|
||||
|
||||
def test_blank_string_not_iterable
|
||||
assert_template_result('', "{% for char in characters %}I WILL NOT BE OUTPUT{% endfor %}", 'characters' => '')
|
||||
end
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
require 'test_helper'
|
||||
|
||||
class TestFileSystem
|
||||
def read_template_file(template_path, context)
|
||||
def read_template_file(template_path)
|
||||
case template_path
|
||||
when "product"
|
||||
"Product: {{ product.title }} "
|
||||
@@ -37,14 +37,14 @@ class TestFileSystem
|
||||
end
|
||||
|
||||
class OtherFileSystem
|
||||
def read_template_file(template_path, context)
|
||||
def read_template_file(template_path)
|
||||
'from OtherFileSystem'
|
||||
end
|
||||
end
|
||||
|
||||
class CountingFileSystem
|
||||
attr_reader :count
|
||||
def read_template_file(template_path, context)
|
||||
def read_template_file(template_path)
|
||||
@count ||= 0
|
||||
@count += 1
|
||||
'from CountingFileSystem'
|
||||
@@ -132,7 +132,7 @@ class IncludeTagTest < Minitest::Test
|
||||
def test_recursively_included_template_does_not_produce_endless_loop
|
||||
|
||||
infinite_file_system = Class.new do
|
||||
def read_template_file(template_path, context)
|
||||
def read_template_file(template_path)
|
||||
"-{% include 'loop' %}"
|
||||
end
|
||||
end
|
||||
@@ -145,18 +145,6 @@ class IncludeTagTest < Minitest::Test
|
||||
|
||||
end
|
||||
|
||||
def test_backwards_compatability_support_for_overridden_read_template_file
|
||||
infinite_file_system = Class.new do
|
||||
def read_template_file(template_path) # testing only one argument here.
|
||||
"- hi mom"
|
||||
end
|
||||
end
|
||||
|
||||
Liquid::Template.file_system = infinite_file_system.new
|
||||
|
||||
Template.parse("{% include 'hi_mom' %}").render!
|
||||
end
|
||||
|
||||
def test_dynamically_choosen_template
|
||||
assert_template_result "Test123", "{% include template %}", "template" => 'Test123'
|
||||
assert_template_result "Test321", "{% include template %}", "template" => 'Test321'
|
||||
@@ -231,4 +219,12 @@ class IncludeTagTest < Minitest::Test
|
||||
assert_equal 'x', Template.parse("{% include template %}", error_mode: :strict, include_options_blacklist: [:error_mode]).render!("template" => '{{ "X" || downcase }}')
|
||||
end
|
||||
end
|
||||
|
||||
def test_including_via_variable_value
|
||||
assert_template_result "from TestFileSystem", "{% assign page = 'pick_a_source' %}{% include page %}"
|
||||
|
||||
assert_template_result "Product: Draft 151cm ", "{% assign page = 'product' %}{% include page %}", "product" => {'title' => 'Draft 151cm'}
|
||||
|
||||
assert_template_result "Product: Draft 151cm ", "{% assign page = 'product' %}{% include page for foo %}", "foo" => {'title' => 'Draft 151cm'}
|
||||
end
|
||||
end # IncludeTagTest
|
||||
|
||||
@@ -57,7 +57,7 @@ class TableRowTest < Minitest::Test
|
||||
|
||||
def test_offset_and_limit
|
||||
assert_template_result("<tr class=\"row1\">\n<td class=\"col1\"> 1 </td><td class=\"col2\"> 2 </td><td class=\"col3\"> 3 </td></tr>\n<tr class=\"row2\"><td class=\"col1\"> 4 </td><td class=\"col2\"> 5 </td><td class=\"col3\"> 6 </td></tr>\n",
|
||||
'{% tablerow n in numbers cols:3 offset:1 limit:6%} {{n}} {% endtablerow %}',
|
||||
'{% tablerow n in numbers cols:3, offset:1, limit:6%} {{n}} {% endtablerow %}',
|
||||
'numbers' => [0,1,2,3,4,5,6,7])
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
require 'test_helper'
|
||||
require 'timeout'
|
||||
|
||||
class TemplateContextDrop < Liquid::Drop
|
||||
def before_method(method)
|
||||
@@ -37,6 +38,16 @@ class TemplateTest < Minitest::Test
|
||||
assert_equal 'from instance assigns', t.parse("{{ foo }}").render!
|
||||
end
|
||||
|
||||
def test_warnings_is_not_exponential_time
|
||||
str = "false"
|
||||
100.times do
|
||||
str = "{% if true %}true{% else %}#{str}{% endif %}"
|
||||
end
|
||||
|
||||
t = Template.parse(str)
|
||||
assert_equal [], Timeout::timeout(1) { t.warnings }
|
||||
end
|
||||
|
||||
def test_instance_assigns_persist_on_same_template_parsing_between_renders
|
||||
t = Template.new.parse("{{ foo }}{% assign foo = 'foo' %}{{ foo }}")
|
||||
assert_equal 'foo', t.render!
|
||||
@@ -82,69 +93,92 @@ class TemplateTest < Minitest::Test
|
||||
|
||||
def test_resource_limits_works_with_custom_length_method
|
||||
t = Template.parse("{% assign foo = bar %}")
|
||||
t.resource_limits = { :render_length_limit => 42 }
|
||||
t.resource_limits.render_length_limit = 42
|
||||
assert_equal "", t.render!("bar" => SomethingWithLength.new)
|
||||
end
|
||||
|
||||
def test_resource_limits_render_length
|
||||
t = Template.parse("0123456789")
|
||||
t.resource_limits = { :render_length_limit => 5 }
|
||||
t.resource_limits.render_length_limit = 5
|
||||
assert_equal "Liquid error: Memory limits exceeded", t.render()
|
||||
assert t.resource_limits[:reached]
|
||||
t.resource_limits = { :render_length_limit => 10 }
|
||||
assert t.resource_limits.reached?
|
||||
|
||||
t.resource_limits.render_length_limit = 10
|
||||
assert_equal "0123456789", t.render!()
|
||||
refute_nil t.resource_limits[:render_length_current]
|
||||
refute_nil t.resource_limits.render_length
|
||||
end
|
||||
|
||||
def test_resource_limits_render_score
|
||||
t = Template.parse("{% for a in (1..10) %} {% for a in (1..10) %} foo {% endfor %} {% endfor %}")
|
||||
t.resource_limits = { :render_score_limit => 50 }
|
||||
t.resource_limits.render_score_limit = 50
|
||||
assert_equal "Liquid error: Memory limits exceeded", t.render()
|
||||
assert t.resource_limits[:reached]
|
||||
assert t.resource_limits.reached?
|
||||
|
||||
t = Template.parse("{% for a in (1..100) %} foo {% endfor %}")
|
||||
t.resource_limits = { :render_score_limit => 50 }
|
||||
t.resource_limits.render_score_limit = 50
|
||||
assert_equal "Liquid error: Memory limits exceeded", t.render()
|
||||
assert t.resource_limits[:reached]
|
||||
t.resource_limits = { :render_score_limit => 200 }
|
||||
assert t.resource_limits.reached?
|
||||
|
||||
t.resource_limits.render_score_limit = 200
|
||||
assert_equal (" foo " * 100), t.render!()
|
||||
refute_nil t.resource_limits[:render_score_current]
|
||||
refute_nil t.resource_limits.render_score
|
||||
end
|
||||
|
||||
def test_resource_limits_assign_score
|
||||
t = Template.parse("{% assign foo = 42 %}{% assign bar = 23 %}")
|
||||
t.resource_limits = { :assign_score_limit => 1 }
|
||||
t.resource_limits.assign_score_limit = 1
|
||||
assert_equal "Liquid error: Memory limits exceeded", t.render()
|
||||
assert t.resource_limits[:reached]
|
||||
t.resource_limits = { :assign_score_limit => 2 }
|
||||
assert t.resource_limits.reached?
|
||||
|
||||
t.resource_limits.assign_score_limit = 2
|
||||
assert_equal "", t.render!()
|
||||
refute_nil t.resource_limits[:assign_score_current]
|
||||
refute_nil t.resource_limits.assign_score
|
||||
end
|
||||
|
||||
def test_resource_limits_aborts_rendering_after_first_error
|
||||
t = Template.parse("{% for a in (1..100) %} foo1 {% endfor %} bar {% for a in (1..100) %} foo2 {% endfor %}")
|
||||
t.resource_limits = { :render_score_limit => 50 }
|
||||
t.resource_limits.render_score_limit = 50
|
||||
assert_equal "Liquid error: Memory limits exceeded", t.render()
|
||||
assert t.resource_limits[:reached]
|
||||
assert t.resource_limits.reached?
|
||||
end
|
||||
|
||||
def test_resource_limits_hash_in_template_gets_updated_even_if_no_limits_are_set
|
||||
t = Template.parse("{% for a in (1..100) %} {% assign foo = 1 %} {% endfor %}")
|
||||
t.render!()
|
||||
assert t.resource_limits[:assign_score_current] > 0
|
||||
assert t.resource_limits[:render_score_current] > 0
|
||||
assert t.resource_limits[:render_length_current] > 0
|
||||
assert t.resource_limits.assign_score > 0
|
||||
assert t.resource_limits.render_score > 0
|
||||
assert t.resource_limits.render_length > 0
|
||||
end
|
||||
|
||||
def test_render_length_persists_between_blocks
|
||||
t = Template.parse("{% if true %}aaaa{% endif %}")
|
||||
t.resource_limits.render_length_limit = 7
|
||||
assert_equal "Liquid error: Memory limits exceeded", t.render()
|
||||
t.resource_limits.render_length_limit = 8
|
||||
assert_equal "aaaa", t.render()
|
||||
|
||||
t = Template.parse("{% if true %}aaaa{% endif %}{% if true %}bbb{% endif %}")
|
||||
t.resource_limits.render_length_limit = 13
|
||||
assert_equal "Liquid error: Memory limits exceeded", t.render()
|
||||
t.resource_limits.render_length_limit = 14
|
||||
assert_equal "aaaabbb", t.render()
|
||||
|
||||
t = Template.parse("{% if true %}a{% endif %}{% if true %}b{% endif %}{% if true %}a{% endif %}{% if true %}b{% endif %}{% if true %}a{% endif %}{% if true %}b{% endif %}")
|
||||
t.resource_limits.render_length_limit = 5
|
||||
assert_equal "Liquid error: Memory limits exceeded", t.render()
|
||||
t.resource_limits.render_length_limit = 11
|
||||
assert_equal "Liquid error: Memory limits exceeded", t.render()
|
||||
t.resource_limits.render_length_limit = 12
|
||||
assert_equal "ababab", t.render()
|
||||
end
|
||||
|
||||
def test_default_resource_limits_unaffected_by_render_with_context
|
||||
context = Context.new
|
||||
t = Template.parse("{% for a in (1..100) %} {% assign foo = 1 %} {% endfor %}")
|
||||
t.render!(context)
|
||||
assert context.resource_limits[:assign_score_current] > 0
|
||||
assert context.resource_limits[:render_score_current] > 0
|
||||
assert context.resource_limits[:render_length_current] > 0
|
||||
refute Template.default_resource_limits.key?(:assign_score_current)
|
||||
refute Template.default_resource_limits.key?(:render_score_current)
|
||||
refute Template.default_resource_limits.key?(:render_length_current)
|
||||
assert context.resource_limits.assign_score > 0
|
||||
assert context.resource_limits.render_score > 0
|
||||
assert context.resource_limits.render_length > 0
|
||||
end
|
||||
|
||||
def test_can_use_drop_as_context
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
#!/usr/bin/env ruby
|
||||
|
||||
ENV["MT_NO_EXPECTATIONS"] = "1"
|
||||
require 'minitest/autorun'
|
||||
require 'spy/integration'
|
||||
|
||||
$:.unshift(File.join(File.expand_path(File.dirname(__FILE__)), '..', 'lib'))
|
||||
require 'liquid.rb'
|
||||
require 'liquid/profiler'
|
||||
|
||||
mode = :strict
|
||||
if env_mode = ENV['LIQUID_PARSER_MODE']
|
||||
@@ -48,13 +50,19 @@ module Minitest
|
||||
end
|
||||
|
||||
def with_global_filter(*globals)
|
||||
original_filters = Array.new(Liquid::Strainer.class_variable_get(:@@filters))
|
||||
original_global_strainer = Liquid::Strainer.class_variable_get(:@@global_strainer)
|
||||
Liquid::Strainer.class_variable_set(:@@global_strainer, Class.new(Liquid::Strainer) do
|
||||
@filter_methods = Set.new
|
||||
end)
|
||||
Liquid::Strainer.class_variable_get(:@@strainer_class_cache).clear
|
||||
|
||||
globals.each do |global|
|
||||
Liquid::Template.register_filter(global)
|
||||
end
|
||||
yield
|
||||
ensure
|
||||
Liquid::Strainer.class_variable_set(:@@filters, original_filters)
|
||||
Liquid::Strainer.class_variable_get(:@@strainer_class_cache).clear
|
||||
Liquid::Strainer.class_variable_set(:@@global_strainer, original_global_strainer)
|
||||
end
|
||||
|
||||
def with_taint_mode(mode)
|
||||
|
||||
@@ -5,7 +5,7 @@ class FileSystemUnitTest < Minitest::Test
|
||||
|
||||
def test_default
|
||||
assert_raises(FileSystemError) do
|
||||
BlankFileSystem.new.read_template_file("dummy", {'dummy'=>'smarty'})
|
||||
BlankFileSystem.new.read_template_file("dummy")
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -31,11 +31,11 @@ class StrainerUnitTest < Minitest::Test
|
||||
|
||||
def test_strainer_only_invokes_public_filter_methods
|
||||
strainer = Strainer.create(nil)
|
||||
assert_equal false, strainer.invokable?('__test__')
|
||||
assert_equal false, strainer.invokable?('test')
|
||||
assert_equal false, strainer.invokable?('instance_eval')
|
||||
assert_equal false, strainer.invokable?('__send__')
|
||||
assert_equal true, strainer.invokable?('size') # from the standard lib
|
||||
assert_equal false, strainer.class.invokable?('__test__')
|
||||
assert_equal false, strainer.class.invokable?('test')
|
||||
assert_equal false, strainer.class.invokable?('instance_eval')
|
||||
assert_equal false, strainer.class.invokable?('__send__')
|
||||
assert_equal true, strainer.class.invokable?('size') # from the standard lib
|
||||
end
|
||||
|
||||
def test_strainer_returns_nil_if_no_filter_method_found
|
||||
@@ -63,9 +63,7 @@ class StrainerUnitTest < Minitest::Test
|
||||
assert_kind_of Strainer, strainer
|
||||
assert_kind_of a, strainer
|
||||
assert_kind_of b, strainer
|
||||
Strainer.class_variable_get(:@@filters).each do |m|
|
||||
assert_kind_of m, strainer
|
||||
end
|
||||
assert_kind_of Liquid::StandardFilters, strainer
|
||||
end
|
||||
|
||||
end # StrainerTest
|
||||
|
||||
@@ -147,4 +147,10 @@ class VariableUnitTest < Minitest::Test
|
||||
var = Variable.new(%! name_of_variable | upcase !)
|
||||
assert_equal " name_of_variable | upcase ", var.raw
|
||||
end
|
||||
|
||||
def test_variable_lookup_interface
|
||||
lookup = VariableLookup.new('a.b.c')
|
||||
assert_equal 'a', lookup.name
|
||||
assert_equal ['b', 'c'], lookup.lookups
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user