Skip to content

fix(docx): close an open list before a table that follows it - #5309

Merged
PeterStaar-IBM merged 1 commit into
docling-project:mainfrom
astroknot-sheep:fix/docx-table-after-list
Nov 13, 2026
Merged

PeterStaar-IBM merged 1 commit into
docling-project:mainfrom
astroknot-sheep:fix/docx-table-after-list

Conversation

@astroknot-sheep

Copy link
Copy Markdown
Contributor

When a table comes directly after a list item in a Word document (no empty paragraph in between), the Word backend adds the table to the ListGroup, as a sibling of the list items. In Markdown the table then gets glued onto the last item with no blank line, so most renderers show it as part of the list text, and a list item after the table goes back into the same list after the table.

Paragraphs already close the open list; tables didn't. This adds _close_list_before_table(), which does the same list-closing steps as the paragraph path (reset the parents from level_at_new_list, drop the cached list group, record history) and is called before a multi-cell table is added. Single-cell tables are already unwrapped into body text, so they're unchanged. (#4305 fixed the variant with an empty paragraph between the list and the table.)

Checklist:

  • Documentation has been updated, if necessary.
  • Examples have been added, if necessary.
  • Tests have been added, if necessary.

Review evidence:

  • Before: a List Bullet paragraph followed by a 2×2 table gives a TableItem whose parent is the list group; Markdown is - item\n| cell 00 | … (in the nested case the table sits inside the sub-list).
  • After: the table's parent is the body (or the heading it's under), Markdown has a blank line before the table, and ListGroups contain only ListItems.
  • New test_table_right_after_a_list_closes_the_list (flat and nested) fails on main and passes with the fix.
  • pytest tests/test_backend_msword*.py: 328 passed, including test_e2e_docx_conversions with LibreOffice; no reference data changed.
  • ruff format / ruff check clean on the changed files.

I used an AI assistant while working on this. I reproduced the bug, checked the document tree before and after, and ran the tests myself.

@github-actions

Copy link
Copy Markdown
Contributor

✅ DCO Check Passed

Thanks @astroknot-sheep, all your commits are properly signed off. 🎉

@mergify

mergify Bot commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

Merge Protections

🟢 Merge protection satisfied — ready to merge.

Show 1 satisfied protection

🟢 Enforce conventional commit

Make sure that we follow https://www.conventionalcommits.org/en/v1.0.0/

  • title ~= ^(fix|feat|docs|style|refactor|perf|test|build|ci|chore|revert)(?:\(.+\))?(!)?:

@github-actions github-actions Bot added ai:ci-safe AI triage found no CI-safety concern (advisory) bug Something isn't working docx issue related to docx backend labels Nov 12, 2026
@github-actions

Copy link
Copy Markdown
Contributor

AI triage (advisory)

Checked commit fc54a79bb7. The analysis read the diff only. It did not run code from this pull request.

The PR adds _close_list_before_table() to the Word backend. It runs before a multi-cell table is added, so a table that directly follows a list item no longer becomes a child of the ListGroup. A new test covers the flat and nested cases.

Topic labels added: bug, docx.

CI safety: no concern found. A maintainer must still approve CI.

1 related pull request(s) in the same code
PR State Signal Assessment
#4305 merged referenced as #4305 related: PR 4305 is merged. It changed _get_or_create_list_group so a list group is not reused across a table that has an empty paragraph before it. This PR closes the open list before the table is added, which is a different code path and a different case. The two changes do not overlap.

The ai:* labels are advisory. They do not approve CI and they do not add tests:full. A new push runs this check again.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

AI first review (advisory): no blocker found

This review covers the full diff at fc54a79bb7. It read the code and did not run it.

I found no defect that decides the merge. _close_list_before_table() in docling/backend/msword_backend.py copies the paragraph list-closing steps. It runs only for multi-cell tables, after the single-cell early return. It sets last_numid and nested_lists, clears the list-group cache, resets parents/level/level_at_new_list, and records history with a None numid. The history entry makes the next list item with the same numId reopen a fresh list. Table-cell lists keep their own state because _isolated_list_context restores it after each cell.

A maintainer makes the merge decision. This review never approves the pull request.

@github-actions github-actions Bot added the ai:review-lgtm AI first review found no blocker or major issue (advisory) label Nov 12, 2026
@codecov

codecov Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 64.70588% with 6 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
docling/backend/msword_backend.py 64.70% 5 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@PeterStaar-IBM
PeterStaar-IBM merged commit fec4dc3 into docling-project:main Nov 13, 2026
34 checks passed
LindseyZ1205 added a commit to LindseyZ1205/docling that referenced this pull request Nov 27, 2026
A heading paragraph did not close an open list, and _add_heading
clears the parents tail only for a lower heading level. So when a list
came before the first heading, or a heading skipped a level after a
list (for example Heading 1, a list, then Heading 3), the heading and
the text under it became children of the ListGroup. This gave invalid
HTML (<h4> inside <ul>) and put the section into the list chunk.

Close the list before the heading with the same steps as before a
table (docling-project#5309), which are now in _close_open_list().

Signed-off-by: LindseyZ1205 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai:ci-safe AI triage found no CI-safety concern (advisory) ai:review-lgtm AI first review found no blocker or major issue (advisory) bug Something isn't working docx issue related to docx backend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants