Skip to content

Commit 98ecd38

Browse files
authored
Fix revealjs auto-stretch moving top figure captions below the image (#15387)
* Keep figure caption location when revealjs auto-stretches an image (#8938) Auto-stretch moves a slide's only image to slide level and rebuilds its figcaption as a p.caption next to it. That caption was always inserted after the image, so fig-cap-location: top was lost on every slide where auto-stretch applied; only auto-stretch: false kept the caption on top. The caption now goes back on the side of the image it had in the figure. This uses document order rather than the quarto-float-caption-top class because a figure without a crossref label gets a figcaption with no class. Fixes #8938 * Size revealjs stretched-figure captions placed above the image (#8938) The revealjs caption size rule only matched a p.caption that follows a stretched image. Now that auto-stretch keeps a top caption before the image, that caption fell back to the full slide text size. The rule now also matches a caption followed by a stretched image, using :has(). A Playwright test checks the computed font size, since the HTML alone cannot show whether the rule applies.
1 parent bd36e20 commit 98ecd38

6 files changed

Lines changed: 79 additions & 5 deletions

File tree

‎news/changelog-1.12.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ All changes included in 1.12:
5757
### `revealjs`
5858

5959
- ([#7378](https://github.com/quarto-dev/quarto-cli/issues/7378)): Add `code-line-fragment-indices` to set the fragment index of each step of stepped `code-line-numbers` highlighting. (author: @mcanouil)
60+
- ([#8938](https://github.com/quarto-dev/quarto-cli/issues/8938)): Fix figure captions placed below the image when `fig-cap-location: top` is set and the image is auto-stretched to fill the slide.
6061

6162
### `typst`
6263

‎src/format/reveal/format-reveal.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
import { join } from "../../deno_ral/path.ts";
77

8-
import { Document, Element, NodeType } from "../../core/deno-dom.ts";
8+
import { Document, Element, Node, NodeType } from "../../core/deno-dom.ts";
99
import {
1010
kBrandMode,
1111
kCodeLineNumbers,
@@ -1071,6 +1071,7 @@ function applyStretch(doc: Document, autoStretch: boolean) {
10711071
// Figure environment ? Get caption, id and alignment
10721072
const quartoFig = slideEl.querySelector("div.quarto-figure");
10731073
const caption = doc.createElement("p");
1074+
let captionBefore = false;
10741075
if (quartoFig) {
10751076
// Get alignment
10761077
const align = quartoFig.className.match(
@@ -1085,6 +1086,9 @@ function applyStretch(doc: Document, autoStretch: boolean) {
10851086
if (figCaption) {
10861087
caption.classList.add("caption");
10871088
caption.innerHTML = figCaption.innerHTML;
1089+
captionBefore =
1090+
(figCaption.compareDocumentPosition(imageEl) &
1091+
Node.DOCUMENT_POSITION_FOLLOWING) !== 0;
10881092
}
10891093
}
10901094

@@ -1096,11 +1100,11 @@ function applyStretch(doc: Document, autoStretch: boolean) {
10961100
// insert at target position
10971101
slideEl.insertBefore(image, nextEl);
10981102

1099-
// If there was a caption processed add it after
1103+
// If there was a caption processed, keep it on its side of the image
11001104
if (caption.classList.contains("caption")) {
11011105
slideEl.insertBefore(
11021106
caption,
1103-
imageEl.nextElementSibling,
1107+
captionBefore ? imageEl : imageEl.nextElementSibling,
11041108
);
11051109
}
11061110
// Remove container if still there

‎src/resources/formats/revealjs/quarto.scss‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,9 @@ $panel-sidebar-padding: 0.5em;
607607
.reveal .slide {
608608
figure > figcaption,
609609
img.stretch + p.caption,
610-
img.r-stretch + p.caption {
610+
img.r-stretch + p.caption,
611+
p.caption:has(+ img.stretch),
612+
p.caption:has(+ img.r-stretch) {
611613
font-size: #{$presentation-font-smaller}em;
612614
}
613615
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
title: Auto-stretched figure caption size
3+
format: revealjs
4+
---
5+
6+
## Caption on top {#top}
7+
8+
![Caption on top](quarto.png){#fig-top cap-location="top"}
9+
10+
## Caption at bottom {#bottom}
11+
12+
![Caption at bottom](quarto.png){#fig-bottom cap-location="bottom"}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
title: Auto-stretch keeps the figure caption location
3+
format: revealjs
4+
fig-cap-location: top
5+
_quarto:
6+
tests:
7+
revealjs:
8+
ensureHtmlElements:
9+
-
10+
- 'section#top > p.caption + img.r-stretch#fig-top'
11+
- 'section#unlabelled > p.caption + img.r-stretch'
12+
- 'section#knitr > div.cell + p.caption + img.r-stretch#fig-knitr'
13+
- 'section#bottom > img.r-stretch#fig-bottom + p.caption'
14+
-
15+
- 'section#top > img.r-stretch + p.caption'
16+
- 'section#unlabelled > img.r-stretch + p.caption'
17+
- 'section#knitr > img.r-stretch + p.caption'
18+
- 'section#bottom > p.caption + img.r-stretch'
19+
---
20+
21+
## Top {#top}
22+
23+
A figure alone on a slide is auto-stretched: the image moves out of its figure to the slide level and its caption is rebuilt as a `p.caption` next to it. That caption must stay above the image when `fig-cap-location: top` is set.
24+
25+
![Caption on top](https://placehold.co/100x50.png){#fig-top}
26+
27+
## Unlabelled {#unlabelled}
28+
29+
![Caption on top](https://placehold.co/100x50.png)
30+
31+
## Knitr {#knitr}
32+
33+
```{r}
34+
#| label: fig-knitr
35+
#| fig-cap: "Caption on top"
36+
#| echo: true
37+
plot(1:10)
38+
```
39+
40+
## Bottom {#bottom}
41+
42+
![Caption at bottom](https://placehold.co/100x50.png){#fig-bottom cap-location="bottom"}

‎tests/integration/playwright/tests/revealjs.spec.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { test, expect, Locator, Page } from '@playwright/test';
2-
import { tabKey } from '../src/utils';
2+
import { expectCloseTo, getCSSProperty, tabKey } from '../src/utils';
33

44
test('logo and footer are correctly shown in default mode', async ({ page }) => {
55
await page.goto('./revealjs/logo-footer.html#/slide-1');
@@ -84,6 +84,19 @@ test('internal id for links between slides are working', async ({ page }) => {
8484
await page.waitForURL(/theorem$/);
8585
});
8686

87+
test('auto-stretched figure caption keeps the caption size above the image (#8938)', async ({ page }) => {
88+
await page.goto('./revealjs/stretch-caption-location.html#/top');
89+
const topCaption = page.locator('#top > p.caption:has(+ img.r-stretch)');
90+
const bottomCaption = page.locator('#bottom > img.r-stretch + p.caption');
91+
await expect(topCaption).toBeAttached();
92+
await expect(bottomCaption).toBeAttached();
93+
const mainFontSize = await getCSSProperty(page.locator('body'), '--r-main-font-size', true) as number;
94+
const topFontSize = await getCSSProperty(topCaption, 'font-size', true) as number;
95+
const bottomFontSize = await getCSSProperty(bottomCaption, 'font-size', true) as number;
96+
expect(bottomFontSize).toBeLessThan(mainFontSize);
97+
expectCloseTo(topFontSize, bottomFontSize);
98+
});
99+
87100
test('Home and End on a focused tabset tab do not change slide', async ({ page }) => {
88101
await page.goto('./revealjs/tabset-focus-order.html#/slide-3');
89102
// Located by attribute rather than role: if reveal leaves the slide, it marks

0 commit comments

Comments
 (0)