Markup and escape should be imported from markupsafe
This commit is contained in:
parent
39846a887b
commit
aafe94d97a
18
docs/api.rst
18
docs/api.rst
@ -609,28 +609,10 @@ functions to a Jinja environment.
|
||||
|
||||
.. 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
|
||||
----------
|
||||
|
||||
@ -2,9 +2,6 @@
|
||||
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
|
||||
@ -36,8 +33,10 @@ from .runtime import Undefined
|
||||
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
|
||||
|
||||
@ -12,8 +12,7 @@ from threading import Lock
|
||||
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])
|
||||
@ -332,9 +331,9 @@ def urlize(
|
||||
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, ""
|
||||
@ -448,7 +447,9 @@ def generate_lorem_ipsum(n=5, html=True, min=20, max=100):
|
||||
|
||||
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:
|
||||
@ -701,7 +702,7 @@ def select_autoescape(
|
||||
|
||||
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`.
|
||||
@ -730,7 +731,7 @@ def htmlsafe_json_dumps(
|
||||
if dumps is None:
|
||||
dumps = json.dumps
|
||||
|
||||
return Markup(
|
||||
return markupsafe.Markup(
|
||||
dumps(obj, **kwargs)
|
||||
.replace("<", "\\u003c")
|
||||
.replace(">", "\\u003e")
|
||||
@ -837,3 +838,24 @@ try:
|
||||
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)
|
||||
|
||||
@ -1,10 +1,10 @@
|
||||
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):
|
||||
|
||||
@ -2,9 +2,9 @@ import random
|
||||
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
|
||||
|
||||
@ -613,7 +613,7 @@ class TestBug:
|
||||
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())) == ""
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
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
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
import pytest
|
||||
from markupsafe import Markup
|
||||
|
||||
from jinja2 import Environment
|
||||
from jinja2 import Markup
|
||||
from jinja2 import TemplateAssertionError
|
||||
from jinja2 import TemplateRuntimeError
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user