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 HTML elements not unhashing correctly (issue 508) #509

Merged
merged 3 commits into from
Apr 28, 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 CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- [pull #501] Fix link patterns extra matching against internal hashes
- [pull #502] Replace deprecated `optparse` with `argparse`
- [pull #506] Fix `_uniform_outdent` failing with empty strings (issue #505)
- [pull #509] Fix HTML elements not unhashing correctly (issue 508)


## python-markdown2 2.4.8
Expand Down
6 changes: 5 additions & 1 deletion lib/markdown2.py
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,7 @@ def _strict_tag_block_sub(self, text, html_tags_re, callback):
tag_count -= 1
else:
# if close tag is in same line
if '</%s>' % is_markup.group(2) in chunk[is_markup.end():]:
if self._tag_is_closed(is_markup.group(2), chunk):
# we must ignore these
is_markup = None
else:
Expand All @@ -908,6 +908,10 @@ def _strict_tag_block_sub(self, text, html_tags_re, callback):

return result

def _tag_is_closed(self, tag_name, text):
# super basic check if number of open tags == number of closing tags
return len(re.findall('<%s(?:.*?)>' % tag_name, text)) == len(re.findall('</%s>' % tag_name, text))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Wonder if this could be even simpler on the left side, something as simple as even:

len(re.findall('<%s' % tag_name, text)) == len(re.findall('</%s>' % tag_name, text))

which just bails on matching the opening element's end > and middle content.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I mainly put that there because I was concerned about incomplete tags potentially messing it up. Not sure as to how something like <div> <div </div> should be interpreted

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yup good call


def _strip_link_definitions(self, text):
# Strips link definitions from text, stores the URLs and titles in
# hash references.
Expand Down
12 changes: 12 additions & 0 deletions test/tm-cases/hash_html_blocks_issue_508.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div><div></div>
</div>

<div></div>

<div></div>

<div></div>

<ul>
<li>A</li>
</ul>
7 changes: 7 additions & 0 deletions test/tm-cases/hash_html_blocks_issue_508.text
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div><div></div>
</div>
<div></div>
<div></div>
<div></div>

- A