summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrey Lisin <andrey.lisin@jetbrains.com>2020-03-27 13:20:01 +0300
committerDavid Lord <davidism@gmail.com>2020-03-30 11:11:07 -0700
commit77a212bf93933237cd3574fb44368eea2659f9c5 (patch)
treeed598a39b2d9b953803738cfe8bcec99441312ee
parent07b5c01338bbfc06be9afc80a127a327611d9a6d (diff)
downloadjinja2-77a212bf93933237cd3574fb44368eea2659f9c5.tar.gz
Fix tokens line number calculation when whitespace stripping is used
-rw-r--r--CHANGES.rst2
-rw-r--r--src/jinja2/lexer.py8
-rw-r--r--tests/test_lexnparse.py18
3 files changed, 26 insertions, 2 deletions
diff --git a/CHANGES.rst b/CHANGES.rst
index b16ed2b..62b1b07 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -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
diff --git a/src/jinja2/lexer.py b/src/jinja2/lexer.py
index a2b44e9..5fa940d 100644
--- a/src/jinja2/lexer.py
+++ b/src/jinja2/lexer.py
@@ -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:
diff --git a/tests/test_lexnparse.py b/tests/test_lexnparse.py
index e799acd..3355791 100644
--- a/tests/test_lexnparse.py
+++ b/tests/test_lexnparse.py
@@ -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):