Ticket #7261: 7261-add-__html__.diff

File 7261-add-__html__.diff, 1.9 KB (added by ivank, 13 years ago)

html.diff updated for django trunk

  • django/utils/html.py

    diff --git a/django/utils/html.py b/django/utils/html.py
    index 094bc66..c93e922 100644
    a b def conditional_escape(html):  
    6363    """
    6464    Similar to escape(), except that it doesn't operate on pre-escaped strings.
    6565    """
    66     if isinstance(html, SafeData):
    67         return html
     66    if hasattr(html, '__html__'):
     67        return html.__html__()
    6868    else:
    6969        return escape(html)
    7070
  • django/utils/safestring.py

    diff --git a/django/utils/safestring.py b/django/utils/safestring.py
    index 2e31c23..6f7a22a 100644
    a b class EscapeUnicode(unicode, EscapeData):  
    2222    pass
    2323
    2424class 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
    2633
    2734class SafeString(str, SafeData):
    2835    """
  • tests/regressiontests/utils/html.py

    diff --git a/tests/regressiontests/utils/html.py b/tests/regressiontests/utils/html.py
    index 3acb218..3057a07 100644
    a b  
    11import unittest
    22
    3 from django.utils import html
     3from django.utils import html, safestring
    44
    55class TestUtilsHtml(unittest.TestCase):
    66
    class TestUtilsHtml(unittest.TestCase):  
    121121        )
    122122        for value, output in items:
    123123            self.check_output(f, value, output)
     124
     125    def test_conditional_escape(self):
     126        s = '<h1>interop</h1>'
     127        self.assertEqual(html.conditional_escape(s),
     128                         '&lt;h1&gt;interop&lt;/h1&gt;')
     129        self.assertEqual(html.conditional_escape(safestring.mark_safe(s)), s)
     130
     131class TestSafeData(unittest.TestCase):
     132
     133    def test_html(self):
     134        s = '<h1>interop</h1>'
     135        self.assertEqual(s, safestring.mark_safe(s).__html__())
Back to Top