Compare commits

...

98 Commits

Author SHA1 Message Date
Justin Li
622b8011db Install latest bundler on travis 2016-03-21 11:36:50 -04:00
Justin Li
021bafd260 Merge pull request #725 from jeroenvisser101/performance-start-with-vs-regex
Use start_with? instead of Regex
2016-03-21 10:34:28 -04:00
Jeroen Visser
04c393ab07 Use start_with? instead of Regex
Performance is increased by doing this:

  require 'benchmark'
  require 'tempfile'

  n = 50000
  test = File.expand_path(Tempfile.new('foo'))
  Benchmark.bm(20) do |x|
    x.report("Regex:") do
      n.times { test =~ /\A#{test}/ }
    end
    x.report("String#start_with?:") do
      n.times { test =~ test.start_with?(test) }
    end
  end

Benchmark result:
                             user     system      total        real
  Regex:                 0.440000   0.010000   0.450000 (  0.447357)
  String#start_with?:    0.000000   0.000000   0.000000 (  0.006313)
2016-03-21 14:23:35 +01:00
Gaurav Chande
9a7778e52c Merge pull request #707 from Shopify/drop-without-context
@context not always present on a Drop
2016-03-01 18:07:16 -05:00
Gaurav Chande
dde00253f9 context is not always present on a Drop 2016-03-01 21:22:11 +00:00
Gaurav Chande
18d1644980 Merge pull request #705 from Shopify/register-filter-warn
Strainer#add_filter Raises on Private Override
2016-02-24 15:31:41 -05:00
Gaurav Chande
c424d47274 Add changelog entry for Strainer method override change 2016-02-24 20:23:57 +00:00
Gaurav Chande
8e6b9d503d Make Strainer also raise when registered method is overriden as protected 2016-02-24 20:23:49 +00:00
Gaurav Chande
8be38d1795 Strainer#add_filter should raise when registered method is overriden as private 2016-02-24 20:03:17 +00:00
Justin Li
3146d5c3f2 Grammatic and other fixes to CONTRIBUTING.md 2016-02-02 23:45:37 -05:00
Justin Li
0cc8b68a97 Make logic in Context#lookup_and_evaluate more understandable 2016-02-02 23:22:46 -05:00
Justin Li
5a50c12953 Update history to reflect merge of #691
[ci skip]
2016-02-02 23:14:41 -05:00
Justin Li
a6fa4c5c38 Merge pull request #691 from urbandictionary/missing_variables_and_filters
Merge pull request 691
2016-02-02 23:13:44 -05:00
Ivan Kuznetsov
dadd9b4dd2 Add strict_variables/strict_filters render options to check for undefined variables and filters 2016-02-03 10:49:33 +07:00
Justin Li
6434b8d2bb Merge pull request #696 from Shopify/no-send
Remove possibility for arbitrary sends
2016-02-02 11:01:46 -05:00
Justin Li
2d891ddd8f Merge pull request #695 from Shopify/assign-score
Take nested values into account for assign score
2016-02-01 13:14:40 -05:00
Justin Li
60b508b151 Use #each to avoid extra allocations 2016-02-01 13:01:25 -05:00
Justin Li
3891f14a1a Take nested values into account for assign score 2016-02-01 13:01:25 -05:00
Justin Li
198f0aa366 Add test for nested assign score bookkeeping 2016-02-01 13:01:23 -05:00
Justin Li
f2e6adf566 Remove arbitrary send vector 2016-02-01 11:38:40 -05:00
Justin Li
08de6ed2c5 Merge pull request #687 from pathawks/default
Performance improvement: `default` filter
2016-01-24 11:34:05 -05:00
Pat Hawks
7e322f5cf8 Performance improvement: default filter 2016-01-23 23:18:51 -08:00
Justin Li
bf86a5a069 Merge pull request #688 from Shopify/gmp
Install libgmp3-dev in travis
2016-01-23 21:46:37 -05:00
Justin Li
0141444814 Install libgmp3-dev in travis 2016-01-23 21:41:14 -05:00
Justin Li
6d30226768 Update changelog for 4.0.0rc1 2016-01-08 15:08:06 -05:00
Florian Weingarten
63e8bac1a4 meh 2016-01-08 20:00:45 +00:00
Florian Weingarten
8449849ed5 Merge pull request #682 from Shopify/4-pre-beta1
4.0.0.rc1
2016-01-08 20:59:59 +01:00
Florian Weingarten
4bc198a0db 4.0.0.rc1 2016-01-08 19:59:38 +00:00
Florian Weingarten
3921dbe919 Merge pull request #683 from Shopify/dropify-tablerowloop
Liquid::TablerowloopDrop
2016-01-08 20:41:54 +01:00
Florian Weingarten
79e2d1d8b4 Liquid::TablerowloopDrop 2016-01-08 18:46:23 +00:00
Florian Weingarten
b7c4041db8 Merge pull request #681 from Shopify/save-some-loop-allocations
Reuse 'forloop' hash to save memory allocations
2016-01-06 22:47:39 +01:00
Florian Weingarten
e113c891ec Convert forloop hash to drop 2016-01-06 21:30:32 +00:00
Guillaume Malette
a32ad449c0 Merge pull request #672 from Shopify/fix-proc-mapping
Test mapping over procs
2016-01-06 15:59:53 -05:00
Florian Weingarten
1662ba6679 Reuse 'forloop' hash to save memory allocations 2016-01-06 20:30:25 +00:00
Dylan Thacker-Smith
99b5e86f0a Merge pull request #680 from jcheatham/master
Ensure truncate is operating on a string
2015-12-23 18:41:39 -05:00
Jonathan Cheatham
b892a73463 Ensure truncate is operating on a string 2015-12-22 19:40:35 -08:00
Guillaume Malette
0b55d09cea Fix mapping over proc attributes 2015-11-20 13:04:42 -05:00
Dylan Thacker-Smith
5f8086572b Merge pull request #667 from Shopify/remove-empty-string-check
Remove nil and empty string check in invoke_drop.
2015-11-10 10:43:11 -05:00
Dylan Thacker-Smith
bdb9a4a47f Remove nil and empty string check in invoke_drop. 2015-11-09 15:03:36 -05:00
Dylan Thacker-Smith
c38eec0293 Merge pull request #665 from tanelj/escape_filter_nil_fix
Fixed issue where "nil" value for "escape" filter breaks rendering
2015-11-06 10:54:48 -05:00
Tanel Jakobsoo
8d5a907dc8 Fixed issue where "nil" value for "escape" filter breaks rendering
Closes #664
2015-11-06 16:32:02 +02:00
Florian Weingarten
74cc41ce74 Merge pull request #662 from nickpearson/keep-argument-error-backtrace
Keep original stack trace in Liquid::ArgumentError
2015-10-29 15:24:54 +01:00
Thierry Joyal
a120cc587a Merge pull request #661 from Shopify/rename-before-method-as-dynamic-method
Rename before_method as liquid_method_missing
2015-10-29 09:49:15 -04:00
Nick Pearson
c582023321 Keep original stack trace in Liquid::ArgumentError 2015-10-29 08:15:37 -05:00
Thierry Joyal
ac041c4ad1 Rename before_method as liquid_method_missing 2015-10-28 17:28:19 +00:00
Justin Li
31d7682f4e Update history to reflect merge of #658
[ci skip]
2015-10-21 12:50:12 -04:00
Justin Li
5f1acbc086 Merge pull request #658 from Shopify/url_decode-filter
Merge pull request 658
2015-10-21 12:49:14 -04:00
Justin Li
8612716129 Remove rescue in unescape filter 2015-10-21 02:01:21 -04:00
Larry Archer
e6392d1cc1 Tests for new url_decode filter 2015-10-21 01:58:22 -04:00
Larry Archer
04381418d3 Add url_decode filter to accompany url_encode 2015-10-21 01:58:22 -04:00
Justin Li
89ccdabe9a Merge pull request #655 from dijonkitchen/patch-1
Rename MIT-LICENSE to LICENSE
2015-10-14 12:08:37 -04:00
Jonathan Chen
c0fc6777b0 Rename MIT-LICENSE to LICENSE
Standard name format
2015-10-14 12:06:08 -04:00
Justin Li
cd03346239 Update history to reflect merge of #652
[ci skip]
2015-09-29 21:06:21 -04:00
Justin Li
b4f19da127 Merge pull request #652 from mcary/empty-array-sort
Merge pull request 652
2015-09-29 21:05:10 -04:00
Marcel M. Cary
4100f8d641 Fix "sort" filter on empty array to return empty array
When sorting an empty array with the "sort" filter, it returns nil
instead of [].  This confuses subsequent filters in the chain that
expect an array.  For example, when followed by the "map" filter, it
produces an array containing one nil element: [nil].

I could special-case the nil return value, but that would be more
cumbersome than making sure "sort" always returns an array.

Add a case to the "sort" method to return [] if the array is empty,
before performing any checks on ary.first that assume a non-empty array.

There is still a danger of returning nil if the first item in the array
is nil and it is non-empty, but I'm not sure how better to handle that
case.

Apply a similar fix to sort_natural, uniq, and compact filters.
2015-09-29 10:24:31 -07:00
Dylan Thacker-Smith
d8bda2c892 Merge pull request #653 from Shopify/fix-rubocop-offenses
Fix offenses from the new version of rubocop.
2015-09-25 19:48:09 -04:00
Dylan Thacker-Smith
4f81c0a658 Lock rubocop version to avoid CI failures from new releases. 2015-09-25 19:42:35 -04:00
Dylan Thacker-Smith
704937bc00 Fix offenses from the new version of rubocop. 2015-09-25 19:34:44 -04:00
Justin Li
27c6b8074a Update history to reflect merge of #610
[ci skip]
2015-08-03 20:51:41 -04:00
Justin Li
affae5ebef Merge pull request #610 from boobooninja/gf3
Merge pull request 610
2015-08-03 20:50:14 -04:00
Florian Weingarten
fc1c0d0d83 Merge pull request #632 from knu/fix_date_error
Properly rescue ::ArgumentError in the date filter
2015-07-24 10:50:52 -04:00
Akinori MUSHA
a215b70de9 Properly rescue ::ArgumentError in the date filter 2015-07-24 13:35:06 +09:00
Justin Li
1f70928f8a Update history to reflect merge of #631
[ci skip]
2015-07-23 17:07:40 -04:00
Justin Li
7713f6709d Update history for 3.0.5 2015-07-23 17:06:12 -04:00
Justin Li
239cf0e5f5 Update history for 2.6.3 2015-07-23 17:05:58 -04:00
Dylan Thacker-Smith
fa187665b3 Merge pull request #631 from Shopify/fix-tz-test-failure
Fix a timezone test failure.
2015-07-23 16:34:48 -04:00
Dylan Thacker-Smith
cd0c5e954c Fix a timezone test failure. 2015-07-23 16:19:59 -04:00
Florian Weingarten
490b457738 Merge pull request #626 from Shopify/fix_bracket_thing
Fix bracket thing
2015-07-17 17:19:06 +02:00
Florian Weingarten
4d6dec9b5a Fix chained access to multi-dimensional hash 2015-07-17 10:10:00 -04:00
Loren Hale
0b11b573d9 add global_filter
add a global filter using a proc
only add one proc and not an array
add tests to make sure the global_filter is applied after native filters
2015-07-12 16:46:43 +08:00
Justin Li
b42d35ff36 Merge pull request #620 from Shopify/accept-invalid-range-args
Add param to accept invalid input in to_integer
2015-07-09 13:24:28 -04:00
Justin Li
b4e133e26f Fix regression in range lookup 2015-07-09 13:21:46 -04:00
Justin Li
1f9bd1d809 Add param to accept invalid input in to_integer 2015-07-09 13:18:06 -04:00
Justin Li
e88be60818 Merge pull request #618 from Shopify/move-reraise-for-line-number
Move the syntax error rescue for adding error line numbers.
2015-07-09 11:42:41 -04:00
Dylan Thacker-Smith
14416b3c49 Move the syntax error rescue for adding error line numbers. 2015-07-09 11:25:05 -04:00
Dylan Thacker-Smith
bde14a650d Merge pull request #617 from Shopify/rename-options-iv
Rename options instance variable in Variable and Tag.
2015-07-08 20:50:20 -04:00
Dylan Thacker-Smith
c535af021a Rename options instance variable in Variable and Tag. 2015-07-08 19:59:44 -04:00
Dylan Thacker-Smith
9c9345869b Merge pull request #614 from Shopify/remove-token-class
Implement line numbers without the Liquid::Token class.
2015-07-08 19:48:55 -04:00
Dylan Thacker-Smith
73834a7e52 Use reject rather than dup and delete. 2015-07-08 19:27:24 -04:00
Dylan Thacker-Smith
c45310170b Use parse_context or options instead of @options. 2015-07-08 19:21:59 -04:00
Dylan Thacker-Smith
920e1df643 Rescue and re-raise syntax errors in Template#parse to add line numbers.
This can be done now that the parse context has the line number
information, so it doesn't need to be added on closer to the original
exception.  This has the advantage of not having to rescue and re-raise the
exception multiple times, and simplifies liquid-c which would otherwise
have to rescue the exception in BlockBody#parse.
2015-07-08 19:21:59 -04:00
Dylan Thacker-Smith
cebf75b8d7 Implement line numbers without the Liquid::Token class. 2015-07-08 19:21:59 -04:00
Justin Li
afda01adbb Merge pull request #616 from Shopify/handle-non-int-range-args
Handle non-int range lookup arguments
2015-07-08 17:47:27 -04:00
Justin Li
959cd6d2a2 Temporarily disable rubinius in CI
It takes much longer than the others and is currently broken
2015-07-08 17:47:05 -04:00
Justin Li
4c1b89e20e Add regression test for ranges on non-integer types 2015-07-08 17:41:18 -04:00
Justin Li
83b6dd0268 Use to_integer for range lookup arguments 2015-07-08 17:37:07 -04:00
Justin Li
6fb402e60d Move to_integer, to_date, and to_number to Liquid::Utils 2015-07-08 17:33:05 -04:00
Dylan Thacker-Smith
338287df5e Merge pull request #613 from Shopify/taint-context-warning
Add taint warnings to the context rather than the template.
2015-07-07 16:23:10 -04:00
Dylan Thacker-Smith
c4c398174b Use early returns rather than large if in Variable#taint_check 2015-07-07 15:56:03 -04:00
Dylan Thacker-Smith
80b6ac3bc7 Add taint warnings to the context rather than the template. 2015-07-07 15:53:02 -04:00
Dylan Thacker-Smith
15974d9168 Merge pull request #612 from Shopify/fix-block-body-naming
Use node to refer to objects from the nodelist rather than token.
2015-07-07 15:49:58 -04:00
Dylan Thacker-Smith
f22ab4358b Merge pull request #611 from Shopify/no-escape-rescue
Remove standard exception rescue in escape filter.
2015-07-07 15:49:43 -04:00
Justin Li
9cf0d264e1 Require RuboCop v0.32.0 or later 2015-07-06 15:58:36 -04:00
Justin Li
575e3cae7a Remove class length metric cop 2015-07-06 15:52:11 -04:00
Dylan Thacker-Smith
fad3b8275c Use node to refer to objects from the nodelist rather than token. 2015-07-04 20:57:35 -04:00
Dylan Thacker-Smith
5a071cb7f2 Remove standard exception rescue in escape filter. 2015-07-04 13:48:25 -04:00
Justin Li
8cb2364179 Merge pull request #608 from Shopify/tag-tag_name
Add Liquid::Tag#tag_name
2015-07-02 16:28:37 -04:00
Gaurav Chande
3c23cfc167 Add Liquid::Tag#tag_name 2015-07-02 20:18:09 +00:00
54 changed files with 992 additions and 514 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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:

View File

@@ -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.

View File

@@ -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

View File

@@ -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]

View File

@@ -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
```

View File

@@ -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
#

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View 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

View 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

View File

@@ -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)

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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)

View File

@@ -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

View 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

View File

@@ -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)

View File

@@ -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)

View File

@@ -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

View File

@@ -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

View File

@@ -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)

View File

@@ -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)

View File

@@ -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"

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -1,4 +1,4 @@
# encoding: utf-8
module Liquid
VERSION = "4.0.0.alpha"
VERSION = "4.0.0.rc1"
end

View File

@@ -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"

View File

@@ -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)

View File

@@ -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

View File

@@ -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}} )

View File

@@ -118,6 +118,7 @@ class StandardFiltersTest < Minitest::Test
def test_escape
assert_equal '&lt;strong&gt;', @filters.escape('<strong>')
assert_equal nil, @filters.escape(nil)
assert_equal '&lt;strong&gt;', @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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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