From b8c0d21b3f1b3714c1e188836fc84ca8211a7e56 Mon Sep 17 00:00:00 2001 From: santhreal <64453045+santhreal@users.noreply.github.com> Date: Fri, 17 Jul 2026 22:30:43 -0700 Subject: [PATCH] Fix smart_truncate ValueError on empty separator word_boundary truncation splits on separator; an empty separator is invalid for str.split and should hard-truncate instead. --- slugify/slugify.py | 3 ++- test.py | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/slugify/slugify.py b/slugify/slugify.py index 9b5f27f..54843b6 100644 --- a/slugify/slugify.py +++ b/slugify/slugify.py @@ -49,7 +49,8 @@ def smart_truncate( if len(string) < max_length: return string - if not word_boundary: + # Empty separator cannot split words; fall back to hard truncate. + if not word_boundary or not separator: return string[:max_length].strip(separator) if separator not in string: diff --git a/test.py b/test.py index fcec4b6..743a6df 100644 --- a/test.py +++ b/test.py @@ -540,6 +540,17 @@ def test_smart_truncate_no_seperator(self): r = smart_truncate(txt, max_length=100, separator='_') self.assertEqual(r, txt) + def test_smart_truncate_empty_separator_word_boundary(self): + txt = 'helloworld' + self.assertEqual( + smart_truncate(txt, max_length=5, word_boundary=True, separator=''), + 'hello', + ) + self.assertEqual( + slugify('hello world', max_length=5, word_boundary=True, separator=''), + 'hello', + ) + PY3 = sys.version_info.major == 3