diff --git a/django/utils/html.py b/django/utils/html.py index 094bc66..c93e922 100644 --- a/django/utils/html.py +++ b/django/utils/html.py @@ -63,8 +63,8 @@ def conditional_escape(html): """ Similar to escape(), except that it doesn't operate on pre-escaped strings. """ - if isinstance(html, SafeData): - return html + if hasattr(html, '__html__'): + return html.__html__() else: return escape(html) diff --git a/django/utils/safestring.py b/django/utils/safestring.py index 2e31c23..6f7a22a 100644 --- a/django/utils/safestring.py +++ b/django/utils/safestring.py @@ -22,7 +22,14 @@ class EscapeUnicode(unicode, EscapeData): pass class SafeData(object): - pass + + def __html__(self): + """ + Returns the html representation of a string. + + Allows interoperability with other template engines. + """ + return self class SafeString(str, SafeData): """ diff --git a/tests/regressiontests/utils/html.py b/tests/regressiontests/utils/html.py index 3acb218..3057a07 100644 --- a/tests/regressiontests/utils/html.py +++ b/tests/regressiontests/utils/html.py @@ -1,6 +1,6 @@ import unittest -from django.utils import html +from django.utils import html, safestring class TestUtilsHtml(unittest.TestCase): @@ -121,3 +121,15 @@ class TestUtilsHtml(unittest.TestCase): ) for value, output in items: self.check_output(f, value, output) + + def test_conditional_escape(self): + s = '