diff options
| author | Claude Paroz <claude@2xlibre.net> | 2017-01-30 19:15:59 +0100 |
|---|---|---|
| committer | Claude Paroz <claude@2xlibre.net> | 2017-01-30 21:10:32 +0100 |
| commit | ccfd1295f986cdf628d774937d0b38a14584721f (patch) | |
| tree | c3d37f5fff520822674127940c37c42f09c821d0 | |
| parent | 274ca999825bb782bbbddd769783cf2aa91de7f9 (diff) | |
Refs #27795 -- Prevented SafeText from losing safe status on str()
This will allow to replace force_text() by str() in several places (as one of
the features of force_text is to keep the safe status).
| -rw-r--r-- | django/utils/safestring.py | 3 | ||||
| -rw-r--r-- | tests/utils_tests/test_safestring.py | 7 |
2 files changed, 10 insertions, 0 deletions
diff --git a/django/utils/safestring.py b/django/utils/safestring.py index 5f5e7098f2..5baaca2e08 100644 --- a/django/utils/safestring.py +++ b/django/utils/safestring.py @@ -54,6 +54,9 @@ class SafeText(str, SafeData): return SafeText(t) return t + def __str__(self): + return self + SafeString = SafeText diff --git a/tests/utils_tests/test_safestring.py b/tests/utils_tests/test_safestring.py index be20cea11e..bb4dfd1280 100644 --- a/tests/utils_tests/test_safestring.py +++ b/tests/utils_tests/test_safestring.py @@ -24,6 +24,13 @@ class SafeStringTest(SimpleTestCase): self.assertRenderEqual('{{ s }}', 'a&b', s=s) self.assertRenderEqual('{{ s|force_escape }}', 'a&b', s=s) + def test_mark_safe_str(self): + """ + Calling str() on a SafeText instance doesn't lose the safe status. + """ + s = mark_safe('a&b') + self.assertIsInstance(str(s), type(s)) + def test_mark_safe_object_implementing_dunder_html(self): e = customescape('<a&b>') s = mark_safe(e) |
