.. autofunction:: jinja2.environmentfunction
-.. function:: escape(s)
-
- Convert the characters ``&``, ``<``, ``>``, ``'``, and ``"`` in string `s`
- to HTML-safe sequences. Use this if you need to display text that might
- contain such characters in HTML. This function will not escaped objects
- that do have an HTML representation such as already escaped data.
-
- The return value is a :class:`Markup` string.
-
.. autofunction:: jinja2.clear_caches
.. autofunction:: jinja2.is_undefined
-.. autoclass:: jinja2.Markup([string])
- :members: escape, unescape, striptags
-
-.. admonition:: Note
-
- The Jinja :class:`Markup` class is compatible with at least Pylons and
- Genshi. It's expected that more template engines and framework will pick
- up the `__html__` concept soon.
-
Exceptions
----------
non-XML syntax that supports inline expressions and an optional
sandboxed environment.
"""
-from markupsafe import escape
-from markupsafe import Markup
-
from .bccache import BytecodeCache
from .bccache import FileSystemBytecodeCache
from .bccache import MemcachedBytecodeCache
from .utils import clear_caches
from .utils import contextfunction
from .utils import environmentfunction
+from .utils import escape
from .utils import evalcontextfunction
from .utils import is_undefined
+from .utils import Markup
from .utils import pass_context
from .utils import pass_environment
from .utils import pass_eval_context
from types import CodeType
from urllib.parse import quote_from_bytes
-from markupsafe import escape
-from markupsafe import Markup
+import markupsafe
if t.TYPE_CHECKING:
F = t.TypeVar("F", bound=t.Callable[..., t.Any])
def trim_url(x):
return x
- words = re.split(r"(\s+)", str(escape(text)))
- rel_attr = f' rel="{escape(rel)}"' if rel else ""
- target_attr = f' target="{escape(target)}"' if target else ""
+ words = re.split(r"(\s+)", str(markupsafe.escape(text)))
+ rel_attr = f' rel="{markupsafe.escape(rel)}"' if rel else ""
+ target_attr = f' target="{markupsafe.escape(target)}"' if target else ""
for i, word in enumerate(words):
head, middle, tail = "", word, ""
if not html:
return "\n\n".join(result)
- return Markup("\n".join(f"<p>{escape(x)}</p>" for x in result))
+ return markupsafe.Markup(
+ "\n".join(f"<p>{markupsafe.escape(x)}</p>" for x in result)
+ )
def url_quote(obj: t.Any, charset: str = "utf-8", for_qs: bool = False) -> str:
def htmlsafe_json_dumps(
obj: t.Any, dumps: t.Optional[t.Callable[..., str]] = None, **kwargs: t.Any
-) -> Markup:
+) -> markupsafe.Markup:
"""Serialize an object to a string of JSON with :func:`json.dumps`,
then replace HTML-unsafe characters with Unicode escapes and mark
the result safe with :class:`~markupsafe.Markup`.
if dumps is None:
dumps = json.dumps
- return Markup(
+ return markupsafe.Markup(
dumps(obj, **kwargs)
.replace("<", "\\u003c")
.replace(">", "\\u003e")
have_async_gen = True
except SyntaxError:
have_async_gen = False
+
+
+class Markup(markupsafe.Markup):
+ def __init__(self, *args, **kwargs):
+ warnings.warn(
+ "'jinja2.Markup' is deprecated and will be removed in Jinja"
+ " 3.1. Import 'markupsafe.Markup' instead.",
+ DeprecationWarning,
+ stacklevel=2,
+ )
+ super().__init__(*args, **kwargs)
+
+
+def escape(s):
+ warnings.warn(
+ "'jinja2.escape' is deprecated and will be removed in Jinja"
+ " 3.1. Import 'markupsafe.escape' instead.",
+ DeprecationWarning,
+ stacklevel=2,
+ )
+ return markupsafe.escape(s)
from collections import namedtuple
import pytest
+from markupsafe import Markup
from jinja2 import Environment
from jinja2.asyncsupport import auto_aiter
-from jinja2.utils import Markup
async def make_aiter(iter):
from collections import namedtuple
import pytest
+from markupsafe import Markup
from jinja2 import Environment
-from jinja2 import Markup
from jinja2 import StrictUndefined
from jinja2 import TemplateRuntimeError
from jinja2 import UndefinedError
assert tmpl.render(values=[]) == "0"
def test_markup_and_chainable_undefined(self):
- from jinja2 import Markup
+ from markupsafe import Markup
from jinja2.runtime import ChainableUndefined
assert str(Markup(ChainableUndefined())) == ""
import pytest
+from markupsafe import escape
from jinja2 import Environment
-from jinja2 import escape
from jinja2.exceptions import SecurityError
from jinja2.exceptions import TemplateRuntimeError
from jinja2.exceptions import TemplateSyntaxError
import pytest
+from markupsafe import Markup
from jinja2 import Environment
-from jinja2 import Markup
from jinja2 import TemplateAssertionError
from jinja2 import TemplateRuntimeError