Ticket #7267: 7267.3.patch

File 7267.3.patch, 2.3 KB (added by Aymeric Augustin, 14 years ago)
  • tests/regressiontests/utils/html.py

     
    121121        )
    122122        for value, output in items:
    123123            self.check_output(f, value, output)
     124
     125    def test_clean_html(self):
     126        f = html.clean_html
     127        items = (
     128            (u'<p>I <i>believe</i> in <b>semantic markup</b>!</p>', u'<p>I <em>believe</em> in <strong>semantic markup</strong>!</p>'),
     129            (u'I escape & I don\'t <a href="#" target="_blank">target</a>', u'I escape &amp; I don\'t <a href="#" >target</a>'),
     130            (u'<p>I kill whitespace</p><br clear="all"><p>&nbsp;</p>', u'<p>I kill whitespace</p>'),
     131            # also a regression test for #7267: this used to raise an UnicodeDecodeError
     132            (u'<p>* foo</p><p>* bar</p>', u'<ul>\n<li> foo</li><li> bar</li>\n</ul>'),
     133        )
     134        for value, output in items:
     135            self.check_output(f, value, output)
  • django/utils/html.py

     
    1313TRAILING_PUNCTUATION = ['.', ',', ')', '>', '\n', '&gt;']
    1414
    1515# List of possible strings used for bullets in bulleted lists.
    16 DOTS = ['&middot;', '*', '\xe2\x80\xa2', '&#149;', '&bull;', '&#8226;']
     16DOTS = [u'&middot;', u'*', u'\u2022', u'&#149;', u'&bull;', u'&#8226;']
    1717
    1818unencoded_ampersands_re = re.compile(r'&(?!(\w+|#\d+);)')
    1919word_split_re = re.compile(r'(\s+)')
     
    180180    text = html_gunk_re.sub('', text)
    181181    # Convert hard-coded bullets into HTML unordered lists.
    182182    def replace_p_tags(match):
    183         s = match.group().replace('</p>', '</li>')
     183        s = match.group().replace(u'</p>', u'</li>')
    184184        for d in DOTS:
    185             s = s.replace('<p>%s' % d, '<li>')
     185            s = s.replace(u'<p>%s' % d, u'<li>')
    186186        return u'<ul>\n%s\n</ul>' % s
    187187    text = hard_coded_bullets_re.sub(replace_p_tags, text)
    188188    # Remove stuff like "<p>&nbsp;&nbsp;</p>", but only if it's at the bottom
    189189    # of the text.
    190     text = trailing_empty_content_re.sub('', text)
     190    text = trailing_empty_content_re.sub(u'', text)
    191191    return text
    192192clean_html = allow_lazy(clean_html, unicode)
Back to Top