Merge pull request #1183 from exponea/1138-fix-unintended-lstrip-behavior-change
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fix unintended lstrip_blocks behavior
authored by David Lord · committed by GitHub · parents 4a70cd4, 7163513 · Browse files · 3 files·+102 added−5 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+102 added−5 removed
- tests:+92 added−0 removed
- other:+10 added−5 removed
Provenance
- Commit
f1756a3- Tree
d01904e- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
4a70cd4- Parent 2
7163513
3 files changed+102 added−5 removed
modified:src/jinja2/lexer.py+8 added-5 removed
680 unmodified lines681 source_length = len(source)682 balancing_stack = []683 lstrip_unless_re = self.lstrip_unless_re684 newlines_stripped = 0685+ line_starting = True686 687 while 1:688 # tokenizer loop689 for regex, tokens, new_state in statetokens:41 unmodified lines731 and not m.groupdict().get(TOKEN_VARIABLE_BEGIN)732 ):733 # The start of text between the last newline and the tag.734 l_pos = text.rfind("\n") + 1735+ if l_pos > 0 or line_starting:736+ # If there's only whitespace between the newline and the737+ # tag, strip it.738+ if not lstrip_unless_re.search(text, l_pos):739+ groups = (text[:l_pos],) + groups[1:]740 735- # If there's only whitespace between the newline and the736- # tag, strip it.737- if not lstrip_unless_re.search(text, l_pos):738- groups = (text[:l_pos],) + groups[1:]739-741 for idx, token in enumerate(tokens):742 # failure group743 if token.__class__ is Failure:744 raise token(lineno, filename)49 unmodified lines794 if data or tokens not in ignore_if_empty:795 yield lineno, tokens, data796 lineno += data.count("\n")797 798+ line_starting = m.group()[-1:] == "\n"799+800 # fetch new position into new variable so that we can check801 # if there is a internal parsing error which would result802 # in an infinite loop803 pos2 = m.end()45 unmodified linesmodified:tests/test_lexnparse.py+92 added
727 unmodified lines728 <%endfor%>"""729 )730 assert tmpl.render(seq=range(5)) == "".join("%s\n" % x for x in range(5))731 732+ def test_lstrip_blocks_outside_with_new_line(self):733+ env = Environment(lstrip_blocks=True, trim_blocks=False)734+ tmpl = env.from_string(735+ " {% if kvs %}(\n"736+ " {% for k, v in kvs %}{{ k }}={{ v }} {% endfor %}\n"737+ " ){% endif %}"738+ )739+ out = tmpl.render(kvs=[("a", 1), ("b", 2)])740+ assert out == "(\na=1 b=2 \n )"741+742+ def test_lstrip_trim_blocks_outside_with_new_line(self):743+ env = Environment(lstrip_blocks=True, trim_blocks=True)744+ tmpl = env.from_string(745+ " {% if kvs %}(\n"746+ " {% for k, v in kvs %}{{ k }}={{ v }} {% endfor %}\n"747+ " ){% endif %}"748+ )749+ out = tmpl.render(kvs=[("a", 1), ("b", 2)])750+ assert out == "(\na=1 b=2 )"751+752+ def test_lstrip_blocks_inside_with_new_line(self):753+ env = Environment(lstrip_blocks=True, trim_blocks=False)754+ tmpl = env.from_string(755+ " ({% if kvs %}\n"756+ " {% for k, v in kvs %}{{ k }}={{ v }} {% endfor %}\n"757+ " {% endif %})"758+ )759+ out = tmpl.render(kvs=[("a", 1), ("b", 2)])760+ assert out == " (\na=1 b=2 \n)"761+762+ def test_lstrip_trim_blocks_inside_with_new_line(self):763+ env = Environment(lstrip_blocks=True, trim_blocks=True)764+ tmpl = env.from_string(765+ " ({% if kvs %}\n"766+ " {% for k, v in kvs %}{{ k }}={{ v }} {% endfor %}\n"767+ " {% endif %})"768+ )769+ out = tmpl.render(kvs=[("a", 1), ("b", 2)])770+ assert out == " (a=1 b=2 )"771+772+ def test_lstrip_blocks_without_new_line(self):773+ env = Environment(lstrip_blocks=True, trim_blocks=False)774+ tmpl = env.from_string(775+ " {% if kvs %}"776+ " {% for k, v in kvs %}{{ k }}={{ v }} {% endfor %}"777+ " {% endif %}"778+ )779+ out = tmpl.render(kvs=[("a", 1), ("b", 2)])780+ assert out == " a=1 b=2 "781+782+ def test_lstrip_trim_blocks_without_new_line(self):783+ env = Environment(lstrip_blocks=True, trim_blocks=True)784+ tmpl = env.from_string(785+ " {% if kvs %}"786+ " {% for k, v in kvs %}{{ k }}={{ v }} {% endfor %}"787+ " {% endif %}"788+ )789+ out = tmpl.render(kvs=[("a", 1), ("b", 2)])790+ assert out == " a=1 b=2 "791+792+ def test_lstrip_blocks_consume_after_without_new_line(self):793+ env = Environment(lstrip_blocks=True, trim_blocks=False)794+ tmpl = env.from_string(795+ " {% if kvs -%}"796+ " {% for k, v in kvs %}{{ k }}={{ v }} {% endfor -%}"797+ " {% endif -%}"798+ )799+ out = tmpl.render(kvs=[("a", 1), ("b", 2)])800+ assert out == "a=1 b=2 "801+802+ def test_lstrip_trim_blocks_consume_before_without_new_line(self):803+ env = Environment(lstrip_blocks=False, trim_blocks=False)804+ tmpl = env.from_string(805+ " {%- if kvs %}"806+ " {%- for k, v in kvs %}{{ k }}={{ v }} {% endfor -%}"807+ " {%- endif %}"808+ )809+ out = tmpl.render(kvs=[("a", 1), ("b", 2)])810+ assert out == "a=1 b=2 "811+812+ def test_lstrip_trim_blocks_comment(self):813+ env = Environment(lstrip_blocks=True, trim_blocks=True)814+ tmpl = env.from_string(" {# 1 space #}\n {# 2 spaces #} {# 4 spaces #}")815+ out = tmpl.render()816+ assert out == " " * 4817+818+ def test_lstrip_trim_blocks_raw(self):819+ env = Environment(lstrip_blocks=True, trim_blocks=True)820+ tmpl = env.from_string("{{x}}\n{%- raw %} {% endraw -%}\n{{ y }}")821+ out = tmpl.render(x=1, y=2)822+ assert out == "1 2"823+824 def test_php_syntax_with_manual(self, env):825 env = Environment(826 "<?", "?>", "<?=", "?>", "<!--", "-->", lstrip_blocks=True, trim_blocks=True827 )103 unmodified linesmodified:CHANGES.rst+2 added
23 unmodified lines24 - Fix line numbers in error messages when newlines are stripped.25 :pr:`1178`26 - The special ``namespace()`` assignment object in templates works in27 async environments. :issue:`1180`28+- Fix whitespace being removed before tags in the middle of lines when29+ ``lstrip_blocks`` is enabled. :issue:`1138`30 31 32 Version 2.11.133 --------------737 unmodified lines