summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaude Paroz <claude@2xlibre.net>2013-02-13 18:24:49 +0100
committerClaude Paroz <claude@2xlibre.net>2013-02-13 18:24:49 +0100
commitac4faa6dc33407c93566884e53fa5d8ef44c0a2a (patch)
tree2e2dcd120ca8cff7782fd2a29348cff2f63500c0
parente94f405d9499d310ef58b7409a98759a5f5512b0 (diff)
Fixed #19693 -- Made truncatewords_html handle self-closing tags
Thanks sneawo for the report and Jonathan Loy for the patch.
-rw-r--r--django/utils/text.py2
-rw-r--r--tests/regressiontests/utils/text.py27
2 files changed, 20 insertions, 9 deletions
diff --git a/django/utils/text.py b/django/utils/text.py
index 6664b18249..02c3f10678 100644
--- a/django/utils/text.py
+++ b/django/utils/text.py
@@ -24,7 +24,7 @@ capfirst = allow_lazy(capfirst, six.text_type)
# 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):
diff --git a/tests/regressiontests/utils/text.py b/tests/regressiontests/utils/text.py
index 3465282c7f..c9dde6b1b3 100644
--- a/tests/regressiontests/utils/text.py
+++ b/tests/regressiontests/utils/text.py
@@ -55,22 +55,33 @@ class TestUtilsText(SimpleTestCase):
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')