Skip to content

Commit fec24ad

Browse files
Rollup merge of #45812 - GuillaumeGomez:links-and-search, r=QuietMisdreavus
Fix navbar click while in a search Fixes #45790.
2 parents cb71247 + 0d89899 commit fec24ad

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

src/librustdoc/html/static/main.js

+31-3
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@
5252
var start = elemClass.indexOf(className);
5353
if (start == -1) {
5454
return false;
55-
} else if (elemClass.length == className.length) {
55+
} else if (elemClass.length === className.length) {
5656
return true;
5757
} else {
58-
if (start > 0 && elemClass[start - 1] != ' ') {
58+
if (start > 0 && elemClass[start - 1] !== ' ') {
5959
return false;
6060
}
6161
var end = start + className.length;
62-
if (end < elemClass.length && elemClass[end] != ' ') {
62+
if (end < elemClass.length && elemClass[end] !== ' ') {
6363
return false;
6464
}
6565
return true;
@@ -122,6 +122,7 @@
122122
}
123123

124124
function highlightSourceLines(ev) {
125+
var search = document.getElementById("search");
125126
var i, from, to, match = window.location.hash.match(/^#?(\d+)(?:-(\d+))?$/);
126127
if (match) {
127128
from = parseInt(match[1], 10);
@@ -145,6 +146,17 @@
145146
for (i = from; i <= to; ++i) {
146147
addClass(document.getElementById(i), 'line-highlighted');
147148
}
149+
} else if (ev !== null && search && !hasClass(search, "hidden") && ev.newURL) {
150+
addClass(search, "hidden");
151+
removeClass(document.getElementById("main"), "hidden");
152+
var hash = ev.newURL.slice(ev.newURL.indexOf('#') + 1);
153+
if (browserSupportsHistoryApi()) {
154+
history.replaceState(hash, "", "?search=#" + hash);
155+
}
156+
var elem = document.getElementById(hash);
157+
if (elem) {
158+
elem.scrollIntoView();
159+
}
148160
}
149161
}
150162
highlightSourceLines(null);
@@ -1552,6 +1564,22 @@
15521564
});
15531565
}
15541566
});
1567+
1568+
var search_input = document.getElementsByClassName("search-input")[0];
1569+
1570+
if (search_input) {
1571+
search_input.onfocus = function() {
1572+
if (search_input.value !== "") {
1573+
addClass(document.getElementById("main"), "hidden");
1574+
removeClass(document.getElementById("search"), "hidden");
1575+
if (browserSupportsHistoryApi()) {
1576+
history.replaceState(search_input.value,
1577+
"",
1578+
"?search=" + encodeURIComponent(search_input.value));
1579+
}
1580+
}
1581+
};
1582+
}
15551583
}());
15561584

15571585
// Sets the focus on the search bar at the top of the page

0 commit comments

Comments
 (0)