Skip to content

Commit 2c61f3c

Browse files
committed
Expand a collapsed element on onclick
Doing the expansion on onhashchange seems too late. Fixes #48726
1 parent 1f441a0 commit 2c61f3c

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

src/librustdoc/html/static/main.js

+21-16
Original file line numberDiff line numberDiff line change
@@ -224,14 +224,9 @@
224224
}
225225
}
226226

227-
function expandSection() {
228-
var hash = getPageId();
229-
if (hash === null) {
230-
return;
231-
}
232-
233-
var elem = document.getElementById(hash);
234-
if (elem && elem.offsetParent && isHidden(elem.offsetParent)) {
227+
function expandSection(id) {
228+
var elem = document.getElementById(id);
229+
if (elem && isHidden(elem)) {
235230
var h3 = elem.parentNode.previousSibling;
236231
if (h3 && h3.tagName !== 'H3') {
237232
h3 = h3.previousSibling; // skip div.docblock
@@ -247,13 +242,7 @@
247242
}
248243
}
249244

250-
function onHashChange(ev) {
251-
highlightSourceLines(ev);
252-
expandSection();
253-
}
254-
255-
highlightSourceLines(null);
256-
window.onhashchange = onHashChange;
245+
window.onhashchange = highlightSourceLines;
257246

258247
// Gets the human-readable string for the virtual-key code of the
259248
// given KeyboardEvent, ev.
@@ -346,6 +335,15 @@
346335
}
347336
}
348337

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+
349347
document.onkeypress = handleShortcut;
350348
document.onkeydown = handleShortcut;
351349
document.onclick = function(ev) {
@@ -383,6 +381,13 @@
383381
} else if (!hasClass(document.getElementById("help"), "hidden")) {
384382
addClass(document.getElementById("help"), "hidden");
385383
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+
}
386391
}
387392
};
388393

@@ -2242,7 +2247,7 @@
22422247
autoCollapse(getPageId(), getCurrentValue("rustdoc-collapse") === "true");
22432248

22442249
if (window.location.hash && window.location.hash.length > 0) {
2245-
expandSection();
2250+
expandSection(window.location.hash.replace(/^#/, ''));
22462251
}
22472252
}());
22482253

0 commit comments

Comments
 (0)