Skip to content

Commit

Permalink
fix percent escape && add test
Browse files Browse the repository at this point in the history
  • Loading branch information
cocolato committed Jan 17, 2024
1 parent dc66614 commit 773b6bd
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
4 changes: 3 additions & 1 deletion mako/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,10 +357,12 @@ def match_text(self):
r"""
(.*?) # anything, followed by:
(
(?<=\n)(?=[ \t]*(?=%|\#\#)) # an eval or line-based
((?<=\n)|^)(?=[ \t]*(?=%|\#\#)) # an eval or line-based
# comment preceded by a
# consumed newline and whitespace
|
(?<!%)(?=%%+)
|
(?=\${) # an expression
|
(?=</?[%&]) # a substitution or block or call start or end
Expand Down
23 changes: 22 additions & 1 deletion test/test_lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,35 @@ def test_percent_escape(self):
[
Text("""\n\n""", (1, 1)),
Text("""% some whatever.\n\n""", (3, 2)),
Text(" %% more some whatever\n", (5, 2)),
Text(" ", (5, 2)),
Text("% more some whatever\n", (5, 6)),
ControlLine("if", "if foo:", False, (6, 1)),
ControlLine("if", "endif", True, (7, 1)),
Text(" ", (8, 1)),
],
),
)

def test_percent_escape2(self):
template = """%% do something
%%% do something
if <some condition>:
%%%% do something
"""
node = Lexer(template).parse()
self._compare(
node,
TemplateNode(
{},
[
Text("% do something\n", (1, 2)),
Text("%% do something\nif <some condition>:\n", (2, 2)),
Text(" ", (4, 2)),
Text("%%% do something\n ", (4, 6)),
],
),
)

def test_old_multiline_comment(self):
template = """#*"""
node = Lexer(template).parse()
Expand Down
17 changes: 17 additions & 0 deletions test/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -1667,3 +1667,20 @@ class FuturesTest(TemplateTest):
def test_future_import(self):
t = Template("${ x / y }", future_imports=["division"])
assert result_lines(t.render(x=12, y=5)) == ["2.4"]


class EscapeTest(TemplateTest):
def test_percent_escape(self):
t = Template(
"""%% do something
%%% do something
if <some condition>:
%%%% do something
"""
)
assert result_lines(t.render()) == [
"% do something",
"%% do something",
"if <some condition>:",
"%%% do something",
]

0 comments on commit 773b6bd

Please sign in to comment.