]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
Markup and escape should be imported from markupsafe 1391/head
authorDavid Lord <davidism@gmail.com>
Sat, 10 Apr 2021 17:22:24 +0000 (10:22 -0700)
committerDavid Lord <davidism@gmail.com>
Sat, 10 Apr 2021 17:22:24 +0000 (10:22 -0700)
docs/api.rst
src/jinja2/__init__.py
src/jinja2/utils.py
tests/test_asyncfilters.py
tests/test_filters.py
tests/test_regression.py
tests/test_security.py
tests/test_tests.py

index ce3a26eda1026af4178ee70162b4dd17be7433be..79efd872075577f2623607dde15f3ae638be1f2e 100644 (file)
@@ -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
 ----------
index 2682304b165ea8f6c6eddb39c7faa9db949e8fe1..bdb49c5bb5b550d645e6b471e469946cea2ca763 100644 (file)
@@ -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
index 61505780f31707c55564cbbfa66d04a46c064901..80769a7329b9c2c3128e728adfb333b7506a5359 100644 (file)
@@ -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)
index b6025aca040306adbade761e99292a874039bbd3..f5fcbf29fd04cfd0767e4ef7910aea1bf8730908 100644 (file)
@@ -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):
index 0843246bb2aab5773906af72d3773f30c9df8db7..2195157c4f1d8f5ed647ab2ea1ed30dad2735228 100644 (file)
@@ -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
index 8e86e41806c5c4d44a4b9b2f9e051e07c257391e..5d7a7fb232ae3602656f7099780934f506a147b8 100644 (file)
@@ -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())) == ""
index 1b64cd37cb5d850b505aa46f28d988a5980709f3..0e8dc5c0385d3baffffb100353e70d7183b482e9 100644 (file)
@@ -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
index 4d56a15db6b7c7615b94d3554ac2c8ddde3d1f4a..75178d6adfbf257e941ffbe6beead6933329afd6 100644 (file)
@@ -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