Class: Liquid::BlockBody
- Inherits:
-
Object
- Object
- Liquid::BlockBody
- Includes:
- BlockBodyProfilingHook
- Defined in:
- lib/liquid/block_body.rb
Constant Summary collapse
- LiquidTagToken =
/\A\s*(#{TagName})\s*(.*?)\z/o- FullToken =
/\A#{TagStart}#{WhitespaceControl}?(\s*)(#{TagName})(\s*)(.*?)#{WhitespaceControl}?#{TagEnd}\z/om
- FullTokenPossiblyInvalid =
/\A(.*)#{TagStart}#{WhitespaceControl}?\s*(\w+)\s*(.*)?#{WhitespaceControl}?#{TagEnd}\z/om
- ContentOfVariable =
/\A#{VariableStart}#{WhitespaceControl}?(.*?)#{WhitespaceControl}?#{VariableEnd}\z/om
- WhitespaceOrNothing =
/\A\s*\z/- TAGSTART =
"{%"- VARSTART =
"{{"
Instance Attribute Summary collapse
-
#nodelist ⇒ Object
readonly
Returns the value of attribute nodelist.
Class Method Summary collapse
- .raise_missing_tag_terminator(token, parse_context) ⇒ Object private
- .raise_missing_variable_terminator(token, parse_context) ⇒ Object private
- .render_node(context, output, node) ⇒ Object private
- .rescue_render_node(context, output, line_number, exc, blank_tag) ⇒ Object private
- .unknown_tag_in_liquid_tag(tag, parse_context) ⇒ Object private
Instance Method Summary collapse
- #blank? ⇒ Boolean
- #freeze ⇒ Object
-
#initialize ⇒ BlockBody
constructor
A new instance of BlockBody.
- #parse(tokenizer, parse_context, &block) ⇒ Object
-
#remove_blank_strings ⇒ Object
Remove blank strings in the block body for a control flow tag (e.g.
if,for,case,unless) with a blank body. - #render(context) ⇒ Object
- #render_to_output_buffer(context, output) ⇒ Object
- #whitespace_handler(token, parse_context) ⇒ Object
Constructor Details
#initialize ⇒ BlockBody
17 18 19 20 |
# File 'lib/liquid/block_body.rb', line 17 def initialize @nodelist = [] @blank = true end |
Instance Attribute Details
#nodelist ⇒ Object (readonly)
Returns the value of attribute nodelist.
15 16 17 |
# File 'lib/liquid/block_body.rb', line 15 def nodelist @nodelist end |
Class Method Details
.raise_missing_tag_terminator(token, parse_context) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
76 77 78 |
# File 'lib/liquid/block_body.rb', line 76 def self.raise_missing_tag_terminator(token, parse_context) raise SyntaxError, parse_context.locale.t("errors.syntax.tag_termination", token: token, tag_end: TagEnd.inspect) end |
.raise_missing_variable_terminator(token, parse_context) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
81 82 83 |
# File 'lib/liquid/block_body.rb', line 81 def self.raise_missing_variable_terminator(token, parse_context) raise SyntaxError, parse_context.locale.t("errors.syntax.variable_termination", token: token, tag_end: VariableEnd.inspect) end |
.render_node(context, output, node) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
86 87 88 89 90 91 |
# File 'lib/liquid/block_body.rb', line 86 def self.render_node(context, output, node) node.render_to_output_buffer(context, output) rescue => exc blank_tag = !node.instance_of?(Variable) && node.blank? rescue_render_node(context, output, node.line_number, exc, blank_tag) end |
.rescue_render_node(context, output, line_number, exc, blank_tag) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/liquid/block_body.rb', line 94 def self.rescue_render_node(context, output, line_number, exc, blank_tag) case exc when MemoryError raise when UndefinedVariable, UndefinedDropMethod, UndefinedFilter context.handle_error(exc, line_number) else = context.handle_error(exc, line_number) error_mode = context.registers.static[:template_error_mode] suppress_error_text = blank_tag && error_mode != :strict2 && error_mode != :rigid unless suppress_error_text # blank-tag suppression is kept for backwards compatibility outside strict2 output << end end end |
.unknown_tag_in_liquid_tag(tag, parse_context) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
71 72 73 |
# File 'lib/liquid/block_body.rb', line 71 def self.unknown_tag_in_liquid_tag(tag, parse_context) Block.raise_unknown_tag(tag, 'liquid', '%}', parse_context) end |
Instance Method Details
#blank? ⇒ Boolean
192 193 194 |
# File 'lib/liquid/block_body.rb', line 192 def blank? @blank end |
#freeze ⇒ Object
34 35 36 37 |
# File 'lib/liquid/block_body.rb', line 34 def freeze @nodelist.freeze super end |
#parse(tokenizer, parse_context, &block) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/liquid/block_body.rb', line 22 def parse(tokenizer, parse_context, &block) raise FrozenError, "can't modify frozen Liquid::BlockBody" if frozen? parse_context.line_number = tokenizer.line_number if tokenizer.for_liquid_tag parse_for_liquid_tag(tokenizer, parse_context, &block) else parse_for_document(tokenizer, parse_context, &block) end end |
#remove_blank_strings ⇒ Object
Remove blank strings in the block body for a control flow tag (e.g. if, for, case, unless)
with a blank body.
For example, in a conditional assignment like the following
{% if size > max_size %}
{% assign size = max_size %}
{% endif %}
we assume the intention wasn't to output the blank spaces in the if tag's block body, so this method
will remove them to reduce the render output size.
Note that it is now preferred to use the liquid tag for this use case.
211 212 213 214 |
# File 'lib/liquid/block_body.rb', line 211 def remove_blank_strings raise "remove_blank_strings only support being called on a blank block body" unless @blank @nodelist.reject! { |node| node.instance_of?(String) } end |
#render(context) ⇒ Object
216 217 218 |
# File 'lib/liquid/block_body.rb', line 216 def render(context) render_to_output_buffer(context, +'') end |
#render_to_output_buffer(context, output) ⇒ Object
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/liquid/block_body.rb', line 220 def render_to_output_buffer(context, output) freeze unless frozen? context.resource_limits.increment_render_score(@nodelist.length) idx = 0 while (node = @nodelist[idx]) if node.instance_of?(String) output << node else render_node(context, output, node) # If we get an Interrupt that means the block must stop processing. An # Interrupt is any command that stops block execution such as {% break %} # or {% continue %}. These tags may also occur through Block or Include tags. break if context.interrupt? # might have happened in a for-block end idx += 1 context.resource_limits.increment_write_score(output) end output end |
#whitespace_handler(token, parse_context) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/liquid/block_body.rb', line 178 def whitespace_handler(token, parse_context) if token[2] == WhitespaceControl previous_token = @nodelist.last if previous_token.is_a?(String) first_byte = previous_token.getbyte(0) previous_token.rstrip! if previous_token.empty? && parse_context[:bug_compatible_whitespace_trimming] && first_byte previous_token << first_byte end end end parse_context.trim_whitespace = (token[-3] == WhitespaceControl) end |