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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Fix type annotation issues identified by mypy.
- Run CI against pull requests.
- Fix package build warnings.
- Fix custom replacements being applied twice when a replacement contains its own search value (e.g. `['a', 'aa']` no longer yields `aaaa`).

## 8.0.4

Expand Down
5 changes: 5 additions & 0 deletions slugify/slugify.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ def slugify(
# finalize user-specific replacements
if replacements:
for old, new in replacements:
# skip self-referential rules (``old`` present in ``new``); these were
# already applied in the pre-process pass and re-applying them here
# would grow the slug on every run (e.g. ['a', 'aa'] -> 'aaaa').
if old and old in new:
continue
text = text.replace(old, new)

# smart truncate if requested
Expand Down
9 changes: 9 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,15 @@ def test_replacements_german_umlaut_custom(self):
r = slugify(txt, replacements=[['Ü', 'UE'], ['ü', 'ue']])
self.assertEqual(r, "ueber-ueber-german-umlaut")

def test_replacements_not_applied_twice(self):
# A replacement whose value contains its own key must not be applied
# twice (pre + finalize pass), which used to grow the slug.
r = slugify('a', replacements=[['a', 'aa']])
self.assertEqual(r, "aa")

r = slugify('hello', replacements=[['l', 'll']])
self.assertEqual(r, "hellllo")

def test_pre_translation(self):
self.assertEqual(PRE_TRANSLATIONS, [('Ю', 'U'), ('Щ', 'Sch'), ('У', 'Y'), ('Х', 'H'), ('Я', 'Ya'), ('Ё', 'E'), ('ё', 'e'), ('я', 'ya'), ('х', 'h'), ('у', 'y'), ('щ', 'sch'), ('ю', 'u'), ('Ü', 'Ue'), ('Ö', 'Oe'), ('Ä', 'Ae'), ('ä', 'ae'), ('ö', 'oe'), ('ü', 'ue'), ('Ϋ́', 'Y'), ('Ϋ', 'Y'), ('Ύ', 'Y'), ('Υ', 'Y'), ('Χ', 'Ch'), ('χ', 'ch'), ('Ξ', 'X'), ('ϒ', 'Y'), ('υ', 'y'), ('ύ', 'y'), ('ϋ', 'y'), ('ΰ', 'y')])

Expand Down