Merge pull request #1014 from markroth8/mroth/fix/748
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Allow '{%+' to be valid syntax (but NOP) when lstrip_blocks == False. Fixes #748
authored by David Lord · committed by GitHub · parents 55cb0e7, 1e71e31 · Browse files · 3 files·+39 added−17 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+39 added−17 removed
- tests:+8 added−0 removed
- other:+31 added−17 removed
Provenance
- Commit
f76c3ed- Tree
9f50cb8- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
55cb0e7- Parent 2
1e71e31
3 files changed+39 added−17 removed
modified:jinja2/lexer.py+27 added-17 removed
443 unmodified lines444 block_suffix_re = environment.trim_blocks and '\\n?' or ''445 446 # strip leading spaces if lstrip_blocks is enabled447 prefix_re = {}448+ no_lstrip_re = e('+')449+ # detect overlap between block and variable or comment strings450+ block_diff = c(r'^%s(.*)' % e(environment.block_start_string))451+ # make sure we don't mistake a block for a variable or a comment452+ m = block_diff.match(environment.comment_start_string)453+ no_lstrip_re += m and r'|%s' % e(m.group(1)) or ''454+ m = block_diff.match(environment.variable_start_string)455+ no_lstrip_re += m and r'|%s' % e(m.group(1)) or ''456+ # detect overlap between comment and variable strings457+ comment_diff = c(r'^%s(.*)' % e(environment.comment_start_string))458+ m = comment_diff.match(environment.variable_start_string)459+ no_variable_re = m and r'(?!%s)' % e(m.group(1)) or ''460+461 if environment.lstrip_blocks:462 # use '{%+' to manually disable lstrip_blocks behavior450- no_lstrip_re = e('+')451- # detect overlap between block and variable or comment strings452- block_diff = c(r'^%s(.*)' % e(environment.block_start_string))453- # make sure we don't mistake a block for a variable or a comment454- m = block_diff.match(environment.comment_start_string)455- no_lstrip_re += m and r'|%s' % e(m.group(1)) or ''456- m = block_diff.match(environment.variable_start_string)457- no_lstrip_re += m and r'|%s' % e(m.group(1)) or ''458-459- # detect overlap between comment and variable strings460- comment_diff = c(r'^%s(.*)' % e(environment.comment_start_string))461- m = comment_diff.match(environment.variable_start_string)462- no_variable_re = m and r'(?!%s)' % e(m.group(1)) or ''463-463 lstrip_re = r'^[ \t]*'464 block_prefix_re = r'%s%s(?!%s)|%s\+?' % (465 lstrip_re,466 e(environment.block_start_string),5 unmodified lines472 e(environment.comment_start_string),473 no_variable_re,474 e(environment.comment_start_string),475 )477- prefix_re['block'] = block_prefix_re478- prefix_re['comment'] = comment_prefix_re476 else:480- block_prefix_re = '%s' % e(environment.block_start_string)477+ # If lstrip_blocks is False, then '{%+' is allowed but a NOP478+ block_prefix_re = r'%s(?!%s)|%s\+?' % (479+ e(environment.block_start_string),480+ no_lstrip_re,481+ e(environment.block_start_string),482+ )483+ comment_prefix_re = r'%s%s|%s\+?' % (484+ e(environment.comment_start_string),485+ no_variable_re,486+ e(environment.comment_start_string),487+ )488 489+ prefix_re['block'] = block_prefix_re490+ prefix_re['comment'] = comment_prefix_re491+492 self.newline_sequence = environment.newline_sequence493 self.keep_trailing_newline = environment.keep_trailing_newline494 495 # global lexing rules254 unmodified linesmodified:tests/test_lexnparse.py+8 added
501 unmodified lines502 env = Environment(lstrip_blocks=True, trim_blocks=False)503 tmpl = env.from_string(''' {%+ if True %}\n {%+ endif %}''')504 assert tmpl.render() == " \n "505 506+ def test_lstrip_blocks_false_with_no_lstrip(self, env):507+ # Test that + is a NOP (but does not cause an error) if lstrip_blocks=False508+ env = Environment(lstrip_blocks=False, trim_blocks=False)509+ tmpl = env.from_string(''' {% if True %}\n {% endif %}''')510+ assert tmpl.render() == " \n "511+ tmpl = env.from_string(''' {%+ if True %}\n {%+ endif %}''')512+ assert tmpl.render() == " \n "513+514 def test_lstrip_endline(self, env):515 env = Environment(lstrip_blocks=True, trim_blocks=False)516 tmpl = env.from_string(517 ''' hello{% if True %}\n goodbye{% endif %}''')148 unmodified linesmodified:CHANGES.rst+4 added
14 unmodified lines15 - Python 2.6 and 3.3 are not supported anymore.16 - The ``map`` filter in async mode now automatically awaits17 - Added a new ``ChainableUndefined`` class to support getitem18 and getattr on an undefined object. (`#977`_)19+- Allow `'{%+'` syntax (with NOP behavior) when20+ `lstrip_blocks == False` (`#748`_)21 22 .. _#765: https://github.com/pallets/jinja/issues/76523+.. _#748: https://github.com/pallets/jinja/issues/74824 .. _#977: https://github.com/pallets/jinja/issues/97725 26 27 Version 2.10.22 unmodified lines30 _Unreleased_31 32 - Fix Python 3.7 deprecation warnings.33 34+35 Version 2.10.136 --------------37 38 Released 2019-04-06598 unmodified lines