Skip to content

Commit 0ef379a

Browse files
authored
Merge pull request #8139 from quarto-dev/bugfix/8000-b
Lua - don't forward classes from image to floats or figures
2 parents ea55f57 + e728690 commit 0ef379a

File tree

4 files changed

+89
-9
lines changed

4 files changed

+89
-9
lines changed

src/resources/filters/layout/html.lua

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,13 @@ function renderHtmlFigure(el, render)
171171
end
172172

173173
-- create figure div
174-
local figureDiv = pandoc.Div({}, pandoc.Attr(el.identifier, el.classes, figureAttr))
174+
local figureDiv = pandoc.Div({}, pandoc.Attr(el.identifier, {}, figureAttr))
175+
figureDiv.classes = el.classes:filter(function(str)
176+
if str:match("quarto%-figure.*") then
177+
return true
178+
end
179+
return false
180+
end)
175181

176182
-- remove identifier (it is now on the div)
177183
el.identifier = ""

src/resources/filters/layout/pandoc3_figure.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ function render_pandoc3_figure()
5050
image.caption = quarto.utils.as_inlines(figure.caption.long)
5151
end
5252
-- TODO need to find all correct classes to forward
53-
if figure.classes:includes("margin-caption") then
54-
image.classes:insert("margin-caption")
53+
for i, v in pairs(figure.classes) do
54+
if v:match("^margin%-") or v:match("^quarto%-") or v:match("^column%-") then
55+
image.classes:insert(v)
56+
end
5557
end
5658
return htmlImageFigure(image)
5759
end

src/resources/filters/quarto-pre/parsefiguredivs.lua

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@
33

44
local patterns = require("modules/patterns")
55

6+
local attributes_to_not_merge = pandoc.List({
7+
"width", "height"
8+
})
9+
10+
-- Narrow fix for #8000
11+
local classes_to_not_merge = pandoc.List({
12+
"border"
13+
})
14+
615
function handle_subfloatreftargets()
716
-- #7045: pull fig-pos and fig-env attributes from subfloat to parent
817
return {
@@ -238,8 +247,22 @@ function parse_floatreftargets()
238247
-- if the div contains a single image, then we simply use the image as
239248
-- the content
240249
content = content[1].content[1]
241-
attr = merge_attrs(attr, content.attr)
242-
attr.identifier = div.identifier -- never override the identifier
250+
251+
-- don't merge classes because they often have CSS consequences
252+
-- but merge attributes because they're needed to correctly resolve
253+
-- behavior such as fig-pos="h", etc
254+
-- See #8000.
255+
-- We also exclude attributes we know to not be relevant to the div
256+
for k, v in pairs(content.attr.attributes) do
257+
if not attributes_to_not_merge:includes(k) then
258+
attr.attributes[k] = v
259+
end
260+
end
261+
for _, v in ipairs(content.attr.classes) do
262+
if not classes_to_not_merge:includes(v) then
263+
attr.classes:insert(v)
264+
end
265+
end
243266
end
244267

245268
local skip_outer_reftarget = false
@@ -353,9 +376,20 @@ function parse_floatreftargets()
353376
local fig_attr = fig.attr
354377
local new_content = _quarto.ast.walk(fig.content[1], {
355378
Image = function(image)
356-
-- forward attributes and classes from the image to the float
357-
fig_attr = merge_attrs(fig_attr, image.attr)
358-
-- strip redundant image caption
379+
-- don't merge classes because they often have CSS consequences
380+
-- but merge attributes because they're needed to correctly resolve
381+
-- behavior such as fig-pos="h", etc
382+
-- See #8000.
383+
for k, v in pairs(image.attributes) do
384+
if not attributes_to_not_merge:includes(k) then
385+
fig_attr.attributes[k] = v
386+
end
387+
end
388+
for _, v in ipairs(image.classes) do
389+
if not classes_to_not_merge:includes(v) then
390+
fig_attr.classes:insert(v)
391+
end
392+
end
359393
image.caption = {}
360394
return image
361395
end
@@ -485,7 +519,7 @@ function parse_floatreftargets()
485519
end
486520
return quarto.FloatRefTarget({
487521
identifier = identifier,
488-
classes = img.classes,
522+
classes = {},
489523
attributes = as_plain_table(img.attributes),
490524
type = category.name,
491525
content = img,
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
title: issue-8000
3+
_quarto:
4+
tests:
5+
html:
6+
ensureHtmlElements:
7+
-
8+
- "div#fig-2.border"
9+
-
10+
- "div#fig-1.border"
11+
- "div#fig-3.border"
12+
- "#scaffold div.border"
13+
---
14+
15+
::: {#scaffold}
16+
17+
![An _image_ with border](https://placeholder.co/400){.border fig-alt="And alt text."}
18+
19+
:::
20+
21+
![An _image_ with border](https://placeholder.co/400){#fig-1 .border fig-alt="And alt text."}
22+
23+
24+
::: {#fig-2 .border}
25+
26+
![](https://placeholder.co/400)
27+
28+
A _figure_ with border.
29+
30+
:::
31+
32+
::: {#fig-3}
33+
34+
![](https://placeholder.co/400){.border}
35+
36+
An image with border again.
37+
38+
:::

0 commit comments

Comments
 (0)