mirror of https://github.com/django/django.git
Fixed #19693 -- Made truncatewords_html handle self-closing tags
Thanks sneawo for the report and Jonathan Loy for the patch.
This commit is contained in:
parent
e94f405d94
commit
ac4faa6dc3
|
@ -24,7 +24,7 @@ capfirst = allow_lazy(capfirst, six.text_type)
|
||||||
|
|
||||||
# Set up regular expressions
|
# Set up regular expressions
|
||||||
re_words = re.compile(r'&.*?;|<.*?>|(\w[\w-]*)', re.U|re.S)
|
re_words = re.compile(r'&.*?;|<.*?>|(\w[\w-]*)', re.U|re.S)
|
||||||
re_tag = re.compile(r'<(/)?([^ ]+?)(?: (/)| .*?)?>', re.S)
|
re_tag = re.compile(r'<(/)?([^ ]+?)(?:(\s*/)| .*?)?>', re.S)
|
||||||
|
|
||||||
|
|
||||||
def wrap(text, width):
|
def wrap(text, width):
|
||||||
|
|
|
@ -55,22 +55,33 @@ class TestUtilsText(SimpleTestCase):
|
||||||
truncator.words(4, '[snip]'))
|
truncator.words(4, '[snip]'))
|
||||||
|
|
||||||
def test_truncate_html_words(self):
|
def test_truncate_html_words(self):
|
||||||
truncator = text.Truncator('<p><strong><em>The quick brown fox jumped '
|
truncator = text.Truncator('<p id="par"><strong><em>The quick brown fox'
|
||||||
'over the lazy dog.</em></strong></p>')
|
' jumped over the lazy dog.</em></strong></p>')
|
||||||
self.assertEqual('<p><strong><em>The quick brown fox jumped over the '
|
self.assertEqual('<p id="par"><strong><em>The quick brown fox jumped over'
|
||||||
'lazy dog.</em></strong></p>', truncator.words(10, html=True))
|
' the lazy dog.</em></strong></p>', truncator.words(10, html=True))
|
||||||
self.assertEqual('<p><strong><em>The quick brown fox...</em>'
|
self.assertEqual('<p id="par"><strong><em>The quick brown fox...</em>'
|
||||||
'</strong></p>', truncator.words(4, html=True))
|
'</strong></p>', truncator.words(4, html=True))
|
||||||
self.assertEqual('<p><strong><em>The quick brown fox....</em>'
|
self.assertEqual('<p id="par"><strong><em>The quick brown fox....</em>'
|
||||||
'</strong></p>', truncator.words(4, '....', html=True))
|
'</strong></p>', truncator.words(4, '....', html=True))
|
||||||
self.assertEqual('<p><strong><em>The quick brown fox</em></strong>'
|
self.assertEqual('<p id="par"><strong><em>The quick brown fox</em>'
|
||||||
'</p>', truncator.words(4, '', html=True))
|
'</strong></p>', truncator.words(4, '', html=True))
|
||||||
|
|
||||||
# Test with new line inside tag
|
# Test with new line inside tag
|
||||||
truncator = text.Truncator('<p>The quick <a href="xyz.html"\n'
|
truncator = text.Truncator('<p>The quick <a href="xyz.html"\n'
|
||||||
'id="mylink">brown fox</a> jumped over the lazy dog.</p>')
|
'id="mylink">brown fox</a> jumped over the lazy dog.</p>')
|
||||||
self.assertEqual('<p>The quick <a href="xyz.html"\n'
|
self.assertEqual('<p>The quick <a href="xyz.html"\n'
|
||||||
'id="mylink">brown...</a></p>', truncator.words(3, '...', html=True))
|
'id="mylink">brown...</a></p>', truncator.words(3, '...', html=True))
|
||||||
|
|
||||||
|
# Test self-closing tags
|
||||||
|
truncator = text.Truncator('<br/>The <hr />quick brown fox jumped over'
|
||||||
|
' the lazy dog.')
|
||||||
|
self.assertEqual('<br/>The <hr />quick brown...',
|
||||||
|
truncator.words(3, '...', html=True ))
|
||||||
|
truncator = text.Truncator('<br>The <hr/>quick <em>brown fox</em> '
|
||||||
|
'jumped over the lazy dog.')
|
||||||
|
self.assertEqual('<br>The <hr/>quick <em>brown...</em>',
|
||||||
|
truncator.words(3, '...', html=True ))
|
||||||
|
|
||||||
def test_wrap(self):
|
def test_wrap(self):
|
||||||
digits = '1234 67 9'
|
digits = '1234 67 9'
|
||||||
self.assertEqual(text.wrap(digits, 100), '1234 67 9')
|
self.assertEqual(text.wrap(digits, 100), '1234 67 9')
|
||||||
|
|
Loading…
Reference in New Issue