about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2018-08-26 12:05:25 +0200
committerGitHub <noreply@github.com>2018-08-26 12:05:25 +0200
commit9c20f06f738ad75fb8dbf5201ae0e3a23fbf4dcc (patch)
treeb89249f7ff17207d6a558968b32a8973275ec141
parente166e6131ae814086adf6ac6b5874d106d02ac3c (diff)
parent2c61f3ce9e82fa5eb0cb780400b8d95cd5a76571 (diff)
downloadrust-9c20f06f738ad75fb8dbf5201ae0e3a23fbf4dcc.tar.gz
rust-9c20f06f738ad75fb8dbf5201ae0e3a23fbf4dcc.zip
Rollup merge of #53626 - kzys:hashchange, r=GuillaumeGomez
Automatically expand a section even after page load

Fixes #52774
-rw-r--r--src/librustdoc/html/static/main.js52
1 files changed, 36 insertions, 16 deletions
diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js
index 5e78e8fe569..88c25567d29 100644
--- a/src/librustdoc/html/static/main.js
+++ b/src/librustdoc/html/static/main.js
@@ -223,7 +223,25 @@
             }
         }
     }
-    highlightSourceLines(null);
+
+    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
+            }
+
+            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");
+                }
+            }
+        }
+    }
+
     window.onhashchange = highlightSourceLines;
 
     // Gets the human-readable string for the virtual-key code of the
@@ -317,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) {
@@ -354,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(/^#/, ''));
+            }
         }
     };
 
@@ -2213,21 +2247,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(window.location.hash.replace(/^#/, ''));
     }
 }());