Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix percent escape not working when not at the beginning of the line #383

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions mako/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,11 @@ def match_text(self):
r"""
(.*?) # anything, followed by:
(
(?<=\n)(?=[ \t]*(?=%|\#\#)) # an eval or line-based
# comment preceded by a
# consumed newline and whitespace
(?<=\n)(?=[ \t]*(?=%(?!%)|\#\#)) # an eval or line-based
# comment, preceded by a
# consumed newline and whitespace
|
(?<!%)(?=%%+) # consume the first percent sign out of a group of percent signs
|
(?=\${) # an expression
|
Expand Down
7 changes: 7 additions & 0 deletions mako/testing/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ def result_lines(result):
if x.strip() != ""
]

def result_raw_lines(result):
return [
x
for x in re.split(r"\r?\n", result)
if x.strip() != ""
]


def make_path(
filespec: Union[Path, str],
Expand Down
45 changes: 43 additions & 2 deletions test/test_lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,56 @@ 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("""% some whatever.\n\n """, (3, 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("%%% do something\n ", (4, 6)),
],
),
)

def test_percent_escape_with_control_block(self):
template = """
% for i in [1, 2, 3]:
%% do something ${i}
% endfor
"""
node = Lexer(template).parse()
self._compare(
node,
TemplateNode(
{},
[
Text('\n', (1, 1)),
ControlLine('for', 'for i in [1, 2, 3]:', False, (2, 1)),
Text(' ', (3, 1)),
Text('% do something ', (3, 6)),
Expression('i', [], (3, 21)),
Text('\n', (3, 25)),
ControlLine('for', 'endfor', True, (4, 1))]
),
)

def test_old_multiline_comment(self):
template = """#*"""
node = Lexer(template).parse()
Expand Down
33 changes: 32 additions & 1 deletion test/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from mako.testing.config import config
from mako.testing.fixtures import TemplateTest
from mako.testing.helpers import flatten_result
from mako.testing.helpers import result_lines
from mako.testing.helpers import result_lines, result_raw_lines


class ctx:
Expand Down Expand Up @@ -1667,3 +1667,34 @@ 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_raw_lines(t.render()) == [
"% do something",
"%% do something",
"if <some condition>:",
" %%% do something",
]

def test_percent_escape2(self):
t = Template(
"""
% for i in [1, 2, 3]:
%% do something ${i}
% endfor
"""
)
assert result_raw_lines(t.render()) == [
" % do something 1",
" % do something 2",
" % do something 3",
]