Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions slugify/slugify.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ def _modern_truncate(text: str, max_length: int, word_boundary: bool, separator:
"""
if max_length <= 0:
return text.replace(DEFAULT_SEPARATOR, separator)
output_length = len(text) + text.count(DEFAULT_SEPARATOR) * (len(separator) - 1)
if output_length <= max_length:
return text.replace(DEFAULT_SEPARATOR, separator)
tokens = text.split(DEFAULT_SEPARATOR)
if word_boundary:
words: list[str] = []
Expand Down
13 changes: 13 additions & 0 deletions test_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ def test_modern_separator_does_not_strip_word_content(self):
self.assertEqual(slugify('xy xy', separator='xy', max_length=5), 'xyxyx')
self.assertEqual(slugify('a b c', separator='::', max_length=3), 'a')

def test_modern_fitting_post_replacements_preserve_delimiters(self):
for replacement in ('one--two', '-one-two-', 'one---two'):
for separator in ('-', '::', ''):
expected = replacement.replace('-', separator)
for boundary, order, limit in itertools.product(
(False, True), (False, True), (len(expected), len(expected) + 10)):
with self.subTest(replacement=replacement, separator=separator,
boundary=boundary, order=order, limit=limit):
self.assertEqual(slugify('x', replacements=[('x', replacement)],
replacement_stage='post', allow_unicode=True,
separator=separator, max_length=limit,
word_boundary=boundary, save_order=order), expected)

def test_cli_preserves_legacy_default_shape(self):
expected = dict(text='', entities=True, decimal=True, hexadecimal=True,
max_length=0, word_boundary=False, save_order=False, separator='-',
Expand Down