diff --git a/django/utils/html.py b/django/utils/html.py
index 951b3f2..8d8d673 100644
|
a
|
b
|
def conditional_escape(html):
|
| 38 | 38 | """ |
| 39 | 39 | Similar to escape(), except that it doesn't operate on pre-escaped strings. |
| 40 | 40 | """ |
| 41 | | if isinstance(html, SafeData): |
| 42 | | return html |
| | 41 | if hasattr(html, '__html__'): |
| | 42 | return html.__html__() |
| 43 | 43 | else: |
| 44 | 44 | return escape(html) |
| 45 | 45 | |
diff --git a/django/utils/safestring.py b/django/utils/safestring.py
index 2e31c23..6f7a22a 100644
|
a
|
b
|
class EscapeUnicode(unicode, EscapeData):
|
| 22 | 22 | pass |
| 23 | 23 | |
| 24 | 24 | class SafeData(object): |
| 25 | | pass |
| | 25 | |
| | 26 | def __html__(self): |
| | 27 | """ |
| | 28 | Returns the html representation of a string. |
| | 29 | |
| | 30 | Allows interoperability with other template engines. |
| | 31 | """ |
| | 32 | return self |
| 26 | 33 | |
| 27 | 34 | class SafeString(str, SafeData): |
| 28 | 35 | """ |
diff --git a/tests/regressiontests/utils/tests.py b/tests/regressiontests/utils/tests.py
index 4ad4e63..01b712d 100644
|
a
|
b
|
Tests for django.utils.
|
| 4 | 4 | |
| 5 | 5 | from unittest import TestCase |
| 6 | 6 | |
| 7 | | from django.utils import html, checksums, text |
| | 7 | from django.utils import html, checksums, text, safestring |
| 8 | 8 | from django.utils.functional import SimpleLazyObject |
| 9 | 9 | |
| 10 | 10 | import timesince |
| … |
… |
class TestUtilsHtml(TestCase):
|
| 144 | 144 | for value, output in items: |
| 145 | 145 | self.check_output(f, value, output) |
| 146 | 146 | |
| | 147 | def test_conditional_escape(self): |
| | 148 | s = '<h1>interop</h1>' |
| | 149 | self.assertEqual(html.conditional_escape(s), |
| | 150 | '<h1>interop</h1>') |
| | 151 | self.assertEqual(html.conditional_escape(safestring.mark_safe(s)), s) |
| | 152 | |
| 147 | 153 | class TestUtilsChecksums(TestCase): |
| 148 | 154 | |
| 149 | 155 | def check_output(self, function, value, output=None): |
| … |
… |
class TestUtilsText(TestCase):
|
| 262 | 268 | self.assertEqual(u'<p><strong><em>The quick brown fox</em></strong></p>', |
| 263 | 269 | text.truncate_html_words('<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>', 4, None)) |
| 264 | 270 | |
| | 271 | class TestSafeData(TestCase): |
| | 272 | |
| | 273 | def test_html(self): |
| | 274 | s = '<h1>interop</h1>' |
| | 275 | self.assertEqual(s, safestring.mark_safe(s).__html__()) |
| | 276 | |
| 265 | 277 | if __name__ == "__main__": |
| 266 | 278 | import doctest |
| 267 | 279 | doctest.testmod() |