Fixed #27803 -- Kept safe status of lazy safe strings in conditional_escape

This commit is contained in:
Claude Paroz 2017-02-01 16:39:32 +01:00
parent f8d52521ab
commit a21ec12409
2 changed files with 4 additions and 1 deletions

View File

@ -6,7 +6,7 @@ from urllib.parse import (
) )
from django.utils.encoding import force_text from django.utils.encoding import force_text
from django.utils.functional import keep_lazy, keep_lazy_text from django.utils.functional import Promise, keep_lazy, keep_lazy_text
from django.utils.http import RFC3986_GENDELIMS, RFC3986_SUBDELIMS from django.utils.http import RFC3986_GENDELIMS, RFC3986_SUBDELIMS
from django.utils.safestring import SafeData, SafeText, mark_safe from django.utils.safestring import SafeData, SafeText, mark_safe
from django.utils.text import normalize_newlines from django.utils.text import normalize_newlines
@ -79,6 +79,8 @@ def conditional_escape(text):
This function relies on the __html__ convention used both by Django's This function relies on the __html__ convention used both by Django's
SafeData class and by third-party libraries like markupsafe. SafeData class and by third-party libraries like markupsafe.
""" """
if isinstance(text, Promise):
text = str(text)
if hasattr(text, '__html__'): if hasattr(text, '__html__'):
return text.__html__() return text.__html__()
else: else:

View File

@ -170,6 +170,7 @@ class TestUtilsHtml(SimpleTestCase):
s = '<h1>interop</h1>' s = '<h1>interop</h1>'
self.assertEqual(conditional_escape(s), '&lt;h1&gt;interop&lt;/h1&gt;') self.assertEqual(conditional_escape(s), '&lt;h1&gt;interop&lt;/h1&gt;')
self.assertEqual(conditional_escape(mark_safe(s)), s) self.assertEqual(conditional_escape(mark_safe(s)), s)
self.assertEqual(conditional_escape(lazystr(mark_safe(s))), s)
def test_html_safe(self): def test_html_safe(self):
@html_safe @html_safe