Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #20568 -- truncatewords_html no longer splits words containing …

…HTML entities.

Thanks yann0 at hotmail.com for the report.
  • Loading branch information...
commit 40b95a24ae159b6600457a23d6c2779a18037b7b 1 parent 58d555c
Jaap Roes authored July 18, 2013 timgraham committed October 08, 2013
2  django/utils/text.py
@@ -22,7 +22,7 @@
22 22
 capfirst = allow_lazy(capfirst, six.text_type)
23 23
 
24 24
 # Set up regular expressions
25  
-re_words = re.compile(r'&.*?;|<.*?>|(\w[\w-]*)', re.U|re.S)
  25
+re_words = re.compile(r'<.*?>|((?:\w[-\w]*|&.*?;)+)', re.U|re.S)
26 26
 re_tag = re.compile(r'<(/)?([^ ]+?)(?:(\s*/)| .*?)?>', re.S)
27 27
 
28 28
 
3  tests/defaultfilters/tests.py
@@ -184,6 +184,9 @@ def test_truncatewords_html(self):
184 184
             '<p>one <a href="#">two - three <br>four</a> five</p>')
185 185
         self.assertEqual(truncatewords_html(
186 186
             '\xc5ngstr\xf6m was here', 1), '\xc5ngstr\xf6m ...')
  187
+        self.assertEqual(truncatewords_html('<i>Buenos d&iacute;as! '
  188
+            '&#x00bf;C&oacute;mo est&aacute;?</i>', 3),
  189
+            '<i>Buenos d&iacute;as! &#x00bf;C&oacute;mo ...</i>')
187 190
 
188 191
     def test_upper(self):
189 192
         self.assertEqual(upper('Mixed case input'), 'MIXED CASE INPUT')
9  tests/utils_tests/test_text.py
@@ -82,6 +82,15 @@ def test_truncate_html_words(self):
82 82
         self.assertEqual('<br>The <hr/>quick <em>brown...</em>',
83 83
             truncator.words(3, '...', html=True ))
84 84
 
  85
+        # Test html entities
  86
+        truncator = text.Truncator('<i>Buenos d&iacute;as!'
  87
+            ' &#x00bf;C&oacute;mo est&aacute;?</i>')
  88
+        self.assertEqual('<i>Buenos d&iacute;as! &#x00bf;C&oacute;mo...</i>',
  89
+            truncator.words(3, '...', html=True))
  90
+        truncator = text.Truncator('<p>I &lt;3 python, what about you?</p>')
  91
+        self.assertEqual('<p>I &lt;3 python...</p>',
  92
+            truncator.words(3, '...', html=True))
  93
+
85 94
     def test_wrap(self):
86 95
         digits = '1234 67 9'
87 96
         self.assertEqual(text.wrap(digits, 100), '1234 67 9')

0 notes on commit 40b95a2

Please sign in to comment.
Something went wrong with that request. Please try again.