Skip to content

Commit

Permalink
Don't cleanup comments with a regular expression, use ast
Browse files Browse the repository at this point in the history
  • Loading branch information
mardiros authored and jpsca committed Aug 11, 2024
1 parent b1ea631 commit b8de102
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/jinjax/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ def load_metadata(self, source: str) -> None:
if expr:
if def_found:
raise DuplicateDefDeclaration(self.name)
expr = RX_INTER_COMMENTS.sub("", expr).replace("\n", " ")
self.required, self.optional = self.parse_args_expr(expr)
def_found = True
continue
Expand Down Expand Up @@ -208,7 +207,7 @@ def parse_args_expr(self, expr: str) -> tuple[list[str], dict[str, t.Any]]:
optional = {}

try:
p = ast.parse(f"def component(*, {expr}): pass")
p = ast.parse(f"def component(*,\n{expr}\n): pass")
except SyntaxError as err:
raise InvalidArgument(err) from err

Expand Down
95 changes: 95 additions & 0 deletions tests/test_render.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import time
from pathlib import Path
from textwrap import dedent

import jinja2
import pytest
Expand Down Expand Up @@ -815,6 +816,100 @@ def test_autoescaped_attrs(catalog, folder, autoescape):
assert html == Markup(expected)


@pytest.mark.parametrize(
"template",
[
pytest.param(
dedent(
"""
{# def
href,
hx_target="#maincontent",
hx_swap="innerHTML show:body:top",
hx_push_url=true,
#}
<a href="{{href}}" hx-get="{{href}}" hx-target="{{hx_target}}"
hx-swap="{{hx_swap}}"
{% if hx_push_url %}hx-push-url="true"{% endif %}>
{{- content -}}
</a>
"""
),
id="no comment",
),
pytest.param(
dedent(
"""
{# def
href,
hx_target="#maincontent", # css selector
hx_swap="innerHTML show:body:top",
hx_push_url=true,
#}
<a href="{{href}}" hx-get="{{href}}" hx-target="{{hx_target}}"
hx-swap="{{hx_swap}}"
{% if hx_push_url %}hx-push-url="true"{% endif %}>
{{- content -}}
</a>
"""
),
id="comment with # on line",
),
pytest.param(
dedent(
"""
{# def
href, # url of the target page
hx_target="#maincontent", # css selector
hx_swap="innerHTML show:body:top", # browse on top of the page
hx_push_url=true, # replace the url of the browser
#}
<a href="{{href}}" hx-get="{{href}}" hx-target="{{hx_target}}"
hx-swap="{{hx_swap}}"
{% if hx_push_url %}hx-push-url="true"{% endif %}>
{{- content -}}
</a>
"""
),
id="many comments",
),
pytest.param(
dedent(
"""
{# def
href: str, # url of the target page
hx_target: str = "#maincontent", # css selector
hx_swap: str = "innerHTML show:body:top", # browse on top of the page
hx_push_url: bool = true, # replace the url
#}
<a href="{{href}}" hx-get="{{href}}" hx-target="{{hx_target}}"
hx-swap="{{hx_swap}}"
{% if hx_push_url %}hx-push-url="true"{% endif %}>
{{- content -}}
</a>
"""
),
id="many comments and typing",
),
],
)
@pytest.mark.parametrize("autoescape", [True, False])
def test_strip_comment(catalog, folder, autoescape, template):
catalog.jinja_env.autoescape = autoescape

(folder / "A.jinja").write_text(template)

(folder / "Page.jinja").write_text("""<A href="/yolo">Yolo</A>""")

html = catalog.render("Page")
print(html)
expected = """
<a href="/yolo" hx-get="/yolo" hx-target="#maincontent"
hx-swap="innerHTML show:body:top"
hx-push-url="true">Yolo</a>""".strip()
assert html == Markup(expected)


@pytest.mark.parametrize("autoescape", [True, False])
def test_auto_load_assets_with_same_name(catalog, folder, autoescape):
catalog.jinja_env.autoescape = autoescape
Expand Down

0 comments on commit b8de102

Please sign in to comment.