Skip to content

Commit

Permalink
Fix matching multiline control lines in templates with CRLF line endings
Browse files Browse the repository at this point in the history
Fixed issue where control statements on multi lines with a backslash would
not parse correctly if the template itself contained CR/LF pairs as on
Windows. Pull request courtesy Charles Pigott.

A missing '\\' meant that it would actually allow

```
% if foo \r
   bar:
```

in a template file and not match if the file actually had a `\r` char

Closes: #346
Pull-request: #346
Pull-request-sha: e79ebab

Change-Id: I179bdd661cecb1ffb3cf262e31183c8e83d98f12
(cherry picked from commit 338b755)
  • Loading branch information
LordAro authored and zzzeek committed Nov 13, 2021
1 parent 2ae09a3 commit 0c1222d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
9 changes: 9 additions & 0 deletions doc/build/unreleased/346.rst
@@ -0,0 +1,9 @@
.. change::
:tags: bug, lexer
:tickets: 346
:versions: 1.2.0, 1.1.6

Fixed issue where control statements on multi lines with a backslash would
not parse correctly if the template itself contained CR/LF pairs as on
Windows. Pull request courtesy Charles Pigott.

2 changes: 1 addition & 1 deletion mako/lexer.py
Expand Up @@ -443,7 +443,7 @@ def match_expression(self):

def match_control_line(self):
match = self.match(
r"(?<=^)[\t ]*(%(?!%)|##)[\t ]*((?:(?:\\r?\n)|[^\r\n])*)"
r"(?<=^)[\t ]*(%(?!%)|##)[\t ]*((?:(?:\\\r?\n)|[^\r\n])*)"
r"(?:\r?\n|\Z)",
re.M,
)
Expand Down
17 changes: 17 additions & 0 deletions test/test_template.py
Expand Up @@ -35,6 +35,23 @@ def __exit__(self, *arg):
pass


class MiscTest(TemplateTest):
def test_crlf_linebreaks(self):

crlf = r"""
<%
foo = True
bar = True
%>
% if foo and \
bar:
foo and bar
%endif
"""
crlf = crlf.replace("\n", "\r\n")
self._do_test(Template(crlf), "\r\n\r\n foo and bar\r\n")


class EncodingTest(TemplateTest):
def test_escapes_html_tags(self):
from mako.exceptions import html_error_template
Expand Down

0 comments on commit 0c1222d

Please sign in to comment.