liquid 4.0.2 → 5.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (125) hide show
  1. checksums.yaml +4 -4
  2. data/History.md +59 -0
  3. data/README.md +6 -0
  4. data/lib/liquid/block.rb +31 -14
  5. data/lib/liquid/block_body.rb +166 -53
  6. data/lib/liquid/condition.rb +41 -20
  7. data/lib/liquid/context.rb +107 -52
  8. data/lib/liquid/document.rb +47 -9
  9. data/lib/liquid/drop.rb +4 -2
  10. data/lib/liquid/errors.rb +20 -18
  11. data/lib/liquid/expression.rb +29 -34
  12. data/lib/liquid/extensions.rb +2 -0
  13. data/lib/liquid/file_system.rb +6 -4
  14. data/lib/liquid/forloop_drop.rb +11 -4
  15. data/lib/liquid/i18n.rb +5 -3
  16. data/lib/liquid/interrupts.rb +3 -1
  17. data/lib/liquid/lexer.rb +30 -23
  18. data/lib/liquid/locales/en.yml +3 -1
  19. data/lib/liquid/parse_context.rb +20 -4
  20. data/lib/liquid/parse_tree_visitor.rb +2 -2
  21. data/lib/liquid/parser.rb +30 -18
  22. data/lib/liquid/parser_switching.rb +17 -3
  23. data/lib/liquid/partial_cache.rb +24 -0
  24. data/lib/liquid/profiler/hooks.rb +26 -14
  25. data/lib/liquid/profiler.rb +67 -86
  26. data/lib/liquid/range_lookup.rb +13 -3
  27. data/lib/liquid/register.rb +6 -0
  28. data/lib/liquid/resource_limits.rb +47 -8
  29. data/lib/liquid/standardfilters.rb +95 -46
  30. data/lib/liquid/static_registers.rb +44 -0
  31. data/lib/liquid/strainer_factory.rb +36 -0
  32. data/lib/liquid/strainer_template.rb +53 -0
  33. data/lib/liquid/tablerowloop_drop.rb +6 -4
  34. data/lib/liquid/tag/disableable.rb +22 -0
  35. data/lib/liquid/tag/disabler.rb +21 -0
  36. data/lib/liquid/tag.rb +28 -6
  37. data/lib/liquid/tags/assign.rb +24 -10
  38. data/lib/liquid/tags/break.rb +8 -3
  39. data/lib/liquid/tags/capture.rb +11 -8
  40. data/lib/liquid/tags/case.rb +40 -27
  41. data/lib/liquid/tags/comment.rb +5 -3
  42. data/lib/liquid/tags/continue.rb +8 -3
  43. data/lib/liquid/tags/cycle.rb +25 -14
  44. data/lib/liquid/tags/decrement.rb +6 -3
  45. data/lib/liquid/tags/echo.rb +34 -0
  46. data/lib/liquid/tags/for.rb +68 -44
  47. data/lib/liquid/tags/if.rb +39 -23
  48. data/lib/liquid/tags/ifchanged.rb +11 -10
  49. data/lib/liquid/tags/include.rb +34 -47
  50. data/lib/liquid/tags/increment.rb +7 -3
  51. data/lib/liquid/tags/raw.rb +14 -11
  52. data/lib/liquid/tags/render.rb +84 -0
  53. data/lib/liquid/tags/table_row.rb +23 -19
  54. data/lib/liquid/tags/unless.rb +23 -15
  55. data/lib/liquid/template.rb +53 -72
  56. data/lib/liquid/template_factory.rb +9 -0
  57. data/lib/liquid/tokenizer.rb +18 -10
  58. data/lib/liquid/usage.rb +8 -0
  59. data/lib/liquid/utils.rb +13 -3
  60. data/lib/liquid/variable.rb +46 -41
  61. data/lib/liquid/variable_lookup.rb +11 -6
  62. data/lib/liquid/version.rb +2 -1
  63. data/lib/liquid.rb +17 -5
  64. data/test/integration/assign_test.rb +74 -5
  65. data/test/integration/blank_test.rb +11 -8
  66. data/test/integration/block_test.rb +47 -1
  67. data/test/integration/capture_test.rb +18 -10
  68. data/test/integration/context_test.rb +609 -5
  69. data/test/integration/document_test.rb +4 -2
  70. data/test/integration/drop_test.rb +67 -83
  71. data/test/integration/error_handling_test.rb +73 -61
  72. data/test/integration/expression_test.rb +46 -0
  73. data/test/integration/filter_test.rb +53 -42
  74. data/test/integration/hash_ordering_test.rb +5 -3
  75. data/test/integration/output_test.rb +26 -24
  76. data/test/integration/parsing_quirks_test.rb +19 -7
  77. data/test/integration/{render_profiling_test.rb → profiler_test.rb} +84 -25
  78. data/test/integration/security_test.rb +30 -21
  79. data/test/integration/standard_filter_test.rb +385 -281
  80. data/test/integration/tag/disableable_test.rb +59 -0
  81. data/test/integration/tag_test.rb +45 -0
  82. data/test/integration/tags/break_tag_test.rb +4 -2
  83. data/test/integration/tags/continue_tag_test.rb +4 -2
  84. data/test/integration/tags/echo_test.rb +13 -0
  85. data/test/integration/tags/for_tag_test.rb +107 -51
  86. data/test/integration/tags/if_else_tag_test.rb +5 -3
  87. data/test/integration/tags/include_tag_test.rb +76 -52
  88. data/test/integration/tags/increment_tag_test.rb +4 -2
  89. data/test/integration/tags/liquid_tag_test.rb +116 -0
  90. data/test/integration/tags/raw_tag_test.rb +14 -11
  91. data/test/integration/tags/render_tag_test.rb +213 -0
  92. data/test/integration/tags/standard_tag_test.rb +38 -31
  93. data/test/integration/tags/statements_test.rb +23 -21
  94. data/test/integration/tags/table_row_test.rb +2 -0
  95. data/test/integration/tags/unless_else_tag_test.rb +4 -2
  96. data/test/integration/template_test.rb +132 -124
  97. data/test/integration/trim_mode_test.rb +78 -44
  98. data/test/integration/variable_test.rb +74 -32
  99. data/test/test_helper.rb +113 -22
  100. data/test/unit/block_unit_test.rb +19 -24
  101. data/test/unit/condition_unit_test.rb +79 -77
  102. data/test/unit/file_system_unit_test.rb +6 -4
  103. data/test/unit/i18n_unit_test.rb +7 -5
  104. data/test/unit/lexer_unit_test.rb +11 -9
  105. data/test/{integration → unit}/parse_tree_visitor_test.rb +16 -2
  106. data/test/unit/parser_unit_test.rb +37 -35
  107. data/test/unit/partial_cache_unit_test.rb +128 -0
  108. data/test/unit/regexp_unit_test.rb +17 -15
  109. data/test/unit/static_registers_unit_test.rb +156 -0
  110. data/test/unit/strainer_factory_unit_test.rb +100 -0
  111. data/test/unit/strainer_template_unit_test.rb +82 -0
  112. data/test/unit/tag_unit_test.rb +5 -3
  113. data/test/unit/tags/case_tag_unit_test.rb +3 -1
  114. data/test/unit/tags/for_tag_unit_test.rb +4 -2
  115. data/test/unit/tags/if_tag_unit_test.rb +3 -1
  116. data/test/unit/template_factory_unit_test.rb +12 -0
  117. data/test/unit/template_unit_test.rb +19 -10
  118. data/test/unit/tokenizer_unit_test.rb +26 -19
  119. data/test/unit/variable_unit_test.rb +51 -49
  120. metadata +76 -50
  121. data/lib/liquid/strainer.rb +0 -66
  122. data/lib/liquid/truffle.rb +0 -5
  123. data/test/truffle/truffle_test.rb +0 -9
  124. data/test/unit/context_unit_test.rb +0 -489
  125. data/test/unit/strainer_unit_test.rb +0 -164
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Liquid
4
+ class StaticRegisters
5
+ attr_reader :static
6
+
7
+ def initialize(registers = {})
8
+ @static = registers.is_a?(StaticRegisters) ? registers.static : registers
9
+ @registers = {}
10
+ end
11
+
12
+ def []=(key, value)
13
+ @registers[key] = value
14
+ end
15
+
16
+ def [](key)
17
+ if @registers.key?(key)
18
+ @registers[key]
19
+ else
20
+ @static[key]
21
+ end
22
+ end
23
+
24
+ def delete(key)
25
+ @registers.delete(key)
26
+ end
27
+
28
+ UNDEFINED = Object.new
29
+
30
+ def fetch(key, default = UNDEFINED, &block)
31
+ if @registers.key?(key)
32
+ @registers.fetch(key)
33
+ elsif default != UNDEFINED
34
+ @static.fetch(key, default, &block)
35
+ else
36
+ @static.fetch(key, &block)
37
+ end
38
+ end
39
+
40
+ def key?(key)
41
+ @registers.key?(key) || @static.key?(key)
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Liquid
4
+ # StrainerFactory is the factory for the filters system.
5
+ module StrainerFactory
6
+ extend self
7
+
8
+ def add_global_filter(filter)
9
+ strainer_class_cache.clear
10
+ global_filters << filter
11
+ end
12
+
13
+ def create(context, filters = [])
14
+ strainer_from_cache(filters).new(context)
15
+ end
16
+
17
+ private
18
+
19
+ def global_filters
20
+ @global_filters ||= []
21
+ end
22
+
23
+ def strainer_from_cache(filters)
24
+ strainer_class_cache[filters] ||= begin
25
+ klass = Class.new(StrainerTemplate)
26
+ global_filters.each { |f| klass.add_filter(f) }
27
+ filters.each { |f| klass.add_filter(f) }
28
+ klass
29
+ end
30
+ end
31
+
32
+ def strainer_class_cache
33
+ @strainer_class_cache ||= {}
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'set'
4
+
5
+ module Liquid
6
+ # StrainerTemplate is the computed class for the filters system.
7
+ # New filters are mixed into the strainer class which is then instantiated for each liquid template render run.
8
+ #
9
+ # The Strainer only allows method calls defined in filters given to it via StrainerFactory.add_global_filter,
10
+ # Context#add_filters or Template.register_filter
11
+ class StrainerTemplate
12
+ def initialize(context)
13
+ @context = context
14
+ end
15
+
16
+ class << self
17
+ def add_filter(filter)
18
+ return if include?(filter)
19
+
20
+ invokable_non_public_methods = (filter.private_instance_methods + filter.protected_instance_methods).select { |m| invokable?(m) }
21
+ if invokable_non_public_methods.any?
22
+ raise MethodOverrideError, "Filter overrides registered public methods as non public: #{invokable_non_public_methods.join(', ')}"
23
+ end
24
+
25
+ include(filter)
26
+
27
+ filter_methods.merge(filter.public_instance_methods.map(&:to_s))
28
+ end
29
+
30
+ def invokable?(method)
31
+ filter_methods.include?(method.to_s)
32
+ end
33
+
34
+ private
35
+
36
+ def filter_methods
37
+ @filter_methods ||= Set.new
38
+ end
39
+ end
40
+
41
+ def invoke(method, *args)
42
+ if self.class.invokable?(method)
43
+ send(method, *args)
44
+ elsif @context.strict_filters
45
+ raise Liquid::UndefinedFilter, "undefined filter #{method}"
46
+ else
47
+ args.first
48
+ end
49
+ rescue ::ArgumentError => e
50
+ raise Liquid::ArgumentError, e.message, e.backtrace
51
+ end
52
+ end
53
+ end
@@ -1,11 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Liquid
2
4
  class TablerowloopDrop < Drop
3
5
  def initialize(length, cols)
4
6
  @length = length
5
- @row = 1
6
- @col = 1
7
- @cols = cols
8
- @index = 0
7
+ @row = 1
8
+ @col = 1
9
+ @cols = cols
10
+ @index = 0
9
11
  end
10
12
 
11
13
  attr_reader :length, :col, :row
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Liquid
4
+ class Tag
5
+ module Disableable
6
+ def render_to_output_buffer(context, output)
7
+ if context.tag_disabled?(tag_name)
8
+ output << disabled_error(context)
9
+ return
10
+ end
11
+ super
12
+ end
13
+
14
+ def disabled_error(context)
15
+ # raise then rescue the exception so that the Context#exception_renderer can re-raise it
16
+ raise DisabledError, "#{tag_name} #{parse_context[:locale].t('errors.disabled.tag')}"
17
+ rescue DisabledError => exc
18
+ context.handle_error(exc, line_number)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Liquid
4
+ class Tag
5
+ module Disabler
6
+ module ClassMethods
7
+ attr_reader :disabled_tags
8
+ end
9
+
10
+ def self.prepended(base)
11
+ base.extend(ClassMethods)
12
+ end
13
+
14
+ def render_to_output_buffer(context, output)
15
+ context.with_disabled_tags(self.class.disabled_tags) do
16
+ super
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
data/lib/liquid/tag.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Liquid
2
4
  class Tag
3
5
  attr_reader :nodelist, :tag_name, :line_number, :parse_context
@@ -5,20 +7,26 @@ module Liquid
5
7
  include ParserSwitching
6
8
 
7
9
  class << self
8
- def parse(tag_name, markup, tokenizer, options)
9
- tag = new(tag_name, markup, options)
10
+ def parse(tag_name, markup, tokenizer, parse_context)
11
+ tag = new(tag_name, markup, parse_context)
10
12
  tag.parse(tokenizer)
11
13
  tag
12
14
  end
13
15
 
16
+ def disable_tags(*tag_names)
17
+ @disabled_tags ||= []
18
+ @disabled_tags.concat(tag_names)
19
+ prepend(Disabler)
20
+ end
21
+
14
22
  private :new
15
23
  end
16
24
 
17
25
  def initialize(tag_name, markup, parse_context)
18
- @tag_name = tag_name
19
- @markup = markup
26
+ @tag_name = tag_name
27
+ @markup = markup
20
28
  @parse_context = parse_context
21
- @line_number = parse_context.line_number
29
+ @line_number = parse_context.line_number
22
30
  end
23
31
 
24
32
  def parse(_tokens)
@@ -33,11 +41,25 @@ module Liquid
33
41
  end
34
42
 
35
43
  def render(_context)
36
- ''.freeze
44
+ ''
45
+ end
46
+
47
+ # For backwards compatibility with custom tags. In a future release, the semantics
48
+ # of the `render_to_output_buffer` method will become the default and the `render`
49
+ # method will be removed.
50
+ def render_to_output_buffer(context, output)
51
+ output << render(context)
52
+ output
37
53
  end
38
54
 
39
55
  def blank?
40
56
  false
41
57
  end
58
+
59
+ private
60
+
61
+ def parse_expression(markup)
62
+ parse_context.parse_expression(markup)
63
+ end
42
64
  end
43
65
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Liquid
2
4
  # Assign sets a variable in your template.
3
5
  #
@@ -10,23 +12,28 @@ module Liquid
10
12
  class Assign < Tag
11
13
  Syntax = /(#{VariableSignature}+)\s*=\s*(.*)\s*/om
12
14
 
15
+ # @api private
16
+ def self.raise_syntax_error(parse_context)
17
+ raise Liquid::SyntaxError, parse_context.locale.t('errors.syntax.assign')
18
+ end
19
+
13
20
  attr_reader :to, :from
14
21
 
15
- def initialize(tag_name, markup, options)
22
+ def initialize(tag_name, markup, parse_context)
16
23
  super
17
24
  if markup =~ Syntax
18
- @to = $1
19
- @from = Variable.new($2, options)
25
+ @to = Regexp.last_match(1)
26
+ @from = Variable.new(Regexp.last_match(2), parse_context)
20
27
  else
21
- raise SyntaxError.new options[:locale].t("errors.syntax.assign".freeze)
28
+ self.class.raise_syntax_error(parse_context)
22
29
  end
23
30
  end
24
31
 
25
- def render(context)
32
+ def render_to_output_buffer(context, output)
26
33
  val = @from.render(context)
27
34
  context.scopes.last[@to] = val
28
- context.resource_limits.assign_score += assign_score_of(val)
29
- ''.freeze
35
+ context.resource_limits.increment_assign_score(assign_score_of(val))
36
+ output
30
37
  end
31
38
 
32
39
  def blank?
@@ -37,12 +44,19 @@ module Liquid
37
44
 
38
45
  def assign_score_of(val)
39
46
  if val.instance_of?(String)
40
- val.length
41
- elsif val.instance_of?(Array) || val.instance_of?(Hash)
47
+ val.bytesize
48
+ elsif val.instance_of?(Array)
42
49
  sum = 1
43
50
  # Uses #each to avoid extra allocations.
44
51
  val.each { |child| sum += assign_score_of(child) }
45
52
  sum
53
+ elsif val.instance_of?(Hash)
54
+ sum = 1
55
+ val.each do |key, entry_value|
56
+ sum += assign_score_of(key)
57
+ sum += assign_score_of(entry_value)
58
+ end
59
+ sum
46
60
  else
47
61
  1
48
62
  end
@@ -55,5 +69,5 @@ module Liquid
55
69
  end
56
70
  end
57
71
 
58
- Template.register_tag('assign'.freeze, Assign)
72
+ Template.register_tag('assign', Assign)
59
73
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Liquid
2
4
  # Break tag to be used to break out of a for loop.
3
5
  #
@@ -9,10 +11,13 @@ module Liquid
9
11
  # {% endfor %}
10
12
  #
11
13
  class Break < Tag
12
- def interrupt
13
- BreakInterrupt.new
14
+ INTERRUPT = BreakInterrupt.new.freeze
15
+
16
+ def render_to_output_buffer(context, output)
17
+ context.push_interrupt(INTERRUPT)
18
+ output
14
19
  end
15
20
  end
16
21
 
17
- Template.register_tag('break'.freeze, Break)
22
+ Template.register_tag('break', Break)
18
23
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Liquid
2
4
  # Capture stores the result of a block into a variable without rendering it inplace.
3
5
  #
@@ -16,17 +18,18 @@ module Liquid
16
18
  def initialize(tag_name, markup, options)
17
19
  super
18
20
  if markup =~ Syntax
19
- @to = $1
21
+ @to = Regexp.last_match(1)
20
22
  else
21
- raise SyntaxError.new(options[:locale].t("errors.syntax.capture"))
23
+ raise SyntaxError, options[:locale].t("errors.syntax.capture")
22
24
  end
23
25
  end
24
26
 
25
- def render(context)
26
- output = super
27
- context.scopes.last[@to] = output
28
- context.resource_limits.assign_score += output.length
29
- ''.freeze
27
+ def render_to_output_buffer(context, output)
28
+ context.resource_limits.with_capture do
29
+ capture_output = render(context)
30
+ context.scopes.last[@to] = capture_output
31
+ end
32
+ output
30
33
  end
31
34
 
32
35
  def blank?
@@ -34,5 +37,5 @@ module Liquid
34
37
  end
35
38
  end
36
39
 
37
- Template.register_tag('capture'.freeze, Capture)
40
+ Template.register_tag('capture', Capture)
38
41
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Liquid
2
4
  class Case < Block
3
5
  Syntax = /(#{QuotedFragment})/o
@@ -10,17 +12,23 @@ module Liquid
10
12
  @blocks = []
11
13
 
12
14
  if markup =~ Syntax
13
- @left = Expression.parse($1)
15
+ @left = parse_expression(Regexp.last_match(1))
14
16
  else
15
- raise SyntaxError.new(options[:locale].t("errors.syntax.case".freeze))
17
+ raise SyntaxError, options[:locale].t("errors.syntax.case")
16
18
  end
17
19
  end
18
20
 
19
21
  def parse(tokens)
20
- body = BlockBody.new
21
- while parse_body(body, tokens)
22
- body = @blocks.last.attachment
22
+ body = case_body = new_body
23
+ body = @blocks.last.attachment while parse_body(body, tokens)
24
+ @blocks.reverse_each do |condition|
25
+ body = condition.attachment
26
+ unless body.frozen?
27
+ body.remove_blank_strings if blank?
28
+ body.freeze
29
+ end
23
30
  end
31
+ case_body.freeze
24
32
  end
25
33
 
26
34
  def nodelist
@@ -29,45 +37,50 @@ module Liquid
29
37
 
30
38
  def unknown_tag(tag, markup, tokens)
31
39
  case tag
32
- when 'when'.freeze
40
+ when 'when'
33
41
  record_when_condition(markup)
34
- when 'else'.freeze
42
+ when 'else'
35
43
  record_else_condition(markup)
36
44
  else
37
45
  super
38
46
  end
39
47
  end
40
48
 
41
- def render(context)
42
- context.stack do
43
- execute_else_block = true
44
-
45
- output = ''
46
- @blocks.each do |block|
47
- if block.else?
48
- return block.attachment.render(context) if execute_else_block
49
- elsif block.evaluate(context)
50
- execute_else_block = false
51
- output << block.attachment.render(context)
52
- end
49
+ def render_to_output_buffer(context, output)
50
+ execute_else_block = true
51
+
52
+ @blocks.each do |block|
53
+ if block.else?
54
+ block.attachment.render_to_output_buffer(context, output) if execute_else_block
55
+ next
56
+ end
57
+
58
+ result = Liquid::Utils.to_liquid_value(
59
+ block.evaluate(context)
60
+ )
61
+
62
+ if result
63
+ execute_else_block = false
64
+ block.attachment.render_to_output_buffer(context, output)
53
65
  end
54
- output
55
66
  end
67
+
68
+ output
56
69
  end
57
70
 
58
71
  private
59
72
 
60
73
  def record_when_condition(markup)
61
- body = BlockBody.new
74
+ body = new_body
62
75
 
63
76
  while markup
64
77
  unless markup =~ WhenSyntax
65
- raise SyntaxError.new(options[:locale].t("errors.syntax.case_invalid_when".freeze))
78
+ raise SyntaxError, options[:locale].t("errors.syntax.case_invalid_when")
66
79
  end
67
80
 
68
- markup = $2
81
+ markup = Regexp.last_match(2)
69
82
 
70
- block = Condition.new(@left, '=='.freeze, Expression.parse($1))
83
+ block = Condition.new(@left, '==', Condition.parse_expression(parse_context, Regexp.last_match(1)))
71
84
  block.attach(body)
72
85
  @blocks << block
73
86
  end
@@ -75,11 +88,11 @@ module Liquid
75
88
 
76
89
  def record_else_condition(markup)
77
90
  unless markup.strip.empty?
78
- raise SyntaxError.new(options[:locale].t("errors.syntax.case_invalid_else".freeze))
91
+ raise SyntaxError, options[:locale].t("errors.syntax.case_invalid_else")
79
92
  end
80
93
 
81
94
  block = ElseCondition.new
82
- block.attach(BlockBody.new)
95
+ block.attach(new_body)
83
96
  @blocks << block
84
97
  end
85
98
 
@@ -90,5 +103,5 @@ module Liquid
90
103
  end
91
104
  end
92
105
 
93
- Template.register_tag('case'.freeze, Case)
106
+ Template.register_tag('case', Case)
94
107
  end