From 6a8459bc0f42ce9063f02af280a85a08462c15c3 Mon Sep 17 00:00:00 2001 From: Carlos Scheidegger Date: Tue, 2 Sep 2025 15:23:54 -0500 Subject: [PATCH] code-copy - get source from the right place (#13196) --- .../html/templates/quarto-html-after-body.ejs | 13 ++++++----- .../docs/smoke-all/2025/09/02/issue-13196.qmd | 22 +++++++++++++++++++ 2 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 tests/docs/smoke-all/2025/09/02/issue-13196.qmd diff --git a/src/resources/formats/html/templates/quarto-html-after-body.ejs b/src/resources/formats/html/templates/quarto-html-after-body.ejs index 761893582e7..55fade9537c 100644 --- a/src/resources/formats/html/templates/quarto-html-after-body.ejs +++ b/src/resources/formats/html/templates/quarto-html-after-body.ejs @@ -96,13 +96,14 @@ } const getTextToCopy = function(trigger) { - const codeEl = trigger.previousElementSibling.cloneNode(true); - for (const childEl of codeEl.children) { - if (isCodeAnnotation(childEl)) { - childEl.remove(); - } + const outerScaffold = trigger.parentElement.cloneNode(true); + const codeEl = outerScaffold.querySelector('code'); + for (const childEl of codeEl.children) { + if (isCodeAnnotation(childEl)) { + childEl.remove(); } - return codeEl.innerText; + } + return codeEl.innerText; } const clipboard = new window.ClipboardJS('.code-copy-button:not([data-in-quarto-modal])', { diff --git a/tests/docs/smoke-all/2025/09/02/issue-13196.qmd b/tests/docs/smoke-all/2025/09/02/issue-13196.qmd new file mode 100644 index 00000000000..559e0360bff --- /dev/null +++ b/tests/docs/smoke-all/2025/09/02/issue-13196.qmd @@ -0,0 +1,22 @@ +--- +format: html +code-copy: true +code-annotations: hover +_quarto: + tests: + html: + # we need to check two things: + # 1. the parent of the button is the scaffold div + # 2. the scaffold div contains the code element + ensureHtmlElements: + - - "div.code-copy-outer-scaffold > button.code-copy-button" + - "div.code-copy-outer-scaffold code.sourceCode" + - [] +--- + +```python +# A commented line +print("Hello, World!") # <1> +``` + +1. This line prints "Hello, World!" to the console.