Skip to content

Commit a1cfb03

Browse files
authored
Merge pull request #11306 from quarto-dev/bugfix/issue-11303
Lua - fix conditional content for divs with repeated attributes
2 parents 3b8d57b + 8f0dedd commit a1cfb03

File tree

3 files changed

+57
-22
lines changed

3 files changed

+57
-22
lines changed

news/changelog-1.6.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ All changes included in 1.6:
2424
- ([#10858](https://github.com/quarto-dev/quarto-cli/issues/10858)): Don't crash in `gfm` when `content` of a `FloatRefTarget` is of type `Blocks`.
2525
- ([#10894](https://github.com/quarto-dev/quarto-cli/issues/10894)): Fix configuration of title and prefix in callouts for `html`, `revealjs`, `pdf`, and `typst`.
2626
- ([#10999](https://github.com/quarto-dev/quarto-cli/issues/10999)): New API entry point: `quarto.paths.rscript()` to resolve `Rscript` path in Lua filters and extensions consistently with Quarto itself.
27-
- ([#11124](https://github.com/quarto-dev/quarto-cli/pull/11124)): Sort keys when encoding tables as JSON
27+
- ([#11124](https://github.com/quarto-dev/quarto-cli/pull/11124)): Sort keys when encoding tables as JSON.
28+
- ([#11303](https://github.com/quarto-dev/quarto-cli/issues/11303)): Fix conditional content for divs with repeated attributes.
2829

2930
## `dashboard` Format
3031

src/resources/filters/customnodes/content-hidden.lua

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ _quarto.ast.add_handler({
7979
error("Ignoring invalid condition in conditional block: " .. v[1])
8080
-- luacov: enable
8181
else
82-
result.condition[v[1]] = v[2]
82+
if result.condition[v[1]] == nil then
83+
result.condition[v[1]] = pandoc.List({})
84+
end
85+
result.condition[v[1]]:insert(v[2])
8386
end
8487
end
8588

@@ -152,30 +155,46 @@ end
152155
-- "properties" here will come either from "conditions", in the case of a custom AST node
153156
-- or from the attributes of the element itself in the case of spans or codeblocks
154157
function propertiesMatch(properties, profiles)
155-
local match = true
156-
if properties[constants.kWhenMeta] ~= nil then
157-
local v = properties[constants.kWhenMeta]
158-
v = split(v, ".") or { v }
159-
local r = get_meta(v)
160-
match = match and (type(r) == "boolean" and r)
161-
end
162-
if properties[constants.kUnlessMeta] ~= nil then
163-
local v = properties[constants.kUnlessMeta]
164-
v = split(v, ".") or { v }
158+
local function check_meta(v)
159+
local v = split(v, ".") or { v }
165160
local r = get_meta(v)
166-
match = match and not (type(r) == "boolean" and r)
161+
return type(r) == "boolean" and r
167162
end
168-
if properties[constants.kWhenFormat] ~= nil then
169-
match = match and _quarto.format.isFormat(properties[constants.kWhenFormat])
163+
local function check_profile(value)
164+
return profiles:includes(value)
170165
end
171-
if properties[constants.kUnlessFormat] ~= nil then
172-
match = match and not _quarto.format.isFormat(properties[constants.kUnlessFormat])
173-
end
174-
if properties[constants.kWhenProfile] ~= nil then
175-
match = match and profiles:includes(properties[constants.kWhenProfile])
166+
local function check_property(key, f)
167+
local v = properties[key]
168+
if type(v) == "string" then
169+
return f(v)
170+
elseif type(v) == "table" then
171+
local r = false
172+
for _, value in ipairs(v) do
173+
r = r or f(value)
174+
end
175+
return r
176+
else
177+
-- luacov: disable
178+
error("Invalid value type for condition: " .. type(v))
179+
-- luacov: enable
180+
end
176181
end
177-
if properties[constants.kUnlessProfile] ~= nil then
178-
match = match and not profiles:includes(properties[constants.kUnlessProfile])
182+
local tests = {
183+
{ constants.kWhenMeta, check_meta, false },
184+
{ constants.kUnlessMeta, check_meta, true },
185+
{ constants.kWhenFormat, _quarto.format.isFormat, false },
186+
{ constants.kUnlessFormat, _quarto.format.isFormat, true },
187+
{ constants.kWhenProfile, check_profile, false },
188+
{ constants.kUnlessProfile, check_profile, true }
189+
}
190+
local match = true
191+
for _, test in ipairs(tests) do
192+
local key = test[1]
193+
local f = test[2]
194+
local invert = test[3]
195+
if properties[key] ~= nil then
196+
match = match and (invert ~= check_property(key, f))
197+
end
179198
end
180199
return match
181200
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
format: html
3+
_quarto:
4+
tests:
5+
html:
6+
ensureFileRegexMatches:
7+
- ["38ea19c4-c121-43c3-9f6c-632e938d9332"]
8+
- []
9+
---
10+
11+
::: {.content-visible when-format="html" when-format="pdf"}
12+
13+
38ea19c4-c121-43c3-9f6c-632e938d9332
14+
15+
:::

0 commit comments

Comments
 (0)