Ticket #4573: django-linebreaks-html-v4.diff

File django-linebreaks-html-v4.diff, 5.6 KB (added by Johan Bergström <bugs@…>, 17 years ago)

Fourth revision of the patch

  • django/utils/html.py

     
    3030    return force_unicode(html).replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;').replace('"', '&quot;').replace("'", '&#39;')
    3131escape = allow_lazy(escape, unicode)
    3232
    33 def linebreaks(value):
    34     "Convert newlines into <p> and <br />s."
     33def linebreaks(value, paragraph=True, xhtml=True):
     34    """
     35    Converts newlines into paragraphs (<p>) containing line breaks (<br/>, <br>)
     36    Note: Using <br/> instead of <br /> may be less cosmetic but avoids
     37    potential stripping bugs from other utils such as truncatewords.
     38    """
     39    br = '<br/>'
     40    if not xhtml:
     41        br = '<br>'
    3542    value = re.sub(r'\r\n|\r|\n', '\n', force_unicode(value)) # normalize newlines
    36     paras = re.split('\n{2,}', value)
    37     paras = [u'<p>%s</p>' % p.strip().replace('\n', '<br />') for p in paras]
    38     return u'\n\n'.join(paras)
     43    if paragraph:
     44        paras = re.split('\n{2,}', value)
     45        paras = [u'<p>%s</p>' % p.strip().replace('\n', br) for p in paras]
     46        return u'\n\n'.join(paras)
     47    else:
     48        return  value.strip().replace('\n', br)
    3949linebreaks = allow_lazy(linebreaks, unicode)
    4050
    4151def strip_tags(value):
  • django/template/defaultfilters.py

     
    253253    return escape(value)
    254254escape = stringfilter(escape)
    255255
    256 def linebreaks(value):
    257     "Converts newlines into <p> and <br />s"
     256def linebreaks(value , arg=""):
     257    """
     258    Converts newlines into paragraphs and linebreaks. Accepts 'html' as
     259    optional parameter; this will return html compliant newlines (<br>)
     260    instead of the default xhtml (<br />).
     261    """
    258262    from django.utils.html import linebreaks
    259     return linebreaks(value)
     263    xhtml = (arg != 'html')
     264    return linebreaks(value, paragraph=True, xhtml=xhtml)
    260265linebreaks = stringfilter(linebreaks)
    261266
    262 def linebreaksbr(value):
    263     "Converts newlines into <br />s"
    264     return value.replace('\n', '<br />')
     267def linebreaksbr(value, arg=""):
     268    """
     269    Converts newlines into linebreaks. Accepts 'html' as optional parameter;
     270    this will return html compliant newlines (<br>) instead of the default
     271    xhtml (<br />).
     272    """
     273    from django.utils.html import linebreaks
     274    xhtml = (arg != 'html')
     275    return linebreaks(value, paragraph=False, xhtml=xhtml)
    265276linebreaksbr = stringfilter(linebreaksbr)
    266277
    267278def removetags(value, tags):
  • tests/regressiontests/utils/tests.py

     
    3939    def test_linebreaks(self):
    4040        f = html.linebreaks
    4141        items = (
    42             ("para1\n\npara2\r\rpara3", "<p>para1</p>\n\n<p>para2</p>\n\n<p>para3</p>"),
    43             ("para1\nsub1\rsub2\n\npara2", "<p>para1<br />sub1<br />sub2</p>\n\n<p>para2</p>"),
    44             ("para1\r\n\r\npara2\rsub1\r\rpara4", "<p>para1</p>\n\n<p>para2<br />sub1</p>\n\n<p>para4</p>"),
    45             ("para1\tmore\n\npara2", "<p>para1\tmore</p>\n\n<p>para2</p>"),
     42            (("para1\n\npara2\r\rpara3", True, True ), "<p>para1</p>\n\n<p>para2</p>\n\n<p>para3</p>"),
     43            (("para1\n\npara2\r\rpara3", False, False ), "para1<br><br>para2<br><br>para3"),
     44            (("para1\nsub1\rsub2\n\npara2", True, True ), "<p>para1<br/>sub1<br/>sub2</p>\n\n<p>para2</p>"),
     45            (("para1\r\n\r\npara2\rsub1\r\rpara4", False, True ), "para1<br/><br/>para2<br/>sub1<br/><br/>para4"),
     46            (("para1\tmore\n\npara2", True, False), "<p>para1\tmore</p>\n\n<p>para2</p>"),
    4647        )
    4748        for value, output in items:
    48             self.check_output(f, value, output)
     49            self.assertEqual(f(value[0],paragraph=value[1],xhtml=value[2]),output)
    4950
    5051    def test_strip_tags(self):
    5152        f = html.strip_tags
  • tests/regressiontests/defaultfilters/tests.py

     
    200200u'<p>line 1</p>'
    201201
    202202>>> linebreaks(u'line 1\nline 2')
    203 u'<p>line 1<br />line 2</p>'
     203u'<p>line 1<br/>line 2</p>'
    204204
     205>>> linebreaks(u'line 1\nline 2', "html")
     206u'<p>line 1<br>line 2</p>'
     207
    205208>>> removetags(u'some <b>html</b> with <script>alert("You smell")</script> disallowed <img /> tags', 'script img')
    206209u'some <b>html</b> with alert("You smell") disallowed  tags'
    207210
     
    467470u'<p>123</p>'
    468471>>> linebreaksbr(123)
    469472u'123'
     473>>> linebreaksbr('123\n123')
     474u'123<br/>123'
     475>>> linebreaksbr('123\n123',"bad syntax")
     476u'123<br/>123'
     477>>> linebreaksbr('123\n123',"html")
     478u'123<br>123'
    470479>>> removetags(123, 'a')
    471480u'123'
    472481>>> striptags(123)
  • docs/templates.txt

     
    11181118linebreaks
    11191119~~~~~~~~~~
    11201120
    1121 Converts newlines into ``<p>`` and ``<br />`` tags.
     1121Converts newlines into paragraphs ``<p>`` containing line breaks ``<br/>``,
     1122``<br>``.
    11221123
     1124**Argument:** ``'html'`` will result in html compliant output.
     1125
    11231126linebreaksbr
    11241127~~~~~~~~~~~~
    11251128
    1126 Converts newlines into ``<br />`` tags.
     1129Converts newlines into ``<br/>``, ``<br>`` tags.
    11271130
     1131**Argument:** ``'html'`` will result in html compliant output.
     1132
    11281133linenumbers
    11291134~~~~~~~~~~~
    11301135
Back to Top