Skip to content

Fix empty list items being parsed as nested lists - #734

Merged
nicholasserra merged 2 commits into
trentm:masterfrom
LuisFigueroaG:fix/empty-list-items
Oct 13, 2026
Merged

nicholasserra merged 2 commits into
trentm:masterfrom
LuisFigueroaG:fix/empty-list-items

Conversation

@LuisFigueroaG

Copy link
Copy Markdown
Contributor

Fixes #167

A list item with nothing after the marker (just the marker and a trailing space) took the next line as its content. That line then got parsed as a sub-list, so the output was nested and broken:

 1. 
 2. 
<ol>
<li>
<ol start="2">
2. 
</ol></li>
</ol>

- \n- \n did the same thing with <ul>, and an empty item in the middle of a list (- foo\n- \n- bar) turned bar into a sub-list of the empty item.

The item text in _list_item_re was (?:.+?), which needs at least one character, so for an empty item it went past the newline into the next item. I changed it to (?:.*?). For items that have text this matches the same as before. For empty items it now stops at the end of the line, so the output is:

<ol>
<li></li>
<li></li>
</ol>

That's also what GitHub gives for this input.

Tests:

  • New case tm-cases/empty_list_items_issue167 covers the example from the issue, bullet lists, an empty item between other items, an empty numbered item and empty items in a sub-list. It fails without the fix.
  • xss_smuggling_spans_in_image_attrs has a - line followed by - ![](...), and its expected HTML had the old nested-list output. I updated it to the two-item list. The <img> line itself is unchanged.

Test runs:

  • make testone: 289 tests, OK (288 on master)
  • make testredos: 7 tests, OK
  • pycodestyle lib/markdown2.py gives the same count before and after (415)

A list item with nothing after the marker swallowed the next line as its
content, so "1. \n2. " produced a nested, unclosed list instead of two
empty items. Allow the item text to be empty.

Fixes trentm#167
@nicholasserra

Copy link
Copy Markdown
Collaborator

Thank you!

@nicholasserra
nicholasserra merged commit cac71b6 into trentm:master Oct 13, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

List with empty items corner case generates invalid HTML

2 participants