Skip to content

Fix navbar click while in a search #45812

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 11, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions src/librustdoc/html/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@
var start = elemClass.indexOf(className);
if (start == -1) {
return false;
} else if (elemClass.length == className.length) {
} else if (elemClass.length === className.length) {
return true;
} else {
if (start > 0 && elemClass[start - 1] != ' ') {
if (start > 0 && elemClass[start - 1] !== ' ') {
return false;
}
var end = start + className.length;
if (end < elemClass.length && elemClass[end] != ' ') {
if (end < elemClass.length && elemClass[end] !== ' ') {
return false;
}
return true;
Expand Down Expand Up @@ -122,6 +122,7 @@
}

function highlightSourceLines(ev) {
var search = document.getElementById("search");
var i, from, to, match = window.location.hash.match(/^#?(\d+)(?:-(\d+))?$/);
if (match) {
from = parseInt(match[1], 10);
Expand All @@ -145,6 +146,17 @@
for (i = from; i <= to; ++i) {
addClass(document.getElementById(i), 'line-highlighted');
}
} else if (ev !== null && search && !hasClass(search, "hidden") && ev.newURL) {
addClass(search, "hidden");
removeClass(document.getElementById("main"), "hidden");
var hash = ev.newURL.slice(ev.newURL.indexOf('#') + 1);
if (browserSupportsHistoryApi()) {
history.replaceState(hash, "", "?search=#" + hash);
}
var elem = document.getElementById(hash);
if (elem) {
elem.scrollIntoView();
}
}
}
highlightSourceLines(null);
Expand Down Expand Up @@ -1551,6 +1563,22 @@
});
}
});

var search_input = document.getElementsByClassName("search-input")[0];

if (search_input) {
search_input.onfocus = function() {
if (search_input.value !== "") {
addClass(document.getElementById("main"), "hidden");
removeClass(document.getElementById("search"), "hidden");
if (browserSupportsHistoryApi()) {
history.replaceState(search_input.value,
"",
"?search=" + encodeURIComponent(search_input.value));
}
}
};
}
}());

// Sets the focus on the search bar at the top of the page
Expand Down