Skip to content

Commit 9c20f06

Browse files
Rollup merge of rust-lang#53626 - kzys:hashchange, r=GuillaumeGomez
Automatically expand a section even after page load Fixes rust-lang#52774
2 parents e166e61 + 2c61f3c commit 9c20f06

File tree

1 file changed

+36
-16
lines changed

1 file changed

+36
-16
lines changed

src/librustdoc/html/static/main.js

+36-16
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,25 @@
223223
}
224224
}
225225
}
226-
highlightSourceLines(null);
226+
227+
function expandSection(id) {
228+
var elem = document.getElementById(id);
229+
if (elem && isHidden(elem)) {
230+
var h3 = elem.parentNode.previousSibling;
231+
if (h3 && h3.tagName !== 'H3') {
232+
h3 = h3.previousSibling; // skip div.docblock
233+
}
234+
235+
if (h3) {
236+
var collapses = h3.getElementsByClassName("collapse-toggle");
237+
if (collapses.length > 0) {
238+
// The element is not visible, we need to make it appear!
239+
collapseDocs(collapses[0], "show");
240+
}
241+
}
242+
}
243+
}
244+
227245
window.onhashchange = highlightSourceLines;
228246

229247
// Gets the human-readable string for the virtual-key code of the
@@ -317,6 +335,15 @@
317335
}
318336
}
319337

338+
function findParentElement(elem, tagName) {
339+
do {
340+
if (elem && elem.tagName === tagName) {
341+
return elem;
342+
}
343+
} while (elem = elem.parentNode);
344+
return null;
345+
}
346+
320347
document.onkeypress = handleShortcut;
321348
document.onkeydown = handleShortcut;
322349
document.onclick = function(ev) {
@@ -354,6 +381,13 @@
354381
} else if (!hasClass(document.getElementById("help"), "hidden")) {
355382
addClass(document.getElementById("help"), "hidden");
356383
removeClass(document.body, "blur");
384+
} else {
385+
// Making a collapsed element visible on onhashchange seems
386+
// too late
387+
var a = findParentElement(ev.target, 'A');
388+
if (a && a.hash) {
389+
expandSection(a.hash.replace(/^#/, ''));
390+
}
357391
}
358392
};
359393

@@ -2213,21 +2247,7 @@
22132247
autoCollapse(getPageId(), getCurrentValue("rustdoc-collapse") === "true");
22142248

22152249
if (window.location.hash && window.location.hash.length > 0) {
2216-
var hash = getPageId();
2217-
if (hash !== null) {
2218-
var elem = document.getElementById(hash);
2219-
if (elem && elem.offsetParent === null) {
2220-
if (elem.parentNode && elem.parentNode.previousSibling) {
2221-
var collapses = elem.parentNode
2222-
.previousSibling
2223-
.getElementsByClassName("collapse-toggle");
2224-
if (collapses.length > 0) {
2225-
// The element is not visible, we need to make it appear!
2226-
collapseDocs(collapses[0], "show");
2227-
}
2228-
}
2229-
}
2230-
}
2250+
expandSection(window.location.hash.replace(/^#/, ''));
22312251
}
22322252
}());
22332253

0 commit comments

Comments
 (0)