Skip to content

Commit a713867

Browse files
committed
Rustdoc: Clean Up Some JS
There are more possible optimizations left (cached length in loops) as well as some possible bugs (shadowed variables) to fix. This is mostly syntactic.
1 parent c3d60ab commit a713867

File tree

1 file changed

+57
-60
lines changed

1 file changed

+57
-60
lines changed

src/librustdoc/html/static/main.js

Lines changed: 57 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
(function() {
1515
"use strict";
16-
var resizeTimeout, interval;
1716

1817
// This mapping table should match the discriminants of
1918
// `rustdoc::html::item_type::ItemType` type in Rust.
@@ -64,7 +63,7 @@
6463
if ($('#' + from).length === 0) {
6564
return;
6665
}
67-
if (ev === null) $('#' + from)[0].scrollIntoView();
66+
if (ev === null) { $('#' + from)[0].scrollIntoView(); };
6867
$('.line-numbers span').removeClass('line-highlighted');
6968
for (i = from; i <= to; ++i) {
7069
$('#' + i).addClass('line-highlighted');
@@ -74,7 +73,7 @@
7473
highlightSourceLines(null);
7574
$(window).on('hashchange', highlightSourceLines);
7675

77-
$(document).on('keyup', function(e) {
76+
$(document).on('keyup', function handleKeyboardShortcut(e) {
7877
if (document.activeElement.tagName === 'INPUT') {
7978
return;
8079
}
@@ -133,29 +132,28 @@
133132
return function(s1, s2) {
134133
if (s1 === s2) {
135134
return 0;
136-
} else {
137-
var s1_len = s1.length, s2_len = s2.length;
138-
if (s1_len && s2_len) {
139-
var i1 = 0, i2 = 0, a, b, c, c2, row = row2;
140-
while (i1 < s1_len)
141-
row[i1] = ++i1;
142-
while (i2 < s2_len) {
143-
c2 = s2.charCodeAt(i2);
144-
a = i2;
145-
++i2;
146-
b = i2;
147-
for (i1 = 0; i1 < s1_len; ++i1) {
148-
c = a + (s1.charCodeAt(i1) !== c2 ? 1 : 0);
149-
a = row[i1];
150-
b = b < a ? (b < c ? b + 1 : c) : (a < c ? a + 1 : c);
151-
row[i1] = b;
152-
}
135+
}
136+
var s1_len = s1.length, s2_len = s2.length;
137+
if (s1_len && s2_len) {
138+
var i1 = 0, i2 = 0, a, b, c, c2, row = row2;
139+
while (i1 < s1_len) {
140+
row[i1] = ++i1;
141+
}
142+
while (i2 < s2_len) {
143+
c2 = s2.charCodeAt(i2);
144+
a = i2;
145+
++i2;
146+
b = i2;
147+
for (i1 = 0; i1 < s1_len; ++i1) {
148+
c = a + (s1.charCodeAt(i1) !== c2 ? 1 : 0);
149+
a = row[i1];
150+
b = b < a ? (b < c ? b + 1 : c) : (a < c ? a + 1 : c);
151+
row[i1] = b;
153152
}
154-
return b;
155-
} else {
156-
return s1_len + s2_len;
157153
}
154+
return b;
158155
}
156+
return s1_len + s2_len;
159157
};
160158
})();
161159

@@ -187,7 +185,7 @@
187185
results = [],
188186
split = valLower.split("::");
189187

190-
//remove empty keywords
188+
// remove empty keywords
191189
for (var j = 0; j < split.length; ++j) {
192190
split[j].toLowerCase();
193191
if (split[j] === "") {
@@ -286,58 +284,59 @@
286284
return [];
287285
}
288286

289-
results.sort(function(aaa, bbb) {
287+
results.sort(function sortResults(aaa, bbb) {
290288
var a, b;
291289

292290
// Sort by non levenshtein results and then levenshtein results by the distance
293291
// (less changes required to match means higher rankings)
294292
a = (aaa.lev);
295293
b = (bbb.lev);
296-
if (a !== b) return a - b;
294+
if (a !== b) { return a - b; }
297295

298296
// sort by crate (non-current crate goes later)
299297
a = (aaa.item.crate !== window.currentCrate);
300298
b = (bbb.item.crate !== window.currentCrate);
301-
if (a !== b) return a - b;
299+
if (a !== b) { return a - b; }
302300

303301
// sort by exact match (mismatch goes later)
304302
a = (aaa.word !== valLower);
305303
b = (bbb.word !== valLower);
306-
if (a !== b) return a - b;
304+
if (a !== b) { return a - b; }
307305

308306
// sort by item name length (longer goes later)
309307
a = aaa.word.length;
310308
b = bbb.word.length;
311-
if (a !== b) return a - b;
309+
if (a !== b) { return a - b; }
312310

313311
// sort by item name (lexicographically larger goes later)
314312
a = aaa.word;
315313
b = bbb.word;
316-
if (a !== b) return (a > b ? +1 : -1);
314+
if (a !== b) { return (a > b ? +1 : -1); }
317315

318316
// sort by index of keyword in item name (no literal occurrence goes later)
319317
a = (aaa.index < 0);
320318
b = (bbb.index < 0);
321-
if (a !== b) return a - b;
319+
if (a !== b) { return a - b; }
322320
// (later literal occurrence, if any, goes later)
323321
a = aaa.index;
324322
b = bbb.index;
325-
if (a !== b) return a - b;
323+
if (a !== b) { return a - b; }
324+
326325

327326
// sort by description (no description goes later)
328327
a = (aaa.item.desc === '');
329328
b = (bbb.item.desc === '');
330-
if (a !== b) return a - b;
329+
if (a !== b) { return a - b; }
331330

332331
// sort by type (later occurrence in `itemTypes` goes later)
333332
a = aaa.item.ty;
334333
b = bbb.item.ty;
335-
if (a !== b) return a - b;
334+
if (a !== b) { return a - b; }
336335

337336
// sort by path (lexicographically larger goes later)
338337
a = aaa.item.path;
339338
b = bbb.item.path;
340-
if (a !== b) return (a > b ? +1 : -1);
339+
if (a !== b) { return (a > b ? +1 : -1); }
341340

342341
// que sera, sera
343342
return 0;
@@ -388,7 +387,7 @@
388387
* @return {[boolean]} [Whether the result is valid or not]
389388
*/
390389
function validateResult(name, path, keys, parent) {
391-
for (var i=0; i < keys.length; ++i) {
390+
for (var i = 0; i < keys.length; ++i) {
392391
// each check is for validation so we negate the conditions and invalidate
393392
if (!(
394393
// check for an exact name match
@@ -423,7 +422,7 @@
423422
raw: raw,
424423
query: query,
425424
type: type,
426-
id: query + type,
425+
id: query + type
427426
};
428427
}
429428

@@ -432,7 +431,7 @@
432431

433432
$results.on('click', function() {
434433
var dst = $(this).find('a')[0];
435-
if (window.location.pathname == dst.pathname) {
434+
if (window.location.pathname === dst.pathname) {
436435
$('#search').addClass('hidden');
437436
$('#main').removeClass('hidden');
438437
document.location.href = dst.href;
@@ -595,7 +594,7 @@
595594

596595
function itemTypeFromName(typename) {
597596
for (var i = 0; i < itemTypes.length; ++i) {
598-
if (itemTypes[i] === typename) return i;
597+
if (itemTypes[i] === typename) { return i; }
599598
}
600599
return -1;
601600
}
@@ -604,7 +603,7 @@
604603
searchIndex = [];
605604
var searchWords = [];
606605
for (var crate in rawSearchIndex) {
607-
if (!rawSearchIndex.hasOwnProperty(crate)) { continue }
606+
if (!rawSearchIndex.hasOwnProperty(crate)) { continue; }
608607

609608
// an array of [(Number) item type,
610609
// (String) name,
@@ -690,32 +689,31 @@
690689
}
691690

692691
function plainSummaryLine(markdown) {
693-
var str = markdown.replace(/\n/g, ' ')
694-
str = str.replace(/'/g, "\'")
695-
str = str.replace(/^#+? (.+?)/, "$1")
696-
str = str.replace(/\[(.*?)\]\(.*?\)/g, "$1")
697-
str = str.replace(/\[(.*?)\]\[.*?\]/g, "$1")
698-
return str;
692+
markdown.replace(/\n/g, ' ')
693+
.replace(/'/g, "\'")
694+
.replace(/^#+? (.+?)/, "$1")
695+
.replace(/\[(.*?)\]\(.*?\)/g, "$1")
696+
.replace(/\[(.*?)\]\[.*?\]/g, "$1");
699697
}
700698

701699
index = buildIndex(rawSearchIndex);
702700
startSearch();
703701

704702
// Draw a convenient sidebar of known crates if we have a listing
705-
if (rootPath == '../') {
703+
if (rootPath === '../') {
706704
var sidebar = $('.sidebar');
707705
var div = $('<div>').attr('class', 'block crate');
708706
div.append($('<h2>').text('Crates'));
709707

710708
var crates = [];
711709
for (var crate in rawSearchIndex) {
712-
if (!rawSearchIndex.hasOwnProperty(crate)) { continue }
710+
if (!rawSearchIndex.hasOwnProperty(crate)) { continue; }
713711
crates.push(crate);
714712
}
715713
crates.sort();
716714
for (var i = 0; i < crates.length; ++i) {
717715
var klass = 'crate';
718-
if (crates[i] == window.currentCrate) {
716+
if (crates[i] === window.currentCrate) {
719717
klass += ' current';
720718
}
721719
if (rawSearchIndex[crates[i]].items[0]) {
@@ -738,7 +736,7 @@
738736

739737
function block(shortty, longty) {
740738
var filtered = items[shortty];
741-
if (!filtered) return;
739+
if (!filtered) { return; }
742740

743741
var div = $('<div>').attr('class', 'block ' + shortty);
744742
div.append($('<h2>').text(longty));
@@ -749,7 +747,7 @@
749747
var desc = item[1]; // can be null
750748

751749
var klass = shortty;
752-
if (name === current.name && shortty == current.ty) {
750+
if (name === current.name && shortty === current.ty) {
753751
klass += ' current';
754752
}
755753
var path;
@@ -779,7 +777,7 @@
779777
var list = $('#implementors-list');
780778
var libs = Object.getOwnPropertyNames(imp);
781779
for (var i = 0; i < libs.length; ++i) {
782-
if (libs[i] == currentCrate) continue;
780+
if (libs[i] === currentCrate) { continue; }
783781
var structs = imp[libs[i]];
784782
for (var j = 0; j < structs.length; ++j) {
785783
var code = $('<code>').append(structs[j]);
@@ -811,11 +809,10 @@
811809
if (sectionIsCollapsed) {
812810
// button will expand the section
813811
return "+";
814-
} else {
815-
// button will collapse the section
816-
// note that this text is also set in the HTML template in render.rs
817-
return "\u2212"; // "\u2212" is '−' minus sign
818812
}
813+
// button will collapse the section
814+
// note that this text is also set in the HTML template in render.rs
815+
return "\u2212"; // "\u2212" is '−' minus sign
819816
}
820817

821818
$("#toggle-all-docs").on("click", function() {
@@ -847,12 +844,12 @@
847844
}
848845
if (relatedDoc.is(".docblock")) {
849846
if (relatedDoc.is(":visible")) {
850-
relatedDoc.slideUp({duration:'fast', easing:'linear'});
847+
relatedDoc.slideUp({duration: 'fast', easing: 'linear'});
851848
toggle.parent(".toggle-wrapper").addClass("collapsed");
852849
toggle.children(".inner").text(labelForToggleButton(true));
853850
toggle.children(".toggle-label").fadeIn();
854851
} else {
855-
relatedDoc.slideDown({duration:'fast', easing:'linear'});
852+
relatedDoc.slideDown({duration: 'fast', easing: 'linear'});
856853
toggle.parent(".toggle-wrapper").removeClass("collapsed");
857854
toggle.children(".inner").text(labelForToggleButton(false));
858855
toggle.children(".toggle-label").hide();
@@ -877,7 +874,7 @@
877874
$('<span/>', {'class': 'toggle-label'})
878875
.css('display', 'none')
879876
.html('&nbsp;Expand&nbsp;description'));
880-
var wrapper = $("<div class='toggle-wrapper'>").append(mainToggle);
877+
var wrapper = $("<div class='toggle-wrapper'>").append(mainToggle);
881878
$("#main > .docblock").before(wrapper);
882879
});
883880

@@ -894,7 +891,7 @@
894891
}
895892

896893
return function(ev) {
897-
var cur_id = parseInt(ev.target.id);
894+
var cur_id = parseInt(ev.target.id, 10);
898895

899896
if (ev.shiftKey && prev_id) {
900897
if (prev_id > cur_id) {

0 commit comments

Comments
 (0)