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 premature HTML-escaping of inline code #393

Merged
merged 1 commit into from
Nov 7, 2024
Merged
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
4 changes: 2 additions & 2 deletions src/mistune/inline_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
parse_link_text,
unescape_char,
)
from .util import escape, escape_url, unikey
from .util import escape_url, unikey

PAREN_END_RE = re.compile(r'\s*\)')

Expand Down Expand Up @@ -310,7 +310,7 @@ def parse_codespan(self, m: Match[str], state: InlineState) -> int:
if len(code.strip()):
if code.startswith(' ') and code.endswith(' '):
code = code[1:-1]
state.append_token({'type': 'codespan', 'raw': escape(code)})
state.append_token({'type': 'codespan', 'raw': code})
return end_pos
else:
state.append_token({'type': 'text', 'raw': marker})
Expand Down
2 changes: 1 addition & 1 deletion src/mistune/renderers/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def image(self, text: str, url: str, title: Optional[str] = None) -> str:
return s + ' />'

def codespan(self, text: str) -> str:
return '<code>' + text + '</code>'
return '<code>' + escape_text(text) + '</code>'

def linebreak(self) -> str:
return '<br />\n'
Expand Down
9 changes: 8 additions & 1 deletion tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def test_markdown_func(self):

def test_ast_output(self):
md = mistune.create_markdown(escape=False, renderer=None)
text = '# h1\n\nfoo **bar**'
text = '# h1\n\nfoo **bar**\n\n`&<>"`'
result = md(text)
expected = [
{
Expand All @@ -94,6 +94,13 @@ def test_ast_output(self):
{'type': 'strong', 'children': [{'type': 'text', 'raw': 'bar'}]}
]
},
{'type': 'blank_line'},
{
'type': 'paragraph',
'children': [
{'type': 'codespan', 'raw': '&<>"'},
]
},
]
self.assertEqual(result, expected)

Expand Down
Loading