Skip to content

Commit f66a331

Browse files
When a URL hash refers to a hidden element, it makes the element visible
1 parent e4931ea commit f66a331

File tree

1 file changed

+51
-10
lines changed

1 file changed

+51
-10
lines changed

src/librustdoc/html/static/main.js

+51-10
Original file line numberDiff line numberDiff line change
@@ -161,17 +161,18 @@ function getSearchElement() {
161161
return window.history && typeof window.history.pushState === "function";
162162
}
163163

164+
function isHidden(elem) {
165+
return elem.offsetHeight === 0;
166+
}
167+
164168
var main = document.getElementById("main");
169+
var savedHash = "";
165170

166-
function onHashChange(ev) {
167-
// If we're in mobile mode, we should hide the sidebar in any case.
168-
hideSidebar();
169-
var match = window.location.hash.match(/^#?(\d+)(?:-(\d+))?$/);
170-
if (match) {
171-
return highlightSourceLines(match, ev);
172-
}
171+
function handleHashes(ev) {
173172
var search = getSearchElement();
174173
if (ev !== null && search && !hasClass(search, "hidden") && ev.newURL) {
174+
// This block occurs when clicking on an element in the navbar while
175+
// in a search.
175176
addClass(search, "hidden");
176177
removeClass(main, "hidden");
177178
var hash = ev.newURL.slice(ev.newURL.indexOf("#") + 1);
@@ -183,6 +184,35 @@ function getSearchElement() {
183184
elem.scrollIntoView();
184185
}
185186
}
187+
// This part is used in case an element is not visible.
188+
if (savedHash !== window.location.hash) {
189+
savedHash = window.location.hash;
190+
if (savedHash.length === 0) {
191+
return;
192+
}
193+
var elem = document.getElementById(savedHash.slice(1)); // we remove the '#'
194+
if (!elem || !isHidden(elem)) {
195+
return;
196+
}
197+
var parent = elem.parentNode;
198+
if (parent && hasClass(parent, "impl-items")) {
199+
// In case this is a trait implementation item, we first need to toggle
200+
// the "Show hidden undocumented items".
201+
onEachLazy(parent.getElementsByClassName("collapsed"), function(e) {
202+
if (e.parentNode === parent) {
203+
// Only click on the toggle we're looking for.
204+
e.click();
205+
return true;
206+
}
207+
});
208+
if (isHidden(elem)) {
209+
// The whole parent is collapsed. We need to click on its toggle as well!
210+
if (hasClass(parent.lastElementChild, "collapse-toggle")) {
211+
parent.lastElementChild.click();
212+
}
213+
}
214+
}
215+
}
186216
}
187217

188218
function highlightSourceLines(match, ev) {
@@ -228,6 +258,16 @@ function getSearchElement() {
228258
}
229259
}
230260

261+
function onHashChange(ev) {
262+
// If we're in mobile mode, we should hide the sidebar in any case.
263+
hideSidebar();
264+
var match = window.location.hash.match(/^#?(\d+)(?:-(\d+))?$/);
265+
if (match) {
266+
return highlightSourceLines(match, ev);
267+
}
268+
handleHashes();
269+
}
270+
231271
function expandSection(id) {
232272
var elem = document.getElementById(id);
233273
if (elem && isHidden(elem)) {
@@ -246,9 +286,6 @@ function getSearchElement() {
246286
}
247287
}
248288

249-
highlightSourceLines();
250-
window.onhashchange = onHashChange;
251-
252289
// Gets the human-readable string for the virtual-key code of the
253290
// given KeyboardEvent, ev.
254291
//
@@ -2615,6 +2652,10 @@ function getSearchElement() {
26152652
insertAfter(popup, getSearchElement());
26162653
}
26172654

2655+
handleHashes();
2656+
highlightSourceLines();
2657+
window.onhashchange = onHashChange;
2658+
26182659
buildHelperPopup();
26192660
}());
26202661

0 commit comments

Comments
 (0)