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 4df1954

Browse filesBrowse files
AA-Turnerhugovk
authored andcommitted
Refactor to getHideableCopyButtonElements
1 parent 7ee524e commit 4df1954
Copy full SHA for 4df1954

File tree

Expand file treeCollapse file tree

1 file changed

+34
-18
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+34
-18
lines changed

‎python_docs_theme/static/copybutton.js

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

230
const loadCopyButton = () => {
331
/* Add a [>>>] button in the top-right corner of code samples to hide
@@ -18,28 +46,16 @@ const loadCopyButton = () => {
1846
const codeEl = buttonEl.nextElementSibling
1947
if (buttonEl.dataset.hidden === 'false') {
2048
// hide the code output
21-
codeEl.querySelectorAll('.go, .gp, .gt').forEach(el => el.hidden = true)
22-
// tracebacks (.gt) contain bare text elements that need to be
23-
// wrapped in a span to hide or show the element
24-
codeEl.querySelectorAll('.gt').forEach(el => {
25-
while ((el = el.nextSibling) && el.nodeType !== Node.DOCUMENT_NODE) {
26-
if (el.nodeType === Node.ELEMENT_NODE && el.matches(".gp, .go")) {
27-
break
28-
}
29-
if (el.nodeType === Node.TEXT_NODE && el.textContent.trim()) {
30-
const wrapper = document.createElement('span')
31-
el.after(wrapper)
32-
wrapper.appendChild(el)
33-
el = wrapper
34-
}
35-
el.hidden = true
36-
}
37-
})
49+
for (const el of getHideableCopyButtonElements(codeEl)) {
50+
el.hidden = true
51+
}
3852
buttonEl.title = show_text
3953
buttonEl.dataset.hidden = "true"
4054
} else {
4155
// show the code output
42-
codeEl.childNodes.forEach(el => el.hidden = false)
56+
for (const el of getHideableCopyButtonElements(codeEl)) {
57+
el.hidden = false
58+
}
4359
buttonEl.title = hide_text
4460
buttonEl.dataset.hidden = "false"
4561
}

0 commit comments

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