mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-12 23:40:45 -07:00
Merge pull request #384 from Shopify/optimize_block_parsing
Optimize block parsing -- don't recreate delimiter, use strings instead of regex
This commit is contained in:
+14
-13
@@ -1,9 +1,14 @@
|
||||
module Liquid
|
||||
class Block < Tag
|
||||
IsTag = /\A#{TagStart}/o
|
||||
IsVariable = /\A#{VariableStart}/o
|
||||
FullToken = /\A#{TagStart}\s*(\w+)\s*(.*)?#{TagEnd}\z/om
|
||||
ContentOfVariable = /\A#{VariableStart}(.*)#{VariableEnd}\z/om
|
||||
TAGSTART = "{%".freeze
|
||||
VARSTART = "{{".freeze
|
||||
|
||||
def initialize(tag_name, markup, options)
|
||||
super
|
||||
@block_delimiter = "end#{tag_name}"
|
||||
end
|
||||
|
||||
def blank?
|
||||
@blank || false
|
||||
@@ -18,13 +23,14 @@ module Liquid
|
||||
@children = []
|
||||
|
||||
while token = tokens.shift
|
||||
case token
|
||||
when IsTag
|
||||
unless token.empty?
|
||||
case
|
||||
when token.start_with?(TAGSTART)
|
||||
if token =~ FullToken
|
||||
|
||||
# if we found the proper block delimiter just end parsing here and let the outer block
|
||||
# proceed
|
||||
if block_delimiter == $1
|
||||
if @block_delimiter == $1
|
||||
end_tag
|
||||
return
|
||||
end
|
||||
@@ -43,18 +49,17 @@ module Liquid
|
||||
else
|
||||
raise SyntaxError.new(options[:locale].t("errors.syntax.tag_termination".freeze, :token => token, :tag_end => TagEnd.inspect))
|
||||
end
|
||||
when IsVariable
|
||||
when token.start_with?(VARSTART)
|
||||
new_var = create_variable(token)
|
||||
@nodelist << new_var
|
||||
@children << new_var
|
||||
@blank = false
|
||||
when ''.freeze
|
||||
# pass
|
||||
else
|
||||
@nodelist << token
|
||||
@blank &&= (token =~ /\A\s*\z/)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Make sure that it's ok to end parsing in the current block.
|
||||
# Effectively this method will throw an exception unless the current block is
|
||||
@@ -85,16 +90,12 @@ module Liquid
|
||||
when 'end'.freeze
|
||||
raise SyntaxError.new(options[:locale].t("errors.syntax.invalid_delimiter".freeze,
|
||||
:block_name => block_name,
|
||||
:block_delimiter => block_delimiter))
|
||||
:block_delimiter => @block_delimiter))
|
||||
else
|
||||
raise SyntaxError.new(options[:locale].t("errors.syntax.unknown_tag".freeze, :tag => tag))
|
||||
end
|
||||
end
|
||||
|
||||
def block_delimiter
|
||||
"end#{block_name}"
|
||||
end
|
||||
|
||||
def block_name
|
||||
@tag_name
|
||||
end
|
||||
|
||||
@@ -8,7 +8,7 @@ module Liquid
|
||||
while token = tokens.shift
|
||||
if token =~ FullTokenPossiblyInvalid
|
||||
@nodelist << $1 if $1 != "".freeze
|
||||
if block_delimiter == $2
|
||||
if @block_delimiter == $2
|
||||
end_tag
|
||||
return
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user