Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit aa604d0

Browse filesBrowse files
committed
Introduce getHideableCopyButtonElements()
1 parent 320a929 commit aa604d0
Copy full SHA for aa604d0

File tree

Expand file treeCollapse file tree

1 file changed

+37
-15
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+37
-15
lines changed

‎python_docs_theme/static/copybutton.js

Copy file name to clipboardExpand all lines: python_docs_theme/static/copybutton.js
+37-15Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,36 @@
1+
// ``function*`` denotes a generator in JavaScript, see
2+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*
3+
function* getHideableCopyButtonElements(rootElement) {
4+
// yield all elements with the "go" (Generic.Output),
5+
// "gp" (Generic.Prompt), or "gt" (Generic.Traceback) CSS class
6+
for (const el of rootElement.querySelectorAll('.go, .gp, .gt')) {
7+
yield el
8+
}
9+
// tracebacks (.gt) contain bare text elements that need to be
10+
// wrapped in a span to hide or show the element
11+
for (let el of rootElement.querySelectorAll('.gt')) {
12+
while ((el = el.nextSibling) && el.nodeType !== Node.DOCUMENT_NODE) {
13+
// stop wrapping text nodes when we hit the next output or
14+
// prompt element
15+
if (el.nodeType === Node.ELEMENT_NODE && el.matches(".gp, .go")) {
16+
break
17+
}
18+
// if the node is a text node with content, wrap it in a
19+
// span element so that we can control visibility
20+
if (el.nodeType === Node.TEXT_NODE && el.textContent.trim()) {
21+
const wrapper = document.createElement("span")
22+
el.after(wrapper)
23+
wrapper.appendChild(el)
24+
el = wrapper
25+
}
26+
yield el
27+
}
28+
}
29+
}
30+
131

232
const loadCopyButton = () => {
3-
/* Add a [>>>] button on the top-right corner of code samples to hide
33+
/* Add a [>>>] button in the top-right corner of code samples to hide
434
* the >>> and ... prompts and the output and thus make the code
535
* copyable. */
636
const hide_text = "Hide the prompts and output"
@@ -18,24 +48,16 @@ const loadCopyButton = () => {
1848
const codeEl = buttonEl.nextElementSibling
1949
if (buttonEl.dataset.hidden === "false") {
2050
// hide the code output
21-
codeEl.querySelectorAll('.go, .gp, .gt').forEach(el => el.hidden = true)
22-
codeEl.querySelectorAll('.gt').forEach(el => {
23-
while ((el = el.nextSibling) && el.nodeType !== Node.DOCUMENT_NODE) {
24-
if (el.nodeType === Node.ELEMENT_NODE && el.matches(".gp, .go")) break;
25-
if (el.nodeType === Node.TEXT_NODE && el.textContent.trim()) {
26-
const wrapper = document.createElement('span');
27-
el.after(wrapper);
28-
wrapper.appendChild(el);
29-
el = wrapper
30-
}
31-
el.hidden = true
32-
}
33-
})
51+
for (const el of getHideableCopyButtonElements(codeEl)) {
52+
el.hidden = true
53+
}
3454
buttonEl.title = show_text
3555
buttonEl.dataset.hidden = "true"
3656
} else {
3757
// show the code output
38-
codeEl.childNodes.forEach(el => el.hidden = false)
58+
for (const el of getHideableCopyButtonElements(codeEl)) {
59+
el.hidden = false
60+
}
3961
buttonEl.title = hide_text
4062
buttonEl.dataset.hidden = "false"
4163
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.