]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
Fix tokens line number calculation when whitespace stripping is used 1178/head
authorAndrey Lisin <andrey.lisin@jetbrains.com>
Fri, 27 Mar 2020 10:20:01 +0000 (13:20 +0300)
committerDavid Lord <davidism@gmail.com>
Mon, 30 Mar 2020 18:11:07 +0000 (11:11 -0700)
CHANGES.rst
src/jinja2/lexer.py
tests/test_lexnparse.py

index b16ed2b4af7c1080cb305be9540bb9fe628b7a1e..62b1b072c2f93709e7c73a559a9163dd9720426c 100644 (file)
@@ -21,6 +21,8 @@ Unreleased
     when using Pytest. Due to the difficulty in supporting Python 2 and
     :pep:`451` simultaneously, the changes are reverted until 3.0.
     :pr:`1182`
+-   Fix line numbers in error messages when newlines are stripped.
+    :pr:`1178`
 
 
 Version 2.11.1
index a2b44e926b06625353f4b1a990ceb3151a32512b..5fa940d90403cae25229d8066dd05fd68fe6df06 100644 (file)
@@ -681,6 +681,7 @@ class Lexer(object):
         source_length = len(source)
         balancing_stack = []
         lstrip_unless_re = self.lstrip_unless_re
+        newlines_stripped = 0
 
         while 1:
             # tokenizer loop
@@ -717,7 +718,9 @@ class Lexer(object):
 
                         if strip_sign == "-":
                             # Strip all whitespace between the text and the tag.
-                            groups = (text.rstrip(),) + groups[1:]
+                            stripped = text.rstrip()
+                            newlines_stripped = text[len(stripped) :].count("\n")
+                            groups = (stripped,) + groups[1:]
                         elif (
                             # Not marked for preserving whitespace.
                             strip_sign != "+"
@@ -758,7 +761,8 @@ class Lexer(object):
                             data = groups[idx]
                             if data or token not in ignore_if_empty:
                                 yield lineno, token, data
-                            lineno += data.count("\n")
+                            lineno += data.count("\n") + newlines_stripped
+                            newlines_stripped = 0
 
                 # strings as token just are yielded as it.
                 else:
index e799acdcbadeb310d5bf50e4d9faee7becd58f82..3355791a0c328659bd3231b8baf7e5392d8de6ab 100644 (file)
@@ -178,6 +178,24 @@ class TestLexer(object):
         else:
             pytest.raises(TemplateSyntaxError, env.from_string, t)
 
+    def test_lineno_with_strip(self, env):
+        tokens = env.lex(
+            """\
+<html>
+    <body>
+    {%- block content -%}
+        <hr>
+        {{ item }}
+    {% endblock %}
+    </body>
+</html>"""
+        )
+        for tok in tokens:
+            lineno, token_type, value = tok
+            if token_type == "name" and value == "item":
+                assert lineno == 5
+                break
+
 
 class TestParser(object):
     def test_php_syntax(self, env):