Skip to content

Commit 338b755

Browse files
LordArozzzeek
authored andcommitted
Fix matching multiline control lines in templates with CRLF line endings
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
1 parent 9b79bf0 commit 338b755

3 files changed

Lines changed: 27 additions & 1 deletion

File tree

doc/build/unreleased/346.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.. change::
2+
:tags: bug, lexer
3+
:tickets: 346
4+
:versions: 1.2.0, 1.1.6
5+
6+
Fixed issue where control statements on multi lines with a backslash would
7+
not parse correctly if the template itself contained CR/LF pairs as on
8+
Windows. Pull request courtesy Charles Pigott.
9+

mako/lexer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ def match_expression(self):
420420

421421
def match_control_line(self):
422422
match = self.match(
423-
r"(?<=^)[\t ]*(%(?!%)|##)[\t ]*((?:(?:\\r?\n)|[^\r\n])*)"
423+
r"(?<=^)[\t ]*(%(?!%)|##)[\t ]*((?:(?:\\\r?\n)|[^\r\n])*)"
424424
r"(?:\r?\n|\Z)",
425425
re.M,
426426
)

test/test_template.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,23 @@ def __exit__(self, *arg):
3232
pass
3333

3434

35+
class MiscTest(TemplateTest):
36+
def test_crlf_linebreaks(self):
37+
38+
crlf = r"""
39+
<%
40+
foo = True
41+
bar = True
42+
%>
43+
% if foo and \
44+
bar:
45+
foo and bar
46+
%endif
47+
"""
48+
crlf = crlf.replace("\n", "\r\n")
49+
self._do_test(Template(crlf), "\r\n\r\n foo and bar\r\n")
50+
51+
3552
class EncodingTest(TemplateTest):
3653
def test_escapes_html_tags(self):
3754
from mako.exceptions import html_error_template

0 commit comments

Comments
 (0)