diff options
author | Charles Pigott <charlespigott@googlemail.com> | 2021-11-13 10:58:09 -0500 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2021-11-13 11:14:37 -0500 |
commit | 0c1222d1ce88d8178c4e0a27809a055c2e0738ec (patch) | |
tree | 4beb1e1ae48d78d0f7a4c7b7d6d511bc25876c48 | |
parent | 2ae09a3d102546c47a723bf268d67926f43c8435 (diff) | |
download | mako-0c1222d1ce88d8178c4e0a27809a055c2e0738ec.tar.gz |
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: https://github.com/sqlalchemy/mako/pull/346
Pull-request-sha: e79ebabe3df7e59c9ea40e62406131e1a0c6c3b4
Change-Id: I179bdd661cecb1ffb3cf262e31183c8e83d98f12
(cherry picked from commit 338b75544b320d538a8b116c2b8416e08e216003)
-rw-r--r-- | doc/build/unreleased/346.rst | 9 | ||||
-rw-r--r-- | mako/lexer.py | 2 | ||||
-rw-r--r-- | test/test_template.py | 17 |
3 files changed, 27 insertions, 1 deletions
diff --git a/doc/build/unreleased/346.rst b/doc/build/unreleased/346.rst new file mode 100644 index 0000000..329116c --- /dev/null +++ b/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. + diff --git a/mako/lexer.py b/mako/lexer.py index 6226e26..bbf0c3a 100644 --- a/mako/lexer.py +++ b/mako/lexer.py @@ -443,7 +443,7 @@ class Lexer(object): 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, ) diff --git a/test/test_template.py b/test/test_template.py index f9727be..47d2f80 100644 --- a/test/test_template.py +++ b/test/test_template.py @@ -35,6 +35,23 @@ class ctx(object): 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 |