Skip to content

Commit

Permalink
Merge pull request #1391 from pallets/markupsafe-imports
Browse files Browse the repository at this point in the history
Markup and escape should be imported from markupsafe
  • Loading branch information
davidism authored Apr 10, 2021
2 parents 39846a8 + aafe94d commit a9b06f4
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 34 deletions.
18 changes: 0 additions & 18 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------
Expand Down
5 changes: 2 additions & 3 deletions src/jinja2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -36,8 +33,10 @@
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
Expand Down
38 changes: 30 additions & 8 deletions src/jinja2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
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])
Expand Down Expand Up @@ -332,9 +331,9 @@ def trim_url(x):
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, ""
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -701,7 +702,7 @@ def autoescape(template_name):

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`.
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -837,3 +838,24 @@ def __repr__(self):
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)
2 changes: 1 addition & 1 deletion tests/test_asyncfilters.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
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
Expand Down
2 changes: 1 addition & 1 deletion tests/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ def test_recursive_loop_bug(self, env):
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())) == ""
Expand Down
2 changes: 1 addition & 1 deletion tests/test_security.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/test_tests.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit a9b06f4

Please sign in to comment.