Skip to content

Don't apply self-referential replacements twice#179

Open
gaoflow wants to merge 1 commit into
un33k:masterfrom
gaoflow:fix-replacements-applied-twice
Open

Don't apply self-referential replacements twice#179
gaoflow wants to merge 1 commit into
un33k:masterfrom
gaoflow:fix-replacements-applied-twice

Conversation

@gaoflow

@gaoflow gaoflow commented Jun 17, 2026

Copy link
Copy Markdown

The problem

Custom replacements are applied in two passes: once before unidecode (line ~109) and once in the finalize step (line ~186). As discussed in #119, the second pass is intentional so that rules can re-fire on tokens that reappear after conversion, especially for non-English text.

That second pass corrupts a self-referential rule, where the replacement value contains its own search key. The finalize pass re-fires on the first pass's output and the slug grows on every run:

slugify('a', replacements=[['a', 'aa']])        # -> 'aaaa'   (expected 'aa')
slugify('hello', replacements=[['l', 'll']])     # -> 'hellllllllo'  (expected 'hellllo')

The fix

Keep the intentional finalize re-scan, but skip the rules where re-application is provably self-corrupting (old in new) — those were already applied in the first pass and cannot be safely re-applied:

for old, new in replacements:
    if old and old in new:
        continue
    text = text.replace(old, new)

Every non-self-referential rule still gets the second pass exactly as before, so the re-scan behavior from #119 is untouched. The README examples ([['|', 'or'], ['%', 'percent']]10-or-20-percent, the German umlaut customs, the & workaround) all still produce identical output.

Tests

Added test_replacements_not_applied_twice to TestSlugify; it fails before the change ('aaaa' != 'aa') and passes after. Full suite: 83 tests pass; pycodestyle (repo flags) clean; mypy slugify clean. Added a CHANGELOG entry under Unreleased.

Since the two-pass behavior is deliberate, I kept the change as narrow as possible — happy to adjust if you'd prefer a different approach (e.g. an explicit pre/post split).

I used an AI assistant to help investigate this under my direction, and I have reviewed and verified the change myself.

Custom replacements run in two passes: once before unidecode and once in the
finalize step (so rules can re-fire on tokens that reappear after conversion,
per issue un33k#119). For a self-referential rule (the replacement contains its own
search value, e.g. ['a', 'aa']) the finalize pass re-fires on the first pass's
output and grows the slug each run: slugify('a', replacements=[['a', 'aa']])
returned 'aaaa' instead of 'aa'.

Skip only those self-referential rules in the finalize pass. The intentional
re-scan is preserved for every other rule, and all existing replacement
behavior is unchanged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant