From 145e64a88c27b921c5891bb257041d33a9fb518f Mon Sep 17 00:00:00 2001 From: Jakob Gerhard Martinussen Date: Wed, 29 Nov 2023 15:04:08 +0100 Subject: [PATCH] Respect autoescape=True from jinja_env passed to jinjax.Catalog --- src/jinjax/catalog.py | 1 + tests/conftest.py | 11 ++++++++++- tests/test_render.py | 5 ++--- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/jinjax/catalog.py b/src/jinjax/catalog.py index 776c41f..7c1ca32 100644 --- a/src/jinjax/catalog.py +++ b/src/jinjax/catalog.py @@ -71,6 +71,7 @@ def __init__( if jinja_env: env.extensions.update(jinja_env.extensions) + env.autoescape = jinja_env.autoescape globals.update(jinja_env.globals) filters.update(jinja_env.filters) tests.update(jinja_env.tests) diff --git a/tests/conftest.py b/tests/conftest.py index 537a068..c1f9b2a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,5 +1,6 @@ -import pytest +import jinja2 import jinjax +import pytest @pytest.fixture() @@ -21,3 +22,11 @@ def catalog(folder): catalog = jinjax.Catalog(auto_reload=False) catalog.add_folder(folder) return catalog + + +@pytest.fixture() +def autoescaped_catalog(folder): + jinja_env = jinja2.Environment(autoescape=True) + catalog = jinjax.Catalog(auto_reload=False, jinja_env=jinja_env) + catalog.add_folder(folder) + return catalog diff --git a/tests/test_render.py b/tests/test_render.py index a30eaed..adf2834 100644 --- a/tests/test_render.py +++ b/tests/test_render.py @@ -466,7 +466,7 @@ def test_auto_reload(catalog, folder): """.strip() in html3 -def test_autoescape_doesnot_escape_subcomponents(catalog, folder): +def test_autoescape_doesnot_escape_subcomponents(autoescaped_catalog, folder): """Issue https://github.com/jpsca/jinjax/issues/32""" (folder / "Page.jinja").write_text(""" {#def message #} @@ -481,8 +481,7 @@ def test_autoescape_doesnot_escape_subcomponents(catalog, folder):

foo bar

""") - catalog.jinja_env.autoescape = True - html = catalog.render("Page", message="<3") + html = autoescaped_catalog.render("Page", message="<3") assert html == """

lorem ipsum