mirror of
https://github.com/kemko/liquid.git
synced 2026-01-02 00:05:42 +03:00
Compare commits
98 Commits
spaceless-
...
latest-bun
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
622b8011db | ||
|
|
021bafd260 | ||
|
|
04c393ab07 | ||
|
|
9a7778e52c | ||
|
|
dde00253f9 | ||
|
|
18d1644980 | ||
|
|
c424d47274 | ||
|
|
8e6b9d503d | ||
|
|
8be38d1795 | ||
|
|
3146d5c3f2 | ||
|
|
0cc8b68a97 | ||
|
|
5a50c12953 | ||
|
|
a6fa4c5c38 | ||
|
|
dadd9b4dd2 | ||
|
|
6434b8d2bb | ||
|
|
2d891ddd8f | ||
|
|
60b508b151 | ||
|
|
3891f14a1a | ||
|
|
198f0aa366 | ||
|
|
f2e6adf566 | ||
|
|
08de6ed2c5 | ||
|
|
7e322f5cf8 | ||
|
|
bf86a5a069 | ||
|
|
0141444814 | ||
|
|
6d30226768 | ||
|
|
63e8bac1a4 | ||
|
|
8449849ed5 | ||
|
|
4bc198a0db | ||
|
|
3921dbe919 | ||
|
|
79e2d1d8b4 | ||
|
|
b7c4041db8 | ||
|
|
e113c891ec | ||
|
|
a32ad449c0 | ||
|
|
1662ba6679 | ||
|
|
99b5e86f0a | ||
|
|
b892a73463 | ||
|
|
0b55d09cea | ||
|
|
5f8086572b | ||
|
|
bdb9a4a47f | ||
|
|
c38eec0293 | ||
|
|
8d5a907dc8 | ||
|
|
74cc41ce74 | ||
|
|
a120cc587a | ||
|
|
c582023321 | ||
|
|
ac041c4ad1 | ||
|
|
31d7682f4e | ||
|
|
5f1acbc086 | ||
|
|
8612716129 | ||
|
|
e6392d1cc1 | ||
|
|
04381418d3 | ||
|
|
89ccdabe9a | ||
|
|
c0fc6777b0 | ||
|
|
cd03346239 | ||
|
|
b4f19da127 | ||
|
|
4100f8d641 | ||
|
|
d8bda2c892 | ||
|
|
4f81c0a658 | ||
|
|
704937bc00 | ||
|
|
27c6b8074a | ||
|
|
affae5ebef | ||
|
|
fc1c0d0d83 | ||
|
|
a215b70de9 | ||
|
|
1f70928f8a | ||
|
|
7713f6709d | ||
|
|
239cf0e5f5 | ||
|
|
fa187665b3 | ||
|
|
cd0c5e954c | ||
|
|
490b457738 | ||
|
|
4d6dec9b5a | ||
|
|
0b11b573d9 | ||
|
|
b42d35ff36 | ||
|
|
b4e133e26f | ||
|
|
1f9bd1d809 | ||
|
|
e88be60818 | ||
|
|
14416b3c49 | ||
|
|
bde14a650d | ||
|
|
c535af021a | ||
|
|
9c9345869b | ||
|
|
73834a7e52 | ||
|
|
c45310170b | ||
|
|
920e1df643 | ||
|
|
cebf75b8d7 | ||
|
|
afda01adbb | ||
|
|
959cd6d2a2 | ||
|
|
4c1b89e20e | ||
|
|
83b6dd0268 | ||
|
|
6fb402e60d | ||
|
|
338287df5e | ||
|
|
c4c398174b | ||
|
|
80b6ac3bc7 | ||
|
|
15974d9168 | ||
|
|
f22ab4358b | ||
|
|
9cf0d264e1 | ||
|
|
575e3cae7a | ||
|
|
fad3b8275c | ||
|
|
5a071cb7f2 | ||
|
|
8cb2364179 | ||
|
|
3c23cfc167 |
10
.rubocop.yml
10
.rubocop.yml
@@ -13,6 +13,9 @@ Metrics/BlockNesting:
|
||||
Metrics/ModuleLength:
|
||||
Enabled: false
|
||||
|
||||
Metrics/ClassLength:
|
||||
Enabled: false
|
||||
|
||||
Lint/AssignmentInCondition:
|
||||
Enabled: false
|
||||
|
||||
@@ -115,9 +118,8 @@ Style/ClassVars:
|
||||
Style/PerlBackrefs:
|
||||
Enabled: false
|
||||
|
||||
Style/TrivialAccessors:
|
||||
AllowPredicates: true
|
||||
|
||||
Style/WordArray:
|
||||
Enabled: false
|
||||
|
||||
Style/ModuleLength:
|
||||
Exclude:
|
||||
- lib/liquid/standardfilters.rb
|
||||
|
||||
@@ -13,11 +13,6 @@ Lint/NestedMethodDefinition:
|
||||
Metrics/AbcSize:
|
||||
Max: 58
|
||||
|
||||
# Offense count: 16
|
||||
# Configuration parameters: CountComments.
|
||||
Metrics/ClassLength:
|
||||
Max: 314
|
||||
|
||||
# Offense count: 12
|
||||
Metrics/CyclomaticComplexity:
|
||||
Max: 15
|
||||
@@ -32,11 +27,6 @@ Metrics/LineLength:
|
||||
Metrics/MethodLength:
|
||||
Max: 46
|
||||
|
||||
# Offense count: 1
|
||||
# Configuration parameters: CountComments.
|
||||
Metrics/ModuleLength:
|
||||
Max: 235
|
||||
|
||||
# Offense count: 6
|
||||
Metrics/PerceivedComplexity:
|
||||
Max: 13
|
||||
|
||||
11
.travis.yml
11
.travis.yml
@@ -6,14 +6,23 @@ rvm:
|
||||
- 2.2
|
||||
- ruby-head
|
||||
- jruby-head
|
||||
- rbx-2
|
||||
# - rbx-2
|
||||
|
||||
sudo: false
|
||||
|
||||
addons:
|
||||
apt:
|
||||
packages:
|
||||
- libgmp3-dev
|
||||
|
||||
matrix:
|
||||
allow_failures:
|
||||
- rvm: jruby-head
|
||||
|
||||
before_install:
|
||||
- gem update --system
|
||||
- gem install bundler --pre
|
||||
|
||||
script: "bundle exec rake"
|
||||
|
||||
notifications:
|
||||
|
||||
@@ -4,23 +4,22 @@
|
||||
|
||||
* Bugfixes
|
||||
* Performance improvements
|
||||
* Features which are likely to be useful to the majority of Liquid users
|
||||
* Features that are likely to be useful to the majority of Liquid users
|
||||
|
||||
## Things we won't merge
|
||||
|
||||
* Code which introduces considerable performance degrations
|
||||
* Code which touches performance critical parts of Liquid and comes without benchmarks
|
||||
* Features which are not important for most people (we want to keep the core Liquid code small and tidy)
|
||||
* Features which can easily be implemented on top of Liquid (for example as a custom filter or custom filesystem)
|
||||
* Code which comes without tests
|
||||
* Code which breaks existing tests
|
||||
* Code that introduces considerable performance degrations
|
||||
* Code that touches performance-critical parts of Liquid and comes without benchmarks
|
||||
* Features that are not important for most people (we want to keep the core Liquid code small and tidy)
|
||||
* Features that can easily be implemented on top of Liquid (for example as a custom filter or custom filesystem)
|
||||
* Code that does not include tests
|
||||
* Code that breaks existing tests
|
||||
|
||||
## Workflow
|
||||
|
||||
* Fork the Liquid repository
|
||||
* Create a new branch in your fork
|
||||
* If it makes sense, add tests for your code and run a performance benchmark
|
||||
* Make sure all tests pass
|
||||
* If it makes sense, add tests for your code and/or run a performance benchmark
|
||||
* Make sure all tests pass (`bundle exec rake`)
|
||||
* Create a pull request
|
||||
* In the description, ping one of [@boourns](https://github.com/boourns), [@fw42](https://github.com/fw42), [@camilo](https://github.com/camilo), [@dylanahsmith](https://github.com/dylanahsmith), or [@arthurnn](https://github.com/arthurnn) and ask for a code review.
|
||||
|
||||
|
||||
4
Gemfile
4
Gemfile
@@ -6,9 +6,9 @@ gem 'stackprof', platforms: :mri_21
|
||||
group :test do
|
||||
gem 'spy', '0.4.1'
|
||||
gem 'benchmark-ips'
|
||||
gem 'rubocop'
|
||||
gem 'rubocop', '0.34.2'
|
||||
|
||||
platform :mri do
|
||||
gem 'liquid-c', github: 'Shopify/liquid-c', ref: '35e9aee48d639ae1d3ac9ba77616aca9800eab7d'
|
||||
gem 'liquid-c', github: 'Shopify/liquid-c', ref: '2570693d8d03faa0df9160ec74348a7149436df3'
|
||||
end
|
||||
end
|
||||
|
||||
31
History.md
31
History.md
@@ -3,6 +3,11 @@
|
||||
## 4.0.0 / not yet released / branch "master"
|
||||
|
||||
### Changed
|
||||
* Add strict_variables and strict_filters options to detect undefined references (#691)
|
||||
* Improve loop performance (#681) [Florian Weingarten]
|
||||
* Rename Drop method `before_method` to `liquid_method_missing` (#661) [Thierry Joyal]
|
||||
* Add url_decode filter to invert url_encode (#645) [Larry Archer]
|
||||
* Add global_filter to apply a filter to all output (#610) [Loren Hale]
|
||||
* Add compact filter (#600) [Carson Reinke]
|
||||
* Rename deprecated "has_key?" and "has_interrupt?" methods (#593) [Florian Weingarten]
|
||||
* Include template name with line numbers in render errors (574) [Dylan Thacker-Smith]
|
||||
@@ -13,8 +18,14 @@
|
||||
* Ruby 1.9 support dropped (#491) [Justin Li]
|
||||
* Liquid::Template.file_system's read_template_file method is no longer passed the context. (#441) [James Reid-Smith]
|
||||
* Remove support for `liquid_methods`
|
||||
* Liquid::Template.register_filter raises when the module overrides registered public methods as private or protected (#705) [Gaurav Chande]
|
||||
|
||||
### Fixed
|
||||
* Fix map filter when value is a Proc (#672) [Guillaume Malette]
|
||||
* Fix truncate filter when value is not a string (#672) [Guillaume Malette]
|
||||
* Fix behaviour of escape filter when input is nil (#665) [Tanel Jakobsoo]
|
||||
* Fix sort filter behaviour with empty array input (#652) [Marcel Cary]
|
||||
* Fix test failure under certain timezones (#631) [Dylan Thacker-Smith]
|
||||
* Fix bug in uniq filter (#595) [Florian Weingarten]
|
||||
* Fix bug when "blank" and "empty" are used as variable names (#592) [Florian Weingarten]
|
||||
* Fix condition parse order in strict mode (#569) [Justin Li]
|
||||
@@ -26,7 +37,15 @@
|
||||
* Disallow variable names in the strict parser that are not valid in the lax parser (#463) [Justin Li]
|
||||
* Fix BlockBody#warnings taking exponential time to compute (#486) [Justin Li]
|
||||
|
||||
## 3.0.3 / 2015-05-28 / branch "3-0-stable"
|
||||
## 3.0.5 / 2015-07-23 / branch "3-0-stable"
|
||||
|
||||
* Fix test failure under certain timezones [Dylan Thacker-Smith]
|
||||
|
||||
## 3.0.4 / 2015-07-17
|
||||
|
||||
* Fix chained access to multi-dimensional hashes [Florian Weingarten]
|
||||
|
||||
## 3.0.3 / 2015-05-28
|
||||
|
||||
* Fix condition parse order in strict mode (#569) [Justin Li]
|
||||
|
||||
@@ -74,7 +93,15 @@
|
||||
* Make map filter work on enumerable drops (#233) [Florian Weingarten]
|
||||
* Improved whitespace stripping for blank blocks, related to #216 [Florian Weingarten]
|
||||
|
||||
## 2.6.1 / 2014-01-10 / branch "2-6-stable"
|
||||
## 2.6.3 / 2015-07-23 / branch "2-6-stable"
|
||||
|
||||
* Fix test failure under certain timezones [Dylan Thacker-Smith]
|
||||
|
||||
## 2.6.2 / 2015-01-23
|
||||
|
||||
* Remove duplicate hash key [Parker Moore]
|
||||
|
||||
## 2.6.1 / 2014-01-10
|
||||
|
||||
Security fix, cherry-picked from master (4e14a65):
|
||||
* Don't call to_sym when creating conditions for security reasons (#273) [Bouke van der Bijl]
|
||||
|
||||
31
README.md
31
README.md
@@ -73,3 +73,34 @@ This is useful for doing things like enabling strict mode only in the theme edit
|
||||
|
||||
It is recommended that you enable `:strict` or `:warn` mode on new apps to stop invalid templates from being created.
|
||||
It is also recommended that you use it in the template editors of existing apps to give editors better error messages.
|
||||
|
||||
### Undefined variables and filters
|
||||
|
||||
By default, the renderer doesn't raise or in any other way notify you if some variables or filters are missing, i.e. not passed to the `render` method.
|
||||
You can improve this situation by passing `strict_variables: true` and/or `strict_filters: true` options to the `render` method.
|
||||
When one of these options is set to true, all errors about undefined variables and undefined filters will be stored in `errors` array of a `Liquid::Template` instance.
|
||||
Here are some examples:
|
||||
|
||||
```ruby
|
||||
template = Liquid::Template.parse("{{x}} {{y}} {{z.a}} {{z.b}}")
|
||||
template.render({ 'x' => 1, 'z' => { 'a' => 2 } }, { strict_variables: true })
|
||||
#=> '1 2 ' # when a variable is undefined, it's rendered as nil
|
||||
template.errors
|
||||
#=> [#<Liquid::UndefinedVariable: Liquid error: undefined variable y>, #<Liquid::UndefinedVariable: Liquid error: undefined variable b>]
|
||||
```
|
||||
|
||||
```ruby
|
||||
template = Liquid::Template.parse("{{x | filter1 | upcase}}")
|
||||
template.render({ 'x' => 'foo' }, { strict_filters: true })
|
||||
#=> '' # when at least one filter in the filter chain is undefined, a whole expression is rendered as nil
|
||||
template.errors
|
||||
#=> [#<Liquid::UndefinedFilter: Liquid error: undefined filter filter1>]
|
||||
```
|
||||
|
||||
If you want to raise on a first exception instead of pushing all of them in `errors`, you can use `render!` method:
|
||||
|
||||
```ruby
|
||||
template = Liquid::Template.parse("{{x}} {{y}}")
|
||||
template.render!({ 'x' => 1}, { strict_variables: true })
|
||||
#=> Liquid::UndefinedVariable: Liquid error: undefined variable y
|
||||
```
|
||||
|
||||
@@ -48,6 +48,8 @@ require 'liquid/lexer'
|
||||
require 'liquid/parser'
|
||||
require 'liquid/i18n'
|
||||
require 'liquid/drop'
|
||||
require 'liquid/tablerowloop_drop'
|
||||
require 'liquid/forloop_drop'
|
||||
require 'liquid/extensions'
|
||||
require 'liquid/errors'
|
||||
require 'liquid/interrupts'
|
||||
@@ -69,7 +71,7 @@ require 'liquid/standardfilters'
|
||||
require 'liquid/condition'
|
||||
require 'liquid/utils'
|
||||
require 'liquid/tokenizer'
|
||||
require 'liquid/token'
|
||||
require 'liquid/parse_context'
|
||||
|
||||
# Load all the tags of the standard library
|
||||
#
|
||||
|
||||
@@ -23,29 +23,17 @@ module Liquid
|
||||
@body.nodelist
|
||||
end
|
||||
|
||||
# warnings of this block and all sub-tags
|
||||
def warnings
|
||||
all_warnings = []
|
||||
all_warnings.concat(@warnings) if @warnings
|
||||
|
||||
(nodelist || []).each do |node|
|
||||
all_warnings.concat(node.warnings || []) if node.respond_to?(:warnings)
|
||||
end
|
||||
|
||||
all_warnings
|
||||
end
|
||||
|
||||
def unknown_tag(tag, _params, _tokens)
|
||||
case tag
|
||||
when 'else'.freeze
|
||||
raise SyntaxError.new(options[:locale].t("errors.syntax.unexpected_else".freeze,
|
||||
raise SyntaxError.new(parse_context.locale.t("errors.syntax.unexpected_else".freeze,
|
||||
block_name: block_name))
|
||||
when 'end'.freeze
|
||||
raise SyntaxError.new(options[:locale].t("errors.syntax.invalid_delimiter".freeze,
|
||||
raise SyntaxError.new(parse_context.locale.t("errors.syntax.invalid_delimiter".freeze,
|
||||
block_name: block_name,
|
||||
block_delimiter: block_delimiter))
|
||||
else
|
||||
raise SyntaxError.new(options[:locale].t("errors.syntax.unknown_tag".freeze, tag: tag))
|
||||
raise SyntaxError.new(parse_context.locale.t("errors.syntax.unknown_tag".freeze, tag: tag))
|
||||
end
|
||||
end
|
||||
|
||||
@@ -60,12 +48,12 @@ module Liquid
|
||||
protected
|
||||
|
||||
def parse_body(body, tokens)
|
||||
body.parse(tokens, options) do |end_tag_name, end_tag_params|
|
||||
body.parse(tokens, parse_context) do |end_tag_name, end_tag_params|
|
||||
@blank &&= body.blank?
|
||||
|
||||
return false if end_tag_name == block_delimiter
|
||||
unless end_tag_name
|
||||
raise SyntaxError.new(@options[:locale].t("errors.syntax.tag_never_closed".freeze, block_name: block_name))
|
||||
raise SyntaxError.new(parse_context.locale.t("errors.syntax.tag_never_closed".freeze, block_name: block_name))
|
||||
end
|
||||
|
||||
# this tag is not registered with the system
|
||||
|
||||
@@ -12,44 +12,37 @@ module Liquid
|
||||
@blank = true
|
||||
end
|
||||
|
||||
def parse(tokens, options)
|
||||
while token = tokens.shift
|
||||
begin
|
||||
unless token.empty?
|
||||
case
|
||||
when token.start_with?(TAGSTART)
|
||||
if token =~ FullToken
|
||||
tag_name = $1
|
||||
markup = $2
|
||||
# fetch the tag from registered blocks
|
||||
if tag = registered_tags[tag_name]
|
||||
markup = token.child(markup) if token.is_a?(Token)
|
||||
new_tag = tag.parse(tag_name, markup, tokens, options)
|
||||
new_tag.line_number = token.line_number if token.is_a?(Token)
|
||||
@blank &&= new_tag.blank?
|
||||
@nodelist << new_tag
|
||||
else
|
||||
# end parsing if we reach an unknown tag and let the caller decide
|
||||
# determine how to proceed
|
||||
return yield tag_name, markup
|
||||
end
|
||||
def parse(tokenizer, parse_context)
|
||||
parse_context.line_number = tokenizer.line_number
|
||||
while token = tokenizer.shift
|
||||
unless token.empty?
|
||||
case
|
||||
when token.start_with?(TAGSTART)
|
||||
if token =~ FullToken
|
||||
tag_name = $1
|
||||
markup = $2
|
||||
# fetch the tag from registered blocks
|
||||
if tag = registered_tags[tag_name]
|
||||
new_tag = tag.parse(tag_name, markup, tokenizer, parse_context)
|
||||
@blank &&= new_tag.blank?
|
||||
@nodelist << new_tag
|
||||
else
|
||||
raise_missing_tag_terminator(token, options)
|
||||
# end parsing if we reach an unknown tag and let the caller decide
|
||||
# determine how to proceed
|
||||
return yield tag_name, markup
|
||||
end
|
||||
when token.start_with?(VARSTART)
|
||||
new_var = create_variable(token, options)
|
||||
new_var.line_number = token.line_number if token.is_a?(Token)
|
||||
@nodelist << new_var
|
||||
@blank = false
|
||||
else
|
||||
@nodelist << token
|
||||
@blank &&= !!(token =~ /\A\s*\z/)
|
||||
raise_missing_tag_terminator(token, parse_context)
|
||||
end
|
||||
when token.start_with?(VARSTART)
|
||||
@nodelist << create_variable(token, parse_context)
|
||||
@blank = false
|
||||
else
|
||||
@nodelist << token
|
||||
@blank &&= !!(token =~ /\A\s*\z/)
|
||||
end
|
||||
rescue SyntaxError => e
|
||||
e.set_line_number_from_token(token)
|
||||
raise
|
||||
end
|
||||
parse_context.line_number = tokenizer.line_number
|
||||
end
|
||||
|
||||
yield nil, nil
|
||||
@@ -59,14 +52,6 @@ module Liquid
|
||||
@blank
|
||||
end
|
||||
|
||||
def warnings
|
||||
all_warnings = []
|
||||
nodelist.each do |node|
|
||||
all_warnings.concat(node.warnings || []) if node.respond_to?(:warnings)
|
||||
end
|
||||
all_warnings
|
||||
end
|
||||
|
||||
def render(context)
|
||||
output = []
|
||||
context.resource_limits.render_score += @nodelist.length
|
||||
@@ -84,15 +69,18 @@ module Liquid
|
||||
break
|
||||
end
|
||||
|
||||
token_output = render_token(token, context)
|
||||
node_output = render_node(token, context)
|
||||
|
||||
unless token.is_a?(Block) && token.blank?
|
||||
output << token_output
|
||||
output << node_output
|
||||
end
|
||||
rescue MemoryError => e
|
||||
raise e
|
||||
rescue UndefinedVariable, UndefinedDropMethod, UndefinedFilter => e
|
||||
context.handle_error(e, token.line_number)
|
||||
output << nil
|
||||
rescue ::StandardError => e
|
||||
output << context.handle_error(e, token)
|
||||
output << context.handle_error(e, token.line_number)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -101,31 +89,31 @@ module Liquid
|
||||
|
||||
private
|
||||
|
||||
def render_token(token, context)
|
||||
token_output = (token.respond_to?(:render) ? token.render(context) : token)
|
||||
token_str = token_output.is_a?(Array) ? token_output.join : token_output.to_s
|
||||
def render_node(node, context)
|
||||
node_output = (node.respond_to?(:render) ? node.render(context) : node)
|
||||
node_output = node_output.is_a?(Array) ? node_output.join : node_output.to_s
|
||||
|
||||
context.resource_limits.render_length += token_str.length
|
||||
context.resource_limits.render_length += node_output.length
|
||||
if context.resource_limits.reached?
|
||||
raise MemoryError.new("Memory limits exceeded".freeze)
|
||||
end
|
||||
token_str
|
||||
node_output
|
||||
end
|
||||
|
||||
def create_variable(token, options)
|
||||
def create_variable(token, parse_context)
|
||||
token.scan(ContentOfVariable) do |content|
|
||||
markup = token.is_a?(Token) ? token.child(content.first) : content.first
|
||||
return Variable.new(markup, options)
|
||||
markup = content.first
|
||||
return Variable.new(markup, parse_context)
|
||||
end
|
||||
raise_missing_variable_terminator(token, options)
|
||||
raise_missing_variable_terminator(token, parse_context)
|
||||
end
|
||||
|
||||
def raise_missing_tag_terminator(token, options)
|
||||
raise SyntaxError.new(options[:locale].t("errors.syntax.tag_termination".freeze, token: token, tag_end: TagEnd.inspect))
|
||||
def raise_missing_tag_terminator(token, parse_context)
|
||||
raise SyntaxError.new(parse_context.locale.t("errors.syntax.tag_termination".freeze, token: token, tag_end: TagEnd.inspect))
|
||||
end
|
||||
|
||||
def raise_missing_variable_terminator(token, options)
|
||||
raise SyntaxError.new(options[:locale].t("errors.syntax.variable_termination".freeze, token: token, tag_end: VariableEnd.inspect))
|
||||
def raise_missing_variable_terminator(token, parse_context)
|
||||
raise SyntaxError.new(parse_context.locale.t("errors.syntax.variable_termination".freeze, token: token, tag_end: VariableEnd.inspect))
|
||||
end
|
||||
|
||||
def registered_tags
|
||||
|
||||
@@ -13,13 +13,14 @@ module Liquid
|
||||
# context['bob'] #=> nil class Context
|
||||
class Context
|
||||
attr_reader :scopes, :errors, :registers, :environments, :resource_limits
|
||||
attr_accessor :exception_handler, :template_name
|
||||
attr_accessor :exception_handler, :template_name, :partial, :global_filter, :strict_variables, :strict_filters
|
||||
|
||||
def initialize(environments = {}, outer_scope = {}, registers = {}, rethrow_errors = false, resource_limits = nil)
|
||||
@environments = [environments].flatten
|
||||
@scopes = [(outer_scope || {})]
|
||||
@registers = registers
|
||||
@errors = []
|
||||
@partial = false
|
||||
@resource_limits = resource_limits || ResourceLimits.new(Template.default_resource_limits)
|
||||
squash_instance_assigns_with_environments
|
||||
|
||||
@@ -31,6 +32,11 @@ module Liquid
|
||||
|
||||
@interrupts = []
|
||||
@filters = []
|
||||
@global_filter = nil
|
||||
end
|
||||
|
||||
def warnings
|
||||
@warnings ||= []
|
||||
end
|
||||
|
||||
def strainer
|
||||
@@ -47,6 +53,10 @@ module Liquid
|
||||
@strainer = nil
|
||||
end
|
||||
|
||||
def apply_global_filter(obj)
|
||||
global_filter.nil? ? obj : global_filter.call(obj)
|
||||
end
|
||||
|
||||
# are there any not handled interrupts?
|
||||
def interrupt?
|
||||
!@interrupts.empty?
|
||||
@@ -62,10 +72,10 @@ module Liquid
|
||||
@interrupts.pop
|
||||
end
|
||||
|
||||
def handle_error(e, token = nil)
|
||||
def handle_error(e, line_number = nil)
|
||||
if e.is_a?(Liquid::Error)
|
||||
e.template_name = template_name
|
||||
e.set_line_number_from_token(token)
|
||||
e.template_name ||= template_name
|
||||
e.line_number ||= line_number
|
||||
end
|
||||
|
||||
output = nil
|
||||
@@ -75,7 +85,10 @@ module Liquid
|
||||
case result
|
||||
when Exception
|
||||
e = result
|
||||
e.set_line_number_from_token(token) if e.is_a?(Liquid::Error)
|
||||
if e.is_a?(Liquid::Error)
|
||||
e.template_name ||= template_name
|
||||
e.line_number ||= line_number
|
||||
end
|
||||
when String
|
||||
output = result
|
||||
else
|
||||
@@ -192,7 +205,13 @@ module Liquid
|
||||
end
|
||||
|
||||
def lookup_and_evaluate(obj, key)
|
||||
if (value = obj[key]).is_a?(Proc) && obj.respond_to?(:[]=)
|
||||
if @strict_variables && obj.respond_to?(:key?) && !obj.key?(key)
|
||||
raise Liquid::UndefinedVariable, "undefined variable #{key}"
|
||||
end
|
||||
|
||||
value = obj[key]
|
||||
|
||||
if value.is_a?(Proc) && obj.respond_to?(:[]=)
|
||||
obj[key] = (value.arity == 0) ? value.call : value.call(self)
|
||||
else
|
||||
value
|
||||
|
||||
@@ -1,27 +1,26 @@
|
||||
module Liquid
|
||||
class Document < BlockBody
|
||||
DEFAULT_OPTIONS = {
|
||||
locale: I18n.new
|
||||
}
|
||||
|
||||
def self.parse(tokens, options)
|
||||
def self.parse(tokens, parse_context)
|
||||
doc = new
|
||||
doc.parse(tokens, DEFAULT_OPTIONS.merge(options))
|
||||
doc.parse(tokens, parse_context)
|
||||
doc
|
||||
end
|
||||
|
||||
def parse(tokens, options)
|
||||
def parse(tokens, parse_context)
|
||||
super do |end_tag_name, end_tag_params|
|
||||
unknown_tag(end_tag_name, options) if end_tag_name
|
||||
unknown_tag(end_tag_name, parse_context) if end_tag_name
|
||||
end
|
||||
rescue SyntaxError => e
|
||||
e.line_number ||= parse_context.line_number
|
||||
raise
|
||||
end
|
||||
|
||||
def unknown_tag(tag, options)
|
||||
def unknown_tag(tag, parse_context)
|
||||
case tag
|
||||
when 'else'.freeze, 'end'.freeze
|
||||
raise SyntaxError.new(options[:locale].t("errors.syntax.unexpected_outer_tag".freeze, tag: tag))
|
||||
raise SyntaxError.new(parse_context.locale.t("errors.syntax.unexpected_outer_tag".freeze, tag: tag))
|
||||
else
|
||||
raise SyntaxError.new(options[:locale].t("errors.syntax.unknown_tag".freeze, tag: tag))
|
||||
raise SyntaxError.new(parse_context.locale.t("errors.syntax.unknown_tag".freeze, tag: tag))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -18,24 +18,23 @@ module Liquid
|
||||
# tmpl = Liquid::Template.parse( ' {% for product in product.top_sales %} {{ product.name }} {%endfor%} ' )
|
||||
# tmpl.render('product' => ProductDrop.new ) # will invoke top_sales query.
|
||||
#
|
||||
# Your drop can either implement the methods sans any parameters or implement the before_method(name) method which is a
|
||||
# catch all.
|
||||
# Your drop can either implement the methods sans any parameters
|
||||
# or implement the liquid_method_missing(name) method which is a catch all.
|
||||
class Drop
|
||||
attr_writer :context
|
||||
|
||||
EMPTY_STRING = ''.freeze
|
||||
|
||||
# Catch all for the method
|
||||
def before_method(_method)
|
||||
nil
|
||||
def liquid_method_missing(method)
|
||||
return nil unless @context && @context.strict_variables
|
||||
raise Liquid::UndefinedDropMethod, "undefined method #{method}"
|
||||
end
|
||||
|
||||
# called by liquid to invoke a drop
|
||||
def invoke_drop(method_or_key)
|
||||
if method_or_key && method_or_key != EMPTY_STRING && self.class.invokable?(method_or_key)
|
||||
if self.class.invokable?(method_or_key)
|
||||
send(method_or_key)
|
||||
else
|
||||
before_method(method_or_key)
|
||||
liquid_method_missing(method_or_key)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -17,12 +17,6 @@ module Liquid
|
||||
str
|
||||
end
|
||||
|
||||
def set_line_number_from_token(token)
|
||||
return unless token.respond_to?(:line_number)
|
||||
return if line_number
|
||||
self.line_number = token.line_number
|
||||
end
|
||||
|
||||
def self.render(e)
|
||||
if e.is_a?(Liquid::Error)
|
||||
e.to_s
|
||||
@@ -62,4 +56,8 @@ module Liquid
|
||||
MemoryError = Class.new(Error)
|
||||
ZeroDivisionError = Class.new(Error)
|
||||
FloatDomainError = Class.new(Error)
|
||||
UndefinedVariable = Class.new(Error)
|
||||
UndefinedDropMethod = Class.new(Error)
|
||||
UndefinedFilter = Class.new(Error)
|
||||
MethodOverrideError = Class.new(Error)
|
||||
end
|
||||
|
||||
@@ -7,44 +7,44 @@ class String # :nodoc:
|
||||
end
|
||||
end
|
||||
|
||||
class Array # :nodoc:
|
||||
class Array # :nodoc:
|
||||
def to_liquid
|
||||
self
|
||||
end
|
||||
end
|
||||
|
||||
class Hash # :nodoc:
|
||||
class Hash # :nodoc:
|
||||
def to_liquid
|
||||
self
|
||||
end
|
||||
end
|
||||
|
||||
class Numeric # :nodoc:
|
||||
class Numeric # :nodoc:
|
||||
def to_liquid
|
||||
self
|
||||
end
|
||||
end
|
||||
|
||||
class Time # :nodoc:
|
||||
class Time # :nodoc:
|
||||
def to_liquid
|
||||
self
|
||||
end
|
||||
end
|
||||
|
||||
class DateTime < Date # :nodoc:
|
||||
class DateTime < Date # :nodoc:
|
||||
def to_liquid
|
||||
self
|
||||
end
|
||||
end
|
||||
|
||||
class Date # :nodoc:
|
||||
class Date # :nodoc:
|
||||
def to_liquid
|
||||
self
|
||||
end
|
||||
end
|
||||
|
||||
class TrueClass
|
||||
def to_liquid # :nodoc:
|
||||
def to_liquid # :nodoc:
|
||||
self
|
||||
end
|
||||
end
|
||||
|
||||
@@ -65,7 +65,7 @@ module Liquid
|
||||
File.join(root, @pattern % template_path)
|
||||
end
|
||||
|
||||
raise FileSystemError, "Illegal template path '#{File.expand_path(full_path)}'" unless File.expand_path(full_path) =~ /\A#{File.expand_path(root)}/
|
||||
raise FileSystemError, "Illegal template path '#{File.expand_path(full_path)}'" unless File.expand_path(full_path).start_with?(File.expand_path(root))
|
||||
|
||||
full_path
|
||||
end
|
||||
|
||||
42
lib/liquid/forloop_drop.rb
Normal file
42
lib/liquid/forloop_drop.rb
Normal file
@@ -0,0 +1,42 @@
|
||||
module Liquid
|
||||
class ForloopDrop < Drop
|
||||
def initialize(name, length, parentloop)
|
||||
@name = name
|
||||
@length = length
|
||||
@parentloop = parentloop
|
||||
@index = 0
|
||||
end
|
||||
|
||||
attr_reader :name, :length, :parentloop
|
||||
|
||||
def index
|
||||
@index + 1
|
||||
end
|
||||
|
||||
def index0
|
||||
@index
|
||||
end
|
||||
|
||||
def rindex
|
||||
@length - @index
|
||||
end
|
||||
|
||||
def rindex0
|
||||
@length - @index - 1
|
||||
end
|
||||
|
||||
def first
|
||||
@index == 0
|
||||
end
|
||||
|
||||
def last
|
||||
@index == @length - 1
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def increment!
|
||||
@index += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
37
lib/liquid/parse_context.rb
Normal file
37
lib/liquid/parse_context.rb
Normal file
@@ -0,0 +1,37 @@
|
||||
module Liquid
|
||||
class ParseContext
|
||||
attr_accessor :partial, :locale, :line_number
|
||||
attr_reader :warnings, :error_mode
|
||||
|
||||
def initialize(options = {})
|
||||
@template_options = options ? options.dup : {}
|
||||
@locale = @template_options[:locale] ||= I18n.new
|
||||
@warnings = []
|
||||
self.partial = false
|
||||
end
|
||||
|
||||
def [](option_key)
|
||||
@options[option_key]
|
||||
end
|
||||
|
||||
def partial=(value)
|
||||
@partial = value
|
||||
@options = value ? partial_options : @template_options
|
||||
@error_mode = @options[:error_mode] || Template.error_mode
|
||||
value
|
||||
end
|
||||
|
||||
def partial_options
|
||||
@partial_options ||= begin
|
||||
dont_pass = @template_options[:include_options_blacklist]
|
||||
if dont_pass == true
|
||||
{ locale: locale }
|
||||
elsif dont_pass.is_a?(Array)
|
||||
@template_options.reject { |k, v| dont_pass.include?(k) }
|
||||
else
|
||||
@template_options
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -75,7 +75,7 @@ module Liquid
|
||||
|
||||
def variable_signature
|
||||
str = consume(:id)
|
||||
if look(:open_square)
|
||||
while look(:open_square)
|
||||
str << consume
|
||||
str << expression
|
||||
str << consume(:close_square)
|
||||
|
||||
@@ -1,16 +1,14 @@
|
||||
module Liquid
|
||||
module ParserSwitching
|
||||
def parse_with_selected_parser(markup)
|
||||
case @options[:error_mode] || Template.error_mode
|
||||
case parse_context.error_mode
|
||||
when :strict then strict_parse_with_error_context(markup)
|
||||
when :lax then lax_parse(markup)
|
||||
when :warn
|
||||
begin
|
||||
return strict_parse_with_error_context(markup)
|
||||
rescue SyntaxError => e
|
||||
e.set_line_number_from_token(markup)
|
||||
@warnings ||= []
|
||||
@warnings << e
|
||||
parse_context.warnings << e
|
||||
return lax_parse(markup)
|
||||
end
|
||||
end
|
||||
@@ -21,6 +19,7 @@ module Liquid
|
||||
def strict_parse_with_error_context(markup)
|
||||
strict_parse(markup)
|
||||
rescue SyntaxError => e
|
||||
e.line_number = line_number
|
||||
e.markup_context = markup_context(markup)
|
||||
raise e
|
||||
end
|
||||
|
||||
@@ -19,7 +19,7 @@ module Liquid
|
||||
# inside of <tt>{% include %}</tt> tags.
|
||||
#
|
||||
# profile.each do |node|
|
||||
# # Access to the token itself
|
||||
# # Access to the node itself
|
||||
# node.code
|
||||
#
|
||||
# # Which template and line number of this node.
|
||||
@@ -46,15 +46,15 @@ module Liquid
|
||||
class Timing
|
||||
attr_reader :code, :partial, :line_number, :children
|
||||
|
||||
def initialize(token, partial)
|
||||
@code = token.respond_to?(:raw) ? token.raw : token
|
||||
def initialize(node, partial)
|
||||
@code = node.respond_to?(:raw) ? node.raw : node
|
||||
@partial = partial
|
||||
@line_number = token.respond_to?(:line_number) ? token.line_number : nil
|
||||
@line_number = node.respond_to?(:line_number) ? node.line_number : nil
|
||||
@children = []
|
||||
end
|
||||
|
||||
def self.start(token, partial)
|
||||
new(token, partial).tap(&:start)
|
||||
def self.start(node, partial)
|
||||
new(node, partial).tap(&:start)
|
||||
end
|
||||
|
||||
def start
|
||||
@@ -70,11 +70,11 @@ module Liquid
|
||||
end
|
||||
end
|
||||
|
||||
def self.profile_token_render(token)
|
||||
if Profiler.current_profile && token.respond_to?(:render)
|
||||
Profiler.current_profile.start_token(token)
|
||||
def self.profile_node_render(node)
|
||||
if Profiler.current_profile && node.respond_to?(:render)
|
||||
Profiler.current_profile.start_node(node)
|
||||
output = yield
|
||||
Profiler.current_profile.end_token(token)
|
||||
Profiler.current_profile.end_node(node)
|
||||
output
|
||||
else
|
||||
yield
|
||||
@@ -132,11 +132,11 @@ module Liquid
|
||||
@root_timing.children.length
|
||||
end
|
||||
|
||||
def start_token(token)
|
||||
@timing_stack.push(Timing.start(token, current_partial))
|
||||
def start_node(node)
|
||||
@timing_stack.push(Timing.start(node, current_partial))
|
||||
end
|
||||
|
||||
def end_token(_token)
|
||||
def end_node(_node)
|
||||
timing = @timing_stack.pop
|
||||
timing.finish
|
||||
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
module Liquid
|
||||
class BlockBody
|
||||
def render_token_with_profiling(token, context)
|
||||
Profiler.profile_token_render(token) do
|
||||
render_token_without_profiling(token, context)
|
||||
def render_node_with_profiling(node, context)
|
||||
Profiler.profile_node_render(node) do
|
||||
render_node_without_profiling(node, context)
|
||||
end
|
||||
end
|
||||
|
||||
alias_method :render_token_without_profiling, :render_token
|
||||
alias_method :render_token, :render_token_with_profiling
|
||||
alias_method :render_node_without_profiling, :render_node
|
||||
alias_method :render_node, :render_node_with_profiling
|
||||
end
|
||||
|
||||
class Include < Tag
|
||||
|
||||
@@ -16,7 +16,22 @@ module Liquid
|
||||
end
|
||||
|
||||
def evaluate(context)
|
||||
context.evaluate(@start_obj).to_i..context.evaluate(@end_obj).to_i
|
||||
start_int = to_integer(context.evaluate(@start_obj))
|
||||
end_int = to_integer(context.evaluate(@end_obj))
|
||||
start_int..end_int
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def to_integer(input)
|
||||
case input
|
||||
when Integer
|
||||
input
|
||||
when NilClass, String
|
||||
input.to_i
|
||||
else
|
||||
Utils.to_integer(input)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -33,7 +33,7 @@ module Liquid
|
||||
end
|
||||
|
||||
def escape(input)
|
||||
CGI.escapeHTML(input).untaint rescue input
|
||||
CGI.escapeHTML(input).untaint unless input.nil?
|
||||
end
|
||||
alias_method :h, :escape
|
||||
|
||||
@@ -42,12 +42,16 @@ module Liquid
|
||||
end
|
||||
|
||||
def url_encode(input)
|
||||
CGI.escape(input) rescue input
|
||||
CGI.escape(input) unless input.nil?
|
||||
end
|
||||
|
||||
def url_decode(input)
|
||||
CGI.unescape(input) unless input.nil?
|
||||
end
|
||||
|
||||
def slice(input, offset, length = nil)
|
||||
offset = to_integer(offset)
|
||||
length = length ? to_integer(length) : 1
|
||||
offset = Utils.to_integer(offset)
|
||||
length = length ? Utils.to_integer(length) : 1
|
||||
|
||||
if input.is_a?(Array)
|
||||
input.slice(offset, length) || []
|
||||
@@ -59,16 +63,17 @@ module Liquid
|
||||
# Truncate a string down to x characters
|
||||
def truncate(input, length = 50, truncate_string = "...".freeze)
|
||||
return if input.nil?
|
||||
length = to_integer(length)
|
||||
input_str = input.to_s
|
||||
length = Utils.to_integer(length)
|
||||
l = length - truncate_string.length
|
||||
l = 0 if l < 0
|
||||
input.length > length ? input[0...l] + truncate_string : input
|
||||
input_str.length > length ? input_str[0...l] + truncate_string : input_str
|
||||
end
|
||||
|
||||
def truncatewords(input, words = 15, truncate_string = "...".freeze)
|
||||
return if input.nil?
|
||||
wordlist = input.to_s.split
|
||||
words = to_integer(words)
|
||||
words = Utils.to_integer(words)
|
||||
l = words - 1
|
||||
l = 0 if l < 0
|
||||
wordlist.length > l ? wordlist[0..l].join(" ".freeze) + truncate_string : input
|
||||
@@ -116,10 +121,10 @@ module Liquid
|
||||
ary = InputIterator.new(input)
|
||||
if property.nil?
|
||||
ary.sort
|
||||
elsif ary.empty? # The next two cases assume a non-empty array.
|
||||
[]
|
||||
elsif ary.first.respond_to?(:[]) && !ary.first[property].nil?
|
||||
ary.sort { |a, b| a[property] <=> b[property] }
|
||||
elsif ary.first.respond_to?(property)
|
||||
ary.sort { |a, b| a.send(property) <=> b.send(property) }
|
||||
end
|
||||
end
|
||||
|
||||
@@ -130,10 +135,10 @@ module Liquid
|
||||
|
||||
if property.nil?
|
||||
ary.sort { |a, b| a.casecmp(b) }
|
||||
elsif ary.empty? # The next two cases assume a non-empty array.
|
||||
[]
|
||||
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
|
||||
|
||||
@@ -144,6 +149,8 @@ module Liquid
|
||||
|
||||
if property.nil?
|
||||
ary.uniq
|
||||
elsif ary.empty? # The next two cases assume a non-empty array.
|
||||
[]
|
||||
elsif ary.first.respond_to?(:[])
|
||||
ary.uniq{ |a| a[property] }
|
||||
end
|
||||
@@ -163,7 +170,8 @@ module Liquid
|
||||
if property == "to_liquid".freeze
|
||||
e
|
||||
elsif e.respond_to?(:[])
|
||||
e[property]
|
||||
r = e[property]
|
||||
r.is_a?(Proc) ? r.call : r
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -175,10 +183,10 @@ module Liquid
|
||||
|
||||
if property.nil?
|
||||
ary.compact
|
||||
elsif ary.empty? # The next two cases assume a non-empty array.
|
||||
[]
|
||||
elsif ary.first.respond_to?(:[])
|
||||
ary.reject{ |a| a[property].nil? }
|
||||
elsif ary.first.respond_to?(property)
|
||||
ary.reject { |a| a.send(property).nil? }
|
||||
end
|
||||
end
|
||||
|
||||
@@ -255,7 +263,7 @@ module Liquid
|
||||
def date(input, format)
|
||||
return input if format.to_s.empty?
|
||||
|
||||
return input unless date = to_date(input)
|
||||
return input unless date = Utils.to_date(input)
|
||||
|
||||
date.strftime(format.to_s)
|
||||
end
|
||||
@@ -307,7 +315,7 @@ module Liquid
|
||||
end
|
||||
|
||||
def round(input, n = 0)
|
||||
result = to_number(input).round(to_number(n))
|
||||
result = Utils.to_number(input).round(Utils.to_number(n))
|
||||
result = result.to_f if result.is_a?(BigDecimal)
|
||||
result = result.to_i if n == 0
|
||||
result
|
||||
@@ -316,69 +324,29 @@ module Liquid
|
||||
end
|
||||
|
||||
def ceil(input)
|
||||
to_number(input).ceil.to_i
|
||||
Utils.to_number(input).ceil.to_i
|
||||
rescue ::FloatDomainError => e
|
||||
raise Liquid::FloatDomainError, e.message
|
||||
end
|
||||
|
||||
def floor(input)
|
||||
to_number(input).floor.to_i
|
||||
Utils.to_number(input).floor.to_i
|
||||
rescue ::FloatDomainError => e
|
||||
raise Liquid::FloatDomainError, e.message
|
||||
end
|
||||
|
||||
def default(input, default_value = "".freeze)
|
||||
is_blank = input.respond_to?(:empty?) ? input.empty? : !input
|
||||
is_blank ? default_value : input
|
||||
def default(input, default_value = ''.freeze)
|
||||
if !input || input.respond_to?(:empty?) && input.empty?
|
||||
default_value
|
||||
else
|
||||
input
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def to_integer(num)
|
||||
return num if num.is_a?(Integer)
|
||||
num = num.to_s
|
||||
begin
|
||||
Integer(num)
|
||||
rescue ::ArgumentError
|
||||
raise Liquid::ArgumentError, "invalid integer"
|
||||
end
|
||||
end
|
||||
|
||||
def to_number(obj)
|
||||
case obj
|
||||
when Float
|
||||
BigDecimal.new(obj.to_s)
|
||||
when Numeric
|
||||
obj
|
||||
when String
|
||||
(obj.strip =~ /\A\d+\.\d+\z/) ? BigDecimal.new(obj) : obj.to_i
|
||||
else
|
||||
0
|
||||
end
|
||||
end
|
||||
|
||||
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
|
||||
when /\A\d+\z/, Integer
|
||||
Time.at(obj.to_i)
|
||||
when String
|
||||
Time.parse(obj)
|
||||
end
|
||||
rescue ArgumentError
|
||||
nil
|
||||
end
|
||||
|
||||
def apply_operation(input, operand, operation)
|
||||
result = to_number(input).send(operation, to_number(operand))
|
||||
result = Utils.to_number(input).send(operation, Utils.to_number(operand))
|
||||
result.is_a?(BigDecimal) ? result.to_f : result
|
||||
end
|
||||
|
||||
@@ -417,6 +385,11 @@ module Liquid
|
||||
to_a.compact
|
||||
end
|
||||
|
||||
def empty?
|
||||
@input.each { return false }
|
||||
true
|
||||
end
|
||||
|
||||
def each
|
||||
@input.each do |e|
|
||||
yield(e.respond_to?(:to_liquid) ? e.to_liquid : e)
|
||||
|
||||
@@ -26,10 +26,15 @@ module Liquid
|
||||
end
|
||||
|
||||
def self.add_filter(filter)
|
||||
raise ArgumentError, "Expected module but got: #{f.class}" unless filter.is_a?(Module)
|
||||
raise ArgumentError, "Expected module but got: #{filter.class}" unless filter.is_a?(Module)
|
||||
unless self.class.include?(filter)
|
||||
send(:include, filter)
|
||||
@filter_methods.merge(filter.public_instance_methods.map(&:to_s))
|
||||
invokable_non_public_methods = (filter.private_instance_methods + filter.protected_instance_methods).select { |m| invokable?(m) }
|
||||
if invokable_non_public_methods.any?
|
||||
raise MethodOverrideError, "Filter overrides registered public methods as non public: #{invokable_non_public_methods.join(', ')}"
|
||||
else
|
||||
send(:include, filter)
|
||||
@filter_methods.merge(filter.public_instance_methods.map(&:to_s))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -48,11 +53,13 @@ module Liquid
|
||||
def invoke(method, *args)
|
||||
if self.class.invokable?(method)
|
||||
send(method, *args)
|
||||
elsif @context && @context.strict_filters
|
||||
raise Liquid::UndefinedFilter, "undefined filter #{method}"
|
||||
else
|
||||
args.first
|
||||
end
|
||||
rescue ::ArgumentError => e
|
||||
raise Liquid::ArgumentError.new(e.message)
|
||||
raise Liquid::ArgumentError, e.message, e.backtrace
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
62
lib/liquid/tablerowloop_drop.rb
Normal file
62
lib/liquid/tablerowloop_drop.rb
Normal file
@@ -0,0 +1,62 @@
|
||||
module Liquid
|
||||
class TablerowloopDrop < Drop
|
||||
def initialize(length, cols)
|
||||
@length = length
|
||||
@row = 1
|
||||
@col = 1
|
||||
@cols = cols
|
||||
@index = 0
|
||||
end
|
||||
|
||||
attr_reader :length, :col, :row
|
||||
|
||||
def index
|
||||
@index + 1
|
||||
end
|
||||
|
||||
def index0
|
||||
@index
|
||||
end
|
||||
|
||||
def col0
|
||||
@col - 1
|
||||
end
|
||||
|
||||
def rindex
|
||||
@length - @index
|
||||
end
|
||||
|
||||
def rindex0
|
||||
@length - @index - 1
|
||||
end
|
||||
|
||||
def first
|
||||
@index == 0
|
||||
end
|
||||
|
||||
def last
|
||||
@index == @length - 1
|
||||
end
|
||||
|
||||
def col_first
|
||||
@col == 1
|
||||
end
|
||||
|
||||
def col_last
|
||||
@col == @cols
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def increment!
|
||||
@index += 1
|
||||
|
||||
if @col == @cols
|
||||
@col = 1
|
||||
@row += 1
|
||||
else
|
||||
@col += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,23 +1,24 @@
|
||||
module Liquid
|
||||
class Tag
|
||||
attr_accessor :options, :line_number
|
||||
attr_reader :nodelist, :warnings
|
||||
attr_reader :nodelist, :tag_name, :line_number, :parse_context
|
||||
alias_method :options, :parse_context
|
||||
include ParserSwitching
|
||||
|
||||
class << self
|
||||
def parse(tag_name, markup, tokens, options)
|
||||
def parse(tag_name, markup, tokenizer, options)
|
||||
tag = new(tag_name, markup, options)
|
||||
tag.parse(tokens)
|
||||
tag.parse(tokenizer)
|
||||
tag
|
||||
end
|
||||
|
||||
private :new
|
||||
end
|
||||
|
||||
def initialize(tag_name, markup, options)
|
||||
def initialize(tag_name, markup, parse_context)
|
||||
@tag_name = tag_name
|
||||
@markup = markup
|
||||
@options = options
|
||||
@parse_context = parse_context
|
||||
@line_number = parse_context.line_number
|
||||
end
|
||||
|
||||
def parse(_tokens)
|
||||
|
||||
@@ -15,7 +15,6 @@ module Liquid
|
||||
if markup =~ Syntax
|
||||
@to = $1
|
||||
@from = Variable.new($2, options)
|
||||
@from.line_number = line_number
|
||||
else
|
||||
raise SyntaxError.new options[:locale].t("errors.syntax.assign".freeze)
|
||||
end
|
||||
@@ -24,16 +23,28 @@ module Liquid
|
||||
def render(context)
|
||||
val = @from.render(context)
|
||||
context.scopes.last[@to] = val
|
||||
|
||||
inc = val.instance_of?(String) || val.instance_of?(Array) || val.instance_of?(Hash) ? val.length : 1
|
||||
context.resource_limits.assign_score += inc
|
||||
|
||||
context.resource_limits.assign_score += assign_score_of(val)
|
||||
''.freeze
|
||||
end
|
||||
|
||||
def blank?
|
||||
true
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def assign_score_of(val)
|
||||
if val.instance_of?(String)
|
||||
val.length
|
||||
elsif val.instance_of?(Array) || val.instance_of?(Hash)
|
||||
sum = 1
|
||||
# Uses #each to avoid extra allocations.
|
||||
val.each { |child| sum += assign_score_of(child) }
|
||||
sum
|
||||
else
|
||||
1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Template.register_tag('assign'.freeze, Assign)
|
||||
|
||||
@@ -37,7 +37,7 @@ module Liquid
|
||||
iteration = context.registers[:cycle][key]
|
||||
result = context.evaluate(@variables[iteration])
|
||||
iteration += 1
|
||||
iteration = 0 if iteration >= @variables.size
|
||||
iteration = 0 if iteration >= @variables.size
|
||||
context.registers[:cycle][key] = iteration
|
||||
result
|
||||
end
|
||||
|
||||
@@ -67,69 +67,13 @@ module Liquid
|
||||
end
|
||||
|
||||
def render(context)
|
||||
for_offsets = context.registers[:for] ||= Hash.new(0)
|
||||
for_stack = context.registers[:for_stack] ||= []
|
||||
segment = collection_segment(context)
|
||||
|
||||
parent_loop = for_stack.last
|
||||
for_stack.push(nil)
|
||||
|
||||
collection = context.evaluate(@collection_name)
|
||||
collection = collection.to_a if collection.is_a?(Range)
|
||||
|
||||
from = if @from == :continue
|
||||
for_offsets[@name].to_i
|
||||
if segment.empty?
|
||||
render_else(context)
|
||||
else
|
||||
context.evaluate(@from).to_i
|
||||
render_segment(context, segment)
|
||||
end
|
||||
|
||||
limit = context.evaluate(@limit)
|
||||
to = limit ? limit.to_i + from : nil
|
||||
|
||||
segment = Utils.slice_collection(collection, from, to)
|
||||
|
||||
return render_else(context) if segment.empty?
|
||||
|
||||
segment.reverse! if @reversed
|
||||
|
||||
result = ''
|
||||
|
||||
length = segment.length
|
||||
|
||||
# Store our progress through the collection for the continue flag
|
||||
for_offsets[@name] = from + segment.length
|
||||
|
||||
context.stack do
|
||||
segment.each_with_index do |item, index|
|
||||
context[@variable_name] = item
|
||||
loop_vars = {
|
||||
'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
|
||||
}
|
||||
|
||||
context['forloop'.freeze] = loop_vars
|
||||
for_stack[-1] = loop_vars
|
||||
|
||||
result << @for_block.render(context)
|
||||
|
||||
# Handle any interrupts if they exist.
|
||||
if context.interrupt?
|
||||
interrupt = context.pop_interrupt
|
||||
break if interrupt.is_a? BreakInterrupt
|
||||
next if interrupt.is_a? ContinueInterrupt
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
result
|
||||
ensure
|
||||
for_stack.pop
|
||||
end
|
||||
|
||||
protected
|
||||
@@ -152,7 +96,7 @@ module Liquid
|
||||
def strict_parse(markup)
|
||||
p = Parser.new(markup)
|
||||
@variable_name = p.consume(:id)
|
||||
raise SyntaxError.new(options[:locale].t("errors.syntax.for_invalid_in".freeze)) unless p.id?('in'.freeze)
|
||||
raise SyntaxError.new(options[:locale].t("errors.syntax.for_invalid_in".freeze)) unless p.id?('in'.freeze)
|
||||
collection_name = p.expression
|
||||
@name = "#{@variable_name}-#{collection_name}"
|
||||
@collection_name = Expression.parse(collection_name)
|
||||
@@ -170,6 +114,63 @@ module Liquid
|
||||
|
||||
private
|
||||
|
||||
def collection_segment(context)
|
||||
offsets = context.registers[:for] ||= Hash.new(0)
|
||||
|
||||
from = if @from == :continue
|
||||
offsets[@name].to_i
|
||||
else
|
||||
context.evaluate(@from).to_i
|
||||
end
|
||||
|
||||
collection = context.evaluate(@collection_name)
|
||||
collection = collection.to_a if collection.is_a?(Range)
|
||||
|
||||
limit = context.evaluate(@limit)
|
||||
to = limit ? limit.to_i + from : nil
|
||||
|
||||
segment = Utils.slice_collection(collection, from, to)
|
||||
segment.reverse! if @reversed
|
||||
|
||||
offsets[@name] = from + segment.length
|
||||
|
||||
segment
|
||||
end
|
||||
|
||||
def render_segment(context, segment)
|
||||
for_stack = context.registers[:for_stack] ||= []
|
||||
length = segment.length
|
||||
|
||||
result = ''
|
||||
|
||||
context.stack do
|
||||
loop_vars = Liquid::ForloopDrop.new(@name, length, for_stack[-1])
|
||||
|
||||
for_stack.push(loop_vars)
|
||||
|
||||
begin
|
||||
context['forloop'.freeze] = loop_vars
|
||||
|
||||
segment.each_with_index do |item, index|
|
||||
context[@variable_name] = item
|
||||
result << @for_block.render(context)
|
||||
loop_vars.send(:increment!)
|
||||
|
||||
# Handle any interrupts if they exist.
|
||||
if context.interrupt?
|
||||
interrupt = context.pop_interrupt
|
||||
break if interrupt.is_a? BreakInterrupt
|
||||
next if interrupt.is_a? ContinueInterrupt
|
||||
end
|
||||
end
|
||||
ensure
|
||||
for_stack.pop
|
||||
end
|
||||
end
|
||||
|
||||
result
|
||||
end
|
||||
|
||||
def set_attribute(key, expr)
|
||||
case key
|
||||
when 'offset'.freeze
|
||||
|
||||
@@ -53,8 +53,10 @@ module Liquid
|
||||
end
|
||||
|
||||
old_template_name = context.template_name
|
||||
old_partial = context.partial
|
||||
begin
|
||||
context.template_name = template_name
|
||||
context.partial = true
|
||||
context.stack do
|
||||
@attributes.each do |key, value|
|
||||
context[key] = context.evaluate(value)
|
||||
@@ -72,11 +74,15 @@ module Liquid
|
||||
end
|
||||
ensure
|
||||
context.template_name = old_template_name
|
||||
context.partial = old_partial
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
alias_method :parse_context, :options
|
||||
private :parse_context
|
||||
|
||||
def load_cached_partial(template_name, context)
|
||||
cached_partials = context.registers[:cached_partials] || {}
|
||||
|
||||
@@ -84,7 +90,12 @@ module Liquid
|
||||
return cached
|
||||
end
|
||||
source = read_template_from_file_system(context)
|
||||
partial = Liquid::Template.parse(source, pass_options)
|
||||
begin
|
||||
parse_context.partial = true
|
||||
partial = Liquid::Template.parse(source, parse_context)
|
||||
ensure
|
||||
parse_context.partial = false
|
||||
end
|
||||
cached_partials[template_name] = partial
|
||||
context.registers[:cached_partials] = cached_partials
|
||||
partial
|
||||
@@ -95,16 +106,6 @@ module Liquid
|
||||
|
||||
file_system.read_template_file(context.evaluate(@template_name_expr))
|
||||
end
|
||||
|
||||
def pass_options
|
||||
dont_pass = @options[:include_options_blacklist]
|
||||
return { locale: @options[:locale] } if dont_pass == true
|
||||
opts = @options.merge(included: true, include_options_blacklist: false)
|
||||
if dont_pass.is_a?(Array)
|
||||
dont_pass.each { |o| opts.delete(o) }
|
||||
end
|
||||
opts
|
||||
end
|
||||
end
|
||||
|
||||
Template.register_tag('include'.freeze, Include)
|
||||
|
||||
@@ -3,11 +3,11 @@ module Liquid
|
||||
Syntax = /\A\s*\z/
|
||||
FullTokenPossiblyInvalid = /\A(.*)#{TagStart}\s*(\w+)\s*(.*)?#{TagEnd}\z/om
|
||||
|
||||
def initialize(tag_name, markup, options)
|
||||
def initialize(tag_name, markup, parse_context)
|
||||
super
|
||||
|
||||
unless markup =~ Syntax
|
||||
raise SyntaxError.new(@options[:locale].t("errors.syntax.tag_unexpected_args".freeze, tag: tag_name))
|
||||
raise SyntaxError.new(parse_context.locale.t("errors.syntax.tag_unexpected_args".freeze, tag: tag_name))
|
||||
end
|
||||
end
|
||||
|
||||
@@ -21,7 +21,7 @@ module Liquid
|
||||
@body << token unless token.empty?
|
||||
end
|
||||
|
||||
raise SyntaxError.new(@options[:locale].t("errors.syntax.tag_never_closed".freeze, block_name: block_name))
|
||||
raise SyntaxError.new(parse_context.locale.t("errors.syntax.tag_never_closed".freeze, block_name: block_name))
|
||||
end
|
||||
|
||||
def render(_context)
|
||||
|
||||
@@ -28,36 +28,21 @@ module Liquid
|
||||
|
||||
cols = context.evaluate(@attributes['cols'.freeze]).to_i
|
||||
|
||||
row = 1
|
||||
col = 0
|
||||
|
||||
result = "<tr class=\"row1\">\n"
|
||||
context.stack do
|
||||
tablerowloop = Liquid::TablerowloopDrop.new(length, cols)
|
||||
context['tablerowloop'.freeze] = tablerowloop
|
||||
|
||||
collection.each_with_index do |item, index|
|
||||
context[@variable_name] = item
|
||||
context['tablerowloop'.freeze] = {
|
||||
'length'.freeze => length,
|
||||
'index'.freeze => index + 1,
|
||||
'index0'.freeze => index,
|
||||
'col'.freeze => col + 1,
|
||||
'col0'.freeze => col,
|
||||
'rindex'.freeze => length - index,
|
||||
'rindex0'.freeze => length - index - 1,
|
||||
'first'.freeze => (index == 0),
|
||||
'last'.freeze => (index == length - 1),
|
||||
'col_first'.freeze => (col == 0),
|
||||
'col_last'.freeze => (col == cols - 1)
|
||||
}
|
||||
|
||||
col += 1
|
||||
result << "<td class=\"col#{tablerowloop.col}\">" << super << '</td>'
|
||||
|
||||
result << "<td class=\"col#{col}\">" << super << '</td>'
|
||||
|
||||
if col == cols && (index != length - 1)
|
||||
col = 0
|
||||
row += 1
|
||||
result << "</tr>\n<tr class=\"row#{row}\">"
|
||||
if tablerowloop.col_last && !tablerowloop.last
|
||||
result << "</tr>\n<tr class=\"row#{tablerowloop.row + 1}\">"
|
||||
end
|
||||
|
||||
tablerowloop.send(:increment!)
|
||||
end
|
||||
end
|
||||
result << "</tr>\n"
|
||||
|
||||
@@ -14,7 +14,7 @@ module Liquid
|
||||
#
|
||||
class Template
|
||||
attr_accessor :root
|
||||
attr_reader :resource_limits
|
||||
attr_reader :resource_limits, :warnings
|
||||
|
||||
@@file_system = BlankFileSystem.new
|
||||
|
||||
@@ -116,16 +116,12 @@ module Liquid
|
||||
@options = options
|
||||
@profiling = options[:profile]
|
||||
@line_numbers = options[:line_numbers] || @profiling
|
||||
@root = Document.parse(tokenize(source), options)
|
||||
@warnings = nil
|
||||
parse_context = options.is_a?(ParseContext) ? options : ParseContext.new(options)
|
||||
@root = Document.parse(tokenize(source), parse_context)
|
||||
@warnings = parse_context.warnings
|
||||
self
|
||||
end
|
||||
|
||||
def warnings
|
||||
return [] unless @root
|
||||
@warnings ||= @root.warnings
|
||||
end
|
||||
|
||||
def registers
|
||||
@registers ||= {}
|
||||
end
|
||||
@@ -183,20 +179,10 @@ module Liquid
|
||||
when Hash
|
||||
options = args.pop
|
||||
|
||||
if options[:registers].is_a?(Hash)
|
||||
registers.merge!(options[:registers])
|
||||
end
|
||||
registers.merge!(options[:registers]) if options[:registers].is_a?(Hash)
|
||||
|
||||
if options[:filters]
|
||||
context.add_filters(options[:filters])
|
||||
end
|
||||
|
||||
if options[:exception_handler]
|
||||
context.exception_handler = options[:exception_handler]
|
||||
end
|
||||
when Module
|
||||
context.add_filters(args.pop)
|
||||
when Array
|
||||
apply_options_to_context(context, options)
|
||||
when Module, Array
|
||||
context.add_filters(args.pop)
|
||||
end
|
||||
|
||||
@@ -206,7 +192,7 @@ module Liquid
|
||||
begin
|
||||
# render the nodelist.
|
||||
# for performance reasons we get an array back here. join will make a string out of it.
|
||||
result = with_profiling do
|
||||
result = with_profiling(context) do
|
||||
@root.render(context)
|
||||
end
|
||||
result.respond_to?(:join) ? result.join : result
|
||||
@@ -228,8 +214,8 @@ module Liquid
|
||||
Tokenizer.new(source, @line_numbers)
|
||||
end
|
||||
|
||||
def with_profiling
|
||||
if @profiling && !@options[:included]
|
||||
def with_profiling(context)
|
||||
if @profiling && !context.partial
|
||||
raise "Profiler not loaded, require 'liquid/profiler' first" unless defined?(Liquid::Profiler)
|
||||
|
||||
@profiler = Profiler.new
|
||||
@@ -244,5 +230,13 @@ module Liquid
|
||||
yield
|
||||
end
|
||||
end
|
||||
|
||||
def apply_options_to_context(context, options)
|
||||
context.add_filters(options[:filters]) if options[:filters]
|
||||
context.global_filter = options[:global_filter] if options[:global_filter]
|
||||
context.exception_handler = options[:exception_handler] if options[:exception_handler]
|
||||
context.strict_variables = options[:strict_variables] if options[:strict_variables]
|
||||
context.strict_filters = options[:strict_filters] if options[:strict_filters]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
module Liquid
|
||||
class Token < String
|
||||
attr_reader :line_number
|
||||
|
||||
def initialize(content, line_number)
|
||||
super(content)
|
||||
@line_number = line_number
|
||||
end
|
||||
|
||||
def raw
|
||||
"<raw>"
|
||||
end
|
||||
|
||||
def child(string)
|
||||
Token.new(string, @line_number)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,13 +1,17 @@
|
||||
module Liquid
|
||||
class Tokenizer
|
||||
attr_reader :line_number
|
||||
|
||||
def initialize(source, line_numbers = false)
|
||||
@source = source
|
||||
@line_numbers = line_numbers
|
||||
@line_number = 1 if line_numbers
|
||||
@tokens = tokenize
|
||||
end
|
||||
|
||||
def shift
|
||||
@tokens.shift
|
||||
token = @tokens.shift
|
||||
@line_number += token.count("\n") if @line_number && token
|
||||
token
|
||||
end
|
||||
|
||||
private
|
||||
@@ -17,21 +21,11 @@ module Liquid
|
||||
return [] if @source.to_s.empty?
|
||||
|
||||
tokens = @source.split(TemplateParser)
|
||||
tokens = @line_numbers ? calculate_line_numbers(tokens) : tokens
|
||||
|
||||
# removes the rogue empty element at the beginning of the array
|
||||
tokens.shift if tokens[0] && tokens[0].empty?
|
||||
|
||||
tokens
|
||||
end
|
||||
|
||||
def calculate_line_numbers(tokens)
|
||||
current_line = 1
|
||||
tokens.map do |token|
|
||||
Token.new(token, current_line).tap do
|
||||
current_line += token.count("\n")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -32,5 +32,48 @@ module Liquid
|
||||
|
||||
segments
|
||||
end
|
||||
|
||||
def self.to_integer(num)
|
||||
return num if num.is_a?(Integer)
|
||||
num = num.to_s
|
||||
begin
|
||||
Integer(num)
|
||||
rescue ::ArgumentError
|
||||
raise Liquid::ArgumentError, "invalid integer"
|
||||
end
|
||||
end
|
||||
|
||||
def self.to_number(obj)
|
||||
case obj
|
||||
when Float
|
||||
BigDecimal.new(obj.to_s)
|
||||
when Numeric
|
||||
obj
|
||||
when String
|
||||
(obj.strip =~ /\A\d+\.\d+\z/) ? BigDecimal.new(obj) : obj.to_i
|
||||
else
|
||||
0
|
||||
end
|
||||
end
|
||||
|
||||
def self.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
|
||||
when /\A\d+\z/, Integer
|
||||
Time.at(obj.to_i)
|
||||
when String
|
||||
Time.parse(obj)
|
||||
end
|
||||
rescue ::ArgumentError
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -11,14 +11,16 @@ module Liquid
|
||||
#
|
||||
class Variable
|
||||
FilterParser = /(?:\s+|#{QuotedFragment}|#{ArgumentSeparator})+/o
|
||||
attr_accessor :filters, :name, :warnings
|
||||
attr_accessor :line_number
|
||||
attr_accessor :filters, :name, :line_number
|
||||
attr_reader :parse_context
|
||||
alias_method :options, :parse_context
|
||||
include ParserSwitching
|
||||
|
||||
def initialize(markup, options = {})
|
||||
def initialize(markup, parse_context)
|
||||
@markup = markup
|
||||
@name = nil
|
||||
@options = options || {}
|
||||
@parse_context = parse_context
|
||||
@line_number = parse_context.line_number
|
||||
|
||||
parse_with_selected_parser(markup)
|
||||
end
|
||||
@@ -71,10 +73,16 @@ module Liquid
|
||||
end
|
||||
|
||||
def render(context)
|
||||
@filters.inject(context.evaluate(@name)) do |output, (filter_name, filter_args, filter_kwargs)|
|
||||
obj = @filters.inject(context.evaluate(@name)) do |output, (filter_name, filter_args, filter_kwargs)|
|
||||
filter_args = evaluate_filter_expressions(context, filter_args, filter_kwargs)
|
||||
context.invoke(filter_name, output, *filter_args)
|
||||
end.tap{ |obj| taint_check(obj) }
|
||||
end
|
||||
|
||||
obj = context.apply_global_filter(obj)
|
||||
|
||||
taint_check(context, obj)
|
||||
|
||||
obj
|
||||
end
|
||||
|
||||
private
|
||||
@@ -106,17 +114,22 @@ module Liquid
|
||||
parsed_args
|
||||
end
|
||||
|
||||
def taint_check(obj)
|
||||
if obj.tainted?
|
||||
@markup =~ QuotedFragment
|
||||
name = Regexp.last_match(0)
|
||||
case Template.taint_mode
|
||||
when :warn
|
||||
@warnings ||= []
|
||||
@warnings << "variable '#{name}' is tainted and was not escaped"
|
||||
when :error
|
||||
raise TaintedError, "Error - variable '#{name}' is tainted and was not escaped"
|
||||
end
|
||||
def taint_check(context, obj)
|
||||
return unless obj.tainted?
|
||||
return if Template.taint_mode == :lax
|
||||
|
||||
@markup =~ QuotedFragment
|
||||
name = Regexp.last_match(0)
|
||||
|
||||
error = TaintedError.new("variable '#{name}' is tainted and was not escaped")
|
||||
error.line_number = line_number
|
||||
error.template_name = context.template_name
|
||||
|
||||
case Template.taint_mode
|
||||
when :warn
|
||||
context.warnings << error
|
||||
when :error
|
||||
raise error
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -55,9 +55,11 @@ module Liquid
|
||||
object = object.send(key).to_liquid
|
||||
|
||||
# No key was present with the desired value and it wasn't one of the directly supported
|
||||
# keywords either. The only thing we got left is to return nil
|
||||
# keywords either. The only thing we got left is to return nil or
|
||||
# raise an exception if `strict_variables` option is set to true
|
||||
else
|
||||
return nil
|
||||
return nil unless context.strict_variables
|
||||
raise Liquid::UndefinedVariable, "undefined variable #{key}"
|
||||
end
|
||||
|
||||
# If we are dealing with a drop here we have to
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# encoding: utf-8
|
||||
module Liquid
|
||||
VERSION = "4.0.0.alpha"
|
||||
VERSION = "4.0.0.rc1"
|
||||
end
|
||||
|
||||
@@ -18,9 +18,9 @@ Gem::Specification.new do |s|
|
||||
s.required_rubygems_version = ">= 1.3.7"
|
||||
|
||||
s.test_files = Dir.glob("{test}/**/*")
|
||||
s.files = Dir.glob("{lib}/**/*") + %w(MIT-LICENSE README.md)
|
||||
s.files = Dir.glob("{lib}/**/*") + %w(LICENSE README.md)
|
||||
|
||||
s.extra_rdoc_files = ["History.md", "README.md"]
|
||||
s.extra_rdoc_files = ["History.md", "README.md"]
|
||||
|
||||
s.require_path = "lib"
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ class ContextDrop < Liquid::Drop
|
||||
@context['forloop.index']
|
||||
end
|
||||
|
||||
def before_method(method)
|
||||
def liquid_method_missing(method)
|
||||
@context[method]
|
||||
end
|
||||
end
|
||||
@@ -30,8 +30,8 @@ class ProductDrop < Liquid::Drop
|
||||
end
|
||||
|
||||
class CatchallDrop < Liquid::Drop
|
||||
def before_method(method)
|
||||
'method: ' << method.to_s
|
||||
def liquid_method_missing(method)
|
||||
'catchall_method: ' << method.to_s
|
||||
end
|
||||
end
|
||||
|
||||
@@ -59,7 +59,7 @@ class ProductDrop < Liquid::Drop
|
||||
end
|
||||
|
||||
class EnumerableDrop < Liquid::Drop
|
||||
def before_method(method)
|
||||
def liquid_method_missing(method)
|
||||
method
|
||||
end
|
||||
|
||||
@@ -93,7 +93,7 @@ end
|
||||
class RealEnumerableDrop < Liquid::Drop
|
||||
include Enumerable
|
||||
|
||||
def before_method(method)
|
||||
def liquid_method_missing(method)
|
||||
method
|
||||
end
|
||||
|
||||
@@ -124,8 +124,10 @@ class DropsTest < Minitest::Test
|
||||
def test_rendering_warns_on_tainted_attr
|
||||
with_taint_mode(:warn) do
|
||||
tpl = Liquid::Template.parse('{{ product.user_input }}')
|
||||
tpl.render!('product' => ProductDrop.new)
|
||||
assert_match /tainted/, tpl.warnings.first
|
||||
context = Context.new('product' => ProductDrop.new)
|
||||
tpl.render!(context)
|
||||
assert_equal [Liquid::TaintedError], context.warnings.map(&:class)
|
||||
assert_equal "variable 'product.user_input' is tainted and was not escaped", context.warnings.first.to_s(false)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -155,14 +157,14 @@ class DropsTest < Minitest::Test
|
||||
assert_equal ' text1 ', output
|
||||
end
|
||||
|
||||
def test_unknown_method
|
||||
def test_catchall_unknown_method
|
||||
output = Liquid::Template.parse(' {{ product.catchall.unknown }} ').render!('product' => ProductDrop.new)
|
||||
assert_equal ' method: unknown ', output
|
||||
assert_equal ' catchall_method: unknown ', output
|
||||
end
|
||||
|
||||
def test_integer_argument_drop
|
||||
def test_catchall_integer_argument_drop
|
||||
output = Liquid::Template.parse(' {{ product.catchall[8] }} ').render!('product' => ProductDrop.new)
|
||||
assert_equal ' method: 8 ', output
|
||||
assert_equal ' catchall_method: 8 ', output
|
||||
end
|
||||
|
||||
def test_text_array_drop
|
||||
@@ -229,7 +231,7 @@ class DropsTest < Minitest::Test
|
||||
assert_equal '3', Liquid::Template.parse('{{collection.size}}').render!('collection' => EnumerableDrop.new)
|
||||
end
|
||||
|
||||
def test_enumerable_drop_will_invoke_before_method_for_clashing_method_names
|
||||
def test_enumerable_drop_will_invoke_liquid_method_missing_for_clashing_method_names
|
||||
["select", "each", "map", "cycle"].each do |method|
|
||||
assert_equal method.to_s, Liquid::Template.parse("{{collection.#{method}}}").render!('collection' => EnumerableDrop.new)
|
||||
assert_equal method.to_s, Liquid::Template.parse("{{collection[\"#{method}\"]}}").render!('collection' => EnumerableDrop.new)
|
||||
|
||||
@@ -39,13 +39,13 @@ class FiltersTest < Minitest::Test
|
||||
@context['var'] = 1000
|
||||
@context.add_filters(MoneyFilter)
|
||||
|
||||
assert_equal ' 1000$ ', Variable.new("var | money").render(@context)
|
||||
assert_equal ' 1000$ ', Template.parse("{{var | money}}").render(@context)
|
||||
end
|
||||
|
||||
def test_underscore_in_filter_name
|
||||
@context['var'] = 1000
|
||||
@context.add_filters(MoneyFilter)
|
||||
assert_equal ' 1000$ ', Variable.new("var | money_with_underscore").render(@context)
|
||||
assert_equal ' 1000$ ', Template.parse("{{var | money_with_underscore}}").render(@context)
|
||||
end
|
||||
|
||||
def test_second_filter_overwrites_first
|
||||
@@ -53,20 +53,20 @@ class FiltersTest < Minitest::Test
|
||||
@context.add_filters(MoneyFilter)
|
||||
@context.add_filters(CanadianMoneyFilter)
|
||||
|
||||
assert_equal ' 1000$ CAD ', Variable.new("var | money").render(@context)
|
||||
assert_equal ' 1000$ CAD ', Template.parse("{{var | money}}").render(@context)
|
||||
end
|
||||
|
||||
def test_size
|
||||
@context['var'] = 'abcd'
|
||||
@context.add_filters(MoneyFilter)
|
||||
|
||||
assert_equal 4, Variable.new("var | size").render(@context)
|
||||
assert_equal '4', Template.parse("{{var | size}}").render(@context)
|
||||
end
|
||||
|
||||
def test_join
|
||||
@context['var'] = [1, 2, 3, 4]
|
||||
|
||||
assert_equal "1 2 3 4", Variable.new("var | join").render(@context)
|
||||
assert_equal "1 2 3 4", Template.parse("{{var | join}}").render(@context)
|
||||
end
|
||||
|
||||
def test_sort
|
||||
@@ -76,11 +76,11 @@ class FiltersTest < Minitest::Test
|
||||
@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)
|
||||
assert_equal '1 2 3 4', Template.parse("{{numbers | sort | join}}").render(@context)
|
||||
assert_equal 'alphabetic as expected', Template.parse("{{words | sort | join}}").render(@context)
|
||||
assert_equal '3', Template.parse("{{value | sort}}").render(@context)
|
||||
assert_equal 'are flower', Template.parse("{{arrays | sort | join}}").render(@context)
|
||||
assert_equal 'Expected case sensitive', Template.parse("{{case_sensitive | sort | join}}").render(@context)
|
||||
end
|
||||
|
||||
def test_sort_natural
|
||||
@@ -89,19 +89,13 @@ class FiltersTest < Minitest::Test
|
||||
@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)
|
||||
assert_equal 'Assert case Insensitive', Template.parse("{{words | sort_natural | join}}").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'
|
||||
assert_equal 'A b C', Template.parse("{{hashes | sort_natural: 'a' | map: 'a' | join}}").render(@context)
|
||||
|
||||
# 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'
|
||||
assert_equal 'A b C', Template.parse("{{objects | sort_natural: 'a' | map: 'a' | join}}").render(@context)
|
||||
end
|
||||
|
||||
def test_compact
|
||||
@@ -110,49 +104,44 @@ class FiltersTest < Minitest::Test
|
||||
@context['objects'] = [TestObject.new('A'), TestObject.new(nil), TestObject.new('C')]
|
||||
|
||||
# Test strings
|
||||
assert_equal ['a', 'b', 'c'], Variable.new("words | compact").render(@context)
|
||||
assert_equal 'a b c', Template.parse("{{words | compact | join}}").render(@context)
|
||||
|
||||
# Test hashes
|
||||
sorted = Variable.new("hashes | compact: 'a'").render(@context)
|
||||
assert_equal sorted[0]['a'], 'A'
|
||||
assert_equal sorted[1]['a'], 'C'
|
||||
assert_nil sorted[2]
|
||||
assert_equal 'A C', Template.parse("{{hashes | compact: 'a' | map: 'a' | join}}").render(@context)
|
||||
|
||||
# Test objects
|
||||
sorted = Variable.new("objects | compact: 'a'").render(@context)
|
||||
assert_equal sorted[0].a, 'A'
|
||||
assert_equal sorted[1].a, 'C'
|
||||
assert_nil sorted[2]
|
||||
assert_equal 'A C', Template.parse("{{objects | compact: 'a' | map: 'a' | join}}").render(@context)
|
||||
end
|
||||
|
||||
def test_strip_html
|
||||
@context['var'] = "<b>bla blub</a>"
|
||||
|
||||
assert_equal "bla blub", Variable.new("var | strip_html").render(@context)
|
||||
assert_equal "bla blub", Template.parse("{{ var | strip_html }}").render(@context)
|
||||
end
|
||||
|
||||
def test_strip_html_ignore_comments_with_html
|
||||
@context['var'] = "<!-- split and some <ul> tag --><b>bla blub</a>"
|
||||
|
||||
assert_equal "bla blub", Variable.new("var | strip_html").render(@context)
|
||||
assert_equal "bla blub", Template.parse("{{ var | strip_html }}").render(@context)
|
||||
end
|
||||
|
||||
def test_capitalize
|
||||
@context['var'] = "blub"
|
||||
|
||||
assert_equal "Blub", Variable.new("var | capitalize").render(@context)
|
||||
assert_equal "Blub", Template.parse("{{ var | capitalize }}").render(@context)
|
||||
end
|
||||
|
||||
def test_nonexistent_filter_is_ignored
|
||||
@context['var'] = 1000
|
||||
|
||||
assert_equal 1000, Variable.new("var | xyzzy").render(@context)
|
||||
assert_equal '1000', Template.parse("{{ var | xyzzy }}").render(@context)
|
||||
end
|
||||
|
||||
def test_filter_with_keyword_arguments
|
||||
@context['surname'] = 'john'
|
||||
@context['input'] = 'hello %{first_name}, %{last_name}'
|
||||
@context.add_filters(SubstituteFilter)
|
||||
output = Variable.new(%( 'hello %{first_name}, %{last_name}' | substitute: first_name: surname, last_name: 'doe' )).render(@context)
|
||||
output = Template.parse(%({{ input | substitute: first_name: surname, last_name: 'doe' }})).render(@context)
|
||||
assert_equal 'hello john, doe', output
|
||||
end
|
||||
|
||||
@@ -181,7 +170,7 @@ class FiltersInTemplate < Minitest::Test
|
||||
end
|
||||
end # FiltersTest
|
||||
|
||||
class TestObject
|
||||
class TestObject < Liquid::Drop
|
||||
attr_accessor :a
|
||||
def initialize(a)
|
||||
@a = a
|
||||
|
||||
@@ -43,6 +43,14 @@ class OutputTest < Minitest::Test
|
||||
assert_equal expected, Template.parse(text).render!(@assigns)
|
||||
end
|
||||
|
||||
def test_variable_traversing_with_two_brackets
|
||||
text = %({{ site.data.menu[include.menu][include.locale] }})
|
||||
assert_equal "it works!", Template.parse(text).render!(
|
||||
"site" => { "data" => { "menu" => { "foo" => { "bar" => "it works!" } } } },
|
||||
"include" => { "menu" => "foo", "locale" => "bar" }
|
||||
)
|
||||
end
|
||||
|
||||
def test_variable_traversing
|
||||
text = %( {{car.bmw}} {{car.gm}} {{car.bmw}} )
|
||||
|
||||
|
||||
@@ -118,6 +118,7 @@ class StandardFiltersTest < Minitest::Test
|
||||
|
||||
def test_escape
|
||||
assert_equal '<strong>', @filters.escape('<strong>')
|
||||
assert_equal nil, @filters.escape(nil)
|
||||
assert_equal '<strong>', @filters.h('<strong>')
|
||||
end
|
||||
|
||||
@@ -130,6 +131,13 @@ class StandardFiltersTest < Minitest::Test
|
||||
assert_equal nil, @filters.url_encode(nil)
|
||||
end
|
||||
|
||||
def test_url_decode
|
||||
assert_equal 'foo bar', @filters.url_decode('foo+bar')
|
||||
assert_equal 'foo bar', @filters.url_decode('foo%20bar')
|
||||
assert_equal 'foo+1@example.com', @filters.url_decode('foo%2B1%40example.com')
|
||||
assert_equal nil, @filters.url_decode(nil)
|
||||
end
|
||||
|
||||
def test_truncatewords
|
||||
assert_equal 'one two three', @filters.truncatewords('one two three', 4)
|
||||
assert_equal 'one two...', @filters.truncatewords('one two three', 2)
|
||||
@@ -158,6 +166,14 @@ 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")
|
||||
end
|
||||
|
||||
def test_sort_empty_array
|
||||
assert_equal [], @filters.sort([], "a")
|
||||
end
|
||||
|
||||
def test_sort_natural_empty_array
|
||||
assert_equal [], @filters.sort_natural([], "a")
|
||||
end
|
||||
|
||||
def test_legacy_sort_hash
|
||||
assert_equal [{ a: 1, b: 2 }], @filters.sort({ a: 1, b: 2 })
|
||||
end
|
||||
@@ -177,6 +193,14 @@ class StandardFiltersTest < Minitest::Test
|
||||
assert_equal [testdrop], @filters.uniq([testdrop, TestDrop.new], 'test')
|
||||
end
|
||||
|
||||
def test_uniq_empty_array
|
||||
assert_equal [], @filters.uniq([], "a")
|
||||
end
|
||||
|
||||
def test_compact_empty_array
|
||||
assert_equal [], @filters.compact([], "a")
|
||||
end
|
||||
|
||||
def test_reverse
|
||||
assert_equal [4, 3, 2, 1], @filters.reverse([1, 2, 3, 4])
|
||||
end
|
||||
@@ -225,6 +249,19 @@ class StandardFiltersTest < Minitest::Test
|
||||
assert_template_result "testfoo", templ, "procs" => [p]
|
||||
end
|
||||
|
||||
def test_map_over_drops_returning_procs
|
||||
drops = [
|
||||
{
|
||||
"proc" => ->{ "foo" },
|
||||
},
|
||||
{
|
||||
"proc" => ->{ "bar" },
|
||||
},
|
||||
]
|
||||
templ = '{{ drops | map: "proc" }}'
|
||||
assert_template_result "foobar", templ, "drops" => drops
|
||||
end
|
||||
|
||||
def test_map_works_on_enumerables
|
||||
assert_template_result "123", '{{ foo | map: "foo" }}', "foo" => TestEnumerable.new
|
||||
end
|
||||
@@ -238,6 +275,10 @@ class StandardFiltersTest < Minitest::Test
|
||||
assert_template_result 'foobar', '{{ foo | last }}', 'foo' => [ThingWithToLiquid.new]
|
||||
end
|
||||
|
||||
def test_truncate_calls_to_liquid
|
||||
assert_template_result "wo...", '{{ foo | truncate: 5 }}', "foo" => TestThing.new
|
||||
end
|
||||
|
||||
def test_date
|
||||
assert_equal 'May', @filters.date(Time.parse("2006-05-05 10:00:00"), "%B")
|
||||
assert_equal 'June', @filters.date(Time.parse("2006-06-05 10:00:00"), "%B")
|
||||
@@ -263,8 +304,10 @@ class StandardFiltersTest < Minitest::Test
|
||||
|
||||
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")
|
||||
with_timezone("UTC") do
|
||||
assert_equal "07/05/2006", @filters.date(1152098955, "%m/%d/%Y")
|
||||
assert_equal "07/05/2006", @filters.date("1152098955", "%m/%d/%Y")
|
||||
end
|
||||
end
|
||||
|
||||
def test_first_last
|
||||
@@ -417,4 +460,19 @@ class StandardFiltersTest < Minitest::Test
|
||||
def test_cannot_access_private_methods
|
||||
assert_template_result('a', "{{ 'a' | to_number }}")
|
||||
end
|
||||
|
||||
def test_date_raises_nothing
|
||||
assert_template_result('', "{{ '' | date: '%D' }}")
|
||||
assert_template_result('abc', "{{ 'abc' | date: '%D' }}")
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def with_timezone(tz)
|
||||
old_tz = ENV['TZ']
|
||||
ENV['TZ'] = tz
|
||||
yield
|
||||
ensure
|
||||
ENV['TZ'] = old_tz
|
||||
end
|
||||
end # StandardFiltersTest
|
||||
|
||||
@@ -38,6 +38,12 @@ HERE
|
||||
|
||||
def test_for_with_range
|
||||
assert_template_result(' 1 2 3 ', '{%for item in (1..3) %} {{item}} {%endfor%}')
|
||||
|
||||
assert_raises(Liquid::ArgumentError) do
|
||||
Template.parse('{% for i in (a..2) %}{% endfor %}').render!("a" => [1, 2])
|
||||
end
|
||||
|
||||
assert_template_result(' 0 1 2 3 ', '{% for item in (a..3) %} {{item}} {% endfor %}', "a" => "invalid integer")
|
||||
end
|
||||
|
||||
def test_for_with_variable_range
|
||||
|
||||
@@ -29,10 +29,10 @@ class IfElseTagTest < Minitest::Test
|
||||
assert_template_result(' YES ', '{% if a or b %} YES {% endif %}', 'a' => true, 'b' => true)
|
||||
assert_template_result(' YES ', '{% if a or b %} YES {% endif %}', 'a' => true, 'b' => false)
|
||||
assert_template_result(' YES ', '{% if a or b %} YES {% endif %}', 'a' => false, 'b' => true)
|
||||
assert_template_result('', '{% if a or b %} YES {% endif %}', 'a' => false, 'b' => false)
|
||||
assert_template_result('', '{% if a or b %} YES {% endif %}', 'a' => false, 'b' => false)
|
||||
|
||||
assert_template_result(' YES ', '{% if a or b or c %} YES {% endif %}', 'a' => false, 'b' => false, 'c' => true)
|
||||
assert_template_result('', '{% if a or b or c %} YES {% endif %}', 'a' => false, 'b' => false, 'c' => false)
|
||||
assert_template_result('', '{% if a or b or c %} YES {% endif %}', 'a' => false, 'b' => false, 'c' => false)
|
||||
end
|
||||
|
||||
def test_if_or_with_operators
|
||||
|
||||
@@ -2,7 +2,7 @@ require 'test_helper'
|
||||
require 'timeout'
|
||||
|
||||
class TemplateContextDrop < Liquid::Drop
|
||||
def before_method(method)
|
||||
def liquid_method_missing(method)
|
||||
method
|
||||
end
|
||||
|
||||
@@ -27,6 +27,12 @@ class ErroneousDrop < Liquid::Drop
|
||||
end
|
||||
end
|
||||
|
||||
class DropWithUndefinedMethod < Liquid::Drop
|
||||
def foo
|
||||
'foo'
|
||||
end
|
||||
end
|
||||
|
||||
class TemplateTest < Minitest::Test
|
||||
include Liquid
|
||||
|
||||
@@ -133,6 +139,17 @@ class TemplateTest < Minitest::Test
|
||||
refute_nil t.resource_limits.assign_score
|
||||
end
|
||||
|
||||
def test_resource_limits_assign_score_nested
|
||||
t = Template.parse("{% assign foo = 'aaaa' | reverse %}")
|
||||
|
||||
t.resource_limits.assign_score_limit = 3
|
||||
assert_equal "Liquid error: Memory limits exceeded", t.render
|
||||
assert t.resource_limits.reached?
|
||||
|
||||
t.resource_limits.assign_score_limit = 5
|
||||
assert_equal "", t.render!
|
||||
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
|
||||
@@ -211,4 +228,82 @@ class TemplateTest < Minitest::Test
|
||||
end
|
||||
assert exception.is_a?(Liquid::ZeroDivisionError)
|
||||
end
|
||||
|
||||
def test_global_filter_option_on_render
|
||||
global_filter_proc = ->(output) { "#{output} filtered" }
|
||||
rendered_template = Template.parse("{{name}}").render({ "name" => "bob" }, global_filter: global_filter_proc)
|
||||
|
||||
assert_equal 'bob filtered', rendered_template
|
||||
end
|
||||
|
||||
def test_global_filter_option_when_native_filters_exist
|
||||
global_filter_proc = ->(output) { "#{output} filtered" }
|
||||
rendered_template = Template.parse("{{name | upcase}}").render({ "name" => "bob" }, global_filter: global_filter_proc)
|
||||
|
||||
assert_equal 'BOB filtered', rendered_template
|
||||
end
|
||||
|
||||
def test_undefined_variables
|
||||
t = Template.parse("{{x}} {{y}} {{z.a}} {{z.b}} {{z.c.d}}")
|
||||
result = t.render({ 'x' => 33, 'z' => { 'a' => 32, 'c' => { 'e' => 31 } } }, { strict_variables: true })
|
||||
|
||||
assert_equal '33 32 ', result
|
||||
assert_equal 3, t.errors.count
|
||||
assert_instance_of Liquid::UndefinedVariable, t.errors[0]
|
||||
assert_equal 'Liquid error: undefined variable y', t.errors[0].message
|
||||
assert_instance_of Liquid::UndefinedVariable, t.errors[1]
|
||||
assert_equal 'Liquid error: undefined variable b', t.errors[1].message
|
||||
assert_instance_of Liquid::UndefinedVariable, t.errors[2]
|
||||
assert_equal 'Liquid error: undefined variable d', t.errors[2].message
|
||||
end
|
||||
|
||||
def test_undefined_variables_raise
|
||||
t = Template.parse("{{x}} {{y}} {{z.a}} {{z.b}} {{z.c.d}}")
|
||||
|
||||
assert_raises UndefinedVariable do
|
||||
t.render!({ 'x' => 33, 'z' => { 'a' => 32, 'c' => { 'e' => 31 } } }, { strict_variables: true })
|
||||
end
|
||||
end
|
||||
|
||||
def test_undefined_drop_methods
|
||||
d = DropWithUndefinedMethod.new
|
||||
t = Template.new.parse('{{ foo }} {{ woot }}')
|
||||
result = t.render(d, { strict_variables: true })
|
||||
|
||||
assert_equal 'foo ', result
|
||||
assert_equal 1, t.errors.count
|
||||
assert_instance_of Liquid::UndefinedDropMethod, t.errors[0]
|
||||
end
|
||||
|
||||
def test_undefined_drop_methods_raise
|
||||
d = DropWithUndefinedMethod.new
|
||||
t = Template.new.parse('{{ foo }} {{ woot }}')
|
||||
|
||||
assert_raises UndefinedDropMethod do
|
||||
t.render!(d, { strict_variables: true })
|
||||
end
|
||||
end
|
||||
|
||||
def test_undefined_filters
|
||||
t = Template.parse("{{a}} {{x | upcase | somefilter1 | somefilter2 | somefilter3}}")
|
||||
filters = Module.new do
|
||||
def somefilter3(v)
|
||||
"-#{v}-"
|
||||
end
|
||||
end
|
||||
result = t.render({ 'a' => 123, 'x' => 'foo' }, { filters: [filters], strict_filters: true })
|
||||
|
||||
assert_equal '123 ', result
|
||||
assert_equal 1, t.errors.count
|
||||
assert_instance_of Liquid::UndefinedFilter, t.errors[0]
|
||||
assert_equal 'Liquid error: undefined filter somefilter1', t.errors[0].message
|
||||
end
|
||||
|
||||
def test_undefined_filters_raise
|
||||
t = Template.parse("{{x | somefilter1 | upcase | somefilter2}}")
|
||||
|
||||
assert_raises UndefinedFilter do
|
||||
t.render!({ 'x' => 'foo' }, { strict_filters: true })
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -267,7 +267,7 @@ class ContextUnitTest < Minitest::Test
|
||||
|
||||
def test_access_hashes_with_hash_notation
|
||||
@context['products'] = { 'count' => 5, 'tags' => ['deepsnow', 'freestyle'] }
|
||||
@context['product'] = { 'variants' => [ { 'title' => 'draft151cm' }, { 'title' => 'element151cm' } ] }
|
||||
@context['product'] = { 'variants' => [ { 'title' => 'draft151cm' }, { 'title' => 'element151cm' } ] }
|
||||
|
||||
assert_equal 5, @context['products["count"]']
|
||||
assert_equal 'deepsnow', @context['products["tags"][0]']
|
||||
@@ -305,7 +305,7 @@ class ContextUnitTest < Minitest::Test
|
||||
end
|
||||
|
||||
def test_first_can_appear_in_middle_of_callchain
|
||||
@context['product'] = { 'variants' => [ { 'title' => 'draft151cm' }, { 'title' => 'element151cm' } ] }
|
||||
@context['product'] = { 'variants' => [ { 'title' => 'draft151cm' }, { 'title' => 'element151cm' } ] }
|
||||
|
||||
assert_equal 'draft151cm', @context['product.variants[0].title']
|
||||
assert_equal 'element151cm', @context['product.variants[1].title']
|
||||
@@ -466,4 +466,18 @@ class ContextUnitTest < Minitest::Test
|
||||
assert contx
|
||||
assert_nil contx['poutine']
|
||||
end
|
||||
|
||||
def test_apply_global_filter
|
||||
global_filter_proc = ->(output) { "#{output} filtered" }
|
||||
|
||||
context = Context.new
|
||||
context.global_filter = global_filter_proc
|
||||
|
||||
assert_equal 'hi filtered', context.apply_global_filter('hi')
|
||||
end
|
||||
|
||||
def test_apply_global_filter_when_no_global_filter_exist
|
||||
context = Context.new
|
||||
assert_equal 'hi', context.apply_global_filter('hi')
|
||||
end
|
||||
end # ContextTest
|
||||
|
||||
@@ -29,6 +29,18 @@ class StrainerUnitTest < Minitest::Test
|
||||
end
|
||||
end
|
||||
|
||||
def test_stainer_argument_error_contains_backtrace
|
||||
strainer = Strainer.create(nil)
|
||||
begin
|
||||
strainer.invoke("public_filter", 1)
|
||||
rescue Liquid::ArgumentError => e
|
||||
assert_match(
|
||||
/\ALiquid error: wrong number of arguments \((1 for 0|given 1, expected 0)\)\z/,
|
||||
e.message)
|
||||
assert_equal e.backtrace[0].split(':')[0], __FILE__
|
||||
end
|
||||
end
|
||||
|
||||
def test_strainer_only_invokes_public_filter_methods
|
||||
strainer = Strainer.create(nil)
|
||||
assert_equal false, strainer.class.invokable?('__test__')
|
||||
@@ -65,4 +77,60 @@ class StrainerUnitTest < Minitest::Test
|
||||
assert_kind_of b, strainer
|
||||
assert_kind_of Liquid::StandardFilters, strainer
|
||||
end
|
||||
|
||||
def test_add_filter_when_wrong_filter_class
|
||||
c = Context.new
|
||||
s = c.strainer
|
||||
wrong_filter = ->(v) { v.reverse }
|
||||
|
||||
assert_raises ArgumentError do
|
||||
s.class.add_filter(wrong_filter)
|
||||
end
|
||||
end
|
||||
|
||||
module PrivateMethodOverrideFilter
|
||||
private
|
||||
|
||||
def public_filter
|
||||
"overriden as private"
|
||||
end
|
||||
end
|
||||
|
||||
def test_add_filter_raises_when_module_privately_overrides_registered_public_methods
|
||||
strainer = Context.new.strainer
|
||||
|
||||
error = assert_raises(Liquid::MethodOverrideError) do
|
||||
strainer.class.add_filter(PrivateMethodOverrideFilter)
|
||||
end
|
||||
assert_equal 'Liquid error: Filter overrides registered public methods as non public: public_filter', error.message
|
||||
end
|
||||
|
||||
module ProtectedMethodOverrideFilter
|
||||
protected
|
||||
|
||||
def public_filter
|
||||
"overriden as protected"
|
||||
end
|
||||
end
|
||||
|
||||
def test_add_filter_raises_when_module_overrides_registered_public_method_as_protected
|
||||
strainer = Context.new.strainer
|
||||
|
||||
error = assert_raises(Liquid::MethodOverrideError) do
|
||||
strainer.class.add_filter(ProtectedMethodOverrideFilter)
|
||||
end
|
||||
assert_equal 'Liquid error: Filter overrides registered public methods as non public: public_filter', error.message
|
||||
end
|
||||
|
||||
module PublicMethodOverrideFilter
|
||||
def public_filter
|
||||
"public"
|
||||
end
|
||||
end
|
||||
|
||||
def test_add_filter_does_not_raise_when_module_overrides_previously_registered_method
|
||||
strainer = Context.new.strainer
|
||||
strainer.class.add_filter(PublicMethodOverrideFilter)
|
||||
assert strainer.class.filter_methods.include?('public_filter')
|
||||
end
|
||||
end # StrainerTest
|
||||
|
||||
@@ -4,13 +4,18 @@ class TagUnitTest < Minitest::Test
|
||||
include Liquid
|
||||
|
||||
def test_tag
|
||||
tag = Tag.parse('tag', [], [], {})
|
||||
tag = Tag.parse('tag', "", Tokenizer.new(""), ParseContext.new)
|
||||
assert_equal 'liquid::tag', tag.name
|
||||
assert_equal '', tag.render(Context.new)
|
||||
end
|
||||
|
||||
def test_return_raw_text_of_tag
|
||||
tag = Tag.parse("long_tag", "param1, param2, param3", [], {})
|
||||
tag = Tag.parse("long_tag", "param1, param2, param3", Tokenizer.new(""), ParseContext.new)
|
||||
assert_equal("long_tag param1, param2, param3", tag.raw)
|
||||
end
|
||||
|
||||
def test_tag_name_should_return_name_of_the_tag
|
||||
tag = Tag.parse("some_tag", "", Tokenizer.new(""), ParseContext.new)
|
||||
assert_equal 'some_tag', tag.tag_name
|
||||
end
|
||||
end
|
||||
|
||||
@@ -22,20 +22,34 @@ class TokenizerTest < Minitest::Test
|
||||
end
|
||||
|
||||
def test_calculate_line_numbers_per_token_with_profiling
|
||||
assert_equal [1], tokenize("{{funk}}", true).map(&:line_number)
|
||||
assert_equal [1, 1, 1], tokenize(" {{funk}} ", true).map(&:line_number)
|
||||
assert_equal [1, 2, 2], tokenize("\n{{funk}}\n", true).map(&:line_number)
|
||||
assert_equal [1, 1, 3], tokenize(" {{\n funk \n}} ", true).map(&:line_number)
|
||||
assert_equal [1], tokenize_line_numbers("{{funk}}")
|
||||
assert_equal [1, 1, 1], tokenize_line_numbers(" {{funk}} ")
|
||||
assert_equal [1, 2, 2], tokenize_line_numbers("\n{{funk}}\n")
|
||||
assert_equal [1, 1, 3], tokenize_line_numbers(" {{\n funk \n}} ")
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def tokenize(source, line_numbers = false)
|
||||
tokenizer = Liquid::Tokenizer.new(source, line_numbers)
|
||||
def tokenize(source)
|
||||
tokenizer = Liquid::Tokenizer.new(source)
|
||||
tokens = []
|
||||
while t = tokenizer.shift
|
||||
tokens << t
|
||||
end
|
||||
tokens
|
||||
end
|
||||
|
||||
def tokenize_line_numbers(source)
|
||||
tokenizer = Liquid::Tokenizer.new(source, true)
|
||||
line_numbers = []
|
||||
loop do
|
||||
line_number = tokenizer.line_number
|
||||
if tokenizer.shift
|
||||
line_numbers << line_number
|
||||
else
|
||||
break
|
||||
end
|
||||
end
|
||||
line_numbers
|
||||
end
|
||||
end
|
||||
|
||||
@@ -4,133 +4,133 @@ class VariableUnitTest < Minitest::Test
|
||||
include Liquid
|
||||
|
||||
def test_variable
|
||||
var = Variable.new('hello')
|
||||
var = create_variable('hello')
|
||||
assert_equal VariableLookup.new('hello'), var.name
|
||||
end
|
||||
|
||||
def test_filters
|
||||
var = Variable.new('hello | textileze')
|
||||
var = create_variable('hello | textileze')
|
||||
assert_equal VariableLookup.new('hello'), var.name
|
||||
assert_equal [['textileze', []]], var.filters
|
||||
|
||||
var = Variable.new('hello | textileze | paragraph')
|
||||
var = create_variable('hello | textileze | paragraph')
|
||||
assert_equal VariableLookup.new('hello'), var.name
|
||||
assert_equal [['textileze', []], ['paragraph', []]], var.filters
|
||||
|
||||
var = Variable.new(%( hello | strftime: '%Y'))
|
||||
var = create_variable(%( hello | strftime: '%Y'))
|
||||
assert_equal VariableLookup.new('hello'), var.name
|
||||
assert_equal [['strftime', ['%Y']]], var.filters
|
||||
|
||||
var = Variable.new(%( 'typo' | link_to: 'Typo', true ))
|
||||
var = create_variable(%( 'typo' | link_to: 'Typo', true ))
|
||||
assert_equal 'typo', var.name
|
||||
assert_equal [['link_to', ['Typo', true]]], var.filters
|
||||
|
||||
var = Variable.new(%( 'typo' | link_to: 'Typo', false ))
|
||||
var = create_variable(%( 'typo' | link_to: 'Typo', false ))
|
||||
assert_equal 'typo', var.name
|
||||
assert_equal [['link_to', ['Typo', false]]], var.filters
|
||||
|
||||
var = Variable.new(%( 'foo' | repeat: 3 ))
|
||||
var = create_variable(%( 'foo' | repeat: 3 ))
|
||||
assert_equal 'foo', var.name
|
||||
assert_equal [['repeat', [3]]], var.filters
|
||||
|
||||
var = Variable.new(%( 'foo' | repeat: 3, 3 ))
|
||||
var = create_variable(%( 'foo' | repeat: 3, 3 ))
|
||||
assert_equal 'foo', var.name
|
||||
assert_equal [['repeat', [3, 3]]], var.filters
|
||||
|
||||
var = Variable.new(%( 'foo' | repeat: 3, 3, 3 ))
|
||||
var = create_variable(%( 'foo' | repeat: 3, 3, 3 ))
|
||||
assert_equal 'foo', var.name
|
||||
assert_equal [['repeat', [3, 3, 3]]], var.filters
|
||||
|
||||
var = Variable.new(%( hello | strftime: '%Y, okay?'))
|
||||
var = create_variable(%( hello | strftime: '%Y, okay?'))
|
||||
assert_equal VariableLookup.new('hello'), var.name
|
||||
assert_equal [['strftime', ['%Y, okay?']]], var.filters
|
||||
|
||||
var = Variable.new(%( hello | things: "%Y, okay?", 'the other one'))
|
||||
var = create_variable(%( hello | things: "%Y, okay?", 'the other one'))
|
||||
assert_equal VariableLookup.new('hello'), var.name
|
||||
assert_equal [['things', ['%Y, okay?', 'the other one']]], var.filters
|
||||
end
|
||||
|
||||
def test_filter_with_date_parameter
|
||||
var = Variable.new(%( '2006-06-06' | date: "%m/%d/%Y"))
|
||||
var = create_variable(%( '2006-06-06' | date: "%m/%d/%Y"))
|
||||
assert_equal '2006-06-06', var.name
|
||||
assert_equal [['date', ['%m/%d/%Y']]], var.filters
|
||||
end
|
||||
|
||||
def test_filters_without_whitespace
|
||||
var = Variable.new('hello | textileze | paragraph')
|
||||
var = create_variable('hello | textileze | paragraph')
|
||||
assert_equal VariableLookup.new('hello'), var.name
|
||||
assert_equal [['textileze', []], ['paragraph', []]], var.filters
|
||||
|
||||
var = Variable.new('hello|textileze|paragraph')
|
||||
var = create_variable('hello|textileze|paragraph')
|
||||
assert_equal VariableLookup.new('hello'), var.name
|
||||
assert_equal [['textileze', []], ['paragraph', []]], var.filters
|
||||
|
||||
var = Variable.new("hello|replace:'foo','bar'|textileze")
|
||||
var = create_variable("hello|replace:'foo','bar'|textileze")
|
||||
assert_equal VariableLookup.new('hello'), var.name
|
||||
assert_equal [['replace', ['foo', 'bar']], ['textileze', []]], var.filters
|
||||
end
|
||||
|
||||
def test_symbol
|
||||
var = Variable.new("http://disney.com/logo.gif | image: 'med' ", error_mode: :lax)
|
||||
var = create_variable("http://disney.com/logo.gif | image: 'med' ", error_mode: :lax)
|
||||
assert_equal VariableLookup.new('http://disney.com/logo.gif'), var.name
|
||||
assert_equal [['image', ['med']]], var.filters
|
||||
end
|
||||
|
||||
def test_string_to_filter
|
||||
var = Variable.new("'http://disney.com/logo.gif' | image: 'med' ")
|
||||
var = create_variable("'http://disney.com/logo.gif' | image: 'med' ")
|
||||
assert_equal 'http://disney.com/logo.gif', var.name
|
||||
assert_equal [['image', ['med']]], var.filters
|
||||
end
|
||||
|
||||
def test_string_single_quoted
|
||||
var = Variable.new(%( "hello" ))
|
||||
var = create_variable(%( "hello" ))
|
||||
assert_equal 'hello', var.name
|
||||
end
|
||||
|
||||
def test_string_double_quoted
|
||||
var = Variable.new(%( 'hello' ))
|
||||
var = create_variable(%( 'hello' ))
|
||||
assert_equal 'hello', var.name
|
||||
end
|
||||
|
||||
def test_integer
|
||||
var = Variable.new(%( 1000 ))
|
||||
var = create_variable(%( 1000 ))
|
||||
assert_equal 1000, var.name
|
||||
end
|
||||
|
||||
def test_float
|
||||
var = Variable.new(%( 1000.01 ))
|
||||
var = create_variable(%( 1000.01 ))
|
||||
assert_equal 1000.01, var.name
|
||||
end
|
||||
|
||||
def test_dashes
|
||||
assert_equal VariableLookup.new('foo-bar'), Variable.new('foo-bar').name
|
||||
assert_equal VariableLookup.new('foo-bar-2'), Variable.new('foo-bar-2').name
|
||||
assert_equal VariableLookup.new('foo-bar'), create_variable('foo-bar').name
|
||||
assert_equal VariableLookup.new('foo-bar-2'), create_variable('foo-bar-2').name
|
||||
|
||||
with_error_mode :strict do
|
||||
assert_raises(Liquid::SyntaxError) { Variable.new('foo - bar') }
|
||||
assert_raises(Liquid::SyntaxError) { Variable.new('-foo') }
|
||||
assert_raises(Liquid::SyntaxError) { Variable.new('2foo') }
|
||||
assert_raises(Liquid::SyntaxError) { create_variable('foo - bar') }
|
||||
assert_raises(Liquid::SyntaxError) { create_variable('-foo') }
|
||||
assert_raises(Liquid::SyntaxError) { create_variable('2foo') }
|
||||
end
|
||||
end
|
||||
|
||||
def test_string_with_special_chars
|
||||
var = Variable.new(%( 'hello! $!@.;"ddasd" ' ))
|
||||
var = create_variable(%( 'hello! $!@.;"ddasd" ' ))
|
||||
assert_equal 'hello! $!@.;"ddasd" ', var.name
|
||||
end
|
||||
|
||||
def test_string_dot
|
||||
var = Variable.new(%( test.test ))
|
||||
var = create_variable(%( test.test ))
|
||||
assert_equal VariableLookup.new('test.test'), var.name
|
||||
end
|
||||
|
||||
def test_filter_with_keyword_arguments
|
||||
var = Variable.new(%( hello | things: greeting: "world", farewell: 'goodbye'))
|
||||
var = create_variable(%( hello | things: greeting: "world", farewell: 'goodbye'))
|
||||
assert_equal VariableLookup.new('hello'), var.name
|
||||
assert_equal [['things', [], { 'greeting' => 'world', 'farewell' => 'goodbye' }]], var.filters
|
||||
end
|
||||
|
||||
def test_lax_filter_argument_parsing
|
||||
var = Variable.new(%( number_of_comments | pluralize: 'comment': 'comments' ), error_mode: :lax)
|
||||
var = create_variable(%( number_of_comments | pluralize: 'comment': 'comments' ), error_mode: :lax)
|
||||
assert_equal VariableLookup.new('number_of_comments'), var.name
|
||||
assert_equal [['pluralize', ['comment', 'comments']]], var.filters
|
||||
end
|
||||
@@ -138,13 +138,13 @@ class VariableUnitTest < Minitest::Test
|
||||
def test_strict_filter_argument_parsing
|
||||
with_error_mode(:strict) do
|
||||
assert_raises(SyntaxError) do
|
||||
Variable.new(%( number_of_comments | pluralize: 'comment': 'comments' ))
|
||||
create_variable(%( number_of_comments | pluralize: 'comment': 'comments' ))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_output_raw_source_of_variable
|
||||
var = Variable.new(%( name_of_variable | upcase ))
|
||||
var = create_variable(%( name_of_variable | upcase ))
|
||||
assert_equal " name_of_variable | upcase ", var.raw
|
||||
end
|
||||
|
||||
@@ -153,4 +153,10 @@ class VariableUnitTest < Minitest::Test
|
||||
assert_equal 'a', lookup.name
|
||||
assert_equal ['b', 'c'], lookup.lookups
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def create_variable(markup, options = {})
|
||||
Variable.new(markup, ParseContext.new(options))
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user