mirror of
https://github.com/django/django.git
synced 2025-10-23 21:59:11 +00:00
Fixed #30686 -- Used Python HTMLParser in utils.text.Truncator.
This commit is contained in:
committed by
Mariusz Felisiak
parent
70f39e46f8
commit
6ee37ada32
@@ -111,7 +111,7 @@ class TestUtilsText(SimpleTestCase):
|
||||
truncator.chars(46, html=True),
|
||||
)
|
||||
self.assertEqual(
|
||||
'<p id="par"><strong><em>The quick brown fox jumped over the lazy dog.</em>'
|
||||
'<p id="par"><strong><em>The quick brown fox jumped over the lazy dog…</em>'
|
||||
"</strong></p>",
|
||||
truncator.chars(45, html=True),
|
||||
)
|
||||
@@ -120,7 +120,7 @@ class TestUtilsText(SimpleTestCase):
|
||||
truncator.chars(10, html=True),
|
||||
)
|
||||
self.assertEqual(
|
||||
"…",
|
||||
'<p id="par"><strong><em>…</em></strong></p>',
|
||||
truncator.chars(1, html=True),
|
||||
)
|
||||
self.assertEqual("", truncator.chars(0, html=True))
|
||||
@@ -142,18 +142,16 @@ class TestUtilsText(SimpleTestCase):
|
||||
bigger_len = text.Truncator.MAX_LENGTH_HTML + 1
|
||||
valid_html = "<p>Joel is a slug</p>" # 14 chars
|
||||
perf_test_values = [
|
||||
("</a" + "\t" * (max_len - 6) + "//>", None),
|
||||
("</p" + "\t" * bigger_len + "//>", "</p" + "\t" * 6 + "…"),
|
||||
("&" * bigger_len, "&" * 9 + "…"),
|
||||
("_X<<<<<<<<<<<>", None),
|
||||
("</a" + "\t" * (max_len - 6) + "//>", "</a>"),
|
||||
("</p" + "\t" * bigger_len + "//>", "</p>"),
|
||||
("&" * bigger_len, ""),
|
||||
("_X<<<<<<<<<<<>", "_X<<<<<<<…"),
|
||||
(valid_html * bigger_len, "<p>Joel is a…</p>"), # 10 chars
|
||||
]
|
||||
for value, expected in perf_test_values:
|
||||
with self.subTest(value=value):
|
||||
truncator = text.Truncator(value)
|
||||
self.assertEqual(
|
||||
expected if expected else value, truncator.chars(10, html=True)
|
||||
)
|
||||
self.assertEqual(expected, truncator.chars(10, html=True))
|
||||
|
||||
def test_truncate_chars_html_with_newline_inside_tag(self):
|
||||
truncator = text.Truncator(
|
||||
@@ -181,7 +179,7 @@ class TestUtilsText(SimpleTestCase):
|
||||
"<br>The <hr/>quick <em>brown…</em>", truncator.chars(16, html=True)
|
||||
)
|
||||
self.assertEqual("<br>The <hr/>q…", truncator.chars(6, html=True))
|
||||
self.assertEqual("<br>The …", truncator.chars(5, html=True))
|
||||
self.assertEqual("<br>The <hr/>…", truncator.chars(5, html=True))
|
||||
self.assertEqual("<br>The…", truncator.chars(4, html=True))
|
||||
self.assertEqual("<br>Th…", truncator.chars(3, html=True))
|
||||
|
||||
@@ -190,11 +188,19 @@ class TestUtilsText(SimpleTestCase):
|
||||
"<i>Buenos días! ¿Cómo está?</i>"
|
||||
)
|
||||
self.assertEqual(
|
||||
"<i>Buenos días! ¿Cómo…</i>",
|
||||
"<i>Buenos días! ¿Cómo está?</i>",
|
||||
truncator.chars(40, html=True),
|
||||
)
|
||||
self.assertEqual(
|
||||
"<i>Buenos días…</i>",
|
||||
truncator.chars(12, html=True),
|
||||
)
|
||||
self.assertEqual(
|
||||
"<i>Buenos días! ¿Cómo está…</i>",
|
||||
truncator.chars(24, html=True),
|
||||
)
|
||||
truncator = text.Truncator("<p>I <3 python, what about you?</p>")
|
||||
self.assertEqual("<p>I <3 python,…</p>", truncator.chars(16, html=True))
|
||||
self.assertEqual("<p>I <3 python, wh…</p>", truncator.chars(16, html=True))
|
||||
|
||||
def test_truncate_words(self):
|
||||
truncator = text.Truncator("The quick brown fox jumped over the lazy dog.")
|
||||
@@ -242,7 +248,7 @@ class TestUtilsText(SimpleTestCase):
|
||||
"<p>The quick \t brown fox jumped over the lazy dog.</p>"
|
||||
)
|
||||
self.assertEqual(
|
||||
"<p>The quick \t brown fox…</p>",
|
||||
"<p>The quick brown fox…</p>",
|
||||
truncator.words(4, html=True),
|
||||
)
|
||||
|
||||
@@ -277,7 +283,7 @@ class TestUtilsText(SimpleTestCase):
|
||||
"<i>Buenos días! ¿Cómo está?</i>"
|
||||
)
|
||||
self.assertEqual(
|
||||
"<i>Buenos días! ¿Cómo…</i>",
|
||||
"<i>Buenos días! ¿Cómo…</i>",
|
||||
truncator.words(3, html=True),
|
||||
)
|
||||
truncator = text.Truncator("<p>I <3 python, what about you?</p>")
|
||||
@@ -292,19 +298,17 @@ class TestUtilsText(SimpleTestCase):
|
||||
bigger_len = text.Truncator.MAX_LENGTH_HTML + 1
|
||||
valid_html = "<p>Joel is a slug</p>" # 4 words
|
||||
perf_test_values = [
|
||||
("</a" + "\t" * (max_len - 6) + "//>", None),
|
||||
("</p" + "\t" * bigger_len + "//>", "</p" + "\t" * (max_len - 3) + "…"),
|
||||
("&" * max_len, None), # no change
|
||||
("&" * bigger_len, "&" * max_len + "…"),
|
||||
("_X<<<<<<<<<<<>", None),
|
||||
("</a" + "\t" * (max_len - 6) + "//>", "</a>"),
|
||||
("</p" + "\t" * bigger_len + "//>", "</p>"),
|
||||
("&" * max_len, ""),
|
||||
("&" * bigger_len, ""),
|
||||
("_X<<<<<<<<<<<>", "_X<<<<<<<<<<<>"),
|
||||
(valid_html * bigger_len, valid_html * 12 + "<p>Joel is…</p>"), # 50 words
|
||||
]
|
||||
for value, expected in perf_test_values:
|
||||
with self.subTest(value=value):
|
||||
truncator = text.Truncator(value)
|
||||
self.assertEqual(
|
||||
expected if expected else value, truncator.words(50, html=True)
|
||||
)
|
||||
self.assertEqual(expected, truncator.words(50, html=True))
|
||||
|
||||
def test_wrap(self):
|
||||
digits = "1234 67 9"
|
||||
|
||||
Reference in New Issue
Block a user