Skip to content

Commit

Permalink
Rollup merge of rust-lang#49652 - GuillaumeGomez:anchor-collapsed, r=…
Browse files Browse the repository at this point in the history
…QuietMisdreavus

Fix anchors issue when everything is collapsed

Fixes rust-lang#49455.

@frewsxcv does it seem good for you like this?

r? @QuietMisdreavus
  • Loading branch information
kennytm authored Apr 6, 2018
2 parents f2f770b + 238583a commit 5751c66
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
30 changes: 21 additions & 9 deletions src/librustdoc/html/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@
};
}

function getPageId() {
var id = document.location.href.split('#')[1];
if (id) {
return id.split('?')[0].split('&')[0];
}
return null;
}

function hasClass(elem, className) {
if (elem && className && elem.className) {
var elemClass = elem.className;
Expand Down Expand Up @@ -1643,7 +1651,7 @@
}
}

function toggleAllDocs() {
function toggleAllDocs(pageId) {
var toggle = document.getElementById("toggle-all-docs");
if (hasClass(toggle, "will-expand")) {
updateLocalStorage("rustdoc-collapse", "false");
Expand All @@ -1664,12 +1672,12 @@
toggle.title = "expand all docs";

onEach(document.getElementsByClassName("collapse-toggle"), function(e) {
collapseDocs(e, "hide");
collapseDocs(e, "hide", pageId);
});
}
}

function collapseDocs(toggle, mode) {
function collapseDocs(toggle, mode, pageId) {
if (!toggle || !toggle.parentNode) {
return;
}
Expand Down Expand Up @@ -1745,14 +1753,18 @@
}
}

var relatedDoc = toggle.parentNode;
var parentElem = toggle.parentNode;
var relatedDoc = parentElem;
var docblock = relatedDoc.nextElementSibling;

while (!hasClass(relatedDoc, "impl-items")) {
relatedDoc = relatedDoc.nextElementSibling;
}

if (!relatedDoc && !hasClass(docblock, "docblock")) {
if ((!relatedDoc && !hasClass(docblock, "docblock")) ||
(pageId && onEach(relatedDoc.childNodes, function(e) {
return e.id === pageId;
}) === true)) {
return;
}

Expand Down Expand Up @@ -1782,15 +1794,15 @@
}
}

function autoCollapseAllImpls() {
function autoCollapseAllImpls(pageId) {
// Automatically minimize all non-inherent impls
onEach(document.getElementsByClassName('impl'), function(n) {
// inherent impl ids are like 'impl' or impl-<number>'
var inherent = (n.id.match(/^impl(?:-\d+)?$/) !== null);
if (!inherent) {
onEach(n.childNodes, function(m) {
if (hasClass(m, "collapse-toggle")) {
collapseDocs(m, "hide");
collapseDocs(m, "hide", pageId);
}
});
}
Expand Down Expand Up @@ -1900,7 +1912,7 @@
}
})

autoCollapseAllImpls();
autoCollapseAllImpls(getPageId());

function createToggleWrapper() {
var span = document.createElement('span');
Expand Down Expand Up @@ -2030,7 +2042,7 @@
};

if (getCurrentValue("rustdoc-collapse") === "true") {
toggleAllDocs();
toggleAllDocs(getPageId());
}
}());

Expand Down
3 changes: 2 additions & 1 deletion src/librustdoc/html/static/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ function onEach(arr, func) {
if (arr && arr.length > 0 && func) {
for (var i = 0; i < arr.length; i++) {
if (func(arr[i]) === true) {
break;
return true;
}
}
}
return false;
}

function updateLocalStorage(name, value) {
Expand Down

0 comments on commit 5751c66

Please sign in to comment.