Skip to content
Myr

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

3 files+39 added−17 removed

modified:jinja2/lexer.py+27 added-17 removed
443 unmodified lines
444 block_suffix_re = environment.trim_blocks and '\\n?' or ''445446 # 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 ''460461 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 ''458459 # 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 ''463463 lstrip_re = r'^[ \t]*'464 block_prefix_re = r'%s%s(?!%s)|%s\+?' % (465 lstrip_re,466 e(environment.block_start_string),
5 unmodified lines
472 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 )488489 prefix_re['block'] = block_prefix_re490 prefix_re['comment'] = comment_prefix_re491492 self.newline_sequence = environment.newline_sequence493 self.keep_trailing_newline = environment.keep_trailing_newline494495 # global lexing rules
254 unmodified lines
modified:tests/test_lexnparse.py+8 added
501 unmodified lines
502 env = Environment(lstrip_blocks=True, trim_blocks=False)503 tmpl = env.from_string(''' {%+ if True %}\n {%+ endif %}''')504 assert tmpl.render() == " \n "505506 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 "513514 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 lines
modified:CHANGES.rst+4 added
14 unmodified lines
15- 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`_)2122.. _#765: https://github.com/pallets/jinja/issues/76523.. _#748: https://github.com/pallets/jinja/issues/74824.. _#977: https://github.com/pallets/jinja/issues/977252627Version 2.10.2
2 unmodified lines
30_Unreleased_3132- Fix Python 3.7 deprecation warnings.333435Version 2.10.136--------------3738Released 2019-04-06
598 unmodified lines