Skip to content

Commit 1c78af7

Browse files
clean highlightSourceLines code
1 parent cba9368 commit 1c78af7

File tree

1 file changed

+54
-42
lines changed

1 file changed

+54
-42
lines changed

src/librustdoc/html/static/main.js

+54-42
Original file line numberDiff line numberDiff line change
@@ -163,59 +163,71 @@ function getSearchElement() {
163163

164164
var main = document.getElementById("main");
165165

166-
function highlightSourceLines(ev) {
167-
// If we're in mobile mode, we should add the sidebar in any case.
166+
function onHashChange(ev) {
167+
// If we're in mobile mode, we should hide the sidebar in any case.
168168
hideSidebar();
169-
var elem;
170-
var search = getSearchElement();
171-
var i, from, to, match = window.location.hash.match(/^#?(\d+)(?:-(\d+))?$/);
169+
var match = window.location.hash.match(/^#?(\d+)(?:-(\d+))?$/);
172170
if (match) {
173-
from = parseInt(match[1], 10);
174-
to = from;
175-
if (typeof match[2] !== "undefined") {
176-
to = parseInt(match[2], 10);
177-
}
178-
if (to < from) {
179-
var tmp = to;
180-
to = from;
181-
from = tmp;
182-
}
183-
elem = document.getElementById(from);
184-
if (!elem) {
185-
return;
186-
}
187-
if (ev === null) {
188-
var x = document.getElementById(from);
189-
if (x) {
190-
x.scrollIntoView();
191-
}
192-
}
193-
onEachLazy(document.getElementsByClassName("line-numbers"), function(e) {
194-
onEachLazy(e.getElementsByTagName("span"), function(i_e) {
195-
removeClass(i_e, "line-highlighted");
196-
});
197-
});
198-
for (i = from; i <= to; ++i) {
199-
elem = document.getElementById(i);
200-
if (!elem) {
201-
break;
202-
}
203-
addClass(elem, "line-highlighted");
204-
}
205-
} else if (ev !== null && search && !hasClass(search, "hidden") && ev.newURL) {
171+
return highlightSourceLines(match, ev);
172+
}
173+
var search = getSearchElement();
174+
if (ev !== null && search && !hasClass(search, "hidden") && ev.newURL) {
206175
addClass(search, "hidden");
207176
removeClass(main, "hidden");
208177
var hash = ev.newURL.slice(ev.newURL.indexOf("#") + 1);
209178
if (browserSupportsHistoryApi()) {
210179
history.replaceState(hash, "", "?search=#" + hash);
211180
}
212-
elem = document.getElementById(hash);
181+
var elem = document.getElementById(hash);
213182
if (elem) {
214183
elem.scrollIntoView();
215184
}
216185
}
217186
}
218187

188+
function highlightSourceLines(match, ev) {
189+
if (typeof match === "undefined") {
190+
// If we're in mobile mode, we should hide the sidebar in any case.
191+
hideSidebar();
192+
match = window.location.hash.match(/^#?(\d+)(?:-(\d+))?$/);
193+
}
194+
if (!match) {
195+
return;
196+
}
197+
var from = parseInt(match[1], 10);
198+
var to = from;
199+
if (typeof match[2] !== "undefined") {
200+
to = parseInt(match[2], 10);
201+
}
202+
if (to < from) {
203+
var tmp = to;
204+
to = from;
205+
from = tmp;
206+
}
207+
var elem = document.getElementById(from);
208+
if (!elem) {
209+
return;
210+
}
211+
if (!ev) {
212+
var x = document.getElementById(from);
213+
if (x) {
214+
x.scrollIntoView();
215+
}
216+
}
217+
onEachLazy(document.getElementsByClassName("line-numbers"), function(e) {
218+
onEachLazy(e.getElementsByTagName("span"), function(i_e) {
219+
removeClass(i_e, "line-highlighted");
220+
});
221+
});
222+
for (var i = from; i <= to; ++i) {
223+
elem = document.getElementById(i);
224+
if (!elem) {
225+
break;
226+
}
227+
addClass(elem, "line-highlighted");
228+
}
229+
}
230+
219231
function expandSection(id) {
220232
var elem = document.getElementById(id);
221233
if (elem && isHidden(elem)) {
@@ -234,8 +246,8 @@ function getSearchElement() {
234246
}
235247
}
236248

237-
highlightSourceLines(null);
238-
window.onhashchange = highlightSourceLines;
249+
highlightSourceLines();
250+
window.onhashchange = onHashChange;
239251

240252
// Gets the human-readable string for the virtual-key code of the
241253
// given KeyboardEvent, ev.
@@ -358,7 +370,7 @@ function getSearchElement() {
358370
var set_fragment = function(name) {
359371
if (browserSupportsHistoryApi()) {
360372
history.replaceState(null, null, "#" + name);
361-
highlightSourceLines(null);
373+
highlightSourceLines();
362374
} else {
363375
location.replace("#" + name);
364376
}

0 commit comments

Comments
 (0)