Skip to content

Commit 4ac7416

Browse files
mcanouilcderv
andauthored
fix(html): make code-tools toggle work when code-copy is enabled (#14707)
* fix(html): make code-tools toggle work with code-copy enabled The code-copy scaffold added in 1.8.15 wraps each code block in a div.code-copy-outer-scaffold, which left the "Show All Code" and "Hide All Code" handler matching nothing: its selectors assumed .sourceCode was a direct child of <details> and of .cell, and it reached the <details> via .parentElement. Key the selectors off details.code-fold and div.sourceCode.cell-code instead, which the scaffold cannot displace. Apply the same selector to the server-side toggle detection, which until now only worked because that post-processor happens to run before the scaffold is inserted. * test(html): add Playwright regression test for code-tools toggle (#13583) The smoke fixture only asserts on rendered HTML, which is byte-identical before and after this fix (the render pipeline is untouched), so it passes on the buggy selectors too and cannot guard the behavior. This Playwright test exercises the actual client-side JS: it clicks Show All Code / Hide All Code and asserts every <details> opens/closes and the echo:false cell's source swaps hidden<->unhidden, with code-copy enabled. It fails on the pre-fix selectors (details never open) and passes on the fix. Uses an {ojs} cell so the fixture needs no Python/R kernel. --------- Co-authored-by: Christophe Dervieux <christophe.dervieux@gmail.com>
1 parent 839948c commit 4ac7416

6 files changed

Lines changed: 106 additions & 5 deletions

File tree

‎news/changelog-1.10.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ All changes included in 1.10:
22

33
## Regression fixes
44

5+
- ([#13583](https://github.com/quarto-dev/quarto-cli/issues/13583)): Fix "Show All Code" and "Hide All Code" in the `code-tools` menu doing nothing when `code-copy` is enabled. (author: @mcanouil)
56
- ([#14267](https://github.com/quarto-dev/quarto-cli/issues/14267)): Fix Windows paths with accented characters (e.g., `C:\Users\Sébastien\`) breaking dart-sass compilation.
67
- ([#14281](https://github.com/quarto-dev/quarto-cli/issues/14281)): Fix transient `.quarto_ipynb` files accumulating during `quarto preview` with Jupyter engine.
78
- ([#14298](https://github.com/quarto-dev/quarto-cli/issues/14298)): Fix `quarto preview` browse URL including output filename (e.g., `hello.html`) for single-file documents, breaking Posit Workbench proxied server access.

‎src/command/render/codetools.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ function resolveCodeTools(format: Format, doc: Document): CodeTools {
353353

354354
// if we have requested toggle, make sure there are things to toggle
355355
if (codeToolsResolved.toggle) {
356-
const codeDetails = doc.querySelector(".cell > details > .sourceCode");
356+
const codeDetails = doc.querySelector(".cell details.code-fold");
357357

358358
// we don't OJS hidden cells in this check, since when echo: false, we emit them hidden
359359
const codeHidden = doc.querySelector(

‎src/resources/formats/html/templates/quarto-html-after-body.ejs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,16 @@
149149
}
150150
function toggleCodeHandler(show) {
151151
return function(e) {
152-
const detailsSrc = window.document.querySelectorAll(".cell > details > .sourceCode");
153-
for (let i=0; i<detailsSrc.length; i++) {
154-
const details = detailsSrc[i].parentElement;
152+
const cellDetails = window.document.querySelectorAll(".cell details.code-fold");
153+
for (let i=0; i<cellDetails.length; i++) {
154+
const details = cellDetails[i];
155155
if (show) {
156156
details.open = true;
157157
} else {
158158
details.removeAttribute("open");
159159
}
160160
}
161-
const cellCodeDivs = window.document.querySelectorAll(".cell > .sourceCode");
161+
const cellCodeDivs = window.document.querySelectorAll(".cell div.sourceCode.cell-code");
162162
const fromCls = show ? "hidden" : "unhidden";
163163
const toCls = show ? "unhidden" : "hidden";
164164
for (let i=0; i<cellCodeDivs.length; i++) {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
title: Code tools toggle with code-copy
3+
format: html
4+
code-fold: true
5+
code-tools: true
6+
code-copy: true
7+
keep-hidden: true
8+
---
9+
10+
Regression test for #13583: with `code-copy` enabled (the default), each code
11+
block is wrapped in `div.code-copy-outer-scaffold`. The "Show All Code" /
12+
"Hide All Code" items in the `code-tools` menu must still open/close every
13+
folded `<details>` and reveal/hide the `echo: false` cell's source.
14+
15+
```{ojs}
16+
x = 5
17+
```
18+
19+
```{ojs}
20+
//| echo: false
21+
y = 10
22+
```
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
format: html
3+
engine: jupyter
4+
code-fold: true
5+
code-tools: true
6+
code-copy: true
7+
keep-hidden: true
8+
_quarto:
9+
tests:
10+
html:
11+
ensureHtmlElements:
12+
- - "details.code-fold" # folded cell survives the code-copy scaffold
13+
- "div.code-copy-outer-scaffold > div.sourceCode"
14+
- "div.sourceCode.cell-code.hidden" # echo: false cell
15+
- []
16+
---
17+
18+
With `code-copy` enabled (the default), each code block is wrapped in a
19+
`div.code-copy-outer-scaffold`, which broke the "Show All Code" / "Hide All
20+
Code" `code-tools` selectors that assumed `.sourceCode` was a direct child of
21+
`<details>` or `.cell` (issue #13583). This document checks the post-fix DOM:
22+
the folded cell keeps its `details.code-fold`, the scaffold wraps the
23+
`div.sourceCode`, and the `echo: false` cell (kept via `keep-hidden: true`)
24+
still carries `div.sourceCode.cell-code.hidden`.
25+
26+
```{python}
27+
print("visible, folded code")
28+
```
29+
30+
```{python}
31+
#| echo: false
32+
print("hidden code")
33+
```
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { expect, test } from "@playwright/test";
2+
import { getUrl } from "../src/utils";
3+
4+
// Regression test for #13583. With code-copy enabled (the default), each code
5+
// block is wrapped in div.code-copy-outer-scaffold, which used to break the
6+
// direct-child selectors the code-tools "Show All Code" / "Hide All Code"
7+
// handler relied on. The toggle must still open/close every folded <details>
8+
// and swap the hidden<->unhidden class on the echo:false cell's source.
9+
test("code-tools Show/Hide All Code toggles folded and hidden code with code-copy", async ({
10+
page,
11+
}) => {
12+
await page.goto(getUrl("html/code-tools-toggle.html"), {
13+
waitUntil: "load",
14+
});
15+
16+
const details = page.locator(".cell details.code-fold");
17+
const hiddenCode = page.locator("div.sourceCode.cell-code.hidden");
18+
const unhiddenCode = page.locator("div.sourceCode.cell-code.unhidden");
19+
20+
// Baseline: two folded cells, both collapsed; the echo:false cell's source
21+
// is emitted hidden (keep-hidden).
22+
await expect(details).toHaveCount(2);
23+
await expect(details.nth(0)).toHaveJSProperty("open", false);
24+
await expect(details.nth(1)).toHaveJSProperty("open", false);
25+
await expect(hiddenCode).toHaveCount(1);
26+
await expect(unhiddenCode).toHaveCount(0);
27+
28+
// Show All Code: every <details> opens, the hidden source becomes unhidden.
29+
await page.locator("#quarto-code-tools-menu").click();
30+
await page.locator("#quarto-show-all-code").click();
31+
32+
await expect(details.nth(0)).toHaveJSProperty("open", true);
33+
await expect(details.nth(1)).toHaveJSProperty("open", true);
34+
await expect(hiddenCode).toHaveCount(0);
35+
await expect(unhiddenCode).toHaveCount(1);
36+
37+
// Hide All Code: every <details> closes, the source goes back to hidden.
38+
await page.locator("#quarto-code-tools-menu").click();
39+
await page.locator("#quarto-hide-all-code").click();
40+
41+
await expect(details.nth(0)).toHaveJSProperty("open", false);
42+
await expect(details.nth(1)).toHaveJSProperty("open", false);
43+
await expect(hiddenCode).toHaveCount(1);
44+
await expect(unhiddenCode).toHaveCount(0);
45+
});

0 commit comments

Comments
 (0)