Skip to content

URL-encode link-patterns destinations - #749

Merged
nicholasserra merged 5 commits into
trentm:masterfrom
Don-Yin:fix-link-pattern-url-encoding
Nov 9, 2026
Merged

nicholasserra merged 5 commits into
trentm:masterfrom
Don-Yin:fix-link-pattern-url-encoding

Conversation

@Don-Yin

@Don-Yin Don-Yin commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

Fixes #55.

link-patterns wrote the generated destination into href as is, so a pattern producing a target with spaces gave href="lorem foo bar ipsum". I URL-encode it with urllib.parse.quote, keeping URL delimiters and existing % escapes, so normal URLs come out unchanged.

repro on master and with this change

I added a test for the case in the issue. The full test suite and make testredos pass locally.

Fixes trentm#55

Signed-off-by: Don Yin <Don_Yin@outlook.com>
Signed-off-by: Don Yin <Don_Yin@outlook.com>
Comment thread test/test_markdown2.py Outdated
Python-markdown (markdown.py).
"""

def test_link_pattern_url_encoding(self):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should go as a test file in test/tm-cases. Better error formatting

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved it to test/tm-cases/link_patterns_url_encoding.*.

Signed-off-by: Don Yin <Don_Yin@outlook.com>
…coding

Signed-off-by: Don Yin <Don_Yin@outlook.com>

# Conflicts:
#	CHANGES.md
@nicholasserra

Copy link
Copy Markdown
Collaborator

@Crozzers ill let you have final say

@Crozzers

Crozzers commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Looks fine. urllib.parse.quote will produce %22 instead of &quot; for ". Not sure if that has any wider implications?

@Don-Yin

Don-Yin commented Nov 7, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for looking! I don't think it changes anything in practice: with &quot; the browser decodes the attribute to a literal " and then percent-encodes it as %22 when it parses the URL anyway (path, query and fragment all do this), so both forms end up at the same address. For example new URL('https://e.com/a"b?q="x"#"f"').href gives https://e.com/a%22b?q=%22x%22#%22f%22, which is exactly what quote produces now.

@nicholasserra
nicholasserra merged commit b7aa45c into trentm:master Nov 9, 2026
15 checks passed
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.

properly URL-encode href attributes

3 participants