From f66a331335f3ac931afabca6f927a9d7dc17db3e Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 5 Nov 2019 17:41:40 +0100 Subject: [PATCH 1/3] When a URL hash refers to a hidden element, it makes the element visible --- src/librustdoc/html/static/main.js | 61 +++++++++++++++++++++++++----- 1 file changed, 51 insertions(+), 10 deletions(-) diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js index 53e16978ff12b..9faddb8bc3f3f 100644 --- a/src/librustdoc/html/static/main.js +++ b/src/librustdoc/html/static/main.js @@ -161,17 +161,18 @@ function getSearchElement() { return window.history && typeof window.history.pushState === "function"; } + function isHidden(elem) { + return elem.offsetHeight === 0; + } + var main = document.getElementById("main"); + var savedHash = ""; - function onHashChange(ev) { - // If we're in mobile mode, we should hide the sidebar in any case. - hideSidebar(); - var match = window.location.hash.match(/^#?(\d+)(?:-(\d+))?$/); - if (match) { - return highlightSourceLines(match, ev); - } + function handleHashes(ev) { var search = getSearchElement(); if (ev !== null && search && !hasClass(search, "hidden") && ev.newURL) { + // This block occurs when clicking on an element in the navbar while + // in a search. addClass(search, "hidden"); removeClass(main, "hidden"); var hash = ev.newURL.slice(ev.newURL.indexOf("#") + 1); @@ -183,6 +184,35 @@ function getSearchElement() { elem.scrollIntoView(); } } + // This part is used in case an element is not visible. + if (savedHash !== window.location.hash) { + savedHash = window.location.hash; + if (savedHash.length === 0) { + return; + } + var elem = document.getElementById(savedHash.slice(1)); // we remove the '#' + if (!elem || !isHidden(elem)) { + return; + } + var parent = elem.parentNode; + if (parent && hasClass(parent, "impl-items")) { + // In case this is a trait implementation item, we first need to toggle + // the "Show hidden undocumented items". + onEachLazy(parent.getElementsByClassName("collapsed"), function(e) { + if (e.parentNode === parent) { + // Only click on the toggle we're looking for. + e.click(); + return true; + } + }); + if (isHidden(elem)) { + // The whole parent is collapsed. We need to click on its toggle as well! + if (hasClass(parent.lastElementChild, "collapse-toggle")) { + parent.lastElementChild.click(); + } + } + } + } } function highlightSourceLines(match, ev) { @@ -228,6 +258,16 @@ function getSearchElement() { } } + function onHashChange(ev) { + // If we're in mobile mode, we should hide the sidebar in any case. + hideSidebar(); + var match = window.location.hash.match(/^#?(\d+)(?:-(\d+))?$/); + if (match) { + return highlightSourceLines(match, ev); + } + handleHashes(); + } + function expandSection(id) { var elem = document.getElementById(id); if (elem && isHidden(elem)) { @@ -246,9 +286,6 @@ function getSearchElement() { } } - highlightSourceLines(); - window.onhashchange = onHashChange; - // Gets the human-readable string for the virtual-key code of the // given KeyboardEvent, ev. // @@ -2615,6 +2652,10 @@ function getSearchElement() { insertAfter(popup, getSearchElement()); } + handleHashes(); + highlightSourceLines(); + window.onhashchange = onHashChange; + buildHelperPopup(); }()); From 24e093c5c387f229d72329326f4474264475c0f4 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 7 Nov 2019 10:13:44 +0100 Subject: [PATCH 2/3] Remove old isHidden function --- src/librustdoc/html/static/main.js | 2 +- src/librustdoc/html/static/storage.js | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js index 9faddb8bc3f3f..c3667dcfe3df1 100644 --- a/src/librustdoc/html/static/main.js +++ b/src/librustdoc/html/static/main.js @@ -3,7 +3,7 @@ // Local js definitions: /* global addClass, getCurrentValue, hasClass */ -/* global isHidden, onEach, removeClass, updateLocalStorage */ +/* global onEach, removeClass, updateLocalStorage */ if (!String.prototype.startsWith) { String.prototype.startsWith = function(searchString, position) { diff --git a/src/librustdoc/html/static/storage.js b/src/librustdoc/html/static/storage.js index eae998ca3ecbf..d142d99ac704d 100644 --- a/src/librustdoc/html/static/storage.js +++ b/src/librustdoc/html/static/storage.js @@ -24,10 +24,6 @@ function removeClass(elem, className) { elem.classList.remove(className); } -function isHidden(elem) { - return elem.offsetParent === null; -} - function onEach(arr, func, reversed) { if (arr && arr.length > 0 && func) { var length = arr.length; From d4527b7e00de1e6ed5b22a3fdb99a4515f2ba482 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 7 Nov 2019 10:16:14 +0100 Subject: [PATCH 3/3] Only call onHashChange instead of both functions --- src/librustdoc/html/static/main.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js index c3667dcfe3df1..2200d19d86c91 100644 --- a/src/librustdoc/html/static/main.js +++ b/src/librustdoc/html/static/main.js @@ -265,7 +265,7 @@ function getSearchElement() { if (match) { return highlightSourceLines(match, ev); } - handleHashes(); + handleHashes(ev); } function expandSection(id) { @@ -2652,8 +2652,7 @@ function getSearchElement() { insertAfter(popup, getSearchElement()); } - handleHashes(); - highlightSourceLines(); + onHashChange(); window.onhashchange = onHashChange; buildHelperPopup();