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 edge-case crash in InlineProcessor #1406

Merged
merged 2 commits into from
Nov 3, 2023
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
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Remove legacy import needed only in Python 2 (#1403)
* Fix typo that left the attribute `AdmonitionProcessor.content_indent` unset
(#1404)
* Fix edge-case crash in `InlineProcessor` with `AtomicString` (#1406).
* Fix edge-case crash in `codehilite` with an empty `code` tag (#1405).
* Improve and expand type annotations in the code base (#1401).

Expand Down
4 changes: 2 additions & 2 deletions markdown/treeprocessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def linkText(text: str | None) -> None:
text = data[strartIndex:index]
linkText(text)

if not isString(node): # it's Element
if not isinstance(node, str): # it's Element
for child in [node] + list(node):
if child.tail:
if child.tail.strip():
Expand Down Expand Up @@ -304,7 +304,7 @@ def __applyPattern(
if node is None:
return data, True, end

if not isString(node):
if not isinstance(node, str):
if not isinstance(node.text, util.AtomicString):
# We need to process current node too
for child in [node] + list(node):
Expand Down
25 changes: 23 additions & 2 deletions tests/test_apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import markdown
import warnings
from markdown.__main__ import parse_options
from markdown import inlinepatterns
from logging import DEBUG, WARNING, CRITICAL
import yaml
import tempfile
Expand Down Expand Up @@ -664,8 +665,8 @@ class testAtomicString(unittest.TestCase):
""" Test that `AtomicStrings` are honored (not parsed). """

def setUp(self):
md = markdown.Markdown()
self.inlineprocessor = md.treeprocessors['inline']
self.md = markdown.Markdown()
self.inlineprocessor = self.md.treeprocessors['inline']

def testString(self):
""" Test that a regular string is parsed. """
Expand Down Expand Up @@ -710,6 +711,26 @@ def testNestedAtomicString(self):
'*to*</span> *test*</span> *with*</p></div>'
)

def testInlineProcessorDoesntCrashWithWrongAtomicString(self):
""" Test that an `AtomicString` returned from a Pattern doesn't cause a crash. """
tree = etree.Element('div')
p = etree.SubElement(tree, 'p')
p.text = 'a marker c'
self.md.inlinePatterns.register(
_InlineProcessorThatReturnsAtomicString(r'marker', self.md), 'test', 100
)
new = self.inlineprocessor.run(tree)
self.assertEqual(
markdown.serializers.to_html_string(new),
'<div><p>a &lt;b&gt;atomic&lt;/b&gt; c</p></div>'
)


class _InlineProcessorThatReturnsAtomicString(inlinepatterns.InlineProcessor):
""" Return a simple text of `group(1)` of a Pattern. """
def handleMatch(self, m, data):
return markdown.util.AtomicString('<b>atomic</b>'), m.start(0), m.end(0)


class TestConfigParsing(unittest.TestCase):
def assertParses(self, value, result):
Expand Down
Loading