Skip to content

Commit 5182b64

Browse files
mvolfikdavidism
authored andcommitted
Re-use existing regex for whitespace detection
Whitespace regex could also detect newlines, but since we're only matching the part from last newline, it's safer (and more readable) to use it here.
1 parent a12ad40 commit 5182b64

1 file changed

Lines changed: 3 additions & 6 deletions

File tree

src/jinja2/lexer.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -507,9 +507,7 @@ def c(x: str) -> t.Pattern[str]:
507507
# block suffix if trimming is enabled
508508
block_suffix_re = "\\n?" if environment.trim_blocks else ""
509509

510-
# If lstrip is enabled, it should not be applied if there is any
511-
# non-whitespace between the newline and block.
512-
self.lstrip_unless_re = c(r"[^ \t]") if environment.lstrip_blocks else None
510+
self.lstrip_blocks = environment.lstrip_blocks
513511

514512
self.newline_sequence = environment.newline_sequence
515513
self.keep_trailing_newline = environment.keep_trailing_newline
@@ -697,7 +695,6 @@ def tokeniter(
697695
statetokens = self.rules[stack[-1]]
698696
source_length = len(source)
699697
balancing_stack: t.List[str] = []
700-
lstrip_unless_re = self.lstrip_unless_re
701698
newlines_stripped = 0
702699
line_starting = True
703700

@@ -743,7 +740,7 @@ def tokeniter(
743740
# Not marked for preserving whitespace.
744741
strip_sign != "+"
745742
# lstrip is enabled.
746-
and lstrip_unless_re is not None
743+
and self.lstrip_blocks
747744
# Not a variable expression.
748745
and not m.groupdict().get(TOKEN_VARIABLE_BEGIN)
749746
):
@@ -753,7 +750,7 @@ def tokeniter(
753750
if l_pos > 0 or line_starting:
754751
# If there's only whitespace between the newline and the
755752
# tag, strip it.
756-
if not lstrip_unless_re.search(text, l_pos):
753+
if whitespace_re.fullmatch(text, l_pos):
757754
groups = [text[:l_pos], *groups[1:]]
758755

759756
for idx, token in enumerate(tokens):

0 commit comments

Comments
 (0)