From b8de102c7644c4af1ac2446d3836ab47f8e5d4af Mon Sep 17 00:00:00 2001 From: Guillaume Gauvrit Date: Sat, 10 Aug 2024 18:05:33 +0200 Subject: [PATCH] Don't cleanup comments with a regular expression, use ast --- src/jinjax/component.py | 3 +- tests/test_render.py | 95 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 2 deletions(-) diff --git a/src/jinjax/component.py b/src/jinjax/component.py index c4f37a5..948d170 100644 --- a/src/jinjax/component.py +++ b/src/jinjax/component.py @@ -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 @@ -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 diff --git a/tests/test_render.py b/tests/test_render.py index 0327521..02d999e 100644 --- a/tests/test_render.py +++ b/tests/test_render.py @@ -1,5 +1,6 @@ import time from pathlib import Path +from textwrap import dedent import jinja2 import pytest @@ -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, + #} + + {{- content -}} + + """ + ), + id="no comment", + ), + pytest.param( + dedent( + """ + {# def + href, + hx_target="#maincontent", # css selector + hx_swap="innerHTML show:body:top", + hx_push_url=true, + #} + + {{- content -}} + + """ + ), + 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 + #} + + {{- content -}} + + """ + ), + 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 + #} + + {{- content -}} + + """ + ), + 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("""Yolo""") + + html = catalog.render("Page") + print(html) + expected = """ +Yolo""".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