From: Peter Dolak Date: Thu, 2 Apr 2020 14:55:57 +0000 (+0200) Subject: Fix unintended lstrip_blocks behavior. Fixes #1138 X-Git-Tag: 2.11.2~2^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=dbbd082bce92cb21ae3e4f49aa32554e05e5d8fc;p=thirdparty%2Fjinja.git Fix unintended lstrip_blocks behavior. Fixes #1138 Introduced in #858. Tests will follow, also results of performance testing. --- diff --git a/src/jinja2/lexer.py b/src/jinja2/lexer.py index 5fa940d9..552356a1 100644 --- a/src/jinja2/lexer.py +++ b/src/jinja2/lexer.py @@ -682,6 +682,7 @@ class Lexer(object): balancing_stack = [] lstrip_unless_re = self.lstrip_unless_re newlines_stripped = 0 + line_starting = True while 1: # tokenizer loop @@ -731,11 +732,11 @@ class Lexer(object): ): # The start of text between the last newline and the tag. l_pos = text.rfind("\n") + 1 - - # If there's only whitespace between the newline and the - # tag, strip it. - if not lstrip_unless_re.search(text, l_pos): - groups = (text[:l_pos],) + groups[1:] + if l_pos > 0 or line_starting: + # If there's only whitespace between the newline and the + # tag, strip it. + if not lstrip_unless_re.search(text, l_pos): + groups = (text[:l_pos],) + groups[1:] for idx, token in enumerate(tokens): # failure group @@ -794,6 +795,8 @@ class Lexer(object): yield lineno, tokens, data lineno += data.count("\n") + line_starting = m.group()[-1:] == "\n" + # fetch new position into new variable so that we can check # if there is a internal parsing error which would result # in an infinite loop