Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 FIX: Double encoded ampersands in query params #929

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
14 changes: 14 additions & 0 deletions myst_parser/parsers/docutils_.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""MyST Markdown parser for docutils."""

import re
from dataclasses import Field
from typing import (
Any,
Expand Down Expand Up @@ -268,6 +269,10 @@ def parse(self, inputstring: str, document: nodes.document) -> None:
HTMLTranslator.depart_rubric = depart_rubric_html
HTMLTranslator.visit_container = visit_container_html
HTMLTranslator.depart_container = depart_container_html
HTMLTranslator.special_chars_no_amp = { # needed by encode_fixed
k: v for k, v in HTMLTranslator.special_characters.items() if k != ord("&")
}
HTMLTranslator.encode = encode_fixed
Comment on lines +272 to +275
Copy link
Member

Choose a reason for hiding this comment

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

I'm afraid this is not something that cannot be merged; it would break restructuredtext parsing.

Monkey patching is done strictly as a last resort, and only for where the patch does not affect upstream use


self.setup_parse(inputstring, document)

Expand Down Expand Up @@ -515,3 +520,12 @@ def depart_container_html(self, node: nodes.Node):
See explanation in `visit_container_html`
"""
self.body.append("</div>\n")


def encode_fixed(self, text: str):
"""Override the default encode method to prevent `&` characters from getting encoded
multiple times.
"""
text = str(text)
translated = text.translate(self.special_chars_no_amp)
return re.sub(r"(&)(?!amp;)", "&amp;", translated)
2 changes: 2 additions & 0 deletions tests/test_sphinx/sourcedirs/references/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

[nested *syntax*](https://example.com)

[query params](https://example.com?foo=bar&a=1)

[](title)

[plain text](title)
Expand Down
5 changes: 5 additions & 0 deletions tests/test_sphinx/test_sphinx_builds/test_references.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ <h1>
</em>
</a>
</p>
<p>
<a class="reference external" href="https://example.com?foo=bar&amp;a=1">
query params
</a>
</p>
<p>
<a class="reference internal" href="#title">
<span class="std std-ref">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
nested
<emphasis>
syntax
<paragraph>
<reference refuri="https://example.com?foo=bar&amp;a=1">
query params
<paragraph>
<reference internal="True" refid="title">
<inline classes="std std-ref">
Expand Down
3 changes: 3 additions & 0 deletions tests/test_sphinx/test_sphinx_builds/test_references.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
nested
<emphasis>
syntax
<paragraph>
<reference refuri="https://example.com?foo=bar&amp;a=1">
query params
<paragraph>
<pending_xref refdoc="index" refdomain="True" refexplicit="False" reftarget="title" reftype="myst">
<inline classes="xref myst">
Expand Down