1
0
mirror of https://github.com/django/django.git synced 2025-05-05 06:27:31 +00:00

Refs #30686 -- Fixed text truncation for negative or zero lengths.

This commit is contained in:
David Smith 2024-02-06 20:52:52 +01:00 committed by Mariusz Felisiak
parent 3e820d10f8
commit 70f39e46f8
3 changed files with 10 additions and 2 deletions

View File

@ -104,6 +104,8 @@ class Truncator(SimpleLazyObject):
""" """
self._setup() self._setup()
length = int(num) length = int(num)
if length <= 0:
return ""
text = unicodedata.normalize("NFC", self._wrapped) text = unicodedata.normalize("NFC", self._wrapped)
# Calculate the length to truncate to (max length - end_text length) # Calculate the length to truncate to (max length - end_text length)
@ -144,6 +146,8 @@ class Truncator(SimpleLazyObject):
""" """
self._setup() self._setup()
length = int(num) length = int(num)
if length <= 0:
return ""
if html: if html:
return self._truncate_html(length, truncate, self._wrapped, length, True) return self._truncate_html(length, truncate, self._wrapped, length, True)
return self._text_words(length, truncate) return self._text_words(length, truncate)

View File

@ -8,7 +8,7 @@ class FunctionTests(SimpleTestCase):
truncatechars_html( truncatechars_html(
'<p>one <a href="#">two - three <br>four</a> five</p>', 0 '<p>one <a href="#">two - three <br>four</a> five</p>', 0
), ),
"", "",
) )
def test_truncate(self): def test_truncate(self):

View File

@ -89,7 +89,7 @@ class TestUtilsText(SimpleTestCase):
# Make a best effort to shorten to the desired length, but requesting # Make a best effort to shorten to the desired length, but requesting
# a length shorter than the ellipsis shouldn't break # a length shorter than the ellipsis shouldn't break
self.assertEqual("", text.Truncator("asdf").chars(0)) self.assertEqual("...", text.Truncator("asdf").chars(1, truncate="..."))
# lazy strings are handled correctly # lazy strings are handled correctly
self.assertEqual( self.assertEqual(
text.Truncator(lazystr("The quick brown fox")).chars(10), "The quick…" text.Truncator(lazystr("The quick brown fox")).chars(10), "The quick…"
@ -123,6 +123,8 @@ class TestUtilsText(SimpleTestCase):
"", "",
truncator.chars(1, html=True), truncator.chars(1, html=True),
) )
self.assertEqual("", truncator.chars(0, html=True))
self.assertEqual("", truncator.chars(-1, html=True))
self.assertEqual( self.assertEqual(
'<p id="par"><strong><em>The qu....</em></strong></p>', '<p id="par"><strong><em>The qu....</em></strong></p>',
truncator.chars(10, "....", html=True), truncator.chars(10, "....", html=True),
@ -206,6 +208,8 @@ class TestUtilsText(SimpleTestCase):
lazystr("The quick brown fox jumped over the lazy dog.") lazystr("The quick brown fox jumped over the lazy dog.")
) )
self.assertEqual("The quick brown fox…", truncator.words(4)) self.assertEqual("The quick brown fox…", truncator.words(4))
self.assertEqual("", truncator.words(0))
self.assertEqual("", truncator.words(-1))
def test_truncate_html_words(self): def test_truncate_html_words(self):
truncator = text.Truncator( truncator = text.Truncator(