Repository navigation
🐛 FIX: Escape regex characters correctly in utils.escapeRE - #499
Open
kevincommits wants to merge 1 commit into
Open
kevincommits wants to merge 1 commit into
kevincommits wants to merge 1 commit into
Conversation
The replacement string was copied from JavaScript, where "\\$&" inserts a backslash and the matched character. Python's re.sub does not support "$&", so every special character was replaced with the literal text "\$&".
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
escapeREinmarkdown_it/common/utils.py, exposed to plugins asmd.utils.escapeRE, returns a broken pattern for any input that contains a regex special character.Actual:
a\$&b\$&cExpected:
a\.b\*c, which is what markdown-it returns formd.utils.escapeRE('a.b*c')(checked with 14.1.0 and 15.0.1).The replacement string
"\\$&"came over from the JavaScriptstr.replace(..., '\\$&'), where$&means the whole match. Python'sre.subhas no$&, so each special character was replaced with the literal text\$&. The fix usesr"\\\g<0>", the Python spelling of the same replacement. Nothing inside markdown-it-py calls this helper, so parsing output does not change.The new test in
tests/test_port/test_misc.pychecks the escaped string and that it compiles to a pattern matching the original text. It fails on master and passes with the fix.Ran:
pytest tests/(1191 passed)pre-commit run --files markdown_it/common/utils.py tests/test_port/test_misc.py(ruff, ruff-format, mypy passed)Not run: the mdit-py-plugins job, the benchmark job, and other Python versions (tested on 3.12 only).