fix: read a table's own caption on HTML import - #1071
Merged
Conversation
`<table><caption>` is how HTML captions a table, and it is what pandoc emits for every captioned table. The importer's row walk looks only for `tr`, so the caption element was skipped: its text left the document, the returned node had no `caption` field, and `report.diagnostics` was empty. Nothing said anything. The slot was already there. `table.caption` is in the AST schema, the parser fills it, and Carve spells it `^ text` after the rows - so this was never an unrepresentable shape, only an unread element. Reading it exposed a second case. A figure-wrapped table can arrive carrying TWO captions, its own `<caption>` and the figure's `<figcaption>`, and Carve spells one `^ ` line per host. Emitting both wrote a second `^ ` line that re-read as a literal paragraph, which is worse than the silent drop it replaced. The figure WRAPPER has no Carve spelling at all, so its caption is the one that cannot survive: the table keeps its own and the figcaption is reported as `table-degraded`, the code the contract already defines as "a table could not be represented structurally". A figure-wrapped table with no caption of its own is unchanged - the figcaption still lands in the slot the table left empty, which loses only the wrapper element and no text. Measured across the engines: carve-php already reads the caption, carve-rs has the same gap (html_import.rs hardcodes `caption: None`).
dereuromark
added a commit
to markup-carve/carve-rs
that referenced
this pull request
Aug 15, 2026
`<table><caption>` is how HTML captions a table, and it is what pandoc emits for every captioned table. The importer's row walk descends looking only for `tr`, so the caption element was never visited: `Table::caption` was hardcoded to `None`, the text left the document, and no diagnostic said so. The slot was already there. `Table::caption` exists on the node, the parser fills it, and Carve spells it `^ text` after the rows - so this was never an unrepresentable shape, only an unread element. carve-js had the same gap (markup-carve/carve-js#1071); carve-php already read it. This brings the third engine into line. Unlike carve-js, this importer has no `<figure>` branch, so the two-captions-one-slot case that fix had to settle does not arise here.
42 tasks
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.
The bug
<table><caption>is how HTML captions a table, and it is what pandoc emits for every captioned table. The importer dropped it silently:The returned AST node came back as
{type, rows}with nocaptionfield.table()builds its rows by walking fortrelements, so a<caption>child is simply never visited.Why this was never a representation problem
The slot already existed on every side:
resources/ast-schema.jsondefinestable.caption^ textafter the rowscarveToHtmlrenders it back to<caption>So the shape was always spellable — the element was just unread.
The second case it exposed
A figure-wrapped table can arrive with two captions: its own
<caption>and the figure's<figcaption>. Carve spells one^line per host, so emitting both produced:That's worse than the silent drop. The figure wrapper has no Carve spelling at all (markup-carve/carve#1211 documents exactly this), so the figure's caption is the one that cannot survive. The table keeps its own, and the figcaption is reported as
table-degraded— the code the contract already defines as "a table could not be represented structurally", which avoids adding a code the spec'shtml-import-schema.jsonwould have to enumerate first.Behavior after
<table><caption>Fruit prices</caption>…|=A|/| 1 |/^ Fruit prices<figure><table>…<figcaption>Outer| 1 |/^ Outer<figure><table><caption>Inner…<figcaption>Outer| 1 |/^ Innertable-degradedThe middle row is unchanged — a figure-wrapped table with no caption of its own still puts the figcaption in the slot the table left empty, losing the wrapper element but no text.
Cross-engine
Measured on the same input:
findFirstDirectChildByTagName($node, 'caption'))html_import.rshardcodescaption: Nonecarve-rs needs the equivalent fix; filing separately.
Verification
Full suite: 383 files, 9484 tests, 0 failures.
npm run typecheckclean. Three regression tests added covering the table's own caption, the figure-wrapped table with an empty slot, and the two-caption conflict (including that the output contains no leaked<p>^).