]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/html/static/main.js
Add test for eval order for a+=b
[rust.git] / src / librustdoc / html / static / main.js
1 // From rust:
2 /* global ALIASES, currentCrate, rootPath */
3
4 // Local js definitions:
5 /* global addClass, getCurrentValue, hasClass */
6 /* global onEachLazy, hasOwnProperty, removeClass, updateLocalStorage */
7 /* global hideThemeButtonState, showThemeButtonState */
8
9 if (!String.prototype.startsWith) {
10     String.prototype.startsWith = function(searchString, position) {
11         position = position || 0;
12         return this.indexOf(searchString, position) === position;
13     };
14 }
15 if (!String.prototype.endsWith) {
16     String.prototype.endsWith = function(suffix, length) {
17         var l = length || this.length;
18         return this.indexOf(suffix, l - suffix.length) !== -1;
19     };
20 }
21
22 if (!DOMTokenList.prototype.add) {
23     DOMTokenList.prototype.add = function(className) {
24         if (className && !hasClass(this, className)) {
25             if (this.className && this.className.length > 0) {
26                 this.className += " " + className;
27             } else {
28                 this.className = className;
29             }
30         }
31     };
32 }
33
34 if (!DOMTokenList.prototype.remove) {
35     DOMTokenList.prototype.remove = function(className) {
36         if (className && this.className) {
37             this.className = (" " + this.className + " ").replace(" " + className + " ", " ")
38                                                          .trim();
39         }
40     };
41 }
42
43 function getSearchInput() {
44     return document.getElementsByClassName("search-input")[0];
45 }
46
47 function getSearchElement() {
48     return document.getElementById("search");
49 }
50
51 function getThemesElement() {
52     return document.getElementById("theme-choices");
53 }
54
55 function getThemePickerElement() {
56     return document.getElementById("theme-picker");
57 }
58
59 // Sets the focus on the search bar at the top of the page
60 function focusSearchBar() {
61     getSearchInput().focus();
62 }
63
64 // Removes the focus from the search bar
65 function defocusSearchBar() {
66     getSearchInput().blur();
67 }
68
69 (function() {
70     "use strict";
71
72     // This mapping table should match the discriminants of
73     // `rustdoc::html::item_type::ItemType` type in Rust.
74     var itemTypes = ["mod",
75                      "externcrate",
76                      "import",
77                      "struct",
78                      "enum",
79                      "fn",
80                      "type",
81                      "static",
82                      "trait",
83                      "impl",
84                      "tymethod",
85                      "method",
86                      "structfield",
87                      "variant",
88                      "macro",
89                      "primitive",
90                      "associatedtype",
91                      "constant",
92                      "associatedconstant",
93                      "union",
94                      "foreigntype",
95                      "keyword",
96                      "existential",
97                      "attr",
98                      "derive",
99                      "traitalias"];
100
101     var disableShortcuts = getSettingValue("disable-shortcuts") === "true";
102     var search_input = getSearchInput();
103     var searchTimeout = null;
104     var toggleAllDocsId = "toggle-all-docs";
105
106     // On the search screen, so you remain on the last tab you opened.
107     //
108     // 0 for "In Names"
109     // 1 for "In Parameters"
110     // 2 for "In Return Types"
111     var currentTab = 0;
112
113     var mouseMovedAfterSearch = true;
114
115     var titleBeforeSearch = document.title;
116     var searchTitle = null;
117
118     function clearInputTimeout() {
119         if (searchTimeout !== null) {
120             clearTimeout(searchTimeout);
121             searchTimeout = null;
122         }
123     }
124
125     function getPageId() {
126         if (window.location.hash) {
127             var tmp = window.location.hash.replace(/^#/, "");
128             if (tmp.length > 0) {
129                 return tmp;
130             }
131         }
132         return null;
133     }
134
135     function showSidebar() {
136         var elems = document.getElementsByClassName("sidebar-elems")[0];
137         if (elems) {
138             addClass(elems, "show-it");
139         }
140         var sidebar = document.getElementsByClassName("sidebar")[0];
141         if (sidebar) {
142             addClass(sidebar, "mobile");
143             var filler = document.getElementById("sidebar-filler");
144             if (!filler) {
145                 var div = document.createElement("div");
146                 div.id = "sidebar-filler";
147                 sidebar.appendChild(div);
148             }
149         }
150     }
151
152     function hideSidebar() {
153         var elems = document.getElementsByClassName("sidebar-elems")[0];
154         if (elems) {
155             removeClass(elems, "show-it");
156         }
157         var sidebar = document.getElementsByClassName("sidebar")[0];
158         removeClass(sidebar, "mobile");
159         var filler = document.getElementById("sidebar-filler");
160         if (filler) {
161             filler.remove();
162         }
163         document.getElementsByTagName("body")[0].style.marginTop = "";
164     }
165
166     function showSearchResults(search) {
167         if (search === null || typeof search === 'undefined') {
168             search = getSearchElement();
169         }
170         addClass(main, "hidden");
171         removeClass(search, "hidden");
172         mouseMovedAfterSearch = false;
173         document.title = searchTitle;
174     }
175
176     function hideSearchResults(search) {
177         if (search === null || typeof search === 'undefined') {
178             search = getSearchElement();
179         }
180         addClass(search, "hidden");
181         removeClass(main, "hidden");
182         document.title = titleBeforeSearch;
183     }
184
185     // used for special search precedence
186     var TY_PRIMITIVE = itemTypes.indexOf("primitive");
187     var TY_KEYWORD = itemTypes.indexOf("keyword");
188
189     function getQueryStringParams() {
190         var params = {};
191         window.location.search.substring(1).split("&").
192             map(function(s) {
193                 var pair = s.split("=");
194                 params[decodeURIComponent(pair[0])] =
195                     typeof pair[1] === "undefined" ? null : decodeURIComponent(pair[1]);
196             });
197         return params;
198     }
199
200     function browserSupportsHistoryApi() {
201         return window.history && typeof window.history.pushState === "function";
202     }
203
204     function isHidden(elem) {
205         return elem.offsetHeight === 0;
206     }
207
208     var main = document.getElementById("main");
209     var savedHash = "";
210
211     function handleHashes(ev) {
212         var elem;
213         var search = getSearchElement();
214         if (ev !== null && search && !hasClass(search, "hidden") && ev.newURL) {
215             // This block occurs when clicking on an element in the navbar while
216             // in a search.
217             hideSearchResults(search);
218             var hash = ev.newURL.slice(ev.newURL.indexOf("#") + 1);
219             if (browserSupportsHistoryApi()) {
220                 history.replaceState(hash, "", "?search=#" + hash);
221             }
222             elem = document.getElementById(hash);
223             if (elem) {
224                 elem.scrollIntoView();
225             }
226         }
227         // This part is used in case an element is not visible.
228         if (savedHash !== window.location.hash) {
229             savedHash = window.location.hash;
230             if (savedHash.length === 0) {
231                 return;
232             }
233             elem = document.getElementById(savedHash.slice(1)); // we remove the '#'
234             if (!elem || !isHidden(elem)) {
235                 return;
236             }
237             var parent = elem.parentNode;
238             if (parent && hasClass(parent, "impl-items")) {
239                 // In case this is a trait implementation item, we first need to toggle
240                 // the "Show hidden undocumented items".
241                 onEachLazy(parent.getElementsByClassName("collapsed"), function(e) {
242                     if (e.parentNode === parent) {
243                         // Only click on the toggle we're looking for.
244                         e.click();
245                         return true;
246                     }
247                 });
248                 if (isHidden(elem)) {
249                     // The whole parent is collapsed. We need to click on its toggle as well!
250                     if (hasClass(parent.lastElementChild, "collapse-toggle")) {
251                         parent.lastElementChild.click();
252                     }
253                 }
254             }
255         }
256     }
257
258     function highlightSourceLines(match, ev) {
259         if (typeof match === "undefined") {
260             // If we're in mobile mode, we should hide the sidebar in any case.
261             hideSidebar();
262             match = window.location.hash.match(/^#?(\d+)(?:-(\d+))?$/);
263         }
264         if (!match) {
265             return;
266         }
267         var from = parseInt(match[1], 10);
268         var to = from;
269         if (typeof match[2] !== "undefined") {
270             to = parseInt(match[2], 10);
271         }
272         if (to < from) {
273             var tmp = to;
274             to = from;
275             from = tmp;
276         }
277         var elem = document.getElementById(from);
278         if (!elem) {
279             return;
280         }
281         if (!ev) {
282             var x = document.getElementById(from);
283             if (x) {
284                 x.scrollIntoView();
285             }
286         }
287         onEachLazy(document.getElementsByClassName("line-numbers"), function(e) {
288             onEachLazy(e.getElementsByTagName("span"), function(i_e) {
289                 removeClass(i_e, "line-highlighted");
290             });
291         });
292         for (var i = from; i <= to; ++i) {
293             elem = document.getElementById(i);
294             if (!elem) {
295                 break;
296             }
297             addClass(elem, "line-highlighted");
298         }
299     }
300
301     function onHashChange(ev) {
302         // If we're in mobile mode, we should hide the sidebar in any case.
303         hideSidebar();
304         var match = window.location.hash.match(/^#?(\d+)(?:-(\d+))?$/);
305         if (match) {
306             return highlightSourceLines(match, ev);
307         }
308         handleHashes(ev);
309     }
310
311     function expandSection(id) {
312         var elem = document.getElementById(id);
313         if (elem && isHidden(elem)) {
314             var h3 = elem.parentNode.previousElementSibling;
315             if (h3 && h3.tagName !== "H3") {
316                 h3 = h3.previousElementSibling; // skip div.docblock
317             }
318
319             if (h3) {
320                 var collapses = h3.getElementsByClassName("collapse-toggle");
321                 if (collapses.length > 0) {
322                     // The element is not visible, we need to make it appear!
323                     collapseDocs(collapses[0], "show");
324                 }
325             }
326         }
327     }
328
329     // Gets the human-readable string for the virtual-key code of the
330     // given KeyboardEvent, ev.
331     //
332     // This function is meant as a polyfill for KeyboardEvent#key,
333     // since it is not supported in Trident.  We also test for
334     // KeyboardEvent#keyCode because the handleShortcut handler is
335     // also registered for the keydown event, because Blink doesn't fire
336     // keypress on hitting the Escape key.
337     //
338     // So I guess you could say things are getting pretty interoperable.
339     function getVirtualKey(ev) {
340         if ("key" in ev && typeof ev.key != "undefined") {
341             return ev.key;
342         }
343
344         var c = ev.charCode || ev.keyCode;
345         if (c == 27) {
346             return "Escape";
347         }
348         return String.fromCharCode(c);
349     }
350
351     function getHelpElement() {
352         buildHelperPopup();
353         return document.getElementById("help");
354     }
355
356     function displayHelp(display, ev, help) {
357         help = help ? help : getHelpElement();
358         if (display === true) {
359             if (hasClass(help, "hidden")) {
360                 ev.preventDefault();
361                 removeClass(help, "hidden");
362                 addClass(document.body, "blur");
363             }
364         } else if (hasClass(help, "hidden") === false) {
365             ev.preventDefault();
366             addClass(help, "hidden");
367             removeClass(document.body, "blur");
368         }
369     }
370
371     function handleEscape(ev) {
372         var help = getHelpElement();
373         var search = getSearchElement();
374         if (hasClass(help, "hidden") === false) {
375             displayHelp(false, ev, help);
376         } else if (hasClass(search, "hidden") === false) {
377             clearInputTimeout();
378             ev.preventDefault();
379             hideSearchResults(search);
380         }
381         defocusSearchBar();
382         hideThemeButtonState();
383     }
384
385     function handleShortcut(ev) {
386         // Don't interfere with browser shortcuts
387         if (ev.ctrlKey || ev.altKey || ev.metaKey || disableShortcuts === true) {
388             return;
389         }
390
391         if (document.activeElement.tagName === "INPUT") {
392             switch (getVirtualKey(ev)) {
393             case "Escape":
394                 handleEscape(ev);
395                 break;
396             }
397         } else {
398             switch (getVirtualKey(ev)) {
399             case "Escape":
400                 handleEscape(ev);
401                 break;
402
403             case "s":
404             case "S":
405                 displayHelp(false, ev);
406                 ev.preventDefault();
407                 focusSearchBar();
408                 break;
409
410             case "+":
411             case "-":
412                 ev.preventDefault();
413                 toggleAllDocs();
414                 break;
415
416             case "?":
417                 displayHelp(true, ev);
418                 break;
419
420             case "t":
421             case "T":
422                 displayHelp(false, ev);
423                 ev.preventDefault();
424                 var themePicker = getThemePickerElement();
425                 themePicker.click();
426                 themePicker.focus();
427                 break;
428
429             default:
430                 var themePicker = getThemePickerElement();
431                 if (themePicker.parentNode.contains(ev.target)) {
432                     handleThemeKeyDown(ev);
433                 }
434             }
435         }
436     }
437
438     function handleThemeKeyDown(ev) {
439         var active = document.activeElement;
440         var themes = getThemesElement();
441         switch (getVirtualKey(ev)) {
442         case "ArrowUp":
443             ev.preventDefault();
444             if (active.previousElementSibling && ev.target.id !== "theme-picker") {
445                 active.previousElementSibling.focus();
446             } else {
447                 showThemeButtonState();
448                 themes.lastElementChild.focus();
449             }
450             break;
451         case "ArrowDown":
452             ev.preventDefault();
453             if (active.nextElementSibling && ev.target.id !== "theme-picker") {
454                 active.nextElementSibling.focus();
455             } else {
456                 showThemeButtonState();
457                 themes.firstElementChild.focus();
458             }
459             break;
460         case "Enter":
461         case "Return":
462         case "Space":
463             if (ev.target.id === "theme-picker" && themes.style.display === "none") {
464                 ev.preventDefault();
465                 showThemeButtonState();
466                 themes.firstElementChild.focus();
467             }
468             break;
469         case "Home":
470             ev.preventDefault();
471             themes.firstElementChild.focus();
472             break;
473         case "End":
474             ev.preventDefault();
475             themes.lastElementChild.focus();
476             break;
477         // The escape key is handled in handleEscape, not here,
478         // so that pressing escape will close the menu even if it isn't focused
479         }
480     }
481
482     function findParentElement(elem, tagName) {
483         do {
484             if (elem && elem.tagName === tagName) {
485                 return elem;
486             }
487             elem = elem.parentNode;
488         } while (elem);
489         return null;
490     }
491
492     document.addEventListener("keypress", handleShortcut);
493     document.addEventListener("keydown", handleShortcut);
494
495     function resetMouseMoved(ev) {
496         mouseMovedAfterSearch = true;
497     }
498
499     document.addEventListener("mousemove", resetMouseMoved);
500
501     var handleSourceHighlight = (function() {
502         var prev_line_id = 0;
503
504         var set_fragment = function(name) {
505             var x = window.scrollX,
506                 y = window.scrollY;
507             if (browserSupportsHistoryApi()) {
508                 history.replaceState(null, null, "#" + name);
509                 highlightSourceLines();
510             } else {
511                 location.replace("#" + name);
512             }
513             // Prevent jumps when selecting one or many lines
514             window.scrollTo(x, y);
515         };
516
517         return function(ev) {
518             var cur_line_id = parseInt(ev.target.id, 10);
519             ev.preventDefault();
520
521             if (ev.shiftKey && prev_line_id) {
522                 // Swap selection if needed
523                 if (prev_line_id > cur_line_id) {
524                     var tmp = prev_line_id;
525                     prev_line_id = cur_line_id;
526                     cur_line_id = tmp;
527                 }
528
529                 set_fragment(prev_line_id + "-" + cur_line_id);
530             } else {
531                 prev_line_id = cur_line_id;
532
533                 set_fragment(cur_line_id);
534             }
535         };
536     }());
537
538     document.addEventListener("click", function(ev) {
539         if (hasClass(ev.target, "help-button")) {
540             displayHelp(true, ev);
541         } else if (hasClass(ev.target, "collapse-toggle")) {
542             collapseDocs(ev.target, "toggle");
543         } else if (hasClass(ev.target.parentNode, "collapse-toggle")) {
544             collapseDocs(ev.target.parentNode, "toggle");
545         } else if (ev.target.tagName === "SPAN" && hasClass(ev.target.parentNode, "line-numbers")) {
546             handleSourceHighlight(ev);
547         } else if (hasClass(getHelpElement(), "hidden") === false) {
548             var help = getHelpElement();
549             var is_inside_help_popup = ev.target !== help && help.contains(ev.target);
550             if (is_inside_help_popup === false) {
551                 addClass(help, "hidden");
552                 removeClass(document.body, "blur");
553             }
554         } else {
555             // Making a collapsed element visible on onhashchange seems
556             // too late
557             var a = findParentElement(ev.target, "A");
558             if (a && a.hash) {
559                 expandSection(a.hash.replace(/^#/, ""));
560             }
561         }
562     });
563
564     (function() {
565         var x = document.getElementsByClassName("version-selector");
566         if (x.length > 0) {
567             x[0].onchange = function() {
568                 var i, match,
569                     url = document.location.href,
570                     stripped = "",
571                     len = rootPath.match(/\.\.\//g).length + 1;
572
573                 for (i = 0; i < len; ++i) {
574                     match = url.match(/\/[^\/]*$/);
575                     if (i < len - 1) {
576                         stripped = match[0] + stripped;
577                     }
578                     url = url.substring(0, url.length - match[0].length);
579                 }
580
581                 var selectedVersion = document.getElementsByClassName("version-selector")[0].value;
582                 url += "/" + selectedVersion + stripped;
583
584                 document.location.href = url;
585             };
586         }
587     }());
588
589     /**
590      * A function to compute the Levenshtein distance between two strings
591      * Licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported
592      * Full License can be found at http://creativecommons.org/licenses/by-sa/3.0/legalcode
593      * This code is an unmodified version of the code written by Marco de Wit
594      * and was found at http://stackoverflow.com/a/18514751/745719
595      */
596     var levenshtein_row2 = [];
597     function levenshtein(s1, s2) {
598         if (s1 === s2) {
599             return 0;
600         }
601         var s1_len = s1.length, s2_len = s2.length;
602         if (s1_len && s2_len) {
603             var i1 = 0, i2 = 0, a, b, c, c2, row = levenshtein_row2;
604             while (i1 < s1_len) {
605                 row[i1] = ++i1;
606             }
607             while (i2 < s2_len) {
608                 c2 = s2.charCodeAt(i2);
609                 a = i2;
610                 ++i2;
611                 b = i2;
612                 for (i1 = 0; i1 < s1_len; ++i1) {
613                     c = a + (s1.charCodeAt(i1) !== c2 ? 1 : 0);
614                     a = row[i1];
615                     b = b < a ? (b < c ? b + 1 : c) : (a < c ? a + 1 : c);
616                     row[i1] = b;
617                 }
618             }
619             return b;
620         }
621         return s1_len + s2_len;
622     }
623
624     window.initSearch = function(rawSearchIndex) {
625         var MAX_LEV_DISTANCE = 3;
626         var MAX_RESULTS = 200;
627         var GENERICS_DATA = 1;
628         var NAME = 0;
629         var INPUTS_DATA = 0;
630         var OUTPUT_DATA = 1;
631         var NO_TYPE_FILTER = -1;
632         var currentResults, index, searchIndex;
633         var ALIASES = {};
634         var params = getQueryStringParams();
635
636         // Populate search bar with query string search term when provided,
637         // but only if the input bar is empty. This avoid the obnoxious issue
638         // where you start trying to do a search, and the index loads, and
639         // suddenly your search is gone!
640         if (search_input.value === "") {
641             search_input.value = params.search || "";
642         }
643
644         /**
645          * Executes the query and builds an index of results
646          * @param  {[Object]} query      [The user query]
647          * @param  {[type]} searchWords  [The list of search words to query
648          *                                against]
649          * @param  {[type]} filterCrates [Crate to search in if defined]
650          * @return {[type]}              [A search index of results]
651          */
652         function execQuery(query, searchWords, filterCrates) {
653             function itemTypeFromName(typename) {
654                 var length = itemTypes.length;
655                 for (var i = 0; i < length; ++i) {
656                     if (itemTypes[i] === typename) {
657                         return i;
658                     }
659                 }
660                 return NO_TYPE_FILTER;
661             }
662
663             var valLower = query.query.toLowerCase(),
664                 val = valLower,
665                 typeFilter = itemTypeFromName(query.type),
666                 results = {}, results_in_args = {}, results_returned = {},
667                 split = valLower.split("::");
668
669             var length = split.length;
670             for (var z = 0; z < length; ++z) {
671                 if (split[z] === "") {
672                     split.splice(z, 1);
673                     z -= 1;
674                 }
675             }
676
677             function transformResults(results, isType) {
678                 var out = [];
679                 var length = results.length;
680                 for (var i = 0; i < length; ++i) {
681                     if (results[i].id > -1) {
682                         var obj = searchIndex[results[i].id];
683                         obj.lev = results[i].lev;
684                         if (isType !== true || obj.type) {
685                             var res = buildHrefAndPath(obj);
686                             obj.displayPath = pathSplitter(res[0]);
687                             obj.fullPath = obj.displayPath + obj.name;
688                             // To be sure than it some items aren't considered as duplicate.
689                             obj.fullPath += "|" + obj.ty;
690                             obj.href = res[1];
691                             out.push(obj);
692                             if (out.length >= MAX_RESULTS) {
693                                 break;
694                             }
695                         }
696                     }
697                 }
698                 return out;
699             }
700
701             function sortResults(results, isType) {
702                 var ar = [];
703                 for (var entry in results) {
704                     if (hasOwnProperty(results, entry)) {
705                         ar.push(results[entry]);
706                     }
707                 }
708                 results = ar;
709                 var i;
710                 var nresults = results.length;
711                 for (i = 0; i < nresults; ++i) {
712                     results[i].word = searchWords[results[i].id];
713                     results[i].item = searchIndex[results[i].id] || {};
714                 }
715                 // if there are no results then return to default and fail
716                 if (results.length === 0) {
717                     return [];
718                 }
719
720                 results.sort(function(aaa, bbb) {
721                     var a, b;
722
723                     // sort by exact match with regard to the last word (mismatch goes later)
724                     a = (aaa.word !== val);
725                     b = (bbb.word !== val);
726                     if (a !== b) { return a - b; }
727
728                     // Sort by non levenshtein results and then levenshtein results by the distance
729                     // (less changes required to match means higher rankings)
730                     a = (aaa.lev);
731                     b = (bbb.lev);
732                     if (a !== b) { return a - b; }
733
734                     // sort by crate (non-current crate goes later)
735                     a = (aaa.item.crate !== window.currentCrate);
736                     b = (bbb.item.crate !== window.currentCrate);
737                     if (a !== b) { return a - b; }
738
739                     // sort by item name length (longer goes later)
740                     a = aaa.word.length;
741                     b = bbb.word.length;
742                     if (a !== b) { return a - b; }
743
744                     // sort by item name (lexicographically larger goes later)
745                     a = aaa.word;
746                     b = bbb.word;
747                     if (a !== b) { return (a > b ? +1 : -1); }
748
749                     // sort by index of keyword in item name (no literal occurrence goes later)
750                     a = (aaa.index < 0);
751                     b = (bbb.index < 0);
752                     if (a !== b) { return a - b; }
753                     // (later literal occurrence, if any, goes later)
754                     a = aaa.index;
755                     b = bbb.index;
756                     if (a !== b) { return a - b; }
757
758                     // special precedence for primitive and keyword pages
759                     if ((aaa.item.ty === TY_PRIMITIVE && bbb.item.ty !== TY_KEYWORD) ||
760                         (aaa.item.ty === TY_KEYWORD && bbb.item.ty !== TY_PRIMITIVE)) {
761                         return -1;
762                     }
763                     if ((bbb.item.ty === TY_PRIMITIVE && aaa.item.ty !== TY_PRIMITIVE) ||
764                         (bbb.item.ty === TY_KEYWORD && aaa.item.ty !== TY_KEYWORD)) {
765                         return 1;
766                     }
767
768                     // sort by description (no description goes later)
769                     a = (aaa.item.desc === "");
770                     b = (bbb.item.desc === "");
771                     if (a !== b) { return a - b; }
772
773                     // sort by type (later occurrence in `itemTypes` goes later)
774                     a = aaa.item.ty;
775                     b = bbb.item.ty;
776                     if (a !== b) { return a - b; }
777
778                     // sort by path (lexicographically larger goes later)
779                     a = aaa.item.path;
780                     b = bbb.item.path;
781                     if (a !== b) { return (a > b ? +1 : -1); }
782
783                     // que sera, sera
784                     return 0;
785                 });
786
787                 var length = results.length;
788                 for (i = 0; i < length; ++i) {
789                     var result = results[i];
790
791                     // this validation does not make sense when searching by types
792                     if (result.dontValidate) {
793                         continue;
794                     }
795                     var name = result.item.name.toLowerCase(),
796                         path = result.item.path.toLowerCase(),
797                         parent = result.item.parent;
798
799                     if (isType !== true &&
800                         validateResult(name, path, split, parent) === false)
801                     {
802                         result.id = -1;
803                     }
804                 }
805                 return transformResults(results);
806             }
807
808             function extractGenerics(val) {
809                 val = val.toLowerCase();
810                 if (val.indexOf("<") !== -1) {
811                     var values = val.substring(val.indexOf("<") + 1, val.lastIndexOf(">"));
812                     return {
813                         name: val.substring(0, val.indexOf("<")),
814                         generics: values.split(/\s*,\s*/),
815                     };
816                 }
817                 return {
818                     name: val,
819                     generics: [],
820                 };
821             }
822
823             function getObjectFromId(id) {
824                 if (typeof id === "number") {
825                     return searchIndex[id];
826                 }
827                 return {'name': id};
828             }
829
830             function checkGenerics(obj, val) {
831                 // The names match, but we need to be sure that all generics kinda
832                 // match as well.
833                 var lev_distance = MAX_LEV_DISTANCE + 1;
834                 if (val.generics.length > 0) {
835                     if (obj.length > GENERICS_DATA &&
836                           obj[GENERICS_DATA].length >= val.generics.length) {
837                         var elems = obj[GENERICS_DATA].slice(0);
838                         var total = 0;
839                         var done = 0;
840                         // We need to find the type that matches the most to remove it in order
841                         // to move forward.
842                         var vlength = val.generics.length;
843                         for (var y = 0; y < vlength; ++y) {
844                             var lev = { pos: -1, lev: MAX_LEV_DISTANCE + 1};
845                             var elength = elems.length;
846                             var firstGeneric = getObjectFromId(val.generics[y]).name;
847                             for (var x = 0; x < elength; ++x) {
848                                 var tmp_lev = levenshtein(getObjectFromId(elems[x]).name,
849                                                           firstGeneric);
850                                 if (tmp_lev < lev.lev) {
851                                     lev.lev = tmp_lev;
852                                     lev.pos = x;
853                                 }
854                             }
855                             if (lev.pos !== -1) {
856                                 elems.splice(lev.pos, 1);
857                                 lev_distance = Math.min(lev.lev, lev_distance);
858                                 total += lev.lev;
859                                 done += 1;
860                             } else {
861                                 return MAX_LEV_DISTANCE + 1;
862                             }
863                         }
864                         return Math.ceil(total / done);
865                     }
866                 }
867                 return MAX_LEV_DISTANCE + 1;
868             }
869
870             // Check for type name and type generics (if any).
871             function checkType(obj, val, literalSearch) {
872                 var lev_distance = MAX_LEV_DISTANCE + 1;
873                 var x;
874                 if (obj[NAME] === val.name) {
875                     if (literalSearch === true) {
876                         if (val.generics && val.generics.length !== 0) {
877                             if (obj.length > GENERICS_DATA &&
878                                   obj[GENERICS_DATA].length >= val.generics.length) {
879                                 var elems = obj[GENERICS_DATA].slice(0);
880                                 var allFound = true;
881
882                                 for (var y = 0; allFound === true && y < val.generics.length; ++y) {
883                                     allFound = false;
884                                     var firstGeneric = getObjectFromId(val.generics[y]).name;
885                                     for (x = 0; allFound === false && x < elems.length; ++x) {
886                                         allFound = getObjectFromId(elems[x]).name === firstGeneric;
887                                     }
888                                     if (allFound === true) {
889                                         elems.splice(x - 1, 1);
890                                     }
891                                 }
892                                 if (allFound === true) {
893                                     return true;
894                                 }
895                             } else {
896                                 return false;
897                             }
898                         }
899                         return true;
900                     }
901                     // If the type has generics but don't match, then it won't return at this point.
902                     // Otherwise, `checkGenerics` will return 0 and it'll return.
903                     if (obj.length > GENERICS_DATA && obj[GENERICS_DATA].length !== 0) {
904                         var tmp_lev = checkGenerics(obj, val);
905                         if (tmp_lev <= MAX_LEV_DISTANCE) {
906                             return tmp_lev;
907                         }
908                     } else {
909                         return 0;
910                     }
911                 }
912                 // Names didn't match so let's check if one of the generic types could.
913                 if (literalSearch === true) {
914                      if (obj.length > GENERICS_DATA && obj[GENERICS_DATA].length > 0) {
915                         var length = obj[GENERICS_DATA].length;
916                         for (x = 0; x < length; ++x) {
917                             if (obj[GENERICS_DATA][x] === val.name) {
918                                 return true;
919                             }
920                         }
921                     }
922                     return false;
923                 }
924                 lev_distance = Math.min(levenshtein(obj[NAME], val.name), lev_distance);
925                 if (lev_distance <= MAX_LEV_DISTANCE) {
926                     // The generics didn't match but the name kinda did so we give it
927                     // a levenshtein distance value that isn't *this* good so it goes
928                     // into the search results but not too high.
929                     lev_distance = Math.ceil((checkGenerics(obj, val) + lev_distance) / 2);
930                 } else if (obj.length > GENERICS_DATA && obj[GENERICS_DATA].length > 0) {
931                     // We can check if the type we're looking for is inside the generics!
932                     var olength = obj[GENERICS_DATA].length;
933                     for (x = 0; x < olength; ++x) {
934                         lev_distance = Math.min(levenshtein(obj[GENERICS_DATA][x], val.name),
935                                                 lev_distance);
936                     }
937                 }
938                 // Now whatever happens, the returned distance is "less good" so we should mark it
939                 // as such, and so we add 1 to the distance to make it "less good".
940                 return lev_distance + 1;
941             }
942
943             function findArg(obj, val, literalSearch, typeFilter) {
944                 var lev_distance = MAX_LEV_DISTANCE + 1;
945
946                 if (obj && obj.type && obj.type[INPUTS_DATA] && obj.type[INPUTS_DATA].length > 0) {
947                     var length = obj.type[INPUTS_DATA].length;
948                     for (var i = 0; i < length; i++) {
949                         var tmp = obj.type[INPUTS_DATA][i];
950                         if (typePassesFilter(typeFilter, tmp[1]) === false) {
951                             continue;
952                         }
953                         tmp = checkType(tmp, val, literalSearch);
954                         if (literalSearch === true) {
955                             if (tmp === true) {
956                                 return true;
957                             }
958                             continue;
959                         }
960                         lev_distance = Math.min(tmp, lev_distance);
961                         if (lev_distance === 0) {
962                             return 0;
963                         }
964                     }
965                 }
966                 return literalSearch === true ? false : lev_distance;
967             }
968
969             function checkReturned(obj, val, literalSearch, typeFilter) {
970                 var lev_distance = MAX_LEV_DISTANCE + 1;
971
972                 if (obj && obj.type && obj.type.length > OUTPUT_DATA) {
973                     var ret = obj.type[OUTPUT_DATA];
974                     if (typeof ret[0] === "string") {
975                         ret = [ret];
976                     }
977                     for (var x = 0; x < ret.length; ++x) {
978                         var tmp = ret[x];
979                         if (typePassesFilter(typeFilter, tmp[1]) === false) {
980                             continue;
981                         }
982                         tmp = checkType(tmp, val, literalSearch);
983                         if (literalSearch === true) {
984                             if (tmp === true) {
985                                 return true;
986                             }
987                             continue;
988                         }
989                         lev_distance = Math.min(tmp, lev_distance);
990                         if (lev_distance === 0) {
991                             return 0;
992                         }
993                     }
994                 }
995                 return literalSearch === true ? false : lev_distance;
996             }
997
998             function checkPath(contains, lastElem, ty) {
999                 if (contains.length === 0) {
1000                     return 0;
1001                 }
1002                 var ret_lev = MAX_LEV_DISTANCE + 1;
1003                 var path = ty.path.split("::");
1004
1005                 if (ty.parent && ty.parent.name) {
1006                     path.push(ty.parent.name.toLowerCase());
1007                 }
1008
1009                 var length = path.length;
1010                 var clength = contains.length;
1011                 if (clength > length) {
1012                     return MAX_LEV_DISTANCE + 1;
1013                 }
1014                 for (var i = 0; i < length; ++i) {
1015                     if (i + clength > length) {
1016                         break;
1017                     }
1018                     var lev_total = 0;
1019                     var aborted = false;
1020                     for (var x = 0; x < clength; ++x) {
1021                         var lev = levenshtein(path[i + x], contains[x]);
1022                         if (lev > MAX_LEV_DISTANCE) {
1023                             aborted = true;
1024                             break;
1025                         }
1026                         lev_total += lev;
1027                     }
1028                     if (aborted === false) {
1029                         ret_lev = Math.min(ret_lev, Math.round(lev_total / clength));
1030                     }
1031                 }
1032                 return ret_lev;
1033             }
1034
1035             function typePassesFilter(filter, type) {
1036                 // No filter
1037                 if (filter <= NO_TYPE_FILTER) return true;
1038
1039                 // Exact match
1040                 if (filter === type) return true;
1041
1042                 // Match related items
1043                 var name = itemTypes[type];
1044                 switch (itemTypes[filter]) {
1045                     case "constant":
1046                         return name === "associatedconstant";
1047                     case "fn":
1048                         return name === "method" || name === "tymethod";
1049                     case "type":
1050                         return name === "primitive" || name === "associatedtype";
1051                     case "trait":
1052                         return name === "traitalias";
1053                 }
1054
1055                 // No match
1056                 return false;
1057             }
1058
1059             function generateId(ty) {
1060                 if (ty.parent && ty.parent.name) {
1061                     return itemTypes[ty.ty] + ty.path + ty.parent.name + ty.name;
1062                 }
1063                 return itemTypes[ty.ty] + ty.path + ty.name;
1064             }
1065
1066             function createAliasFromItem(item) {
1067                 return {
1068                     crate: item.crate,
1069                     name: item.name,
1070                     path: item.path,
1071                     desc: item.desc,
1072                     ty: item.ty,
1073                     parent: item.parent,
1074                     type: item.type,
1075                     is_alias: true,
1076                 };
1077             }
1078
1079             function handleAliases(ret, query, filterCrates) {
1080                 // We separate aliases and crate aliases because we want to have current crate
1081                 // aliases to be before the others in the displayed results.
1082                 var aliases = [];
1083                 var crateAliases = [];
1084                 var i;
1085                 if (filterCrates !== undefined) {
1086                     if (ALIASES[filterCrates] && ALIASES[filterCrates][query.search]) {
1087                         for (i = 0; i < ALIASES[filterCrates][query.search].length; ++i) {
1088                             aliases.push(
1089                                 createAliasFromItem(
1090                                     searchIndex[ALIASES[filterCrates][query.search][i]]));
1091                         }
1092                     }
1093                 } else {
1094                     Object.keys(ALIASES).forEach(function(crate) {
1095                         if (ALIASES[crate][query.search]) {
1096                             var pushTo = crate === window.currentCrate ? crateAliases : aliases;
1097                             for (i = 0; i < ALIASES[crate][query.search].length; ++i) {
1098                                 pushTo.push(
1099                                     createAliasFromItem(
1100                                         searchIndex[ALIASES[crate][query.search][i]]));
1101                             }
1102                         }
1103                     });
1104                 }
1105
1106                 var sortFunc = function(aaa, bbb) {
1107                     if (aaa.path < bbb.path) {
1108                         return 1;
1109                     } else if (aaa.path === bbb.path) {
1110                         return 0;
1111                     }
1112                     return -1;
1113                 };
1114                 crateAliases.sort(sortFunc);
1115                 aliases.sort(sortFunc);
1116
1117                 var pushFunc = function(alias) {
1118                     alias.alias = query.raw;
1119                     var res = buildHrefAndPath(alias);
1120                     alias.displayPath = pathSplitter(res[0]);
1121                     alias.fullPath = alias.displayPath + alias.name;
1122                     alias.href = res[1];
1123
1124                     ret.others.unshift(alias);
1125                     if (ret.others.length > MAX_RESULTS) {
1126                         ret.others.pop();
1127                     }
1128                 };
1129                 onEach(aliases, pushFunc);
1130                 onEach(crateAliases, pushFunc);
1131             }
1132
1133             // quoted values mean literal search
1134             var nSearchWords = searchWords.length;
1135             var i;
1136             var ty;
1137             var fullId;
1138             var returned;
1139             var in_args;
1140             if ((val.charAt(0) === "\"" || val.charAt(0) === "'") &&
1141                 val.charAt(val.length - 1) === val.charAt(0))
1142             {
1143                 val = extractGenerics(val.substr(1, val.length - 2));
1144                 for (i = 0; i < nSearchWords; ++i) {
1145                     if (filterCrates !== undefined && searchIndex[i].crate !== filterCrates) {
1146                         continue;
1147                     }
1148                     in_args = findArg(searchIndex[i], val, true, typeFilter);
1149                     returned = checkReturned(searchIndex[i], val, true, typeFilter);
1150                     ty = searchIndex[i];
1151                     fullId = generateId(ty);
1152
1153                     if (searchWords[i] === val.name
1154                         && typePassesFilter(typeFilter, searchIndex[i].ty)
1155                         && results[fullId] === undefined) {
1156                         results[fullId] = {
1157                             id: i,
1158                             index: -1,
1159                             dontValidate: true,
1160                         };
1161                     }
1162                     if (in_args === true && results_in_args[fullId] === undefined) {
1163                         results_in_args[fullId] = {
1164                             id: i,
1165                             index: -1,
1166                             dontValidate: true,
1167                         };
1168                     }
1169                     if (returned === true && results_returned[fullId] === undefined) {
1170                         results_returned[fullId] = {
1171                             id: i,
1172                             index: -1,
1173                             dontValidate: true,
1174                         };
1175                     }
1176                 }
1177                 query.inputs = [val];
1178                 query.output = val;
1179                 query.search = val;
1180             // searching by type
1181             } else if (val.search("->") > -1) {
1182                 var trimmer = function(s) { return s.trim(); };
1183                 var parts = val.split("->").map(trimmer);
1184                 var input = parts[0];
1185                 // sort inputs so that order does not matter
1186                 var inputs = input.split(",").map(trimmer).sort();
1187                 for (i = 0; i < inputs.length; ++i) {
1188                     inputs[i] = extractGenerics(inputs[i]);
1189                 }
1190                 var output = extractGenerics(parts[1]);
1191
1192                 for (i = 0; i < nSearchWords; ++i) {
1193                     if (filterCrates !== undefined && searchIndex[i].crate !== filterCrates) {
1194                         continue;
1195                     }
1196                     var type = searchIndex[i].type;
1197                     ty = searchIndex[i];
1198                     if (!type) {
1199                         continue;
1200                     }
1201                     fullId = generateId(ty);
1202
1203                     returned = checkReturned(ty, output, true, NO_TYPE_FILTER);
1204                     if (output.name === "*" || returned === true) {
1205                         in_args = false;
1206                         var is_module = false;
1207
1208                         if (input === "*") {
1209                             is_module = true;
1210                         } else {
1211                             var allFound = true;
1212                             for (var it = 0; allFound === true && it < inputs.length; it++) {
1213                                 allFound = checkType(type, inputs[it], true);
1214                             }
1215                             in_args = allFound;
1216                         }
1217                         if (in_args === true) {
1218                             results_in_args[fullId] = {
1219                                 id: i,
1220                                 index: -1,
1221                                 dontValidate: true,
1222                             };
1223                         }
1224                         if (returned === true) {
1225                             results_returned[fullId] = {
1226                                 id: i,
1227                                 index: -1,
1228                                 dontValidate: true,
1229                             };
1230                         }
1231                         if (is_module === true) {
1232                             results[fullId] = {
1233                                 id: i,
1234                                 index: -1,
1235                                 dontValidate: true,
1236                             };
1237                         }
1238                     }
1239                 }
1240                 query.inputs = inputs.map(function(input) {
1241                     return input.name;
1242                 });
1243                 query.output = output.name;
1244             } else {
1245                 query.inputs = [val];
1246                 query.output = val;
1247                 query.search = val;
1248                 // gather matching search results up to a certain maximum
1249                 val = val.replace(/\_/g, "");
1250
1251                 var valGenerics = extractGenerics(val);
1252
1253                 var paths = valLower.split("::");
1254                 var j;
1255                 for (j = 0; j < paths.length; ++j) {
1256                     if (paths[j] === "") {
1257                         paths.splice(j, 1);
1258                         j -= 1;
1259                     }
1260                 }
1261                 val = paths[paths.length - 1];
1262                 var contains = paths.slice(0, paths.length > 1 ? paths.length - 1 : 1);
1263
1264                 var lev;
1265                 for (j = 0; j < nSearchWords; ++j) {
1266                     ty = searchIndex[j];
1267                     if (!ty || (filterCrates !== undefined && ty.crate !== filterCrates)) {
1268                         continue;
1269                     }
1270                     var lev_add = 0;
1271                     if (paths.length > 1) {
1272                         lev = checkPath(contains, paths[paths.length - 1], ty);
1273                         if (lev > MAX_LEV_DISTANCE) {
1274                             continue;
1275                         } else if (lev > 0) {
1276                             lev_add = lev / 10;
1277                         }
1278                     }
1279
1280                     returned = MAX_LEV_DISTANCE + 1;
1281                     in_args = MAX_LEV_DISTANCE + 1;
1282                     var index = -1;
1283                     // we want lev results to go lower than others
1284                     lev = MAX_LEV_DISTANCE + 1;
1285                     fullId = generateId(ty);
1286
1287                     if (searchWords[j].indexOf(split[i]) > -1 ||
1288                         searchWords[j].indexOf(val) > -1 ||
1289                         searchWords[j].replace(/_/g, "").indexOf(val) > -1)
1290                     {
1291                         // filter type: ... queries
1292                         if (typePassesFilter(typeFilter, ty.ty) && results[fullId] === undefined) {
1293                             index = searchWords[j].replace(/_/g, "").indexOf(val);
1294                         }
1295                     }
1296                     if ((lev = levenshtein(searchWords[j], val)) <= MAX_LEV_DISTANCE) {
1297                         if (typePassesFilter(typeFilter, ty.ty) === false) {
1298                             lev = MAX_LEV_DISTANCE + 1;
1299                         } else {
1300                             lev += 1;
1301                         }
1302                     }
1303                     in_args = findArg(ty, valGenerics, false, typeFilter);
1304                     returned = checkReturned(ty, valGenerics, false, typeFilter);
1305
1306                     lev += lev_add;
1307                     if (lev > 0 && val.length > 3 && searchWords[j].indexOf(val) > -1) {
1308                         if (val.length < 6) {
1309                             lev -= 1;
1310                         } else {
1311                             lev = 0;
1312                         }
1313                     }
1314                     if (in_args <= MAX_LEV_DISTANCE) {
1315                         if (results_in_args[fullId] === undefined) {
1316                             results_in_args[fullId] = {
1317                                 id: j,
1318                                 index: index,
1319                                 lev: in_args,
1320                             };
1321                         }
1322                         results_in_args[fullId].lev =
1323                             Math.min(results_in_args[fullId].lev, in_args);
1324                     }
1325                     if (returned <= MAX_LEV_DISTANCE) {
1326                         if (results_returned[fullId] === undefined) {
1327                             results_returned[fullId] = {
1328                                 id: j,
1329                                 index: index,
1330                                 lev: returned,
1331                             };
1332                         }
1333                         results_returned[fullId].lev =
1334                             Math.min(results_returned[fullId].lev, returned);
1335                     }
1336                     if (index !== -1 || lev <= MAX_LEV_DISTANCE) {
1337                         if (index !== -1 && paths.length < 2) {
1338                             lev = 0;
1339                         }
1340                         if (results[fullId] === undefined) {
1341                             results[fullId] = {
1342                                 id: j,
1343                                 index: index,
1344                                 lev: lev,
1345                             };
1346                         }
1347                         results[fullId].lev = Math.min(results[fullId].lev, lev);
1348                     }
1349                 }
1350             }
1351
1352             var ret = {
1353                 "in_args": sortResults(results_in_args, true),
1354                 "returned": sortResults(results_returned, true),
1355                 "others": sortResults(results),
1356             };
1357             handleAliases(ret, query, filterCrates);
1358             return ret;
1359         }
1360
1361         /**
1362          * Validate performs the following boolean logic. For example:
1363          * "File::open" will give IF A PARENT EXISTS => ("file" && "open")
1364          * exists in (name || path || parent) OR => ("file" && "open") exists in
1365          * (name || path )
1366          *
1367          * This could be written functionally, but I wanted to minimise
1368          * functions on stack.
1369          *
1370          * @param  {[string]} name   [The name of the result]
1371          * @param  {[string]} path   [The path of the result]
1372          * @param  {[string]} keys   [The keys to be used (["file", "open"])]
1373          * @param  {[object]} parent [The parent of the result]
1374          * @return {[boolean]}       [Whether the result is valid or not]
1375          */
1376         function validateResult(name, path, keys, parent) {
1377             for (var i = 0; i < keys.length; ++i) {
1378                 // each check is for validation so we negate the conditions and invalidate
1379                 if (!(
1380                     // check for an exact name match
1381                     name.indexOf(keys[i]) > -1 ||
1382                     // then an exact path match
1383                     path.indexOf(keys[i]) > -1 ||
1384                     // next if there is a parent, check for exact parent match
1385                     (parent !== undefined && parent.name !== undefined &&
1386                         parent.name.toLowerCase().indexOf(keys[i]) > -1) ||
1387                     // lastly check to see if the name was a levenshtein match
1388                     levenshtein(name, keys[i]) <= MAX_LEV_DISTANCE)) {
1389                     return false;
1390                 }
1391             }
1392             return true;
1393         }
1394
1395         function getQuery(raw) {
1396             var matches, type, query;
1397             query = raw;
1398
1399             matches = query.match(/^(fn|mod|struct|enum|trait|type|const|macro)\s*:\s*/i);
1400             if (matches) {
1401                 type = matches[1].replace(/^const$/, "constant");
1402                 query = query.substring(matches[0].length);
1403             }
1404
1405             return {
1406                 raw: raw,
1407                 query: query,
1408                 type: type,
1409                 id: query + type
1410             };
1411         }
1412
1413         function initSearchNav() {
1414             var hoverTimeout;
1415
1416             var click_func = function(e) {
1417                 var el = e.target;
1418                 // to retrieve the real "owner" of the event.
1419                 while (el.tagName !== "TR") {
1420                     el = el.parentNode;
1421                 }
1422                 var dst = e.target.getElementsByTagName("a");
1423                 if (dst.length < 1) {
1424                     return;
1425                 }
1426                 dst = dst[0];
1427                 if (window.location.pathname === dst.pathname) {
1428                     hideSearchResults();
1429                     document.location.href = dst.href;
1430                 }
1431             };
1432             var mouseover_func = function(e) {
1433                 if (mouseMovedAfterSearch) {
1434                     var el = e.target;
1435                     // to retrieve the real "owner" of the event.
1436                     while (el.tagName !== "TR") {
1437                         el = el.parentNode;
1438                     }
1439                     clearTimeout(hoverTimeout);
1440                     hoverTimeout = setTimeout(function() {
1441                         onEachLazy(document.getElementsByClassName("search-results"), function(e) {
1442                             onEachLazy(e.getElementsByClassName("result"), function(i_e) {
1443                                 removeClass(i_e, "highlighted");
1444                             });
1445                         });
1446                         addClass(el, "highlighted");
1447                     }, 20);
1448                 }
1449             };
1450             onEachLazy(document.getElementsByClassName("search-results"), function(e) {
1451                 onEachLazy(e.getElementsByClassName("result"), function(i_e) {
1452                     i_e.onclick = click_func;
1453                     i_e.onmouseover = mouseover_func;
1454                 });
1455             });
1456
1457             search_input.onkeydown = function(e) {
1458                 // "actives" references the currently highlighted item in each search tab.
1459                 // Each array in "actives" represents a tab.
1460                 var actives = [[], [], []];
1461                 // "current" is used to know which tab we're looking into.
1462                 var current = 0;
1463                 onEachLazy(document.getElementById("results").childNodes, function(e) {
1464                     onEachLazy(e.getElementsByClassName("highlighted"), function(h_e) {
1465                         actives[current].push(h_e);
1466                     });
1467                     current += 1;
1468                 });
1469
1470                 if (e.which === 38) { // up
1471                     if (!actives[currentTab].length ||
1472                         !actives[currentTab][0].previousElementSibling) {
1473                         return;
1474                     }
1475
1476                     addClass(actives[currentTab][0].previousElementSibling, "highlighted");
1477                     removeClass(actives[currentTab][0], "highlighted");
1478                     e.preventDefault();
1479                 } else if (e.which === 40) { // down
1480                     if (!actives[currentTab].length) {
1481                         var results = document.getElementById("results").childNodes;
1482                         if (results.length > 0) {
1483                             var res = results[currentTab].getElementsByClassName("result");
1484                             if (res.length > 0) {
1485                                 addClass(res[0], "highlighted");
1486                             }
1487                         }
1488                     } else if (actives[currentTab][0].nextElementSibling) {
1489                         addClass(actives[currentTab][0].nextElementSibling, "highlighted");
1490                         removeClass(actives[currentTab][0], "highlighted");
1491                     }
1492                     e.preventDefault();
1493                 } else if (e.which === 13) { // return
1494                     if (actives[currentTab].length) {
1495                         document.location.href =
1496                             actives[currentTab][0].getElementsByTagName("a")[0].href;
1497                     }
1498                 } else if (e.which === 9) { // tab
1499                     if (e.shiftKey) {
1500                         printTab(currentTab > 0 ? currentTab - 1 : 2);
1501                     } else {
1502                         printTab(currentTab > 1 ? 0 : currentTab + 1);
1503                     }
1504                     e.preventDefault();
1505                 } else if (e.which === 16) { // shift
1506                     // Does nothing, it's just to avoid losing "focus" on the highlighted element.
1507                 } else if (actives[currentTab].length > 0) {
1508                     removeClass(actives[currentTab][0], "highlighted");
1509                 }
1510             };
1511         }
1512
1513         function buildHrefAndPath(item) {
1514             var displayPath;
1515             var href;
1516             var type = itemTypes[item.ty];
1517             var name = item.name;
1518             var path = item.path;
1519
1520             if (type === "mod") {
1521                 displayPath = path + "::";
1522                 href = rootPath + path.replace(/::/g, "/") + "/" +
1523                        name + "/index.html";
1524             } else if (type === "primitive" || type === "keyword") {
1525                 displayPath = "";
1526                 href = rootPath + path.replace(/::/g, "/") +
1527                        "/" + type + "." + name + ".html";
1528             } else if (type === "externcrate") {
1529                 displayPath = "";
1530                 href = rootPath + name + "/index.html";
1531             } else if (item.parent !== undefined) {
1532                 var myparent = item.parent;
1533                 var anchor = "#" + type + "." + name;
1534                 var parentType = itemTypes[myparent.ty];
1535                 var pageType = parentType;
1536                 var pageName = myparent.name;
1537
1538                 if (parentType === "primitive") {
1539                     displayPath = myparent.name + "::";
1540                 } else if (type === "structfield" && parentType === "variant") {
1541                     // Structfields belonging to variants are special: the
1542                     // final path element is the enum name.
1543                     var splitPath = item.path.split("::");
1544                     var enumName = splitPath.pop();
1545                     path = splitPath.join("::");
1546                     displayPath = path + "::" + enumName + "::" + myparent.name + "::";
1547                     anchor = "#variant." + myparent.name + ".field." + name;
1548                     pageType = "enum";
1549                     pageName = enumName;
1550                 } else {
1551                     displayPath = path + "::" + myparent.name + "::";
1552                 }
1553                 href = rootPath + path.replace(/::/g, "/") +
1554                        "/" + pageType +
1555                        "." + pageName +
1556                        ".html" + anchor;
1557             } else {
1558                 displayPath = item.path + "::";
1559                 href = rootPath + item.path.replace(/::/g, "/") +
1560                        "/" + type + "." + name + ".html";
1561             }
1562             return [displayPath, href];
1563         }
1564
1565         function escape(content) {
1566             var h1 = document.createElement("h1");
1567             h1.textContent = content;
1568             return h1.innerHTML;
1569         }
1570
1571         function pathSplitter(path) {
1572             var tmp = "<span>" + path.replace(/::/g, "::</span><span>");
1573             if (tmp.endsWith("<span>")) {
1574                 return tmp.slice(0, tmp.length - 6);
1575             }
1576             return tmp;
1577         }
1578
1579         function addTab(array, query, display) {
1580             var extraStyle = "";
1581             if (display === false) {
1582                 extraStyle = " style=\"display: none;\"";
1583             }
1584
1585             var output = "";
1586             var duplicates = {};
1587             var length = 0;
1588             if (array.length > 0) {
1589                 output = "<table class=\"search-results\"" + extraStyle + ">";
1590
1591                 array.forEach(function(item) {
1592                     var name, type;
1593
1594                     name = item.name;
1595                     type = itemTypes[item.ty];
1596
1597                     if (item.is_alias !== true) {
1598                         if (duplicates[item.fullPath]) {
1599                             return;
1600                         }
1601                         duplicates[item.fullPath] = true;
1602                     }
1603                     length += 1;
1604
1605                     output += "<tr class=\"" + type + " result\"><td>" +
1606                               "<a href=\"" + item.href + "\">" +
1607                               (item.is_alias === true ?
1608                                ("<span class=\"alias\"><b>" + item.alias + " </b></span><span " +
1609                                   "class=\"grey\"><i>&nbsp;- see&nbsp;</i></span>") : "") +
1610                               item.displayPath + "<span class=\"" + type + "\">" +
1611                               name + "</span></a></td><td>" +
1612                               "<a href=\"" + item.href + "\">" +
1613                               "<span class=\"desc\">" + escape(item.desc) +
1614                               "&nbsp;</span></a></td></tr>";
1615                 });
1616                 output += "</table>";
1617             } else {
1618                 output = "<div class=\"search-failed\"" + extraStyle + ">No results :(<br/>" +
1619                     "Try on <a href=\"https://duckduckgo.com/?q=" +
1620                     encodeURIComponent("rust " + query.query) +
1621                     "\">DuckDuckGo</a>?<br/><br/>" +
1622                     "Or try looking in one of these:<ul><li>The <a " +
1623                     "href=\"https://doc.rust-lang.org/reference/index.html\">Rust Reference</a> " +
1624                     " for technical details about the language.</li><li><a " +
1625                     "href=\"https://doc.rust-lang.org/rust-by-example/index.html\">Rust By " +
1626                     "Example</a> for expository code examples.</a></li><li>The <a " +
1627                     "href=\"https://doc.rust-lang.org/book/index.html\">Rust Book</a> for " +
1628                     "introductions to language features and the language itself.</li><li><a " +
1629                     "href=\"https://docs.rs\">Docs.rs</a> for documentation of crates released on" +
1630                     " <a href=\"https://crates.io/\">crates.io</a>.</li></ul></div>";
1631             }
1632             return [output, length];
1633         }
1634
1635         function makeTabHeader(tabNb, text, nbElems) {
1636             if (currentTab === tabNb) {
1637                 return "<div class=\"selected\">" + text +
1638                        " <div class=\"count\">(" + nbElems + ")</div></div>";
1639             }
1640             return "<div>" + text + " <div class=\"count\">(" + nbElems + ")</div></div>";
1641         }
1642
1643         function showResults(results) {
1644             var search = getSearchElement();
1645             if (results.others.length === 1
1646                 && getSettingValue("go-to-only-result") === "true"
1647                 // By default, the search DOM element is "empty" (meaning it has no children not
1648                 // text content). Once a search has been run, it won't be empty, even if you press
1649                 // ESC or empty the search input (which also "cancels" the search).
1650                 && (!search.firstChild || search.firstChild.innerText !== getSearchLoadingText()))
1651             {
1652                 var elem = document.createElement("a");
1653                 elem.href = results.others[0].href;
1654                 elem.style.display = "none";
1655                 // For firefox, we need the element to be in the DOM so it can be clicked.
1656                 document.body.appendChild(elem);
1657                 elem.click();
1658                 return;
1659             }
1660             var query = getQuery(search_input.value);
1661
1662             currentResults = query.id;
1663
1664             var ret_others = addTab(results.others, query);
1665             var ret_in_args = addTab(results.in_args, query, false);
1666             var ret_returned = addTab(results.returned, query, false);
1667
1668             var output = "<h1>Results for " + escape(query.query) +
1669                 (query.type ? " (type: " + escape(query.type) + ")" : "") + "</h1>" +
1670                 "<div id=\"titles\">" +
1671                 makeTabHeader(0, "In Names", ret_others[1]) +
1672                 makeTabHeader(1, "In Parameters", ret_in_args[1]) +
1673                 makeTabHeader(2, "In Return Types", ret_returned[1]) +
1674                 "</div><div id=\"results\">" +
1675                 ret_others[0] + ret_in_args[0] + ret_returned[0] + "</div>";
1676
1677             search.innerHTML = output;
1678             showSearchResults(search);
1679             var tds = search.getElementsByTagName("td");
1680             var td_width = 0;
1681             if (tds.length > 0) {
1682                 td_width = tds[0].offsetWidth;
1683             }
1684             var width = search.offsetWidth - 40 - td_width;
1685             onEachLazy(search.getElementsByClassName("desc"), function(e) {
1686                 e.style.width = width + "px";
1687             });
1688             initSearchNav();
1689             var elems = document.getElementById("titles").childNodes;
1690             elems[0].onclick = function() { printTab(0); };
1691             elems[1].onclick = function() { printTab(1); };
1692             elems[2].onclick = function() { printTab(2); };
1693             printTab(currentTab);
1694         }
1695
1696         function execSearch(query, searchWords, filterCrates) {
1697             function getSmallest(arrays, positions, notDuplicates) {
1698                 var start = null;
1699
1700                 for (var it = 0; it < positions.length; ++it) {
1701                     if (arrays[it].length > positions[it] &&
1702                         (start === null || start > arrays[it][positions[it]].lev) &&
1703                         !notDuplicates[arrays[it][positions[it]].fullPath]) {
1704                         start = arrays[it][positions[it]].lev;
1705                     }
1706                 }
1707                 return start;
1708             }
1709
1710             function mergeArrays(arrays) {
1711                 var ret = [];
1712                 var positions = [];
1713                 var notDuplicates = {};
1714
1715                 for (var x = 0; x < arrays.length; ++x) {
1716                     positions.push(0);
1717                 }
1718                 while (ret.length < MAX_RESULTS) {
1719                     var smallest = getSmallest(arrays, positions, notDuplicates);
1720
1721                     if (smallest === null) {
1722                         break;
1723                     }
1724                     for (x = 0; x < arrays.length && ret.length < MAX_RESULTS; ++x) {
1725                         if (arrays[x].length > positions[x] &&
1726                                 arrays[x][positions[x]].lev === smallest &&
1727                                 !notDuplicates[arrays[x][positions[x]].fullPath]) {
1728                             ret.push(arrays[x][positions[x]]);
1729                             notDuplicates[arrays[x][positions[x]].fullPath] = true;
1730                             positions[x] += 1;
1731                         }
1732                     }
1733                 }
1734                 return ret;
1735             }
1736
1737             var queries = query.raw.split(",");
1738             var results = {
1739                 "in_args": [],
1740                 "returned": [],
1741                 "others": [],
1742             };
1743
1744             for (var i = 0; i < queries.length; ++i) {
1745                 query = queries[i].trim();
1746                 if (query.length !== 0) {
1747                     var tmp = execQuery(getQuery(query), searchWords, filterCrates);
1748
1749                     results.in_args.push(tmp.in_args);
1750                     results.returned.push(tmp.returned);
1751                     results.others.push(tmp.others);
1752                 }
1753             }
1754             if (queries.length > 1) {
1755                 return {
1756                     "in_args": mergeArrays(results.in_args),
1757                     "returned": mergeArrays(results.returned),
1758                     "others": mergeArrays(results.others),
1759                 };
1760             }
1761             return {
1762                 "in_args": results.in_args[0],
1763                 "returned": results.returned[0],
1764                 "others": results.others[0],
1765             };
1766         }
1767
1768         function getFilterCrates() {
1769             var elem = document.getElementById("crate-search");
1770
1771             if (elem && elem.value !== "All crates" && hasOwnProperty(rawSearchIndex, elem.value)) {
1772                 return elem.value;
1773             }
1774             return undefined;
1775         }
1776
1777         function search(e, forced) {
1778             var params = getQueryStringParams();
1779             var query = getQuery(search_input.value.trim());
1780
1781             if (e) {
1782                 e.preventDefault();
1783             }
1784
1785             if (query.query.length === 0) {
1786                 return;
1787             }
1788             if (forced !== true && query.id === currentResults) {
1789                 if (query.query.length > 0) {
1790                     putBackSearch(search_input);
1791                 }
1792                 return;
1793             }
1794
1795             // Update document title to maintain a meaningful browser history
1796             searchTitle = "Results for " + query.query + " - Rust";
1797
1798             // Because searching is incremental by character, only the most
1799             // recent search query is added to the browser history.
1800             if (browserSupportsHistoryApi()) {
1801                 if (!history.state && !params.search) {
1802                     history.pushState(query, "", "?search=" + encodeURIComponent(query.raw));
1803                 } else {
1804                     history.replaceState(query, "", "?search=" + encodeURIComponent(query.raw));
1805                 }
1806             }
1807
1808             var filterCrates = getFilterCrates();
1809             showResults(execSearch(query, index, filterCrates));
1810         }
1811
1812         function buildIndex(rawSearchIndex) {
1813             searchIndex = [];
1814             var searchWords = [];
1815             var i;
1816             var currentIndex = 0;
1817
1818             for (var crate in rawSearchIndex) {
1819                 if (!hasOwnProperty(rawSearchIndex, crate)) { continue; }
1820
1821                 var crateSize = 0;
1822
1823                 searchWords.push(crate);
1824                 searchIndex.push({
1825                     crate: crate,
1826                     ty: 1, // == ExternCrate
1827                     name: crate,
1828                     path: "",
1829                     desc: rawSearchIndex[crate].doc,
1830                     type: null,
1831                 });
1832                 currentIndex += 1;
1833
1834                 // an array of [(Number) item type,
1835                 //              (String) name,
1836                 //              (String) full path or empty string for previous path,
1837                 //              (String) description,
1838                 //              (Number | null) the parent path index to `paths`]
1839                 //              (Object | null) the type of the function (if any)
1840                 var items = rawSearchIndex[crate].i;
1841                 // an array of [(Number) item type,
1842                 //              (String) name]
1843                 var paths = rawSearchIndex[crate].p;
1844                 // a array of [(String) alias name
1845                 //             [Number] index to items]
1846                 var aliases = rawSearchIndex[crate].a;
1847
1848                 // convert `rawPaths` entries into object form
1849                 var len = paths.length;
1850                 for (i = 0; i < len; ++i) {
1851                     paths[i] = {ty: paths[i][0], name: paths[i][1]};
1852                 }
1853
1854                 // convert `items` into an object form, and construct word indices.
1855                 //
1856                 // before any analysis is performed lets gather the search terms to
1857                 // search against apart from the rest of the data.  This is a quick
1858                 // operation that is cached for the life of the page state so that
1859                 // all other search operations have access to this cached data for
1860                 // faster analysis operations
1861                 len = items.length;
1862                 var lastPath = "";
1863                 for (i = 0; i < len; ++i) {
1864                     var rawRow = items[i];
1865                     if (!rawRow[2]) {
1866                         rawRow[2] = lastPath;
1867                     }
1868                     var row = {
1869                         crate: crate,
1870                         ty: rawRow[0],
1871                         name: rawRow[1],
1872                         path: rawRow[2],
1873                         desc: rawRow[3],
1874                         parent: paths[rawRow[4]],
1875                         type: rawRow[5],
1876                     };
1877                     searchIndex.push(row);
1878                     if (typeof row.name === "string") {
1879                         var word = row.name.toLowerCase();
1880                         searchWords.push(word);
1881                     } else {
1882                         searchWords.push("");
1883                     }
1884                     lastPath = row.path;
1885                     crateSize += 1;
1886                 }
1887
1888                 if (aliases) {
1889                     ALIASES[crate] = {};
1890                     var j, local_aliases;
1891                     for (var alias_name in aliases) {
1892                         if (!aliases.hasOwnProperty(alias_name)) { continue; }
1893
1894                         if (!ALIASES[crate].hasOwnProperty(alias_name)) {
1895                             ALIASES[crate][alias_name] = [];
1896                         }
1897                         local_aliases = aliases[alias_name];
1898                         for (j = 0; j < local_aliases.length; ++j) {
1899                             ALIASES[crate][alias_name].push(local_aliases[j] + currentIndex);
1900                         }
1901                     }
1902                 }
1903                 currentIndex += crateSize;
1904             }
1905             return searchWords;
1906         }
1907
1908         function startSearch() {
1909             var callback = function() {
1910                 clearInputTimeout();
1911                 if (search_input.value.length === 0) {
1912                     if (browserSupportsHistoryApi()) {
1913                         history.replaceState("", window.currentCrate + " - Rust", "?search=");
1914                     }
1915                     hideSearchResults();
1916                 } else {
1917                     searchTimeout = setTimeout(search, 500);
1918                 }
1919             };
1920             search_input.onkeyup = callback;
1921             search_input.oninput = callback;
1922             document.getElementsByClassName("search-form")[0].onsubmit = function(e) {
1923                 e.preventDefault();
1924                 clearInputTimeout();
1925                 search();
1926             };
1927             search_input.onchange = function(e) {
1928                 if (e.target !== document.activeElement) {
1929                     // To prevent doing anything when it's from a blur event.
1930                     return;
1931                 }
1932                 // Do NOT e.preventDefault() here. It will prevent pasting.
1933                 clearInputTimeout();
1934                 // zero-timeout necessary here because at the time of event handler execution the
1935                 // pasted content is not in the input field yet. Shouldn’t make any difference for
1936                 // change, though.
1937                 setTimeout(search, 0);
1938             };
1939             search_input.onpaste = search_input.onchange;
1940
1941             var selectCrate = document.getElementById("crate-search");
1942             if (selectCrate) {
1943                 selectCrate.onchange = function() {
1944                     updateLocalStorage("rustdoc-saved-filter-crate", selectCrate.value);
1945                     search(undefined, true);
1946                 };
1947             }
1948
1949             // Push and pop states are used to add search results to the browser
1950             // history.
1951             if (browserSupportsHistoryApi()) {
1952                 // Store the previous <title> so we can revert back to it later.
1953                 var previousTitle = document.title;
1954
1955                 window.addEventListener("popstate", function(e) {
1956                     var params = getQueryStringParams();
1957                     // Revert to the previous title manually since the History
1958                     // API ignores the title parameter.
1959                     document.title = previousTitle;
1960                     // When browsing forward to search results the previous
1961                     // search will be repeated, so the currentResults are
1962                     // cleared to ensure the search is successful.
1963                     currentResults = null;
1964                     // Synchronize search bar with query string state and
1965                     // perform the search. This will empty the bar if there's
1966                     // nothing there, which lets you really go back to a
1967                     // previous state with nothing in the bar.
1968                     if (params.search && params.search.length > 0) {
1969                         search_input.value = params.search;
1970                         // Some browsers fire "onpopstate" for every page load
1971                         // (Chrome), while others fire the event only when actually
1972                         // popping a state (Firefox), which is why search() is
1973                         // called both here and at the end of the startSearch()
1974                         // function.
1975                         search(e);
1976                     } else {
1977                         search_input.value = "";
1978                         // When browsing back from search results the main page
1979                         // visibility must be reset.
1980                         hideSearchResults();
1981                     }
1982                 });
1983             }
1984             search();
1985         }
1986
1987         index = buildIndex(rawSearchIndex);
1988         startSearch();
1989
1990         // Draw a convenient sidebar of known crates if we have a listing
1991         if (rootPath === "../" || rootPath === "./") {
1992             var sidebar = document.getElementsByClassName("sidebar-elems")[0];
1993             if (sidebar) {
1994                 var div = document.createElement("div");
1995                 div.className = "block crate";
1996                 div.innerHTML = "<h3>Crates</h3>";
1997                 var ul = document.createElement("ul");
1998                 div.appendChild(ul);
1999
2000                 var crates = [];
2001                 for (var crate in rawSearchIndex) {
2002                     if (!hasOwnProperty(rawSearchIndex, crate)) {
2003                         continue;
2004                     }
2005                     crates.push(crate);
2006                 }
2007                 crates.sort();
2008                 for (var i = 0; i < crates.length; ++i) {
2009                     var klass = "crate";
2010                     if (rootPath !== "./" && crates[i] === window.currentCrate) {
2011                         klass += " current";
2012                     }
2013                     var link = document.createElement("a");
2014                     link.href = rootPath + crates[i] + "/index.html";
2015                     link.title = rawSearchIndex[crates[i]].doc;
2016                     link.className = klass;
2017                     link.textContent = crates[i];
2018
2019                     var li = document.createElement("li");
2020                     li.appendChild(link);
2021                     ul.appendChild(li);
2022                 }
2023                 sidebar.appendChild(div);
2024             }
2025         }
2026     };
2027
2028
2029     // delayed sidebar rendering.
2030     window.initSidebarItems = function(items) {
2031         var sidebar = document.getElementsByClassName("sidebar-elems")[0];
2032         var current = window.sidebarCurrent;
2033
2034         function block(shortty, longty) {
2035             var filtered = items[shortty];
2036             if (!filtered) {
2037                 return;
2038             }
2039
2040             var div = document.createElement("div");
2041             div.className = "block " + shortty;
2042             var h3 = document.createElement("h3");
2043             h3.textContent = longty;
2044             div.appendChild(h3);
2045             var ul = document.createElement("ul");
2046
2047             var length = filtered.length;
2048             for (var i = 0; i < length; ++i) {
2049                 var item = filtered[i];
2050                 var name = item[0];
2051                 var desc = item[1]; // can be null
2052
2053                 var klass = shortty;
2054                 if (name === current.name && shortty === current.ty) {
2055                     klass += " current";
2056                 }
2057                 var path;
2058                 if (shortty === "mod") {
2059                     path = name + "/index.html";
2060                 } else {
2061                     path = shortty + "." + name + ".html";
2062                 }
2063                 var link = document.createElement("a");
2064                 link.href = current.relpath + path;
2065                 link.title = desc;
2066                 link.className = klass;
2067                 link.textContent = name;
2068                 var li = document.createElement("li");
2069                 li.appendChild(link);
2070                 ul.appendChild(li);
2071             }
2072             div.appendChild(ul);
2073             if (sidebar) {
2074                 sidebar.appendChild(div);
2075             }
2076         }
2077
2078         block("primitive", "Primitive Types");
2079         block("mod", "Modules");
2080         block("macro", "Macros");
2081         block("struct", "Structs");
2082         block("enum", "Enums");
2083         block("union", "Unions");
2084         block("constant", "Constants");
2085         block("static", "Statics");
2086         block("trait", "Traits");
2087         block("fn", "Functions");
2088         block("type", "Type Definitions");
2089         block("foreigntype", "Foreign Types");
2090         block("keyword", "Keywords");
2091         block("traitalias", "Trait Aliases");
2092     };
2093
2094     window.register_implementors = function(imp) {
2095         var implementors = document.getElementById("implementors-list");
2096         var synthetic_implementors = document.getElementById("synthetic-implementors-list");
2097
2098         if (synthetic_implementors) {
2099             // This `inlined_types` variable is used to avoid having the same implementation
2100             // showing up twice. For example "String" in the "Sync" doc page.
2101             //
2102             // By the way, this is only used by and useful for traits implemented automatically
2103             // (like "Send" and "Sync").
2104             var inlined_types = new Set();
2105             onEachLazy(synthetic_implementors.getElementsByClassName("impl"), function(el) {
2106                 var aliases = el.getAttribute("aliases");
2107                 if (!aliases) {
2108                     return;
2109                 }
2110                 aliases.split(",").forEach(function(alias) {
2111                     inlined_types.add(alias);
2112                 });
2113             });
2114         }
2115
2116         var libs = Object.getOwnPropertyNames(imp);
2117         var llength = libs.length;
2118         for (var i = 0; i < llength; ++i) {
2119             if (libs[i] === currentCrate) { continue; }
2120             var structs = imp[libs[i]];
2121
2122             var slength = structs.length;
2123             struct_loop:
2124             for (var j = 0; j < slength; ++j) {
2125                 var struct = structs[j];
2126
2127                 var list = struct.synthetic ? synthetic_implementors : implementors;
2128
2129                 if (struct.synthetic) {
2130                     var stlength = struct.types.length;
2131                     for (var k = 0; k < stlength; k++) {
2132                         if (inlined_types.has(struct.types[k])) {
2133                             continue struct_loop;
2134                         }
2135                         inlined_types.add(struct.types[k]);
2136                     }
2137                 }
2138
2139                 var code = document.createElement("code");
2140                 code.innerHTML = struct.text;
2141
2142                 var x = code.getElementsByTagName("a");
2143                 var xlength = x.length;
2144                 for (var it = 0; it < xlength; it++) {
2145                     var href = x[it].getAttribute("href");
2146                     if (href && href.indexOf("http") !== 0) {
2147                         x[it].setAttribute("href", rootPath + href);
2148                     }
2149                 }
2150                 var display = document.createElement("h3");
2151                 addClass(display, "impl");
2152                 display.innerHTML = "<span class=\"in-band\"><table class=\"table-display\">" +
2153                     "<tbody><tr><td><code>" + code.outerHTML + "</code></td><td></td></tr>" +
2154                     "</tbody></table></span>";
2155                 list.appendChild(display);
2156             }
2157         }
2158     };
2159     if (window.pending_implementors) {
2160         window.register_implementors(window.pending_implementors);
2161     }
2162
2163     function labelForToggleButton(sectionIsCollapsed) {
2164         if (sectionIsCollapsed) {
2165             // button will expand the section
2166             return "+";
2167         }
2168         // button will collapse the section
2169         // note that this text is also set in the HTML template in render.rs
2170         return "\u2212"; // "\u2212" is "−" minus sign
2171     }
2172
2173     function onEveryMatchingChild(elem, className, func) {
2174         if (elem && className && func) {
2175             var length = elem.childNodes.length;
2176             var nodes = elem.childNodes;
2177             for (var i = 0; i < length; ++i) {
2178                 if (hasClass(nodes[i], className)) {
2179                     func(nodes[i]);
2180                 } else {
2181                     onEveryMatchingChild(nodes[i], className, func);
2182                 }
2183             }
2184         }
2185     }
2186
2187     function toggleAllDocs(pageId, fromAutoCollapse) {
2188         var innerToggle = document.getElementById(toggleAllDocsId);
2189         if (!innerToggle) {
2190             return;
2191         }
2192         if (hasClass(innerToggle, "will-expand")) {
2193             updateLocalStorage("rustdoc-collapse", "false");
2194             removeClass(innerToggle, "will-expand");
2195             onEveryMatchingChild(innerToggle, "inner", function(e) {
2196                 e.innerHTML = labelForToggleButton(false);
2197             });
2198             innerToggle.title = "collapse all docs";
2199             if (fromAutoCollapse !== true) {
2200                 onEachLazy(document.getElementsByClassName("collapse-toggle"), function(e) {
2201                     collapseDocs(e, "show");
2202                 });
2203             }
2204         } else {
2205             updateLocalStorage("rustdoc-collapse", "true");
2206             addClass(innerToggle, "will-expand");
2207             onEveryMatchingChild(innerToggle, "inner", function(e) {
2208                 var parent = e.parentNode;
2209                 var superParent = null;
2210
2211                 if (parent) {
2212                     superParent = parent.parentNode;
2213                 }
2214                 if (!parent || !superParent || superParent.id !== "main" ||
2215                     hasClass(parent, "impl") === false) {
2216                     e.innerHTML = labelForToggleButton(true);
2217                 }
2218             });
2219             innerToggle.title = "expand all docs";
2220             if (fromAutoCollapse !== true) {
2221                 onEachLazy(document.getElementsByClassName("collapse-toggle"), function(e) {
2222                     var parent = e.parentNode;
2223                     var superParent = null;
2224
2225                     if (parent) {
2226                         superParent = parent.parentNode;
2227                     }
2228                     if (!parent || !superParent || superParent.id !== "main" ||
2229                         hasClass(parent, "impl") === false) {
2230                         collapseDocs(e, "hide", pageId);
2231                     }
2232                 });
2233             }
2234         }
2235     }
2236
2237     function collapseDocs(toggle, mode, pageId) {
2238         if (!toggle || !toggle.parentNode) {
2239             return;
2240         }
2241
2242         function adjustToggle(arg) {
2243             return function(e) {
2244                 if (hasClass(e, "toggle-label")) {
2245                     if (arg) {
2246                         e.style.display = "inline-block";
2247                     } else {
2248                         e.style.display = "none";
2249                     }
2250                 }
2251                 if (hasClass(e, "inner")) {
2252                     e.innerHTML = labelForToggleButton(arg);
2253                 }
2254             };
2255         }
2256
2257         function implHider(addOrRemove, fullHide) {
2258             return function(n) {
2259                 var is_method = hasClass(n, "method") || fullHide;
2260                 if (is_method || hasClass(n, "type")) {
2261                     if (is_method === true) {
2262                         if (addOrRemove) {
2263                             addClass(n, "hidden-by-impl-hider");
2264                         } else {
2265                             removeClass(n, "hidden-by-impl-hider");
2266                         }
2267                     }
2268                     var ns = n.nextElementSibling;
2269                     while (ns && (hasClass(ns, "docblock") || hasClass(ns, "stability"))) {
2270                         if (addOrRemove) {
2271                             addClass(ns, "hidden-by-impl-hider");
2272                         } else {
2273                             removeClass(ns, "hidden-by-impl-hider");
2274                         }
2275                         ns = ns.nextElementSibling;
2276                     }
2277                 }
2278             };
2279         }
2280
2281         var relatedDoc;
2282         var action = mode;
2283         if (hasClass(toggle.parentNode, "impl") === false) {
2284             relatedDoc = toggle.parentNode.nextElementSibling;
2285             if (hasClass(relatedDoc, "stability")) {
2286                 relatedDoc = relatedDoc.nextElementSibling;
2287             }
2288             if (hasClass(relatedDoc, "docblock") || hasClass(relatedDoc, "sub-variant")) {
2289                 if (mode === "toggle") {
2290                     if (hasClass(relatedDoc, "hidden-by-usual-hider")) {
2291                         action = "show";
2292                     } else {
2293                         action = "hide";
2294                     }
2295                 }
2296                 if (action === "hide") {
2297                     addClass(relatedDoc, "hidden-by-usual-hider");
2298                     onEachLazy(toggle.childNodes, adjustToggle(true));
2299                     addClass(toggle.parentNode, "collapsed");
2300                 } else if (action === "show") {
2301                     removeClass(relatedDoc, "hidden-by-usual-hider");
2302                     removeClass(toggle.parentNode, "collapsed");
2303                     onEachLazy(toggle.childNodes, adjustToggle(false));
2304                 }
2305             }
2306         } else {
2307             // we are collapsing the impl block(s).
2308
2309             var parentElem = toggle.parentNode;
2310             relatedDoc = parentElem;
2311             var docblock = relatedDoc.nextElementSibling;
2312
2313             while (hasClass(relatedDoc, "impl-items") === false) {
2314                 relatedDoc = relatedDoc.nextElementSibling;
2315             }
2316
2317             if (!relatedDoc && hasClass(docblock, "docblock") === false) {
2318                 return;
2319             }
2320
2321             // Hide all functions, but not associated types/consts.
2322
2323             if (mode === "toggle") {
2324                 if (hasClass(relatedDoc, "fns-now-collapsed") ||
2325                     hasClass(docblock, "hidden-by-impl-hider")) {
2326                     action = "show";
2327                 } else {
2328                     action = "hide";
2329                 }
2330             }
2331
2332             var dontApplyBlockRule = toggle.parentNode.parentNode.id !== "main";
2333             if (action === "show") {
2334                 removeClass(relatedDoc, "fns-now-collapsed");
2335                 removeClass(docblock, "hidden-by-usual-hider");
2336                 onEachLazy(toggle.childNodes, adjustToggle(false, dontApplyBlockRule));
2337                 onEachLazy(relatedDoc.childNodes, implHider(false, dontApplyBlockRule));
2338             } else if (action === "hide") {
2339                 addClass(relatedDoc, "fns-now-collapsed");
2340                 addClass(docblock, "hidden-by-usual-hider");
2341                 onEachLazy(toggle.childNodes, adjustToggle(true, dontApplyBlockRule));
2342                 onEachLazy(relatedDoc.childNodes, implHider(true, dontApplyBlockRule));
2343             }
2344         }
2345     }
2346
2347     function collapser(pageId, e, collapse) {
2348         // inherent impl ids are like "impl" or impl-<number>'.
2349         // they will never be hidden by default.
2350         var n = e.parentElement;
2351         if (n.id.match(/^impl(?:-\d+)?$/) === null) {
2352             // Automatically minimize all non-inherent impls
2353             if (collapse || hasClass(n, "impl")) {
2354                 collapseDocs(e, "hide", pageId);
2355             }
2356         }
2357     }
2358
2359     function autoCollapse(pageId, collapse) {
2360         if (collapse) {
2361             toggleAllDocs(pageId, true);
2362         } else if (getSettingValue("auto-hide-trait-implementations") !== "false") {
2363             var impl_list = document.getElementById("trait-implementations-list");
2364
2365             if (impl_list !== null) {
2366                 onEachLazy(impl_list.getElementsByClassName("collapse-toggle"), function(e) {
2367                     collapser(pageId, e, collapse);
2368                 });
2369             }
2370
2371             var blanket_list = document.getElementById("blanket-implementations-list");
2372
2373             if (blanket_list !== null) {
2374                 onEachLazy(blanket_list.getElementsByClassName("collapse-toggle"), function(e) {
2375                     collapser(pageId, e, collapse);
2376                 });
2377             }
2378         }
2379     }
2380
2381     function insertAfter(newNode, referenceNode) {
2382         referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
2383     }
2384
2385     function createSimpleToggle(sectionIsCollapsed) {
2386         var toggle = document.createElement("a");
2387         toggle.href = "javascript:void(0)";
2388         toggle.className = "collapse-toggle";
2389         toggle.innerHTML = "[<span class=\"inner\">" + labelForToggleButton(sectionIsCollapsed) +
2390                            "</span>]";
2391         return toggle;
2392     }
2393
2394     function createToggle(toggle, otherMessage, fontSize, extraClass, show) {
2395         var span = document.createElement("span");
2396         span.className = "toggle-label";
2397         if (show) {
2398             span.style.display = "none";
2399         }
2400         if (!otherMessage) {
2401             span.innerHTML = "&nbsp;Expand&nbsp;description";
2402         } else {
2403             span.innerHTML = otherMessage;
2404         }
2405
2406         if (fontSize) {
2407             span.style.fontSize = fontSize;
2408         }
2409
2410         var mainToggle = toggle.cloneNode(true);
2411         mainToggle.appendChild(span);
2412
2413         var wrapper = document.createElement("div");
2414         wrapper.className = "toggle-wrapper";
2415         if (!show) {
2416             addClass(wrapper, "collapsed");
2417             var inner = mainToggle.getElementsByClassName("inner");
2418             if (inner && inner.length > 0) {
2419                 inner[0].innerHTML = "+";
2420             }
2421         }
2422         if (extraClass) {
2423             addClass(wrapper, extraClass);
2424         }
2425         wrapper.appendChild(mainToggle);
2426         return wrapper;
2427     }
2428
2429     (function() {
2430         var toggles = document.getElementById(toggleAllDocsId);
2431         if (toggles) {
2432             toggles.onclick = toggleAllDocs;
2433         }
2434
2435         var toggle = createSimpleToggle(false);
2436         var hideMethodDocs = getSettingValue("auto-hide-method-docs") === "true";
2437         var hideImplementors = getSettingValue("auto-collapse-implementors") !== "false";
2438         var pageId = getPageId();
2439
2440         var func = function(e) {
2441             var next = e.nextElementSibling;
2442             if (next && hasClass(next, "stability")) {
2443               next = next.nextElementSibling;
2444             }
2445             if (!next) {
2446                 return;
2447             }
2448             if (hasClass(next, "docblock")) {
2449                 var newToggle = toggle.cloneNode(true);
2450                 insertAfter(newToggle, e.childNodes[e.childNodes.length - 1]);
2451                 if (hideMethodDocs === true && hasClass(e, "method") === true) {
2452                     collapseDocs(newToggle, "hide", pageId);
2453                 }
2454             }
2455         };
2456
2457         var funcImpl = function(e) {
2458             var next = e.nextElementSibling;
2459             if (next && hasClass(next, "stability")) {
2460                 next = next.nextElementSibling;
2461             }
2462             if (next && hasClass(next, "docblock")) {
2463                 next = next.nextElementSibling;
2464             }
2465             if (!next) {
2466                 return;
2467             }
2468             if (hasClass(e, "impl") &&
2469                 (next.getElementsByClassName("method").length > 0 ||
2470                  next.getElementsByClassName("associatedconstant").length > 0)) {
2471                 var newToggle = toggle.cloneNode(true);
2472                 insertAfter(newToggle, e.childNodes[e.childNodes.length - 1]);
2473                 // In case the option "auto-collapse implementors" is not set to false, we collapse
2474                 // all implementors.
2475                 if (hideImplementors === true && e.parentNode.id === "implementors-list") {
2476                     collapseDocs(newToggle, "hide", pageId);
2477                 }
2478             }
2479         };
2480
2481         onEachLazy(document.getElementsByClassName("method"), func);
2482         onEachLazy(document.getElementsByClassName("associatedconstant"), func);
2483         onEachLazy(document.getElementsByClassName("impl"), funcImpl);
2484         var impl_call = function() {};
2485         if (hideMethodDocs === true) {
2486             impl_call = function(e, newToggle) {
2487                 if (e.id.match(/^impl(?:-\d+)?$/) === null) {
2488                     // Automatically minimize all non-inherent impls
2489                     if (hasClass(e, "impl") === true) {
2490                         collapseDocs(newToggle, "hide", pageId);
2491                     }
2492                 }
2493             };
2494         }
2495         var newToggle = document.createElement("a");
2496         newToggle.href = "javascript:void(0)";
2497         newToggle.className = "collapse-toggle hidden-default collapsed";
2498         newToggle.innerHTML = "[<span class=\"inner\">" + labelForToggleButton(true) +
2499                               "</span>] Show hidden undocumented items";
2500         function toggleClicked() {
2501             if (hasClass(this, "collapsed")) {
2502                 removeClass(this, "collapsed");
2503                 onEachLazy(this.parentNode.getElementsByClassName("hidden"), function(x) {
2504                     if (hasClass(x, "content") === false) {
2505                         removeClass(x, "hidden");
2506                         addClass(x, "x");
2507                     }
2508                 }, true);
2509                 this.innerHTML = "[<span class=\"inner\">" + labelForToggleButton(false) +
2510                                  "</span>] Hide undocumented items";
2511             } else {
2512                 addClass(this, "collapsed");
2513                 onEachLazy(this.parentNode.getElementsByClassName("x"), function(x) {
2514                     if (hasClass(x, "content") === false) {
2515                         addClass(x, "hidden");
2516                         removeClass(x, "x");
2517                     }
2518                 }, true);
2519                 this.innerHTML = "[<span class=\"inner\">" + labelForToggleButton(true) +
2520                                  "</span>] Show hidden undocumented items";
2521             }
2522         }
2523         onEachLazy(document.getElementsByClassName("impl-items"), function(e) {
2524             onEachLazy(e.getElementsByClassName("associatedconstant"), func);
2525             var hiddenElems = e.getElementsByClassName("hidden");
2526             var needToggle = false;
2527
2528             var hlength = hiddenElems.length;
2529             for (var i = 0; i < hlength; ++i) {
2530                 if (hasClass(hiddenElems[i], "content") === false &&
2531                     hasClass(hiddenElems[i], "docblock") === false) {
2532                     needToggle = true;
2533                     break;
2534                 }
2535             }
2536             if (needToggle === true) {
2537                 var inner_toggle = newToggle.cloneNode(true);
2538                 inner_toggle.onclick = toggleClicked;
2539                 e.insertBefore(inner_toggle, e.firstChild);
2540                 impl_call(e.previousSibling, inner_toggle);
2541             }
2542         });
2543
2544         var currentType = document.getElementsByClassName("type-decl")[0];
2545         var className = null;
2546         if (currentType) {
2547             currentType = currentType.getElementsByClassName("rust")[0];
2548             if (currentType) {
2549                 currentType.classList.forEach(function(item) {
2550                     if (item !== "main") {
2551                         className = item;
2552                         return true;
2553                     }
2554                 });
2555             }
2556         }
2557         var showItemDeclarations = getSettingValue("auto-hide-" + className);
2558         if (showItemDeclarations === null) {
2559             if (className === "enum" || className === "macro") {
2560                 showItemDeclarations = "false";
2561             } else if (className === "struct" || className === "union" || className === "trait") {
2562                 showItemDeclarations = "true";
2563             } else {
2564                 // In case we found an unknown type, we just use the "parent" value.
2565                 showItemDeclarations = getSettingValue("auto-hide-declarations");
2566             }
2567         }
2568         showItemDeclarations = showItemDeclarations === "false";
2569         function buildToggleWrapper(e) {
2570             if (hasClass(e, "autohide")) {
2571                 var wrap = e.previousElementSibling;
2572                 if (wrap && hasClass(wrap, "toggle-wrapper")) {
2573                     var inner_toggle = wrap.childNodes[0];
2574                     var extra = e.childNodes[0].tagName === "H3";
2575
2576                     e.style.display = "none";
2577                     addClass(wrap, "collapsed");
2578                     onEachLazy(inner_toggle.getElementsByClassName("inner"), function(e) {
2579                         e.innerHTML = labelForToggleButton(true);
2580                     });
2581                     onEachLazy(inner_toggle.getElementsByClassName("toggle-label"), function(e) {
2582                         e.style.display = "inline-block";
2583                         if (extra === true) {
2584                             e.innerHTML = " Show " + e.childNodes[0].innerHTML;
2585                         }
2586                     });
2587                 }
2588             }
2589             if (e.parentNode.id === "main") {
2590                 var otherMessage = "";
2591                 var fontSize;
2592                 var extraClass;
2593
2594                 if (hasClass(e, "type-decl")) {
2595                     fontSize = "20px";
2596                     otherMessage = "&nbsp;Show&nbsp;declaration";
2597                     if (showItemDeclarations === false) {
2598                         extraClass = "collapsed";
2599                     }
2600                 } else if (hasClass(e, "sub-variant")) {
2601                     otherMessage = "&nbsp;Show&nbsp;fields";
2602                 } else if (hasClass(e, "non-exhaustive")) {
2603                     otherMessage = "&nbsp;This&nbsp;";
2604                     if (hasClass(e, "non-exhaustive-struct")) {
2605                         otherMessage += "struct";
2606                     } else if (hasClass(e, "non-exhaustive-enum")) {
2607                         otherMessage += "enum";
2608                     } else if (hasClass(e, "non-exhaustive-variant")) {
2609                         otherMessage += "enum variant";
2610                     } else if (hasClass(e, "non-exhaustive-type")) {
2611                         otherMessage += "type";
2612                     }
2613                     otherMessage += "&nbsp;is&nbsp;marked&nbsp;as&nbsp;non-exhaustive";
2614                 } else if (hasClass(e.childNodes[0], "impl-items")) {
2615                     extraClass = "marg-left";
2616                 }
2617
2618                 e.parentNode.insertBefore(
2619                     createToggle(
2620                         toggle,
2621                         otherMessage,
2622                         fontSize,
2623                         extraClass,
2624                         hasClass(e, "type-decl") === false || showItemDeclarations === true),
2625                     e);
2626                 if (hasClass(e, "type-decl") === true && showItemDeclarations === true) {
2627                     collapseDocs(e.previousSibling.childNodes[0], "toggle");
2628                 }
2629                 if (hasClass(e, "non-exhaustive") === true) {
2630                     collapseDocs(e.previousSibling.childNodes[0], "toggle");
2631                 }
2632             }
2633         }
2634
2635         onEachLazy(document.getElementsByClassName("docblock"), buildToggleWrapper);
2636         onEachLazy(document.getElementsByClassName("sub-variant"), buildToggleWrapper);
2637         var pageId = getPageId();
2638
2639         autoCollapse(pageId, getSettingValue("collapse") === "true");
2640
2641         if (pageId !== null) {
2642             expandSection(pageId);
2643         }
2644     }());
2645
2646     function createToggleWrapper(tog) {
2647         var span = document.createElement("span");
2648         span.className = "toggle-label";
2649         span.style.display = "none";
2650         span.innerHTML = "&nbsp;Expand&nbsp;attributes";
2651         tog.appendChild(span);
2652
2653         var wrapper = document.createElement("div");
2654         wrapper.className = "toggle-wrapper toggle-attributes";
2655         wrapper.appendChild(tog);
2656         return wrapper;
2657     }
2658
2659     (function() {
2660         // To avoid checking on "rustdoc-item-attributes" value on every loop...
2661         var itemAttributesFunc = function() {};
2662         if (getSettingValue("auto-hide-attributes") !== "false") {
2663             itemAttributesFunc = function(x) {
2664                 collapseDocs(x.previousSibling.childNodes[0], "toggle");
2665             };
2666         }
2667         var attributesToggle = createToggleWrapper(createSimpleToggle(false));
2668         onEachLazy(main.getElementsByClassName("attributes"), function(i_e) {
2669             var attr_tog = attributesToggle.cloneNode(true);
2670             if (hasClass(i_e, "top-attr") === true) {
2671                 addClass(attr_tog, "top-attr");
2672             }
2673             i_e.parentNode.insertBefore(attr_tog, i_e);
2674             itemAttributesFunc(i_e);
2675         });
2676     }());
2677
2678     (function() {
2679         // To avoid checking on "rustdoc-line-numbers" value on every loop...
2680         var lineNumbersFunc = function() {};
2681         if (getSettingValue("line-numbers") === "true") {
2682             lineNumbersFunc = function(x) {
2683                 var count = x.textContent.split("\n").length;
2684                 var elems = [];
2685                 for (var i = 0; i < count; ++i) {
2686                     elems.push(i + 1);
2687                 }
2688                 var node = document.createElement("pre");
2689                 addClass(node, "line-number");
2690                 node.innerHTML = elems.join("\n");
2691                 x.parentNode.insertBefore(node, x);
2692             };
2693         }
2694         onEachLazy(document.getElementsByClassName("rust-example-rendered"), function(e) {
2695             if (hasClass(e, "compile_fail")) {
2696                 e.addEventListener("mouseover", function() {
2697                     this.parentElement.previousElementSibling.childNodes[0].style.color = "#f00";
2698                 });
2699                 e.addEventListener("mouseout", function() {
2700                     this.parentElement.previousElementSibling.childNodes[0].style.color = "";
2701                 });
2702             } else if (hasClass(e, "ignore")) {
2703                 e.addEventListener("mouseover", function() {
2704                     this.parentElement.previousElementSibling.childNodes[0].style.color = "#ff9200";
2705                 });
2706                 e.addEventListener("mouseout", function() {
2707                     this.parentElement.previousElementSibling.childNodes[0].style.color = "";
2708                 });
2709             }
2710             lineNumbersFunc(e);
2711         });
2712     }());
2713
2714     onEachLazy(document.getElementsByClassName("notable-traits"), function(e) {
2715         e.onclick = function() {
2716             this.getElementsByClassName('notable-traits-tooltiptext')[0]
2717                 .classList.toggle("force-tooltip");
2718         };
2719     });
2720
2721     // In the search display, allows to switch between tabs.
2722     function printTab(nb) {
2723         if (nb === 0 || nb === 1 || nb === 2) {
2724             currentTab = nb;
2725         }
2726         var nb_copy = nb;
2727         onEachLazy(document.getElementById("titles").childNodes, function(elem) {
2728             if (nb_copy === 0) {
2729                 addClass(elem, "selected");
2730             } else {
2731                 removeClass(elem, "selected");
2732             }
2733             nb_copy -= 1;
2734         });
2735         onEachLazy(document.getElementById("results").childNodes, function(elem) {
2736             if (nb === 0) {
2737                 elem.style.display = "";
2738             } else {
2739                 elem.style.display = "none";
2740             }
2741             nb -= 1;
2742         });
2743     }
2744
2745     function putBackSearch(search_input) {
2746         var search = getSearchElement();
2747         if (search_input.value !== "" && hasClass(search, "hidden")) {
2748             showSearchResults(search);
2749             if (browserSupportsHistoryApi()) {
2750                 history.replaceState(search_input.value,
2751                                      "",
2752                                      "?search=" + encodeURIComponent(search_input.value));
2753             }
2754             document.title = searchTitle;
2755         }
2756     }
2757
2758     function getSearchLoadingText() {
2759         return "Loading search results...";
2760     }
2761
2762     if (search_input) {
2763         search_input.onfocus = function() {
2764             putBackSearch(this);
2765         };
2766     }
2767
2768     var params = getQueryStringParams();
2769     if (params && params.search) {
2770         var search = getSearchElement();
2771         search.innerHTML = "<h3 style=\"text-align: center;\">" + getSearchLoadingText() + "</h3>";
2772         showSearchResults(search);
2773     }
2774
2775     var sidebar_menu = document.getElementsByClassName("sidebar-menu")[0];
2776     if (sidebar_menu) {
2777         sidebar_menu.onclick = function() {
2778             var sidebar = document.getElementsByClassName("sidebar")[0];
2779             if (hasClass(sidebar, "mobile") === true) {
2780                 hideSidebar();
2781             } else {
2782                 showSidebar();
2783             }
2784         };
2785     }
2786
2787     if (main) {
2788         onEachLazy(main.getElementsByClassName("loading-content"), function(e) {
2789             e.remove();
2790         });
2791         onEachLazy(main.childNodes, function(e) {
2792             // Unhide the actual content once loading is complete. Headers get
2793             // flex treatment for their horizontal layout, divs get block treatment
2794             // for vertical layout (column-oriented flex layout for divs caused
2795             // errors in mobile browsers).
2796             if (e.tagName === "H2" || e.tagName === "H3") {
2797                 var nextTagName = e.nextElementSibling.tagName;
2798                 if (nextTagName == "H2" || nextTagName == "H3") {
2799                     e.nextElementSibling.style.display = "flex";
2800                 } else {
2801                     e.nextElementSibling.style.display = "block";
2802                 }
2803             }
2804         });
2805     }
2806
2807     function enableSearchInput() {
2808         if (search_input) {
2809             search_input.removeAttribute('disabled');
2810         }
2811     }
2812
2813     window.addSearchOptions = function(crates) {
2814         var elem = document.getElementById("crate-search");
2815
2816         if (!elem) {
2817             enableSearchInput();
2818             return;
2819         }
2820         var crates_text = [];
2821         if (Object.keys(crates).length > 1) {
2822             for (var crate in crates) {
2823                 if (hasOwnProperty(crates, crate)) {
2824                     crates_text.push(crate);
2825                 }
2826             }
2827         }
2828         crates_text.sort(function(a, b) {
2829             var lower_a = a.toLowerCase();
2830             var lower_b = b.toLowerCase();
2831
2832             if (lower_a < lower_b) {
2833                 return -1;
2834             } else if (lower_a > lower_b) {
2835                 return 1;
2836             }
2837             return 0;
2838         });
2839         var savedCrate = getSettingValue("saved-filter-crate");
2840         for (var i = 0; i < crates_text.length; ++i) {
2841             var option = document.createElement("option");
2842             option.value = crates_text[i];
2843             option.innerText = crates_text[i];
2844             elem.appendChild(option);
2845             // Set the crate filter from saved storage, if the current page has the saved crate
2846             // filter.
2847             //
2848             // If not, ignore the crate filter -- we want to support filtering for crates on sites
2849             // like doc.rust-lang.org where the crates may differ from page to page while on the
2850             // same domain.
2851             if (crates_text[i] === savedCrate) {
2852                 elem.value = savedCrate;
2853             }
2854         }
2855         enableSearchInput();
2856     };
2857
2858     function buildHelperPopup() {
2859         var popup = document.createElement("aside");
2860         addClass(popup, "hidden");
2861         popup.id = "help";
2862
2863         var book_info = document.createElement("span");
2864         book_info.innerHTML = "You can find more information in \
2865             <a href=\"https://doc.rust-lang.org/rustdoc/\">the rustdoc book</a>.";
2866
2867         var container = document.createElement("div");
2868         var shortcuts = [
2869             ["?", "Show this help dialog"],
2870             ["S", "Focus the search field"],
2871             ["T", "Focus the theme picker menu"],
2872             ["↑", "Move up in search results"],
2873             ["↓", "Move down in search results"],
2874             ["↹", "Switch tab"],
2875             ["&#9166;", "Go to active search result"],
2876             ["+", "Expand all sections"],
2877             ["-", "Collapse all sections"],
2878         ].map(x => "<dt><kbd>" + x[0] + "</kbd></dt><dd>" + x[1] + "</dd>").join("");
2879         var div_shortcuts = document.createElement("div");
2880         addClass(div_shortcuts, "shortcuts");
2881         div_shortcuts.innerHTML = "<h2>Keyboard Shortcuts</h2><dl>" + shortcuts + "</dl></div>";
2882
2883         var infos = [
2884             "Prefix searches with a type followed by a colon (e.g., <code>fn:</code>) to \
2885              restrict the search to a given item kind.",
2886             "Accepted kinds are: <code>fn</code>, <code>mod</code>, <code>struct</code>, \
2887              <code>enum</code>, <code>trait</code>, <code>type</code>, <code>macro</code>, \
2888              and <code>const</code>.",
2889             "Search functions by type signature (e.g., <code>vec -&gt; usize</code> or \
2890              <code>* -&gt; vec</code>)",
2891             "Search multiple things at once by splitting your query with comma (e.g., \
2892              <code>str,u8</code> or <code>String,struct:Vec,test</code>)",
2893             "You can look for items with an exact name by putting double quotes around \
2894              your request: <code>\"string\"</code>",
2895             "Look for items inside another one by searching for a path: <code>vec::Vec</code>",
2896         ].map(x => "<p>" + x + "</p>").join("");
2897         var div_infos = document.createElement("div");
2898         addClass(div_infos, "infos");
2899         div_infos.innerHTML = "<h2>Search Tricks</h2>" + infos;
2900
2901         container.appendChild(book_info);
2902         container.appendChild(div_shortcuts);
2903         container.appendChild(div_infos);
2904
2905         popup.appendChild(container);
2906         insertAfter(popup, getSearchElement());
2907         // So that it's only built once and then it'll do nothing when called!
2908         buildHelperPopup = function() {};
2909     }
2910
2911     onHashChange(null);
2912     window.onhashchange = onHashChange;
2913 }());
2914
2915 // This is required in firefox. Explanations: when going back in the history, firefox doesn't re-run
2916 // the JS, therefore preventing rustdoc from setting a few things required to be able to reload the
2917 // previous search results (if you navigated to a search result with the keyboard, pressed enter on
2918 // it to navigate to that result, and then came back to this page).
2919 window.onunload = function(){};