fix(docx): descend into a content control that also holds a picture (#3950) - #3952
Open
Anai-Guo wants to merge 1 commit into
Open
fix(docx): descend into a content control that also holds a picture (#3950)#3952Anai-Guo wants to merge 1 commit into
Anai-Guo wants to merge 1 commit into
Conversation
…ocling-project#3950) The element walk computes the image branches with descendant XPaths (``.//a:blip``, ``.//w:drawing``, ``.//v:imagedata``), so a ``w:sdt`` holding a picture anywhere inside matched ``elif drawing_blip`` first and the ``elif tag_name == "sdt"`` branch was never reached. The picture was emitted, every paragraph in the control was dropped. Word's built-in cover pages are a body-level ``w:sdt`` holding the logo together with the title, subtitle, company and date fields, so the document's own title never reached the DoclingDocument. Test the content control before the image branches, so an ``sdt`` is always descended into and its children are dispatched normally. The picture is still emitted exactly once -- the image branch now fires on the inner ``w:p`` -- and at its true position inside the control. Signed-off-by: Tai An <antai12232931@outlook.com>
Contributor
Merge Protections🟢 Merge protection satisfied — ready to merge. Show 1 satisfied protection🟢 Enforce conventional commitMake sure that we follow https://www.conventionalcommits.org/en/v1.0.0/
|
Contributor
|
✅ DCO Check Passed Thanks @Anai-Guo, all your commits are properly signed off. 🎉 |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3950.
Problem
_walk_linearcomputes the image hits with descendant XPaths before it dispatches:and then dispatches with a single
if/elifchain in whichelif tag_name == "sdt"sits afterelif drawing_blip/elif vml_images/elif drawingml_els.So whenever a content control contains a picture anywhere inside it, the
sdtelement itself matches the image branch, the picture is emitted, and_walk_linear(sdt_content, doc)never runs — every paragraph in that control is silently dropped.Word's built-in "Cover Page" building blocks are exactly this shape: a body-level
<w:sdt>holding the logo and the title, subtitle, company and date fields. The result is a document whose title never reaches theDoclingDocument—export_to_markdown()starts at the table of contents, preceded by a lone<!-- image -->.Fix
Test for the content control before the image branches, so an
sdtis always descended into and its children are then dispatched normally. Thesdtbranch body is unchanged; only its position in the chain moves (it now sits directly after thetblbranch).The picture is still emitted, and exactly once — the image branch now fires on the inner
<w:p>rather than on thesdt. It also lands at its true position inside the control rather than at the point where thesdtstarts, which is the correct reading order.Verification
Reproduction from the issue, on
mainvs. this branch:mainwith_image.docx'<!-- image -->\n\nBODY TEXT OUTSIDE SDT''<!-- image -->\n\nCOVER TITLE INSIDE SDT\n\nBODY TEXT OUTSIDE SDT'no_image.docxAdded
tests/test_backend_msword_sdt_pictures.py(a new file, sotest_backend_msword.pystays under the per-file line limit). It builds the DOCX in-test — no new fixture or groundtruth — and asserts both that the control's text survives and that<!-- image -->still appears exactly once. It fails onmainand passes here.Existing DOCX suites are unchanged:
tests/test_backend_msword.py,test_backend_msword_lists.pyandtest_backend_msword_spacer.pygive the identical result before and after (56 passed / 1 failed either way; the single failure is the pre-existingtest_e2e_docx_conversionsDrawingML case that needs LibreOffice, which is not installed here).test_block_sdt_tables_are_extractedandtest_inline_sdt_references— the two existingsdttests — still pass, as do the textbox and picture tests.ruff==0.15.12 check/format --checkare clean on both files.Companion to #3951 (table cells inside a content control) — different code path, same theme of
<w:sdt>not being descended into. The two are independent and touch disjoint regions of the file.🤖 Generated with Claude Code