From 8f0dedd501d9b824689e6753d37090e23c1e4539 Mon Sep 17 00:00:00 2001 From: Carlos Scheidegger Date: Mon, 4 Nov 2024 13:39:40 -0700 Subject: [PATCH] lua - fix conditional content for divs with repeated attributes --- news/changelog-1.6.md | 3 +- .../filters/customnodes/content-hidden.lua | 61 ++++++++++++------- .../docs/smoke-all/2024/11/04/issue-11303.qmd | 15 +++++ 3 files changed, 57 insertions(+), 22 deletions(-) create mode 100644 tests/docs/smoke-all/2024/11/04/issue-11303.qmd diff --git a/news/changelog-1.6.md b/news/changelog-1.6.md index 63375c9565..46d48ab15d 100644 --- a/news/changelog-1.6.md +++ b/news/changelog-1.6.md @@ -24,7 +24,8 @@ All changes included in 1.6: - ([#10858](https://github.com/quarto-dev/quarto-cli/issues/10858)): Don't crash in `gfm` when `content` of a `FloatRefTarget` is of type `Blocks`. - ([#10894](https://github.com/quarto-dev/quarto-cli/issues/10894)): Fix configuration of title and prefix in callouts for `html`, `revealjs`, `pdf`, and `typst`. - ([#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. -- ([#11124](https://github.com/quarto-dev/quarto-cli/pull/11124)): Sort keys when encoding tables as JSON +- ([#11124](https://github.com/quarto-dev/quarto-cli/pull/11124)): Sort keys when encoding tables as JSON. +- ([#11303](https://github.com/quarto-dev/quarto-cli/issues/11303)): Fix conditional content for divs with repeated attributes. ## `dashboard` Format diff --git a/src/resources/filters/customnodes/content-hidden.lua b/src/resources/filters/customnodes/content-hidden.lua index a94ba91153..f27af5a541 100644 --- a/src/resources/filters/customnodes/content-hidden.lua +++ b/src/resources/filters/customnodes/content-hidden.lua @@ -79,7 +79,10 @@ _quarto.ast.add_handler({ error("Ignoring invalid condition in conditional block: " .. v[1]) -- luacov: enable else - result.condition[v[1]] = v[2] + if result.condition[v[1]] == nil then + result.condition[v[1]] = pandoc.List({}) + end + result.condition[v[1]]:insert(v[2]) end end @@ -152,30 +155,46 @@ end -- "properties" here will come either from "conditions", in the case of a custom AST node -- or from the attributes of the element itself in the case of spans or codeblocks function propertiesMatch(properties, profiles) - local match = true - if properties[constants.kWhenMeta] ~= nil then - local v = properties[constants.kWhenMeta] - v = split(v, ".") or { v } - local r = get_meta(v) - match = match and (type(r) == "boolean" and r) - end - if properties[constants.kUnlessMeta] ~= nil then - local v = properties[constants.kUnlessMeta] - v = split(v, ".") or { v } + local function check_meta(v) + local v = split(v, ".") or { v } local r = get_meta(v) - match = match and not (type(r) == "boolean" and r) + return type(r) == "boolean" and r end - if properties[constants.kWhenFormat] ~= nil then - match = match and _quarto.format.isFormat(properties[constants.kWhenFormat]) + local function check_profile(value) + return profiles:includes(value) end - if properties[constants.kUnlessFormat] ~= nil then - match = match and not _quarto.format.isFormat(properties[constants.kUnlessFormat]) - end - if properties[constants.kWhenProfile] ~= nil then - match = match and profiles:includes(properties[constants.kWhenProfile]) + local function check_property(key, f) + local v = properties[key] + if type(v) == "string" then + return f(v) + elseif type(v) == "table" then + local r = false + for _, value in ipairs(v) do + r = r or f(value) + end + return r + else + -- luacov: disable + error("Invalid value type for condition: " .. type(v)) + -- luacov: enable + end end - if properties[constants.kUnlessProfile] ~= nil then - match = match and not profiles:includes(properties[constants.kUnlessProfile]) + local tests = { + { constants.kWhenMeta, check_meta, false }, + { constants.kUnlessMeta, check_meta, true }, + { constants.kWhenFormat, _quarto.format.isFormat, false }, + { constants.kUnlessFormat, _quarto.format.isFormat, true }, + { constants.kWhenProfile, check_profile, false }, + { constants.kUnlessProfile, check_profile, true } + } + local match = true + for _, test in ipairs(tests) do + local key = test[1] + local f = test[2] + local invert = test[3] + if properties[key] ~= nil then + match = match and (invert ~= check_property(key, f)) + end end return match end diff --git a/tests/docs/smoke-all/2024/11/04/issue-11303.qmd b/tests/docs/smoke-all/2024/11/04/issue-11303.qmd new file mode 100644 index 0000000000..444f6ae65b --- /dev/null +++ b/tests/docs/smoke-all/2024/11/04/issue-11303.qmd @@ -0,0 +1,15 @@ +--- +format: html +_quarto: + tests: + html: + ensureFileRegexMatches: + - ["38ea19c4-c121-43c3-9f6c-632e938d9332"] + - [] +--- + +::: {.content-visible when-format="html" when-format="pdf"} + +38ea19c4-c121-43c3-9f6c-632e938d9332 + +::: \ No newline at end of file