Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #19693 -- Made truncatewords_html handle self-closing tags

Thanks sneawo for the report and Jonathan Loy for the patch.
  • Loading branch information...
commit ac4faa6dc33407c93566884e53fa5d8ef44c0a2a 1 parent e94f405
@claudep claudep authored
Showing with 20 additions and 9 deletions.
  1. +1 −1  django/utils/text.py
  2. +19 −8 tests/regressiontests/utils/text.py
View
2  django/utils/text.py
@@ -24,7 +24,7 @@
# Set up regular expressions
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):
View
27 tests/regressiontests/utils/text.py
@@ -55,22 +55,33 @@ def test_truncate_words(self):
truncator.words(4, '[snip]'))
def test_truncate_html_words(self):
- truncator = text.Truncator('<p><strong><em>The quick brown fox jumped '
- 'over the lazy dog.</em></strong></p>')
- self.assertEqual('<p><strong><em>The quick brown fox jumped over the '
- 'lazy dog.</em></strong></p>', truncator.words(10, html=True))
- self.assertEqual('<p><strong><em>The quick brown fox...</em>'
+ truncator = text.Truncator('<p id="par"><strong><em>The quick brown fox'
+ ' jumped over the lazy dog.</em></strong></p>')
+ self.assertEqual('<p id="par"><strong><em>The quick brown fox jumped over'
+ ' the lazy dog.</em></strong></p>', truncator.words(10, html=True))
+ self.assertEqual('<p id="par"><strong><em>The quick brown fox...</em>'
'</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))
- self.assertEqual('<p><strong><em>The quick brown fox</em></strong>'
- '</p>', truncator.words(4, '', html=True))
+ self.assertEqual('<p id="par"><strong><em>The quick brown fox</em>'
+ '</strong></p>', truncator.words(4, '', html=True))
+
# Test with new line inside tag
truncator = text.Truncator('<p>The quick <a href="xyz.html"\n'
'id="mylink">brown fox</a> jumped over the lazy dog.</p>')
self.assertEqual('<p>The quick <a href="xyz.html"\n'
'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):
digits = '1234 67 9'
self.assertEqual(text.wrap(digits, 100), '1234 67 9')
Please sign in to comment.
Something went wrong with that request. Please try again.