Skip to content

Commit 392ebad

Browse files
authored
Rollup merge of #66123 - GuillaumeGomez:no-more-hidden-elements, r=Dylan-DPC
No more hidden elements Fixes #66046. Follow-up of #66082. r? @kinnison
2 parents 996d94a + d4527b7 commit 392ebad

File tree

2 files changed

+51
-15
lines changed

2 files changed

+51
-15
lines changed

src/librustdoc/html/static/main.js

+51-11
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
// Local js definitions:
55
/* global addClass, getCurrentValue, hasClass */
6-
/* global isHidden, onEach, removeClass, updateLocalStorage */
6+
/* global onEach, removeClass, updateLocalStorage */
77

88
if (!String.prototype.startsWith) {
99
String.prototype.startsWith = function(searchString, position) {
@@ -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(ev);
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
//
@@ -2639,6 +2676,9 @@ function getSearchElement() {
26392676
insertAfter(popup, getSearchElement());
26402677
}
26412678

2679+
onHashChange();
2680+
window.onhashchange = onHashChange;
2681+
26422682
buildHelperPopup();
26432683
}());
26442684

src/librustdoc/html/static/storage.js

-4
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@ function removeClass(elem, className) {
2424
elem.classList.remove(className);
2525
}
2626

27-
function isHidden(elem) {
28-
return elem.offsetParent === null;
29-
}
30-
3127
function onEach(arr, func, reversed) {
3228
if (arr && arr.length > 0 && func) {
3329
var length = arr.length;

0 commit comments

Comments
 (0)