Skip to content

Commit

Permalink
Merge pull request #36 from JakobGM/forward-autoescape-from-jinja-env
Browse files Browse the repository at this point in the history
Forward autoescape configuration of `jinja2.Environment` injected into `jinjax.Catalog`
  • Loading branch information
jpsca authored Jan 25, 2024
2 parents f2d0c5e + 145e64a commit 68620ce
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/jinjax/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,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)
Expand Down
11 changes: 10 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
import jinja2
import jinjax
import pytest


@pytest.fixture()
Expand All @@ -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
5 changes: 2 additions & 3 deletions tests/test_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,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 #}
Expand All @@ -485,8 +485,7 @@ def test_autoescape_doesnot_escape_subcomponents(catalog, folder):
<p>foo bar</p>
""")

catalog.jinja_env.autoescape = True
html = catalog.render("Page", message="<3")
html = autoescaped_catalog.render("Page", message="<3")
assert html == """
<html>
<p>lorem ipsum</p>
Expand Down

0 comments on commit 68620ce

Please sign in to comment.