liquid 4.0.3 → 5.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (115) hide show
  1. checksums.yaml +4 -4
  2. data/History.md +89 -0
  3. data/README.md +10 -4
  4. data/lib/liquid/block.rb +31 -14
  5. data/lib/liquid/block_body.rb +169 -57
  6. data/lib/liquid/condition.rb +48 -21
  7. data/lib/liquid/context.rb +111 -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 +28 -32
  12. data/lib/liquid/extensions.rb +2 -0
  13. data/lib/liquid/file_system.rb +6 -4
  14. data/lib/liquid/forloop_drop.rb +54 -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 +8 -5
  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/registers.rb +51 -0
  28. data/lib/liquid/resource_limits.rb +47 -8
  29. data/lib/liquid/standardfilters.rb +551 -114
  30. data/lib/liquid/strainer_factory.rb +41 -0
  31. data/lib/liquid/strainer_template.rb +62 -0
  32. data/lib/liquid/tablerowloop_drop.rb +64 -5
  33. data/lib/liquid/tag/disableable.rb +22 -0
  34. data/lib/liquid/tag/disabler.rb +21 -0
  35. data/lib/liquid/tag.rb +28 -6
  36. data/lib/liquid/tags/assign.rb +36 -18
  37. data/lib/liquid/tags/break.rb +16 -3
  38. data/lib/liquid/tags/capture.rb +24 -18
  39. data/lib/liquid/tags/case.rb +61 -27
  40. data/lib/liquid/tags/comment.rb +18 -3
  41. data/lib/liquid/tags/continue.rb +16 -12
  42. data/lib/liquid/tags/cycle.rb +37 -25
  43. data/lib/liquid/tags/decrement.rb +22 -20
  44. data/lib/liquid/tags/echo.rb +41 -0
  45. data/lib/liquid/tags/for.rb +90 -87
  46. data/lib/liquid/tags/if.rb +50 -32
  47. data/lib/liquid/tags/ifchanged.rb +11 -10
  48. data/lib/liquid/tags/include.rb +49 -60
  49. data/lib/liquid/tags/increment.rb +23 -17
  50. data/lib/liquid/tags/inline_comment.rb +43 -0
  51. data/lib/liquid/tags/raw.rb +25 -11
  52. data/lib/liquid/tags/render.rb +109 -0
  53. data/lib/liquid/tags/table_row.rb +45 -19
  54. data/lib/liquid/tags/unless.rb +38 -19
  55. data/lib/liquid/template.rb +52 -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 +49 -44
  61. data/lib/liquid/variable_lookup.rb +18 -10
  62. data/lib/liquid/version.rb +2 -1
  63. data/lib/liquid.rb +18 -6
  64. metadata +20 -108
  65. data/lib/liquid/strainer.rb +0 -66
  66. data/lib/liquid/truffle.rb +0 -5
  67. data/test/fixtures/en_locale.yml +0 -9
  68. data/test/integration/assign_test.rb +0 -48
  69. data/test/integration/blank_test.rb +0 -106
  70. data/test/integration/block_test.rb +0 -12
  71. data/test/integration/capture_test.rb +0 -50
  72. data/test/integration/context_test.rb +0 -32
  73. data/test/integration/document_test.rb +0 -19
  74. data/test/integration/drop_test.rb +0 -273
  75. data/test/integration/error_handling_test.rb +0 -260
  76. data/test/integration/filter_test.rb +0 -178
  77. data/test/integration/hash_ordering_test.rb +0 -23
  78. data/test/integration/output_test.rb +0 -123
  79. data/test/integration/parse_tree_visitor_test.rb +0 -247
  80. data/test/integration/parsing_quirks_test.rb +0 -122
  81. data/test/integration/render_profiling_test.rb +0 -154
  82. data/test/integration/security_test.rb +0 -80
  83. data/test/integration/standard_filter_test.rb +0 -776
  84. data/test/integration/tags/break_tag_test.rb +0 -15
  85. data/test/integration/tags/continue_tag_test.rb +0 -15
  86. data/test/integration/tags/for_tag_test.rb +0 -410
  87. data/test/integration/tags/if_else_tag_test.rb +0 -188
  88. data/test/integration/tags/include_tag_test.rb +0 -253
  89. data/test/integration/tags/increment_tag_test.rb +0 -23
  90. data/test/integration/tags/raw_tag_test.rb +0 -31
  91. data/test/integration/tags/standard_tag_test.rb +0 -296
  92. data/test/integration/tags/statements_test.rb +0 -111
  93. data/test/integration/tags/table_row_test.rb +0 -64
  94. data/test/integration/tags/unless_else_tag_test.rb +0 -26
  95. data/test/integration/template_test.rb +0 -332
  96. data/test/integration/trim_mode_test.rb +0 -529
  97. data/test/integration/variable_test.rb +0 -96
  98. data/test/test_helper.rb +0 -116
  99. data/test/truffle/truffle_test.rb +0 -9
  100. data/test/unit/block_unit_test.rb +0 -58
  101. data/test/unit/condition_unit_test.rb +0 -166
  102. data/test/unit/context_unit_test.rb +0 -489
  103. data/test/unit/file_system_unit_test.rb +0 -35
  104. data/test/unit/i18n_unit_test.rb +0 -37
  105. data/test/unit/lexer_unit_test.rb +0 -51
  106. data/test/unit/parser_unit_test.rb +0 -82
  107. data/test/unit/regexp_unit_test.rb +0 -44
  108. data/test/unit/strainer_unit_test.rb +0 -164
  109. data/test/unit/tag_unit_test.rb +0 -21
  110. data/test/unit/tags/case_tag_unit_test.rb +0 -10
  111. data/test/unit/tags/for_tag_unit_test.rb +0 -13
  112. data/test/unit/tags/if_tag_unit_test.rb +0 -8
  113. data/test/unit/template_unit_test.rb +0 -78
  114. data/test/unit/tokenizer_unit_test.rb +0 -55
  115. data/test/unit/variable_unit_test.rb +0 -162
@@ -0,0 +1,41 @@
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
+ GlobalCache.add_filter(filter)
11
+ end
12
+
13
+ def create(context, filters = [])
14
+ strainer_from_cache(filters).new(context)
15
+ end
16
+
17
+ def global_filter_names
18
+ GlobalCache.filter_method_names
19
+ end
20
+
21
+ GlobalCache = Class.new(StrainerTemplate)
22
+
23
+ private
24
+
25
+ def strainer_from_cache(filters)
26
+ if filters.empty?
27
+ GlobalCache
28
+ else
29
+ strainer_class_cache[filters] ||= begin
30
+ klass = Class.new(GlobalCache)
31
+ filters.each { |f| klass.add_filter(f) }
32
+ klass
33
+ end
34
+ end
35
+ end
36
+
37
+ def strainer_class_cache
38
+ @strainer_class_cache ||= {}
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,62 @@
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
+ def inherited(subclass)
35
+ super
36
+ subclass.instance_variable_set(:@filter_methods, @filter_methods.dup)
37
+ end
38
+
39
+ def filter_method_names
40
+ filter_methods.map(&:to_s).to_a
41
+ end
42
+
43
+ private
44
+
45
+ def filter_methods
46
+ @filter_methods ||= Set.new
47
+ end
48
+ end
49
+
50
+ def invoke(method, *args)
51
+ if self.class.invokable?(method)
52
+ send(method, *args)
53
+ elsif @context.strict_filters
54
+ raise Liquid::UndefinedFilter, "undefined filter #{method}"
55
+ else
56
+ args.first
57
+ end
58
+ rescue ::ArgumentError => e
59
+ raise Liquid::ArgumentError, e.message, e.backtrace
60
+ end
61
+ end
62
+ end
@@ -1,47 +1,106 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Liquid
4
+ # @liquid_public_docs
5
+ # @liquid_type object
6
+ # @liquid_name tablerowloop
7
+ # @liquid_summary
8
+ # Information about a parent [`tablerow` loop](/api/liquid/tags#tablerow).
2
9
  class TablerowloopDrop < Drop
3
10
  def initialize(length, cols)
4
11
  @length = length
5
- @row = 1
6
- @col = 1
7
- @cols = cols
8
- @index = 0
12
+ @row = 1
13
+ @col = 1
14
+ @cols = cols
15
+ @index = 0
9
16
  end
10
17
 
11
- attr_reader :length, :col, :row
18
+ # @liquid_public_docs
19
+ # @liquid_summary
20
+ # The total number of iterations in the loop.
21
+ # @liquid_return [number]
22
+ attr_reader :length
23
+
24
+ # @liquid_public_docs
25
+ # @liquid_summary
26
+ # The 1-based index of the current column.
27
+ # @liquid_return [number]
28
+ attr_reader :col
29
+
30
+ # @liquid_public_docs
31
+ # @liquid_summary
32
+ # The 1-based index of current row.
33
+ # @liquid_return [number]
34
+ attr_reader :row
12
35
 
36
+ # @liquid_public_docs
37
+ # @liquid_summary
38
+ # The 1-based index of the current iteration.
39
+ # @liquid_return [number]
13
40
  def index
14
41
  @index + 1
15
42
  end
16
43
 
44
+ # @liquid_public_docs
45
+ # @liquid_summary
46
+ # The 0-based index of the current iteration.
47
+ # @liquid_return [number]
17
48
  def index0
18
49
  @index
19
50
  end
20
51
 
52
+ # @liquid_public_docs
53
+ # @liquid_summary
54
+ # The 0-based index of the current column.
55
+ # @liquid_return [number]
21
56
  def col0
22
57
  @col - 1
23
58
  end
24
59
 
60
+ # @liquid_public_docs
61
+ # @liquid_summary
62
+ # The 1-based index of the current iteration, in reverse order.
63
+ # @liquid_return [number]
25
64
  def rindex
26
65
  @length - @index
27
66
  end
28
67
 
68
+ # @liquid_public_docs
69
+ # @liquid_summary
70
+ # The 0-based index of the current iteration, in reverse order.
71
+ # @liquid_return [number]
29
72
  def rindex0
30
73
  @length - @index - 1
31
74
  end
32
75
 
76
+ # @liquid_public_docs
77
+ # @liquid_summary
78
+ # Returns `true` if the current iteration is the first. Returns `false` if not.
79
+ # @liquid_return [boolean]
33
80
  def first
34
81
  @index == 0
35
82
  end
36
83
 
84
+ # @liquid_public_docs
85
+ # @liquid_summary
86
+ # Returns `true` if the current iteration is the last. Returns `false` if not.
87
+ # @liquid_return [boolean]
37
88
  def last
38
89
  @index == @length - 1
39
90
  end
40
91
 
92
+ # @liquid_public_docs
93
+ # @liquid_summary
94
+ # Returns `true` if the current column is the first in the row. Returns `false` if not.
95
+ # @liquid_return [boolean]
41
96
  def col_first
42
97
  @col == 1
43
98
  end
44
99
 
100
+ # @liquid_public_docs
101
+ # @liquid_summary
102
+ # Returns `true` if the current column is the last in the row. Returns `false` if not.
103
+ # @liquid_return [boolean]
45
104
  def col_last
46
105
  @col == @cols
47
106
  end
@@ -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,32 +1,43 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Liquid
2
- # Assign sets a variable in your template.
3
- #
4
- # {% assign foo = 'monkey' %}
5
- #
6
- # You can then use the variable later in the page.
7
- #
8
- # {{ foo }}
9
- #
4
+ # @liquid_public_docs
5
+ # @liquid_type tag
6
+ # @liquid_category variable
7
+ # @liquid_name assign
8
+ # @liquid_summary
9
+ # Creates a new variable.
10
+ # @liquid_description
11
+ # You can create variables of any [basic type](/api/liquid/basics#types), [object](/api/liquid/objects), or object property.
12
+ # @liquid_syntax
13
+ # {% assign variable_name = value %}
14
+ # @liquid_syntax_keyword variable_name The name of the variable being created.
15
+ # @liquid_syntax_keyword value The value you want to assign to the variable.
10
16
  class Assign < Tag
11
17
  Syntax = /(#{VariableSignature}+)\s*=\s*(.*)\s*/om
12
18
 
19
+ # @api private
20
+ def self.raise_syntax_error(parse_context)
21
+ raise Liquid::SyntaxError, parse_context.locale.t('errors.syntax.assign')
22
+ end
23
+
13
24
  attr_reader :to, :from
14
25
 
15
- def initialize(tag_name, markup, options)
26
+ def initialize(tag_name, markup, parse_context)
16
27
  super
17
28
  if markup =~ Syntax
18
- @to = $1
19
- @from = Variable.new($2, options)
29
+ @to = Regexp.last_match(1)
30
+ @from = Variable.new(Regexp.last_match(2), parse_context)
20
31
  else
21
- raise SyntaxError.new options[:locale].t("errors.syntax.assign".freeze)
32
+ self.class.raise_syntax_error(parse_context)
22
33
  end
23
34
  end
24
35
 
25
- def render(context)
36
+ def render_to_output_buffer(context, output)
26
37
  val = @from.render(context)
27
38
  context.scopes.last[@to] = val
28
- context.resource_limits.assign_score += assign_score_of(val)
29
- ''.freeze
39
+ context.resource_limits.increment_assign_score(assign_score_of(val))
40
+ output
30
41
  end
31
42
 
32
43
  def blank?
@@ -37,12 +48,19 @@ module Liquid
37
48
 
38
49
  def assign_score_of(val)
39
50
  if val.instance_of?(String)
40
- val.length
41
- elsif val.instance_of?(Array) || val.instance_of?(Hash)
51
+ val.bytesize
52
+ elsif val.instance_of?(Array)
42
53
  sum = 1
43
54
  # Uses #each to avoid extra allocations.
44
55
  val.each { |child| sum += assign_score_of(child) }
45
56
  sum
57
+ elsif val.instance_of?(Hash)
58
+ sum = 1
59
+ val.each do |key, entry_value|
60
+ sum += assign_score_of(key)
61
+ sum += assign_score_of(entry_value)
62
+ end
63
+ sum
46
64
  else
47
65
  1
48
66
  end
@@ -55,5 +73,5 @@ module Liquid
55
73
  end
56
74
  end
57
75
 
58
- Template.register_tag('assign'.freeze, Assign)
76
+ Template.register_tag('assign', Assign)
59
77
  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
  #
@@ -8,11 +10,22 @@ module Liquid
8
10
  # {% endif %}
9
11
  # {% endfor %}
10
12
  #
13
+ # @liquid_public_docs
14
+ # @liquid_type tag
15
+ # @liquid_category iteration
16
+ # @liquid_name break
17
+ # @liquid_summary
18
+ # Stops a [`for` loop](/api/liquid/tags#for) from iterating.
19
+ # @liquid_syntax
20
+ # {% break %}
11
21
  class Break < Tag
12
- def interrupt
13
- BreakInterrupt.new
22
+ INTERRUPT = BreakInterrupt.new.freeze
23
+
24
+ def render_to_output_buffer(context, output)
25
+ context.push_interrupt(INTERRUPT)
26
+ output
14
27
  end
15
28
  end
16
29
 
17
- Template.register_tag('break'.freeze, Break)
30
+ Template.register_tag('break', Break)
18
31
  end
@@ -1,32 +1,38 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Liquid
2
- # Capture stores the result of a block into a variable without rendering it inplace.
3
- #
4
- # {% capture heading %}
5
- # Monkeys!
4
+ # @liquid_public_docs
5
+ # @liquid_type tag
6
+ # @liquid_category variable
7
+ # @liquid_name capture
8
+ # @liquid_summary
9
+ # Creates a new variable with a string value.
10
+ # @liquid_description
11
+ # You can create complex strings with Liquid logic and variables.
12
+ # @liquid_syntax
13
+ # {% capture variable %}
14
+ # value
6
15
  # {% endcapture %}
7
- # ...
8
- # <h1>{{ heading }}</h1>
9
- #
10
- # Capture is useful for saving content for use later in your template, such as
11
- # in a sidebar or footer.
12
- #
16
+ # @liquid_syntax_keyword variable The name of the variable being created.
17
+ # @liquid_syntax_keyword value The value you want to assign to the variable.
13
18
  class Capture < Block
14
19
  Syntax = /(#{VariableSignature}+)/o
15
20
 
16
21
  def initialize(tag_name, markup, options)
17
22
  super
18
23
  if markup =~ Syntax
19
- @to = $1
24
+ @to = Regexp.last_match(1)
20
25
  else
21
- raise SyntaxError.new(options[:locale].t("errors.syntax.capture"))
26
+ raise SyntaxError, options[:locale].t("errors.syntax.capture")
22
27
  end
23
28
  end
24
29
 
25
- def render(context)
26
- output = super
27
- context.scopes.last[@to] = output
28
- context.resource_limits.assign_score += output.length
29
- ''.freeze
30
+ def render_to_output_buffer(context, output)
31
+ context.resource_limits.with_capture do
32
+ capture_output = render(context)
33
+ context.scopes.last[@to] = capture_output
34
+ end
35
+ output
30
36
  end
31
37
 
32
38
  def blank?
@@ -34,5 +40,5 @@ module Liquid
34
40
  end
35
41
  end
36
42
 
37
- Template.register_tag('capture'.freeze, Capture)
43
+ Template.register_tag('capture', Capture)
38
44
  end
@@ -1,4 +1,27 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Liquid
4
+ # @liquid_public_docs
5
+ # @liquid_type tag
6
+ # @liquid_category conditional
7
+ # @liquid_name case
8
+ # @liquid_summary
9
+ # Renders a specific expression depending on the value of a specific variable.
10
+ # @liquid_syntax
11
+ # {% case variable %}
12
+ # {% when first_value %}
13
+ # first_expression
14
+ # {% when second_value %}
15
+ # second_expression
16
+ # {% else %}
17
+ # third_expression
18
+ # {% endcase %}
19
+ # @liquid_syntax_keyword variable The name of the variable you want to base your case statement on.
20
+ # @liquid_syntax_keyword first_value A specific value to check for.
21
+ # @liquid_syntax_keyword second_value A specific value to check for.
22
+ # @liquid_syntax_keyword first_expression An expression to be rendered when the variable's value matches `first_value`.
23
+ # @liquid_syntax_keyword second_expression An expression to be rendered when the variable's value matches `second_value`.
24
+ # @liquid_syntax_keyword third_expression An expression to be rendered when the variable's value has no match.
2
25
  class Case < Block
3
26
  Syntax = /(#{QuotedFragment})/o
4
27
  WhenSyntax = /(#{QuotedFragment})(?:(?:\s+or\s+|\s*\,\s*)(#{QuotedFragment}.*))?/om
@@ -10,17 +33,23 @@ module Liquid
10
33
  @blocks = []
11
34
 
12
35
  if markup =~ Syntax
13
- @left = Expression.parse($1)
36
+ @left = parse_expression(Regexp.last_match(1))
14
37
  else
15
- raise SyntaxError.new(options[:locale].t("errors.syntax.case".freeze))
38
+ raise SyntaxError, options[:locale].t("errors.syntax.case")
16
39
  end
17
40
  end
18
41
 
19
42
  def parse(tokens)
20
- body = BlockBody.new
21
- while parse_body(body, tokens)
22
- body = @blocks.last.attachment
43
+ body = case_body = new_body
44
+ body = @blocks.last.attachment while parse_body(body, tokens)
45
+ @blocks.reverse_each do |condition|
46
+ body = condition.attachment
47
+ unless body.frozen?
48
+ body.remove_blank_strings if blank?
49
+ body.freeze
50
+ end
23
51
  end
52
+ case_body.freeze
24
53
  end
25
54
 
26
55
  def nodelist
@@ -29,45 +58,50 @@ module Liquid
29
58
 
30
59
  def unknown_tag(tag, markup, tokens)
31
60
  case tag
32
- when 'when'.freeze
61
+ when 'when'
33
62
  record_when_condition(markup)
34
- when 'else'.freeze
63
+ when 'else'
35
64
  record_else_condition(markup)
36
65
  else
37
66
  super
38
67
  end
39
68
  end
40
69
 
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
70
+ def render_to_output_buffer(context, output)
71
+ execute_else_block = true
72
+
73
+ @blocks.each do |block|
74
+ if block.else?
75
+ block.attachment.render_to_output_buffer(context, output) if execute_else_block
76
+ next
77
+ end
78
+
79
+ result = Liquid::Utils.to_liquid_value(
80
+ block.evaluate(context)
81
+ )
82
+
83
+ if result
84
+ execute_else_block = false
85
+ block.attachment.render_to_output_buffer(context, output)
53
86
  end
54
- output
55
87
  end
88
+
89
+ output
56
90
  end
57
91
 
58
92
  private
59
93
 
60
94
  def record_when_condition(markup)
61
- body = BlockBody.new
95
+ body = new_body
62
96
 
63
97
  while markup
64
98
  unless markup =~ WhenSyntax
65
- raise SyntaxError.new(options[:locale].t("errors.syntax.case_invalid_when".freeze))
99
+ raise SyntaxError, options[:locale].t("errors.syntax.case_invalid_when")
66
100
  end
67
101
 
68
- markup = $2
102
+ markup = Regexp.last_match(2)
69
103
 
70
- block = Condition.new(@left, '=='.freeze, Expression.parse($1))
104
+ block = Condition.new(@left, '==', Condition.parse_expression(parse_context, Regexp.last_match(1)))
71
105
  block.attach(body)
72
106
  @blocks << block
73
107
  end
@@ -75,11 +109,11 @@ module Liquid
75
109
 
76
110
  def record_else_condition(markup)
77
111
  unless markup.strip.empty?
78
- raise SyntaxError.new(options[:locale].t("errors.syntax.case_invalid_else".freeze))
112
+ raise SyntaxError, options[:locale].t("errors.syntax.case_invalid_else")
79
113
  end
80
114
 
81
115
  block = ElseCondition.new
82
- block.attach(BlockBody.new)
116
+ block.attach(new_body)
83
117
  @blocks << block
84
118
  end
85
119
 
@@ -90,5 +124,5 @@ module Liquid
90
124
  end
91
125
  end
92
126
 
93
- Template.register_tag('case'.freeze, Case)
127
+ Template.register_tag('case', Case)
94
128
  end
@@ -1,7 +1,22 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Liquid
4
+ # @liquid_public_docs
5
+ # @liquid_type tag
6
+ # @liquid_category syntax
7
+ # @liquid_name comment
8
+ # @liquid_summary
9
+ # Prevents an expression from being rendered or output.
10
+ # @liquid_description
11
+ # Any text inside `comment` tags won't be output, and any Liquid code won't be rendered.
12
+ # @liquid_syntax
13
+ # {% comment %}
14
+ # content
15
+ # {% endcomment %}
16
+ # @liquid_syntax_keyword content The content of the comment.
2
17
  class Comment < Block
3
- def render(_context)
4
- ''.freeze
18
+ def render_to_output_buffer(_context, output)
19
+ output
5
20
  end
6
21
 
7
22
  def unknown_tag(_tag, _markup, _tokens)
@@ -12,5 +27,5 @@ module Liquid
12
27
  end
13
28
  end
14
29
 
15
- Template.register_tag('comment'.freeze, Comment)
30
+ Template.register_tag('comment', Comment)
16
31
  end