Skip to content
Myr

Markup and escape should be imported from markupsafe

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Lord · committed by David Lord · parent 39846a8 · Browse files · 8 files+37 added−34 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

8 files+37 added−34 removed

  • tests:+5 added−5 removed
  • other:+32 added−29 removed

8 files+37 added−34 removed

modified:docs/api.rst-18 removed
607 unmodified lines
608.. autofunction:: jinja2.evalcontextfunction609610.. autofunction:: jinja2.environmentfunction611612.. function:: escape(s)613614 Convert the characters ``&``, ``<``, ``>``, ``'``, and ``"`` in string `s`615 to HTML-safe sequences. Use this if you need to display text that might616 contain such characters in HTML. This function will not escaped objects617 that do have an HTML representation such as already escaped data.618619 The return value is a :class:`Markup` string.620612.. autofunction:: jinja2.clear_caches613614.. autofunction:: jinja2.is_undefined615625.. autoclass:: jinja2.Markup([string])626 :members: escape, unescape, striptags616628.. admonition:: Note629630 The Jinja :class:`Markup` class is compatible with at least Pylons and631 Genshi. It's expected that more template engines and framework will pick632 up the `__html__` concept soon.633634617Exceptions618----------619620.. autoexception:: jinja2.TemplateError
311 unmodified lines
modified:src/jinja2/__init__.py+2 added-3 removed
1"""Jinja is a template engine written in pure Python. It provides a2non-XML syntax that supports inline expressions and an optional3sandboxed environment.4"""5from markupsafe import escape6from markupsafe import Markup75from .bccache import BytecodeCache6from .bccache import FileSystemBytecodeCache7from .bccache import MemcachedBytecodeCache8from .environment import Environment
23 unmodified lines
32from .runtime import Undefined33from .utils import clear_caches34from .utils import contextfunction35from .utils import environmentfunction36from .utils import escape37from .utils import evalcontextfunction38from .utils import is_undefined39from .utils import Markup40from .utils import pass_context41from .utils import pass_environment42from .utils import pass_eval_context43from .utils import select_autoescape
2 unmodified lines
modified:src/jinja2/utils.py+30 added-8 removed
10 unmodified lines
11from threading import Lock12from types import CodeType13from urllib.parse import quote_from_bytes1415from markupsafe import escape16from markupsafe import Markup15import markupsafe1617if t.TYPE_CHECKING:18 F = t.TypeVar("F", bound=t.Callable[..., t.Any])19
310 unmodified lines
330331 def trim_url(x):332 return x333335 words = re.split(r"(\s+)", str(escape(text)))336 rel_attr = f' rel="{escape(rel)}"' if rel else ""337 target_attr = f' target="{escape(target)}"' if target else ""334 words = re.split(r"(\s+)", str(markupsafe.escape(text)))335 rel_attr = f' rel="{markupsafe.escape(rel)}"' if rel else ""336 target_attr = f' target="{markupsafe.escape(target)}"' if target else ""337338 for i, word in enumerate(words):339 head, middle, tail = "", word, ""340 match = re.match(r"^([(<]|&lt;)+", middle)
105 unmodified lines
446 result.append(p)447448 if not html:449 return "\n\n".join(result)451 return Markup("\n".join(f"<p>{escape(x)}</p>" for x in result))450 return markupsafe.Markup(451 "\n".join(f"<p>{markupsafe.escape(x)}</p>" for x in result)452 )453454455def url_quote(obj: t.Any, charset: str = "utf-8", for_qs: bool = False) -> str:456 """Quote a string for use in a URL using the given charset.
244 unmodified lines
701702703def htmlsafe_json_dumps(704 obj: t.Any, dumps: t.Optional[t.Callable[..., str]] = None, **kwargs: t.Any704) -> Markup:705) -> markupsafe.Markup:706 """Serialize an object to a string of JSON with :func:`json.dumps`,707 then replace HTML-unsafe characters with Unicode escapes and mark708 the result safe with :class:`~markupsafe.Markup`.709
20 unmodified lines
730 """731 if dumps is None:732 dumps = json.dumps733733 return Markup(734 return markupsafe.Markup(735 dumps(obj, **kwargs)736 .replace("<", "\\u003c")737 .replace(">", "\\u003e")738 .replace("&", "\\u0026")
98 unmodified lines
837 exec("async def _():\n async for _ in ():\n yield _")838 have_async_gen = True839except SyntaxError:840 have_async_gen = False841842843class Markup(markupsafe.Markup):844 def __init__(self, *args, **kwargs):845 warnings.warn(846 "'jinja2.Markup' is deprecated and will be removed in Jinja"847 " 3.1. Import 'markupsafe.Markup' instead.",848 DeprecationWarning,849 stacklevel=2,850 )851 super().__init__(*args, **kwargs)852853854def escape(s):855 warnings.warn(856 "'jinja2.escape' is deprecated and will be removed in Jinja"857 " 3.1. Import 'markupsafe.escape' instead.",858 DeprecationWarning,859 stacklevel=2,860 )861 return markupsafe.escape(s)
modified:tests/test_asyncfilters.py+1 added-1 removed
1from collections import namedtuple23import pytest4from markupsafe import Markup56from jinja2 import Environment7from jinja2.asyncsupport import auto_aiter7from jinja2.utils import Markup8910async def make_aiter(iter):11 for item in iter:
242 unmodified lines
modified:tests/test_filters.py+1 added-1 removed
1import random2from collections import namedtuple34import pytest5from markupsafe import Markup67from jinja2 import Environment7from jinja2 import Markup8from jinja2 import StrictUndefined9from jinja2 import TemplateRuntimeError10from jinja2 import UndefinedError11from jinja2.exceptions import TemplateAssertionError
832 unmodified lines
modified:tests/test_regression.py+1 added-1 removed
611 unmodified lines
612 )613 assert tmpl.render(values=[]) == "0"614615 def test_markup_and_chainable_undefined(self):616 from jinja2 import Markup616 from markupsafe import Markup617 from jinja2.runtime import ChainableUndefined618619 assert str(Markup(ChainableUndefined())) == ""620
132 unmodified lines
modified:tests/test_security.py+1 added-1 removed
1import pytest2from markupsafe import escape34from jinja2 import Environment4from jinja2 import escape5from jinja2.exceptions import SecurityError6from jinja2.exceptions import TemplateRuntimeError7from jinja2.exceptions import TemplateSyntaxError8from jinja2.nodes import EvalContext
165 unmodified lines
modified:tests/test_tests.py+1 added-1 removed
1import pytest2from markupsafe import Markup34from jinja2 import Environment4from jinja2 import Markup5from jinja2 import TemplateAssertionError6from jinja2 import TemplateRuntimeError78
225 unmodified lines