From 917cdd295d2eed213c135d6f984c650f016ee3d6 Mon Sep 17 00:00:00 2001 From: Kazuyoshi Kato Date: Thu, 23 Aug 2018 00:43:06 -0700 Subject: [PATCH 1/3] Automatically expand a section even after page load Fixes #52774 --- src/librustdoc/html/static/main.js | 48 ++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js index 70782973e42..a5c93bc8230 100644 --- a/src/librustdoc/html/static/main.js +++ b/src/librustdoc/html/static/main.js @@ -223,8 +223,38 @@ } } } + + function expandSection() { + var hash = getPageId(); + if (hash === null) { + return; + } + + var elem = document.getElementById(hash); + if (elem && isHidden(elem.offsetParent)) { + var h3 = elem.parentNode.previousSibling; + + if (h3.tagName !== 'H3') { + h3 = h3.previousSibling; // skip div.docblock + } + + if (h3) { + var collapses = h3.getElementsByClassName("collapse-toggle"); + if (collapses.length > 0) { + // The element is not visible, we need to make it appear! + collapseDocs(collapses[0], "show"); + } + } + } + } + + function onHashChange(ev) { + highlightSourceLines(ev); + expandSection(); + } + highlightSourceLines(null); - window.onhashchange = highlightSourceLines; + window.onhashchange = onHashChange; // Gets the human-readable string for the virtual-key code of the // given KeyboardEvent, ev. @@ -2213,21 +2243,7 @@ autoCollapse(getPageId(), getCurrentValue("rustdoc-collapse") === "true"); if (window.location.hash && window.location.hash.length > 0) { - var hash = getPageId(); - if (hash !== null) { - var elem = document.getElementById(hash); - if (elem && elem.offsetParent === null) { - if (elem.parentNode && elem.parentNode.previousSibling) { - var collapses = elem.parentNode - .previousSibling - .getElementsByClassName("collapse-toggle"); - if (collapses.length > 0) { - // The element is not visible, we need to make it appear! - collapseDocs(collapses[0], "show"); - } - } - } - } + expandSection(); } }()); From 1f441a0905700e6fa5cad76097afba52a8775435 Mon Sep 17 00:00:00 2001 From: Kazuyoshi Kato Date: Thu, 23 Aug 2018 08:14:54 -0700 Subject: [PATCH 2/3] Check null-able variables before using them --- 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 a5c93bc8230..7c0f58f4ee6 100644 --- a/src/librustdoc/html/static/main.js +++ b/src/librustdoc/html/static/main.js @@ -231,10 +231,9 @@ } var elem = document.getElementById(hash); - if (elem && isHidden(elem.offsetParent)) { + if (elem && elem.offsetParent && isHidden(elem.offsetParent)) { var h3 = elem.parentNode.previousSibling; - - if (h3.tagName !== 'H3') { + if (h3 && h3.tagName !== 'H3') { h3 = h3.previousSibling; // skip div.docblock } From 2c61f3ce9e82fa5eb0cb780400b8d95cd5a76571 Mon Sep 17 00:00:00 2001 From: Kazuyoshi Kato Date: Sat, 25 Aug 2018 03:34:04 -0700 Subject: [PATCH 3/3] Expand a collapsed element on onclick Doing the expansion on onhashchange seems too late. Fixes #48726 --- src/librustdoc/html/static/main.js | 37 +++++++++++++++++------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js index 7c0f58f4ee6..e994c85c7ec 100644 --- a/src/librustdoc/html/static/main.js +++ b/src/librustdoc/html/static/main.js @@ -224,14 +224,9 @@ } } - function expandSection() { - var hash = getPageId(); - if (hash === null) { - return; - } - - var elem = document.getElementById(hash); - if (elem && elem.offsetParent && isHidden(elem.offsetParent)) { + function expandSection(id) { + var elem = document.getElementById(id); + if (elem && isHidden(elem)) { var h3 = elem.parentNode.previousSibling; if (h3 && h3.tagName !== 'H3') { h3 = h3.previousSibling; // skip div.docblock @@ -247,13 +242,7 @@ } } - function onHashChange(ev) { - highlightSourceLines(ev); - expandSection(); - } - - highlightSourceLines(null); - window.onhashchange = onHashChange; + window.onhashchange = highlightSourceLines; // Gets the human-readable string for the virtual-key code of the // given KeyboardEvent, ev. @@ -346,6 +335,15 @@ } } + function findParentElement(elem, tagName) { + do { + if (elem && elem.tagName === tagName) { + return elem; + } + } while (elem = elem.parentNode); + return null; + } + document.onkeypress = handleShortcut; document.onkeydown = handleShortcut; document.onclick = function(ev) { @@ -383,6 +381,13 @@ } else if (!hasClass(document.getElementById("help"), "hidden")) { addClass(document.getElementById("help"), "hidden"); removeClass(document.body, "blur"); + } else { + // Making a collapsed element visible on onhashchange seems + // too late + var a = findParentElement(ev.target, 'A'); + if (a && a.hash) { + expandSection(a.hash.replace(/^#/, '')); + } } }; @@ -2242,7 +2247,7 @@ autoCollapse(getPageId(), getCurrentValue("rustdoc-collapse") === "true"); if (window.location.hash && window.location.hash.length > 0) { - expandSection(); + expandSection(window.location.hash.replace(/^#/, '')); } }());