Repository: lepture/mistune

Issue:
Certain characters in inline code incorrectly parsed (e.g., `&`)
MWE:

```python
import mistune
from mistune.core import BlockState
markdown = mistune.create_markdown(renderer="ast")

md = r"`&<>`"
tokens = markdown(md)

print(tokens)
```

Output:

```python
[{'type': 'paragraph', 'children': [{'type': 'codespan', 'raw': '&amp;&lt;&gt;'}]}]
```

Discussion:

Patch:
diff --git a/src/mistune/inline_parser.py b/src/mistune/inline_parser.py
index 21b04c1..97423db 100644
--- a/src/mistune/inline_parser.py
+++ b/src/mistune/inline_parser.py
@@ -19,7 +19,7 @@ from .helpers import (
     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*\)')
 
@@ -310,7 +310,7 @@ class InlineParser(Parser[InlineState]):
             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})
diff --git a/src/mistune/renderers/html.py b/src/mistune/renderers/html.py
index 0d999a7..a8d2c47 100644
--- a/src/mistune/renderers/html.py
+++ b/src/mistune/renderers/html.py
@@ -91,7 +91,7 @@ class HTMLRenderer(BaseRenderer):
         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'


Test patch:
diff --git a/tests/test_misc.py b/tests/test_misc.py
index f57c53b..5e21a1a 100644
--- a/tests/test_misc.py
+++ b/tests/test_misc.py
@@ -77,7 +77,7 @@ class TestMiscCases(TestCase):
 
     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 = [
             {
@@ -94,6 +94,13 @@ class TestMiscCases(TestCase):
                     {'type': 'strong', 'children': [{'type': 'text', 'raw': 'bar'}]}
                 ]
             },
+            {'type': 'blank_line'},
+            {
+                'type': 'paragraph',
+                'children': [
+                    {'type': 'codespan', 'raw': '&<>"'},
+                ]
+            },
         ]
         self.assertEqual(result, expected)