misc: fix ellipsize for mini strings (#16280)

This commit is contained in:
Lauréline Guérin 2020-06-19 15:24:52 +02:00
parent 411ed67118
commit bf52ee1ac7
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
3 changed files with 25 additions and 5 deletions

View File

@ -90,7 +90,7 @@ def test_string():
def test_text():
assert fields.TextField().get_view_short_value('foo'*10) == ('foo'*10)[:25] + ' (...)'
assert fields.TextField().get_view_short_value('foo'*15) == ('foo'*10)[:27] + '(…)'
assert fields.TextField().get_view_value('foo') == '<p>foo</p>'
assert fields.TextField().get_view_value('foo\n\nfoo') == '<p>foo\n</p><p>\nfoo</p>'
assert fields.TextField(pre=True).get_view_value('foo') == '<pre>foo</pre>'

View File

@ -18,8 +18,9 @@ from quixote import cleanup
import wcs.api # workaround against circular dependencies :/
from wcs.qommon.form import FileSizeWidget, PicklableUpload
from wcs.qommon.humantime import humanduration2seconds, seconds2humanduration
from wcs.qommon.misc import (simplify, json_loads, parse_isotime, format_time,
date_format, get_as_datetime, normalize_geolocation)
from wcs.qommon.misc import (
simplify, json_loads, parse_isotime, format_time,
date_format, get_as_datetime, normalize_geolocation, ellipsize)
from wcs.admin.settings import FileTypesDirectory
from wcs.scripts import Script
from wcs.qommon import force_str, evalutils
@ -555,3 +556,19 @@ def test_objects_repr():
field.id = '1'
field.label = 'test'
assert repr(field) == "<StringField 1 'test'>"
@pytest.mark.parametrize('value, length, expected', [
('', 30, ''),
(None, 30, 'None'),
('foo bar', 30, 'foo bar'),
('01234567890123456789012345678', 30, '01234567890123456789012345678'),
('012345678901234567890123456789', 30, '012345678901234567890123456789'),
('0123456789012345678901234567890', 30, '012345678901234567890123456(…)'),
('foo bar', 4, 'f(…)'),
('foo bar', 3, 'foo'),
('foo bar', 2, 'fo'),
])
def test_ellipsize(value, length, expected):
create_temporary_pub()
assert ellipsize(value, length=length) == expected

View File

@ -261,8 +261,11 @@ def site_encode(s):
def ellipsize(s, length=30):
s = force_text(s, get_publisher().site_charset, errors='replace')
if s and len(s) >= length:
s = s[:length-5] + ' (...)'
if s and len(s) > length:
if length > 3:
s = s[:length-3] + '(…)'
else:
s = s[:length]
return force_str(s)