]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
Fix unintended lstrip_blocks behavior. Fixes #1138
authorPeter Dolak <peter.dolak@exponea.com>
Thu, 2 Apr 2020 14:55:57 +0000 (16:55 +0200)
committerPeter Dolak <peter.dolak@exponea.com>
Mon, 13 Apr 2020 12:13:16 +0000 (14:13 +0200)
Introduced in #858. Tests will follow, also results of performance
testing.

src/jinja2/lexer.py

index 5fa940d90403cae25229d8066dd05fd68fe6df06..552356a12d1d812397bc5b4f0491a1c115c3797a 100644 (file)
@@ -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