class Liquid::BlockBody
def self.raise_missing_tag_terminator(token, parse_context)
- Api: - private
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
def self.raise_missing_variable_terminator(token, parse_context)
- Api: - private
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
def self.render_node(context, output, node)
- Api: - private
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
def self.rescue_render_node(context, output, line_number, exc, blank_tag)
- Api: - private
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 error_message = context.handle_error(exc, line_number) unless blank_tag # conditional for backwards compatibility output << error_message end end end
def self.unknown_tag_in_liquid_tag(tag, parse_context)
- Api: - private
def self.unknown_tag_in_liquid_tag(tag, parse_context) Block.raise_unknown_tag(tag, 'liquid', '%}', parse_context) end
def blank?
def blank? @blank end
def create_variable(token, parse_context)
def create_variable(token, parse_context) if token =~ ContentOfVariable markup = Regexp.last_match(1) return Variable.new(markup, parse_context) end BlockBody.raise_missing_variable_terminator(token, parse_context) end
def freeze
def freeze @nodelist.freeze super end
def handle_invalid_tag_token(token, parse_context)
def handle_invalid_tag_token(token, parse_context) en.end_with?('%}') d token, token kBody.raise_missing_tag_terminator(token, parse_context)
def initialize
def initialize @nodelist = [] @blank = true end
def parse(tokenizer, parse_context, &block)
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
def parse_for_document(tokenizer, parse_context, &block)
def parse_for_document(tokenizer, parse_context, &block) (token = tokenizer.shift) if token.empty? token.start_with?(TAGSTART) itespace_handler(token, parse_context) less token =~ FullToken return handle_invalid_tag_token(token, parse_context, &block) d g_name = Regexp.last_match(2) rkup = Regexp.last_match(4) parse_context.line_number # newlines inside the tag should increase the line number, # particularly important for multiline {% liquid %} tags parse_context.line_number += Regexp.last_match(1).count("\n") + Regexp.last_match(3).count("\n") d tag_name == 'liquid' parse_liquid_tag(markup, parse_context) next d less (tag = registered_tags[tag_name]) # end parsing if we reach an unknown tag and let the caller decide # determine how to proceed return yield tag_name, markup d w_tag = tag.parse(tag_name, markup, tokenizer, parse_context) lank &&= new_tag.blank? odelist << new_tag token.start_with?(VARSTART) itespace_handler(token, parse_context) odelist << create_variable(token, parse_context) lank = false parse_context.trim_whitespace token.lstrip! d rse_context.trim_whitespace = false odelist << token lank &&= token.match?(WhitespaceOrNothing) e_context.line_number = tokenizer.line_number nil, nil
def parse_for_liquid_tag(tokenizer, parse_context)
def parse_for_liquid_tag(tokenizer, parse_context) (token = tokenizer.shift) ss token.empty? || token.match?(WhitespaceOrNothing) less token =~ LiquidTagToken # line isn't empty but didn't match tag syntax, yield and let the # caller raise a syntax error return yield token, token d g_name = Regexp.last_match(1) rkup = Regexp.last_match(2) tag_name == 'liquid' parse_context.line_number -= 1 next parse_liquid_tag(markup, parse_context) d less (tag = registered_tags[tag_name]) # end parsing if we reach an unknown tag and let the caller decide # determine how to proceed return yield tag_name, markup d w_tag = tag.parse(tag_name, markup, tokenizer, parse_context) lank &&= new_tag.blank? odelist << new_tag e_context.line_number = tokenizer.line_number nil, nil
def parse_liquid_tag(markup, parse_context)
def parse_liquid_tag(markup, parse_context) _tag_tokenizer = parse_context.new_tokenizer( up, start_line_number: parse_context.line_number, for_liquid_tag: true for_liquid_tag(liquid_tag_tokenizer, parse_context) do |end_tag_name, _end_tag_markup| nd_tag_name ockBody.unknown_tag_in_liquid_tag(end_tag_name, parse_context)
def raise_missing_tag_terminator(token, parse_context)
- Use {.raise_missing_tag_terminator} instead
def raise_missing_tag_terminator(token, parse_context) BlockBody.raise_missing_tag_terminator(token, parse_context) end
def raise_missing_variable_terminator(token, parse_context)
- Use {.raise_missing_variable_terminator} instead
def raise_missing_variable_terminator(token, parse_context) BlockBody.raise_missing_variable_terminator(token, parse_context) end
def registered_tags
def registered_tags Template.tags end
def remove_blank_strings
will remove them to reduce the render output size.
we assume the intention wasn't to output the blank spaces in the `if` tag's block body, so this method
```
{% endif %}
{% assign size = max_size %}
{% if size > max_size %}
```
For example, in a conditional assignment like the following
with a blank body.
Remove blank strings in the block body for a control flow tag (e.g. `if`, `for`, `case`, `unless`)
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
def render(context)
def render(context) render_to_output_buffer(context, +'') end
def render_node(context, output, node)
def render_node(context, output, node) BlockBody.render_node(context, output, node) end
def render_to_output_buffer(context, output)
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
def whitespace_handler(token, parse_context)
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