Skip to content

Commit 1bfd6f8

Browse files
authored
Merge pull request #13320 from quarto-dev/bugfix/13196
code-copy - get source from the right place (#13196)
2 parents 3563618 + 6a8459b commit 1bfd6f8

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,14 @@
9696
}
9797
9898
const getTextToCopy = function(trigger) {
99-
const codeEl = trigger.previousElementSibling.cloneNode(true);
100-
for (const childEl of codeEl.children) {
101-
if (isCodeAnnotation(childEl)) {
102-
childEl.remove();
103-
}
99+
const outerScaffold = trigger.parentElement.cloneNode(true);
100+
const codeEl = outerScaffold.querySelector('code');
101+
for (const childEl of codeEl.children) {
102+
if (isCodeAnnotation(childEl)) {
103+
childEl.remove();
104104
}
105-
return codeEl.innerText;
105+
}
106+
return codeEl.innerText;
106107
}
107108
108109
const clipboard = new window.ClipboardJS('.code-copy-button:not([data-in-quarto-modal])', {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
format: html
3+
code-copy: true
4+
code-annotations: hover
5+
_quarto:
6+
tests:
7+
html:
8+
# we need to check two things:
9+
# 1. the parent of the button is the scaffold div
10+
# 2. the scaffold div contains the code element
11+
ensureHtmlElements:
12+
- - "div.code-copy-outer-scaffold > button.code-copy-button"
13+
- "div.code-copy-outer-scaffold code.sourceCode"
14+
- []
15+
---
16+
17+
```python
18+
# A commented line
19+
print("Hello, World!") # <1>
20+
```
21+
22+
1. This line prints "Hello, World!" to the console.

0 commit comments

Comments
 (0)