Repository navigation
fix(html): keep one caption for a figure with several images - #4745
PeterStaar-IBM merged 3 commits into
Conversation
|
✅ DCO Check Passed Thanks @ryanmeowy, all your commits are properly signed off. 🎉 |
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/
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
AI triage (advisory)Checked commit This pull request prevents duplicate CI safety: no concern found. A maintainer must still approve CI. 1 related pull request(s) in the same code
The |
There was a problem hiding this comment.
AI first review (advisory): no blocker found
This review covers the full diff at d57ac3e99c. It read the code and did not run it.
The main risk is that figure caption tracking relies on element identities during single-pass traversal. Check that multi-image figures assign the figcaption to the first rendered picture and preserve individual alt text on subsequent pictures.
A maintainer makes the merge decision. This review never approves the pull request.
chrikrah
left a comment
There was a problem hiding this comment.
@ryanmeowy this is one line short of mergeable. The backend creates _captioned_figure_ids in __init__ and fills it in convert(). A second convert() on the same backend then finds every figure claimed, and it drops all <figcaption> text:
# two <figure>s: imgs alt="first"/"second" + figcaption, then one img + figcaption
b = HTMLDocumentBackend(in_doc=d, path_or_stream=s)
for run in (1, 2):
doc = b.convert()
caps = [p.caption_text(doc) for p in doc.pictures]
print(f"convert #{run}: {len(doc.pictures)} pictures, captions {caps}")
convert #1: 3 pictures, captions ['Figure 1. Two views', 'second', 'Figure 2. One view']
convert #2: 3 pictures, captions ['first', 'second', '']
On main at dd477a9 both runs give the same captions, duplicate included, so this is new with the PR.
blocking: main handles a second convert() and this PR breaks it. DocumentConverter builds a fresh backend per document, so only direct reuse hits this, the same reuse dolfim-ibm approved fixing for CSV in #4754. convert() already resets its other per-call state at html_backend.py:980 with self._render_visibility_cache.clear(). Add self._captioned_figure_ids.clear() on the next line, and run 2 matches run 1.
The fix itself checks out:
$ pytest tests/test_backend_html.py -q
106 passed, 1 skipped # at d57ac3e
99 passed, 1 skipped # at a8c01dc
Restoring html_backend.py from a8c01dc with your tests kept fails 3 of the 7 new ones, the two test_figure_keeps_one_caption cases and test_figure_later_image_falls_back_to_its_alt_text. A test that converts twice would pin the reset, and with it I'd approve.
|
@chrikrah thank you, this is a real regression and I can reproduce it. On the second I added |
chrikrah
left a comment
There was a problem hiding this comment.
@ryanmeowy 4a9bb17 clears my blocking finding: self._captioned_figure_ids.clear() now sits beside the visibility-cache reset in convert(), and your new test fails without it. I would merge this, including on top of today's main.
$ PYTHONPATH=$PWD python -m pytest tests/test_backend_html.py -q # git archive 4a9bb171, docling venv
107 passed, 1 skipped in 9.32s # d57ac3e9 was 106 passed, 1 skipped; base a8c01dc was 99 passed, 1 skipped
$ sed -i '/_captioned_figure_ids.clear()/d' docling/backend/html_backend.py # your test kept
$ PYTHONPATH=$PWD python -m pytest tests/test_backend_html.py -q -k figure_caption_claim
FAILED tests/test_backend_html.py::test_figure_caption_claim_is_reset_between_conversions
1 failed, 107 deselected in 2.67s
# diff a8c01dc..4a9bb17 applied to main d01c9c09 (81 commits later, incl. #4468, #4670, #4675, #4713)
$ PYTHONPATH=$PWD python -m pytest tests/test_backend_html.py -q
115 passed, 1 skipped in 9.51s
non-blocking: the reset follows the per-call _render_visibility_cache pattern from 1c74a9b (#2969), which owns the line above it. Blame on the claim path itself leads to a069b11 (#1960) and the no-image fallback in 70eb711 (#4050), and the reset leaves both alone.
@vagenas the push to 4a9bb17 dismissed your approval from the 14th. The only new code is that one reset line, so could you approve it again at this head?
A figure with more than one image emitted its <figcaption> once per image, because every image read the caption of its parent figure. Only the first image of the figure now claims the caption. Signed-off-by: Ryan Yang <ryanxys@gmail.com>
Reading the <figcaption> on the first <img> in document order dropped the caption when that image produced no picture, and images inside a <p> reached the manual caption fallback. The figure now records that its caption was claimed, by the first image that produces a picture, and the fallback skips a claimed figure. Signed-off-by: Ryan Yang <ryanxys@gmail.com>
A backend instance can convert more than one document. The claimed figure identities are per-run state, so convert() clears them with the rest of the context. Without the reset, a second conversion falls back to the alt text for every figure caption. Signed-off-by: Ryan Yang <ryanxys@gmail.com>
4a9bb17 to
3c3ce71
Compare
Issue resolved by this Pull Request:
Resolves #4744
Checklist:
tests/test_backend_html.pycovers a figure with several images in six shapes, and thealtfallback of the images that follow the first one.Review evidence:
Behavior before.
_emit_image()read the<figcaption>of the parent figure for every<img>, so a figure with two images produced the caption twice:Behavior after. The first image that produces a picture claims the caption of the figure, and the figure records that its caption was claimed. A figure keeps exactly one caption, also when the first image is not rendered (a hidden image produces no picture) or when it sits inside the
<figcaption>:<img/><img/><figcaption>cap['cap', 'cap']['cap']<img/><figcaption>cap['cap']['cap']<img style="display:none"/>, second<img/>['cap']['cap']<img aria-hidden="true"/>, second<img/>['cap']['cap']<figcaption><img/>cap</figcaption><img/>['cap']['cap']<p><img/><img/></p><figcaption>cap['cap', 'cap']['cap']Document tree: the caption stays a
DocItemLabel.CAPTIONtext item and becomes the caption of the first picture of the figure; the other pictures have no caption. The images after the first fall back to their ownalttext:Export: one caption per figure instead of one per image.
Checks.
pytest tests/test_backend_html.py -k "keeps_one_caption or falls_back_to_its_alt"onmainat the parent commitpytest tests/test_backend_html.py tests/test_backend_mhtml.py tests/test_backend_epub.py tests/test_backend_email.py tests/test_input_doc.py tests/test_backend_markdown.pyruff format --checkandruff checkon the two changed filesty checkmainat this commitNo reference-data changes: a scan of the HTML, XHTML and MHTML sources under
tests/anddocs/finds no figure with more than one image, and the new tests build their own HTML.Not run: conversion of real-world HTML corpora with multi-image figures.