]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/html/static/main.js
Add "-" shortcut
[rust.git] / src / librustdoc / html / static / main.js
1 /*!
2  * Copyright 2014 The Rust Project Developers. See the COPYRIGHT
3  * file at the top-level directory of this distribution and at
4  * http://rust-lang.org/COPYRIGHT.
5  *
6  * Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
7  * http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
8  * <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
9  * option. This file may not be copied, modified, or distributed
10  * except according to those terms.
11  */
12
13 /*jslint browser: true, es5: true */
14 /*globals $: true, rootPath: true */
15
16 (function() {
17     "use strict";
18
19     // This mapping table should match the discriminants of
20     // `rustdoc::html::item_type::ItemType` type in Rust.
21     var itemTypes = ["mod",
22                      "externcrate",
23                      "import",
24                      "struct",
25                      "enum",
26                      "fn",
27                      "type",
28                      "static",
29                      "trait",
30                      "impl",
31                      "tymethod",
32                      "method",
33                      "structfield",
34                      "variant",
35                      "macro",
36                      "primitive",
37                      "associatedtype",
38                      "constant",
39                      "associatedconstant",
40                      "union"];
41
42     // On the search screen, so you remain on the last tab you opened.
43     //
44     // 0 for "Types/modules"
45     // 1 for "As parameters"
46     // 2 for "As return value"
47     var currentTab = 0;
48
49     function hasClass(elem, className) {
50         if (elem && className && elem.className) {
51             var elemClass = elem.className;
52             var start = elemClass.indexOf(className);
53             if (start == -1) {
54                 return false;
55             } else if (elemClass.length == className.length) {
56                 return true;
57             } else {
58                 if (start > 0 && elemClass[start - 1] != ' ') {
59                     return false;
60                 }
61                 var end = start + className.length;
62                 if (end < elemClass.length && elemClass[end] != ' ') {
63                     return false;
64                 }
65                 return true;
66             }
67         }
68         return false;
69     }
70
71     function addClass(elem, className) {
72         if (elem && className && !hasClass(elem, className)) {
73             if (elem.className && elem.className.length > 0) {
74                 elem.className += ' ' + className;
75             } else {
76                 elem.className = className;
77             }
78         }
79     }
80
81     function removeClass(elem, className) {
82         if (elem && className && elem.className) {
83             elem.className = (" " + elem.className + " ").replace(" " + className + " ", " ")
84                                                          .trim();
85         }
86     }
87
88     function onEach(arr, func) {
89         if (arr && arr.length > 0 && func) {
90             for (var i = 0; i < arr.length; i++) {
91                 func(arr[i]);
92             }
93         }
94     }
95
96     function isHidden(elem) {
97         return (elem.offsetParent === null)
98     }
99
100     // used for special search precedence
101     var TY_PRIMITIVE = itemTypes.indexOf("primitive");
102
103     onEach(document.getElementsByClassName('js-only'), function(e) {
104         removeClass(e, 'js-only');
105     });
106
107     function getQueryStringParams() {
108         var params = {};
109         window.location.search.substring(1).split("&").
110             map(function(s) {
111                 var pair = s.split("=");
112                 params[decodeURIComponent(pair[0])] =
113                     typeof pair[1] === "undefined" ?
114                             null : decodeURIComponent(pair[1]);
115             });
116         return params;
117     }
118
119     function browserSupportsHistoryApi() {
120         return document.location.protocol != "file:" &&
121           window.history && typeof window.history.pushState === "function";
122     }
123
124     function highlightSourceLines(ev) {
125         var i, from, to, match = window.location.hash.match(/^#?(\d+)(?:-(\d+))?$/);
126         if (match) {
127             from = parseInt(match[1], 10);
128             to = Math.min(50000, parseInt(match[2] || match[1], 10));
129             from = Math.min(from, to);
130             var elem = document.getElementById(from);
131             if (!elem) {
132                 return;
133             }
134             if (ev === null) {
135                 var x = document.getElementById(from);
136                 if (x) {
137                     x.scrollIntoView();
138                 }
139             };
140             onEach(document.getElementsByClassName('line-numbers'), function(e) {
141                 onEach(e.getElementsByTagName('span'), function(i_e) {
142                     removeClass(i_e, 'line-highlighted');
143                 });
144             })
145             for (i = from; i <= to; ++i) {
146                 addClass(document.getElementById(i), 'line-highlighted');
147             }
148         }
149     }
150     highlightSourceLines(null);
151     window.onhashchange = highlightSourceLines;
152
153     // Gets the human-readable string for the virtual-key code of the
154     // given KeyboardEvent, ev.
155     //
156     // This function is meant as a polyfill for KeyboardEvent#key,
157     // since it is not supported in Trident.  We also test for
158     // KeyboardEvent#keyCode because the handleShortcut handler is
159     // also registered for the keydown event, because Blink doesn't fire
160     // keypress on hitting the Escape key.
161     //
162     // So I guess you could say things are getting pretty interoperable.
163     function getVirtualKey(ev) {
164         if ("key" in ev && typeof ev.key != "undefined")
165             return ev.key;
166
167         var c = ev.charCode || ev.keyCode;
168         if (c == 27)
169             return "Escape";
170         return String.fromCharCode(c);
171     }
172
173     function displayHelp(display, ev) {
174         if (display === true) {
175             if (hasClass(help, "hidden")) {
176                 ev.preventDefault();
177                 removeClass(help, "hidden");
178                 addClass(document.body, "blur");
179             }
180         } else if (!hasClass(help, "hidden")) {
181             ev.preventDefault();
182             addClass(help, "hidden");
183             removeClass(document.body, "blur");
184         }
185     }
186
187     function handleShortcut(ev) {
188         if (document.activeElement.tagName === "INPUT")
189             return;
190
191         // Don't interfere with browser shortcuts
192         if (ev.ctrlKey || ev.altKey || ev.metaKey)
193             return;
194
195         var help = document.getElementById("help");
196         switch (getVirtualKey(ev)) {
197         case "Escape":
198             var search = document.getElementById("search");
199             if (!hasClass(help, "hidden")) {
200                 displayHelp(false, ev);
201             } else if (!hasClass(search, "hidden")) {
202                 ev.preventDefault();
203                 addClass(search, "hidden");
204                 removeClass(document.getElementById("main"), "hidden");
205             }
206             break;
207
208         case "s":
209         case "S":
210             displayHelp(false, ev);
211             ev.preventDefault();
212             focusSearchBar();
213             break;
214
215         case "+":
216         case "-":
217             ev.preventDefault();
218             toggleAllDocs();
219             break;
220
221         case "?":
222             if (ev.shiftKey) {
223                 displayHelp(true, ev);
224             }
225             break;
226         }
227     }
228
229     document.onkeypress = handleShortcut;
230     document.onkeydown = handleShortcut;
231     document.onclick = function(ev) {
232         if (hasClass(ev.target, 'collapse-toggle')) {
233             collapseDocs(ev.target);
234         } else if (hasClass(ev.target.parentNode, 'collapse-toggle')) {
235             collapseDocs(ev.target.parentNode);
236         } else if (ev.target.tagName === 'SPAN' && hasClass(ev.target.parentNode, 'line-numbers')) {
237             var prev_id = 0;
238
239             var set_fragment = function (name) {
240                 if (browserSupportsHistoryApi()) {
241                     history.replaceState(null, null, '#' + name);
242                     window.hashchange();
243                 } else {
244                     location.replace('#' + name);
245                 }
246             };
247
248             var cur_id = parseInt(ev.target.id, 10);
249
250             if (ev.shiftKey && prev_id) {
251                 if (prev_id > cur_id) {
252                     var tmp = prev_id;
253                     prev_id = cur_id;
254                     cur_id = tmp;
255                 }
256
257                 set_fragment(prev_id + '-' + cur_id);
258             } else {
259                 prev_id = cur_id;
260
261                 set_fragment(cur_id);
262             }
263         } else if (!hasClass(document.getElementById("help"), "hidden")) {
264             addClass(document.getElementById("help"), "hidden");
265             removeClass(document.body, "blur");
266         }
267     };
268
269     var x = document.getElementsByClassName('version-selector');
270     if (x.length > 0) {
271         x[0].onchange = function() {
272             var i, match,
273                 url = document.location.href,
274                 stripped = '',
275                 len = rootPath.match(/\.\.\//g).length + 1;
276
277             for (i = 0; i < len; ++i) {
278                 match = url.match(/\/[^\/]*$/);
279                 if (i < len - 1) {
280                     stripped = match[0] + stripped;
281                 }
282                 url = url.substring(0, url.length - match[0].length);
283             }
284
285             url += '/' + document.getElementsByClassName('version-selector')[0].value + stripped;
286
287             document.location.href = url;
288         };
289     }
290
291     /**
292      * A function to compute the Levenshtein distance between two strings
293      * Licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported
294      * Full License can be found at http://creativecommons.org/licenses/by-sa/3.0/legalcode
295      * This code is an unmodified version of the code written by Marco de Wit
296      * and was found at http://stackoverflow.com/a/18514751/745719
297      */
298     var levenshtein = (function() {
299         var row2 = [];
300         return function(s1, s2) {
301             if (s1 === s2) {
302                 return 0;
303             }
304             var s1_len = s1.length, s2_len = s2.length;
305             if (s1_len && s2_len) {
306                 var i1 = 0, i2 = 0, a, b, c, c2, row = row2;
307                 while (i1 < s1_len) {
308                     row[i1] = ++i1;
309                 }
310                 while (i2 < s2_len) {
311                     c2 = s2.charCodeAt(i2);
312                     a = i2;
313                     ++i2;
314                     b = i2;
315                     for (i1 = 0; i1 < s1_len; ++i1) {
316                         c = a + (s1.charCodeAt(i1) !== c2 ? 1 : 0);
317                         a = row[i1];
318                         b = b < a ? (b < c ? b + 1 : c) : (a < c ? a + 1 : c);
319                         row[i1] = b;
320                     }
321                 }
322                 return b;
323             }
324             return s1_len + s2_len;
325         };
326     })();
327
328     function initSearch(rawSearchIndex) {
329         var currentResults, index, searchIndex;
330         var MAX_LEV_DISTANCE = 3;
331         var params = getQueryStringParams();
332
333         // Populate search bar with query string search term when provided,
334         // but only if the input bar is empty. This avoid the obnoxious issue
335         // where you start trying to do a search, and the index loads, and
336         // suddenly your search is gone!
337         if (document.getElementsByClassName("search-input")[0].value === "") {
338             document.getElementsByClassName("search-input")[0].value = params.search || '';
339         }
340
341         /**
342          * Executes the query and builds an index of results
343          * @param  {[Object]} query     [The user query]
344          * @param  {[type]} max         [The maximum results returned]
345          * @param  {[type]} searchWords [The list of search words to query
346          *                               against]
347          * @return {[type]}             [A search index of results]
348          */
349         function execQuery(query, max, searchWords) {
350             var valLower = query.query.toLowerCase(),
351                 val = valLower,
352                 typeFilter = itemTypeFromName(query.type),
353                 results = {},
354                 split = valLower.split("::");
355
356             // remove empty keywords
357             for (var j = 0; j < split.length; ++j) {
358                 split[j].toLowerCase();
359                 if (split[j] === "") {
360                     split.splice(j, 1);
361                 }
362             }
363
364             function min(a, b) {
365                 if (a < b) {
366                     return a;
367                 }
368                 return b;
369             }
370
371             function nbElements(obj) {
372                 var size = 0, key;
373                 for (key in obj) {
374                     if (obj.hasOwnProperty(key)) {
375                         size += 1;
376                     }
377                 }
378                 return size;
379             }
380
381             function findArg(obj, val) {
382                 var lev_distance = MAX_LEV_DISTANCE + 1;
383                 if (obj && obj.type && obj.type.inputs.length > 0) {
384                     for (var i = 0; i < obj.type.inputs.length; i++) {
385                         if (obj.type.inputs[i].name === val) {
386                             // No need to check anything else: we found it. Let's just move on.
387                             return 0;
388                         }
389                         lev_distance = min(levenshtein(obj.type.inputs[i].name, val), lev_distance);
390                         if (lev_distance === 0) {
391                             return 0;
392                         }
393                     }
394                 }
395                 return lev_distance;
396             }
397
398             function checkReturned(obj, val) {
399                 var lev_distance = MAX_LEV_DISTANCE + 1;
400                 if (obj && obj.type && obj.type.output) {
401                     if (obj.type.output.name.toLowerCase() === val) {
402                         return 0;
403                     }
404                     lev_distance = min(levenshtein(obj.type.output.name, val));
405                     if (lev_distance === 0) {
406                         return 0;
407                     }
408                 }
409                 return lev_distance;
410             }
411
412             function typePassesFilter(filter, type) {
413                 // No filter
414                 if (filter < 0) return true;
415
416                 // Exact match
417                 if (filter === type) return true;
418
419                 // Match related items
420                 var name = itemTypes[type];
421                 switch (itemTypes[filter]) {
422                     case "constant":
423                         return (name == "associatedconstant");
424                     case "fn":
425                         return (name == "method" || name == "tymethod");
426                     case "type":
427                         return (name == "primitive");
428                 }
429
430                 // No match
431                 return false;
432             }
433
434             // quoted values mean literal search
435             var nSearchWords = searchWords.length;
436             if ((val.charAt(0) === "\"" || val.charAt(0) === "'") &&
437                 val.charAt(val.length - 1) === val.charAt(0))
438             {
439                 val = val.substr(1, val.length - 2).toLowerCase();
440                 for (var i = 0; i < nSearchWords; ++i) {
441                     var ty = searchIndex[i];
442                     if (searchWords[i] === val) {
443                         // filter type: ... queries
444                         if (typePassesFilter(typeFilter, searchIndex[i].ty)) {
445                             results[ty.path + ty.name] = {id: i, index: -1};
446                         }
447                     } else if (findArg(searchIndex[i], val) ||
448                                (ty.type &&
449                                 ty.type.output &&
450                                 ty.type.output.name === val)) {
451                         if (typePassesFilter(typeFilter, searchIndex[i].ty)) {
452                             results[ty.path + ty.name] = {
453                                 id: i,
454                                 index: -1,
455                                 dontValidate: true,
456                             };
457                         }
458                     }
459                     if (nbElements(results) === max) {
460                         break;
461                     }
462                 }
463                 query.inputs = [val];
464                 query.output = val;
465                 query.search = val;
466             // searching by type
467             } else if (val.search("->") > -1) {
468                 var trimmer = function (s) { return s.trim(); };
469                 var parts = val.split("->").map(trimmer);
470                 var input = parts[0];
471                 // sort inputs so that order does not matter
472                 var inputs = input.split(",").map(trimmer).sort();
473                 var output = parts[1];
474
475                 for (var i = 0; i < nSearchWords; ++i) {
476                     var type = searchIndex[i].type;
477                     var ty = searchIndex[i];
478                     if (!type) {
479                         continue;
480                     }
481
482                     // sort index inputs so that order does not matter
483                     var typeInputs = type.inputs.map(function (input) {
484                         return input.name;
485                     }).sort();
486
487                     // allow searching for void (no output) functions as well
488                     var typeOutput = type.output ? type.output.name : "";
489                     if (output === "*" || output == typeOutput) {
490                         if (input === "*") {
491                             results[ty.path + ty.name] = {id: i, index: -1, dontValidate: true};
492                         } else {
493                             var allFound = true;
494                             for (var it = 0; allFound === true && it < inputs.length; it++) {
495                                 var found = false;
496                                 for (var y = 0; found === false && y < typeInputs.length; y++) {
497                                     found = typeInputs[y] === inputs[it];
498                                 }
499                                 allFound = found;
500                             }
501                             if (allFound === true) {
502                                 results[ty.path + ty.name] = {
503                                     id: i,
504                                     index: -1,
505                                     dontValidate: true,
506                                 };
507                             }
508                         }
509                     }
510                 }
511                 query.inputs = inputs;
512                 query.output = output;
513             } else {
514                 query.inputs = [val];
515                 query.output = val;
516                 query.search = val;
517                 // gather matching search results up to a certain maximum
518                 val = val.replace(/\_/g, "");
519                 for (var i = 0; i < split.length; ++i) {
520                     for (var j = 0; j < nSearchWords; ++j) {
521                         var lev_distance;
522                         var ty = searchIndex[j];
523                         if (!ty) {
524                             continue;
525                         }
526                         if (searchWords[j].indexOf(split[i]) > -1 ||
527                             searchWords[j].indexOf(val) > -1 ||
528                             searchWords[j].replace(/_/g, "").indexOf(val) > -1)
529                         {
530                             // filter type: ... queries
531                             if (typePassesFilter(typeFilter, searchIndex[j].ty)) {
532                                 results[ty.path + ty.name] = {
533                                     id: j,
534                                     index: searchWords[j].replace(/_/g, "").indexOf(val),
535                                     lev: 0,
536                                 };
537                             }
538                         } else if (
539                             (lev_distance = levenshtein(searchWords[j], val)) <= MAX_LEV_DISTANCE) {
540                             if (typePassesFilter(typeFilter, searchIndex[j].ty)) {
541                                 if (results[ty.path + ty.name] === undefined ||
542                                     results[ty.path + ty.name].lev > lev_distance) {
543                                     results[ty.path + ty.name] = {
544                                         id: j,
545                                         index: 0,
546                                         // we want lev results to go lower than others
547                                         lev: lev_distance,
548                                     };
549                                 }
550                             }
551                         } else if (
552                             (lev_distance = findArg(searchIndex[j], val)) <= MAX_LEV_DISTANCE) {
553                             if (typePassesFilter(typeFilter, searchIndex[j].ty)) {
554                                 if (results[ty.path + ty.name] === undefined ||
555                                     results[ty.path + ty.name].lev > lev_distance) {
556                                     results[ty.path + ty.name] = {
557                                         id: j,
558                                         index: 0,
559                                         // we want lev results to go lower than others
560                                         lev: lev_distance,
561                                     };
562                                 }
563                             }
564                         } else if (
565                             (lev_distance = checkReturned(searchIndex[j], val)) <=
566                             MAX_LEV_DISTANCE) {
567                             if (typePassesFilter(typeFilter, searchIndex[j].ty)) {
568                                 if (results[ty.path + ty.name] === undefined ||
569                                     results[ty.path + ty.name].lev > lev_distance) {
570                                     results[ty.path + ty.name] = {
571                                         id: j,
572                                         index: 0,
573                                         // we want lev results to go lower than others
574                                         lev: lev_distance,
575                                     };
576                                 }
577                             }
578                         }
579                         if (nbElements(results) === max) {
580                             break;
581                         }
582                     }
583                 }
584             }
585
586             var ar = [];
587             for (var entry in results) {
588                 if (results.hasOwnProperty(entry)) {
589                     ar.push(results[entry]);
590                 }
591             }
592             results = ar;
593             var nresults = results.length;
594             for (var i = 0; i < nresults; ++i) {
595                 results[i].word = searchWords[results[i].id];
596                 results[i].item = searchIndex[results[i].id] || {};
597             }
598             // if there are no results then return to default and fail
599             if (results.length === 0) {
600                 return [];
601             }
602
603             results.sort(function sortResults(aaa, bbb) {
604                 var a, b;
605
606                 // Sort by non levenshtein results and then levenshtein results by the distance
607                 // (less changes required to match means higher rankings)
608                 a = (aaa.lev);
609                 b = (bbb.lev);
610                 if (a !== b) { return a - b; }
611
612                 // sort by crate (non-current crate goes later)
613                 a = (aaa.item.crate !== window.currentCrate);
614                 b = (bbb.item.crate !== window.currentCrate);
615                 if (a !== b) { return a - b; }
616
617                 // sort by exact match (mismatch goes later)
618                 a = (aaa.word !== valLower);
619                 b = (bbb.word !== valLower);
620                 if (a !== b) { return a - b; }
621
622                 // sort by item name length (longer goes later)
623                 a = aaa.word.length;
624                 b = bbb.word.length;
625                 if (a !== b) { return a - b; }
626
627                 // sort by item name (lexicographically larger goes later)
628                 a = aaa.word;
629                 b = bbb.word;
630                 if (a !== b) { return (a > b ? +1 : -1); }
631
632                 // sort by index of keyword in item name (no literal occurrence goes later)
633                 a = (aaa.index < 0);
634                 b = (bbb.index < 0);
635                 if (a !== b) { return a - b; }
636                 // (later literal occurrence, if any, goes later)
637                 a = aaa.index;
638                 b = bbb.index;
639                 if (a !== b) { return a - b; }
640
641                 // special precedence for primitive pages
642                 if ((aaa.item.ty === TY_PRIMITIVE) && (bbb.item.ty !== TY_PRIMITIVE)) {
643                     return -1;
644                 }
645                 if ((bbb.item.ty === TY_PRIMITIVE) && (aaa.item.ty !== TY_PRIMITIVE)) {
646                     return 1;
647                 }
648
649                 // sort by description (no description goes later)
650                 a = (aaa.item.desc === '');
651                 b = (bbb.item.desc === '');
652                 if (a !== b) { return a - b; }
653
654                 // sort by type (later occurrence in `itemTypes` goes later)
655                 a = aaa.item.ty;
656                 b = bbb.item.ty;
657                 if (a !== b) { return a - b; }
658
659                 // sort by path (lexicographically larger goes later)
660                 a = aaa.item.path;
661                 b = bbb.item.path;
662                 if (a !== b) { return (a > b ? +1 : -1); }
663
664                 // que sera, sera
665                 return 0;
666             });
667
668             for (var i = 0; i < results.length; ++i) {
669                 var result = results[i],
670                     name = result.item.name.toLowerCase(),
671                     path = result.item.path.toLowerCase(),
672                     parent = result.item.parent;
673
674                 // this validation does not make sense when searching by types
675                 if (result.dontValidate) {
676                     continue;
677                 }
678
679                 var valid = validateResult(name, path, split, parent);
680                 if (!valid) {
681                     result.id = -1;
682                 }
683             }
684             return results;
685         }
686
687         /**
688          * Validate performs the following boolean logic. For example:
689          * "File::open" will give IF A PARENT EXISTS => ("file" && "open")
690          * exists in (name || path || parent) OR => ("file" && "open") exists in
691          * (name || path )
692          *
693          * This could be written functionally, but I wanted to minimise
694          * functions on stack.
695          *
696          * @param  {[string]} name   [The name of the result]
697          * @param  {[string]} path   [The path of the result]
698          * @param  {[string]} keys   [The keys to be used (["file", "open"])]
699          * @param  {[object]} parent [The parent of the result]
700          * @return {[boolean]}       [Whether the result is valid or not]
701          */
702         function validateResult(name, path, keys, parent) {
703             for (var i = 0; i < keys.length; ++i) {
704                 // each check is for validation so we negate the conditions and invalidate
705                 if (!(
706                     // check for an exact name match
707                     name.toLowerCase().indexOf(keys[i]) > -1 ||
708                     // then an exact path match
709                     path.toLowerCase().indexOf(keys[i]) > -1 ||
710                     // next if there is a parent, check for exact parent match
711                     (parent !== undefined &&
712                         parent.name.toLowerCase().indexOf(keys[i]) > -1) ||
713                     // lastly check to see if the name was a levenshtein match
714                     levenshtein(name.toLowerCase(), keys[i]) <= MAX_LEV_DISTANCE)) {
715                     return false;
716                 }
717             }
718             return true;
719         }
720
721         function getQuery() {
722             var matches, type, query, raw =
723                 document.getElementsByClassName('search-input')[0].value;
724             query = raw;
725
726             matches = query.match(/^(fn|mod|struct|enum|trait|type|const|macro)\s*:\s*/i);
727             if (matches) {
728                 type = matches[1].replace(/^const$/, 'constant');
729                 query = query.substring(matches[0].length);
730             }
731
732             return {
733                 raw: raw,
734                 query: query,
735                 type: type,
736                 id: query + type
737             };
738         }
739
740         function initSearchNav() {
741             var hoverTimeout;
742
743             var click_func = function(e) {
744                 var el = e.target;
745                 // to retrieve the real "owner" of the event.
746                 while (el.tagName !== 'TR') {
747                     el = el.parentNode;
748                 }
749                 var dst = e.target.getElementsByTagName('a');
750                 if (dst.length < 1) {
751                     return;
752                 }
753                 dst = dst[0];
754                 if (window.location.pathname === dst.pathname) {
755                     addClass(document.getElementById('search'), 'hidden');
756                     removeClass(document.getElementById('main'), 'hidden');
757                     document.location.href = dst.href;
758                 }
759             };
760             var mouseover_func = function(e) {
761                 var el = e.target;
762                 // to retrieve the real "owner" of the event.
763                 while (el.tagName !== 'TR') {
764                     el = el.parentNode;
765                 }
766                 clearTimeout(hoverTimeout);
767                 hoverTimeout = setTimeout(function() {
768                     onEach(document.getElementsByClassName('search-results'), function(e) {
769                         onEach(e.getElementsByClassName('result'), function(i_e) {
770                             removeClass(i_e, 'highlighted');
771                         });
772                     });
773                     addClass(el, 'highlighted');
774                 }, 20);
775             };
776             onEach(document.getElementsByClassName('search-results'), function(e) {
777                 onEach(e.getElementsByClassName('result'), function(i_e) {
778                     i_e.onclick = click_func;
779                     i_e.onmouseover = mouseover_func;
780                 });
781             });
782
783             var search_input = document.getElementsByClassName('search-input')[0];
784             search_input.onkeydown = function(e) {
785                 // "actives" references the currently highlighted item in each search tab.
786                 // Each array in "actives" represents a tab.
787                 var actives = [[], [], []];
788                 // "current" is used to know which tab we're looking into.
789                 var current = 0;
790                 onEach(document.getElementsByClassName('search-results'), function(e) {
791                     onEach(e.getElementsByClassName('highlighted'), function(e) {
792                         actives[current].push(e);
793                     });
794                     current += 1;
795                 });
796
797                 if (e.which === 38) { // up
798                     if (!actives[currentTab].length ||
799                         !actives[currentTab][0].previousElementSibling) {
800                         return;
801                     }
802
803                     addClass(actives[currentTab][0].previousElementSibling, 'highlighted');
804                     removeClass(actives[currentTab][0], 'highlighted');
805                 } else if (e.which === 40) { // down
806                     if (!actives[currentTab].length) {
807                         var results = document.getElementsByClassName('search-results');
808                         if (results.length > 0) {
809                             var res = results[currentTab].getElementsByClassName('result');
810                             if (res.length > 0) {
811                                 addClass(res[0], 'highlighted');
812                             }
813                         }
814                     } else if (actives[currentTab][0].nextElementSibling) {
815                         addClass(actives[currentTab][0].nextElementSibling, 'highlighted');
816                         removeClass(actives[currentTab][0], 'highlighted');
817                     }
818                 } else if (e.which === 13) { // return
819                     if (actives[currentTab].length) {
820                         document.location.href =
821                             actives[currentTab][0].getElementsByTagName('a')[0].href;
822                     }
823                 } else if (e.which === 9) { // tab
824                     if (e.shiftKey) {
825                         printTab(currentTab > 0 ? currentTab - 1 : 2);
826                     } else {
827                         printTab(currentTab > 1 ? 0 : currentTab + 1);
828                     }
829                     e.preventDefault();
830                 } else if (e.which === 16) { // shift
831                     // Does nothing, it's just to avoid losing "focus" on the highlighted element.
832                 } else if (actives[currentTab].length > 0) {
833                     removeClass(actives[currentTab][0], 'highlighted');
834                 }
835             };
836         }
837
838         function escape(content) {
839             var h1 = document.createElement('h1');
840             h1.textContent = content;
841             return h1.innerHTML;
842         }
843
844         function addTab(array, query, display) {
845             var extraStyle = '';
846             if (display === false) {
847                 extraStyle = ' style="display: none;"';
848             }
849
850             var output = '';
851             if (array.length > 0) {
852                 output = '<table class="search-results"' + extraStyle + '>';
853                 var shown = [];
854
855                 array.forEach(function(item) {
856                     var name, type, href, displayPath;
857
858                     if (shown.indexOf(item) !== -1) {
859                         return;
860                     }
861
862                     shown.push(item);
863                     name = item.name;
864                     type = itemTypes[item.ty];
865
866                     if (type === 'mod') {
867                         displayPath = item.path + '::';
868                         href = rootPath + item.path.replace(/::/g, '/') + '/' +
869                                name + '/index.html';
870                     } else if (type === "primitive") {
871                         displayPath = "";
872                         href = rootPath + item.path.replace(/::/g, '/') +
873                                '/' + type + '.' + name + '.html';
874                     } else if (type === "externcrate") {
875                         displayPath = "";
876                         href = rootPath + name + '/index.html';
877                     } else if (item.parent !== undefined) {
878                         var myparent = item.parent;
879                         var anchor = '#' + type + '.' + name;
880                         var parentType = itemTypes[myparent.ty];
881                         if (parentType === "primitive") {
882                             displayPath = myparent.name + '::';
883                         } else {
884                             displayPath = item.path + '::' + myparent.name + '::';
885                         }
886                         href = rootPath + item.path.replace(/::/g, '/') +
887                                '/' + parentType +
888                                '.' + myparent.name +
889                                '.html' + anchor;
890                     } else {
891                         displayPath = item.path + '::';
892                         href = rootPath + item.path.replace(/::/g, '/') +
893                                '/' + type + '.' + name + '.html';
894                     }
895
896                     output += '<tr class="' + type + ' result"><td>' +
897                               '<a href="' + href + '">' +
898                               displayPath + '<span class="' + type + '">' +
899                               name + '</span></a></td><td>' +
900                               '<a href="' + href + '">' +
901                               '<span class="desc">' + escape(item.desc) +
902                               '&nbsp;</span></a></td></tr>';
903                 });
904                 output += '</table>';
905             } else {
906                 output = '<div class="search-failed"' + extraStyle + '>No results :(<br/>' +
907                     'Try on <a href="https://duckduckgo.com/?q=' +
908                     encodeURIComponent('rust ' + query.query) +
909                     '">DuckDuckGo</a>?</div>';
910             }
911             return output;
912         }
913
914         function makeTabHeader(tabNb, text) {
915             if (currentTab === tabNb) {
916                 return '<div class="selected">' + text + '</div>';
917             }
918             return '<div>' + text + '</div>';
919         }
920
921         function showResults(results) {
922             var output, query = getQuery();
923
924             currentResults = query.id;
925             output = '<h1>Results for ' + escape(query.query) +
926                 (query.type ? ' (type: ' + escape(query.type) + ')' : '') + '</h1>' +
927                 '<div id="titles">' +
928                 makeTabHeader(0, "Types/modules") +
929                 makeTabHeader(1, "As parameters") +
930                 makeTabHeader(2, "As return value") +
931                 '</div><div id="results">';
932
933             output += addTab(results['others'], query);
934             output += addTab(results['in_args'], query, false);
935             output += addTab(results['returned'], query, false);
936             output += '</div>';
937
938             addClass(document.getElementById('main'), 'hidden');
939             var search = document.getElementById('search');
940             removeClass(search, 'hidden');
941             search.innerHTML = output;
942             var tds = search.getElementsByTagName('td');
943             var td_width = 0;
944             if (tds.length > 0) {
945                 td_width = tds[0].offsetWidth;
946             }
947             var width = search.offsetWidth - 40 - td_width;
948             onEach(search.getElementsByClassName('desc'), function(e) {
949                 e.style.width = width + 'px';
950             });
951             initSearchNav();
952             var elems = document.getElementById('titles').childNodes;
953             elems[0].onclick = function() { printTab(0); };
954             elems[1].onclick = function() { printTab(1); };
955             elems[2].onclick = function() { printTab(2); };
956             printTab(currentTab);
957         }
958
959         function search(e) {
960             var query,
961                 filterdata = [],
962                 obj, i, len,
963                 results = {"in_args": [], "returned": [], "others": []},
964                 maxResults = 200,
965                 resultIndex;
966             var params = getQueryStringParams();
967
968             query = getQuery();
969             if (e) {
970                 e.preventDefault();
971             }
972
973             if (!query.query || query.id === currentResults) {
974                 return;
975             }
976
977             // Update document title to maintain a meaningful browser history
978             document.title = "Results for " + query.query + " - Rust";
979
980             // Because searching is incremental by character, only the most
981             // recent search query is added to the browser history.
982             if (browserSupportsHistoryApi()) {
983                 if (!history.state && !params.search) {
984                     history.pushState(query, "", "?search=" + encodeURIComponent(query.raw));
985                 } else {
986                     history.replaceState(query, "", "?search=" + encodeURIComponent(query.raw));
987                 }
988             }
989
990             resultIndex = execQuery(query, 20000, index);
991             len = resultIndex.length;
992             for (i = 0; i < len; ++i) {
993                 if (resultIndex[i].id > -1) {
994                     var added = false;
995                     obj = searchIndex[resultIndex[i].id];
996                     filterdata.push([obj.name, obj.ty, obj.path, obj.desc]);
997                     if (obj.type) {
998                         if (results['returned'].length < maxResults &&
999                             obj.type.output &&
1000                             obj.type.output.name.toLowerCase() === query.output) {
1001                             results['returned'].push(obj);
1002                             added = true;
1003                         }
1004                         if (results['in_args'].length < maxResults && obj.type.inputs.length > 0) {
1005                             var all_founds = true;
1006                             for (var it = 0;
1007                                  all_founds === true && it < query.inputs.length;
1008                                  it++) {
1009                                 var found = false;
1010                                 for (var y = 0;
1011                                      found === false && y < obj.type.inputs.length;
1012                                      y++) {
1013                                     found = query.inputs[it] === obj.type.inputs[y].name;
1014                                 }
1015                                 all_founds = found;
1016                             }
1017                             if (all_founds === true) {
1018                                 results['in_args'].push(obj);
1019                                 added = true;
1020                             }
1021                         }
1022                     }
1023                     if (results['others'].length < maxResults &&
1024                         ((query.search && obj.name.indexOf(query.search) !== -1) ||
1025                           added === false)) {
1026                         results['others'].push(obj);
1027                     }
1028                 }
1029                 if (results['others'].length >= maxResults &&
1030                     results['in_args'].length >= maxResults &&
1031                     results['returned'].length >= maxResults) {
1032                     break;
1033                 }
1034             }
1035
1036             showResults(results);
1037         }
1038
1039         function itemTypeFromName(typename) {
1040             for (var i = 0; i < itemTypes.length; ++i) {
1041                 if (itemTypes[i] === typename) { return i; }
1042             }
1043             return -1;
1044         }
1045
1046         function buildIndex(rawSearchIndex) {
1047             searchIndex = [];
1048             var searchWords = [];
1049             for (var crate in rawSearchIndex) {
1050                 if (!rawSearchIndex.hasOwnProperty(crate)) { continue; }
1051
1052                 searchWords.push(crate);
1053                 searchIndex.push({
1054                     crate: crate,
1055                     ty: 1, // == ExternCrate
1056                     name: crate,
1057                     path: "",
1058                     desc: rawSearchIndex[crate].doc,
1059                     type: null,
1060                 });
1061
1062                 // an array of [(Number) item type,
1063                 //              (String) name,
1064                 //              (String) full path or empty string for previous path,
1065                 //              (String) description,
1066                 //              (Number | null) the parent path index to `paths`]
1067                 //              (Object | null) the type of the function (if any)
1068                 var items = rawSearchIndex[crate].items;
1069                 // an array of [(Number) item type,
1070                 //              (String) name]
1071                 var paths = rawSearchIndex[crate].paths;
1072
1073                 // convert `paths` into an object form
1074                 var len = paths.length;
1075                 for (var i = 0; i < len; ++i) {
1076                     paths[i] = {ty: paths[i][0], name: paths[i][1]};
1077                 }
1078
1079                 // convert `items` into an object form, and construct word indices.
1080                 //
1081                 // before any analysis is performed lets gather the search terms to
1082                 // search against apart from the rest of the data.  This is a quick
1083                 // operation that is cached for the life of the page state so that
1084                 // all other search operations have access to this cached data for
1085                 // faster analysis operations
1086                 var len = items.length;
1087                 var lastPath = "";
1088                 for (var i = 0; i < len; ++i) {
1089                     var rawRow = items[i];
1090                     var row = {crate: crate, ty: rawRow[0], name: rawRow[1],
1091                                path: rawRow[2] || lastPath, desc: rawRow[3],
1092                                parent: paths[rawRow[4]], type: rawRow[5]};
1093                     searchIndex.push(row);
1094                     if (typeof row.name === "string") {
1095                         var word = row.name.toLowerCase();
1096                         searchWords.push(word);
1097                     } else {
1098                         searchWords.push("");
1099                     }
1100                     lastPath = row.path;
1101                 }
1102             }
1103             return searchWords;
1104         }
1105
1106         function startSearch() {
1107             var searchTimeout;
1108             var callback = function() {
1109                 var search_input = document.getElementsByClassName('search-input');
1110                 if (search_input.length < 1) { return; }
1111                 search_input = search_input[0];
1112                 clearTimeout(searchTimeout);
1113                 if (search_input.value.length === 0) {
1114                     if (browserSupportsHistoryApi()) {
1115                         history.replaceState("", "std - Rust", "?search=");
1116                     }
1117                     var main = document.getElementById('main');
1118                     if (hasClass(main, 'content')) {
1119                         removeClass(main, 'hidden');
1120                     }
1121                     var search_c = document.getElementById('search');
1122                     if (hasClass(search_c, 'content')) {
1123                         addClass(search_c, 'hidden');
1124                     }
1125                 } else {
1126                     searchTimeout = setTimeout(search, 500);
1127                 }
1128             };
1129             var search_input = document.getElementsByClassName("search-input")[0];
1130             search_input.onkeyup = callback;
1131             search_input.oninput = callback;
1132             document.getElementsByClassName("search-form")[0].onsubmit = function(e){
1133                 e.preventDefault();
1134                 clearTimeout(searchTimeout);
1135                 search();
1136             };
1137             search_input.onchange = function(e) {
1138                 // Do NOT e.preventDefault() here. It will prevent pasting.
1139                 clearTimeout(searchTimeout);
1140                 // zero-timeout necessary here because at the time of event handler execution the
1141                 // pasted content is not in the input field yet. Shouldn’t make any difference for
1142                 // change, though.
1143                 setTimeout(search, 0);
1144             };
1145             search_input.onpaste = search_input.onchange;
1146
1147             // Push and pop states are used to add search results to the browser
1148             // history.
1149             if (browserSupportsHistoryApi()) {
1150                 // Store the previous <title> so we can revert back to it later.
1151                 var previousTitle = document.title;
1152
1153                 window.onpopstate = function(e) {
1154                     var params = getQueryStringParams();
1155                     // When browsing back from search results the main page
1156                     // visibility must be reset.
1157                     if (!params.search) {
1158                         var main = document.getElementById('main');
1159                         if (hasClass(main, 'content')) {
1160                             removeClass(main, 'hidden');
1161                         }
1162                         var search_c = document.getElementById('search');
1163                         if (hasClass(search_c, 'content')) {
1164                             addClass(search_c, 'hidden');
1165                         }
1166                     }
1167                     // Revert to the previous title manually since the History
1168                     // API ignores the title parameter.
1169                     document.title = previousTitle;
1170                     // When browsing forward to search results the previous
1171                     // search will be repeated, so the currentResults are
1172                     // cleared to ensure the search is successful.
1173                     currentResults = null;
1174                     // Synchronize search bar with query string state and
1175                     // perform the search. This will empty the bar if there's
1176                     // nothing there, which lets you really go back to a
1177                     // previous state with nothing in the bar.
1178                     if (params.search) {
1179                         document.getElementsByClassName('search-input')[0].value = params.search;
1180                     } else {
1181                         document.getElementsByClassName('search-input')[0].value = '';
1182                     }
1183                     // Some browsers fire 'onpopstate' for every page load
1184                     // (Chrome), while others fire the event only when actually
1185                     // popping a state (Firefox), which is why search() is
1186                     // called both here and at the end of the startSearch()
1187                     // function.
1188                     search();
1189                 };
1190             }
1191             search();
1192         }
1193
1194         index = buildIndex(rawSearchIndex);
1195         startSearch();
1196
1197         // Draw a convenient sidebar of known crates if we have a listing
1198         if (rootPath === '../') {
1199             var sidebar = document.getElementsByClassName('sidebar')[0];
1200             var div = document.createElement('div');
1201             div.className = 'block crate';
1202             div.innerHTML = '<h3>Crates</h3>';
1203             var ul = document.createElement('ul');
1204             div.appendChild(ul);
1205
1206             var crates = [];
1207             for (var crate in rawSearchIndex) {
1208                 if (!rawSearchIndex.hasOwnProperty(crate)) { continue; }
1209                 crates.push(crate);
1210             }
1211             crates.sort();
1212             for (var i = 0; i < crates.length; ++i) {
1213                 var klass = 'crate';
1214                 if (crates[i] === window.currentCrate) {
1215                     klass += ' current';
1216                 }
1217                 var link = document.createElement('a');
1218                 link.href = '../' + crates[i] + '/index.html';
1219                 link.title = rawSearchIndex[crates[i]].doc;
1220                 link.className = klass;
1221                 link.textContent = crates[i];
1222
1223                 var li = document.createElement('li');
1224                 li.appendChild(link);
1225                 ul.appendChild(li);
1226             }
1227             sidebar.appendChild(div);
1228         }
1229     }
1230
1231     window.initSearch = initSearch;
1232
1233     // delayed sidebar rendering.
1234     function initSidebarItems(items) {
1235         var sidebar = document.getElementsByClassName('sidebar')[0];
1236         var current = window.sidebarCurrent;
1237
1238         function block(shortty, longty) {
1239             var filtered = items[shortty];
1240             if (!filtered) { return; }
1241
1242             var div = document.createElement('div');
1243             div.className = 'block ' + shortty;
1244             var h3 = document.createElement('h3');
1245             h3.textContent = longty;
1246             div.appendChild(h3);
1247             var ul = document.createElement('ul');
1248
1249             for (var i = 0; i < filtered.length; ++i) {
1250                 var item = filtered[i];
1251                 var name = item[0];
1252                 var desc = item[1]; // can be null
1253
1254                 var klass = shortty;
1255                 if (name === current.name && shortty === current.ty) {
1256                     klass += ' current';
1257                 }
1258                 var path;
1259                 if (shortty === 'mod') {
1260                     path = name + '/index.html';
1261                 } else {
1262                     path = shortty + '.' + name + '.html';
1263                 }
1264                 var link = document.createElement('a');
1265                 link.href = current.relpath + path;
1266                 link.title = desc;
1267                 link.className = klass;
1268                 link.textContent = name;
1269                 var li = document.createElement('li');
1270                 li.appendChild(link);
1271                 ul.appendChild(li);
1272             }
1273             div.appendChild(ul);
1274             sidebar.appendChild(div);
1275         }
1276
1277         block("primitive", "Primitive Types");
1278         block("mod", "Modules");
1279         block("macro", "Macros");
1280         block("struct", "Structs");
1281         block("enum", "Enums");
1282         block("union", "Unions");
1283         block("constant", "Constants");
1284         block("static", "Statics");
1285         block("trait", "Traits");
1286         block("fn", "Functions");
1287         block("type", "Type Definitions");
1288     }
1289
1290     window.initSidebarItems = initSidebarItems;
1291
1292     window.register_implementors = function(imp) {
1293         var list = document.getElementById('implementors-list');
1294         var libs = Object.getOwnPropertyNames(imp);
1295         for (var i = 0; i < libs.length; ++i) {
1296             if (libs[i] === currentCrate) { continue; }
1297             var structs = imp[libs[i]];
1298             for (var j = 0; j < structs.length; ++j) {
1299                 var code = document.createElement('code');
1300                 code.innerHTML = structs[j];
1301
1302                 var x = code.getElementsByTagName('a');
1303                 for (var k = 0; k < x.length; k++) {
1304                     var href = x[k].getAttribute('href');
1305                     if (href && href.indexOf('http') !== 0) {
1306                         x[k].setAttribute('href', rootPath + href);
1307                     }
1308                 }
1309                 var li = document.createElement('li');
1310                 li.appendChild(code);
1311                 list.appendChild(li);
1312             }
1313         }
1314     };
1315     if (window.pending_implementors) {
1316         window.register_implementors(window.pending_implementors);
1317     }
1318
1319     function labelForToggleButton(sectionIsCollapsed) {
1320         if (sectionIsCollapsed) {
1321             // button will expand the section
1322             return "+";
1323         }
1324         // button will collapse the section
1325         // note that this text is also set in the HTML template in render.rs
1326         return "\u2212"; // "\u2212" is '−' minus sign
1327     }
1328
1329     function onEveryMatchingChild(elem, className, func) {
1330         if (elem && className && func) {
1331             for (var i = 0; i < elem.childNodes.length; i++) {
1332                 if (hasClass(elem.childNodes[i], className)) {
1333                     func(elem.childNodes[i]);
1334                 } else {
1335                     onEveryMatchingChild(elem.childNodes[i], className, func);
1336                 }
1337             }
1338         }
1339     }
1340
1341     function toggleAllDocs() {
1342         var toggle = document.getElementById("toggle-all-docs");
1343         if (hasClass(toggle, "will-expand")) {
1344             removeClass(toggle, "will-expand");
1345             onEveryMatchingChild(toggle, "inner", function(e) {
1346                 e.innerHTML = labelForToggleButton(false);
1347             });
1348             toggle.title = "collapse all docs";
1349             onEach(document.getElementsByClassName("docblock"), function(e) {
1350                 e.style.display = 'block';
1351             });
1352             onEach(document.getElementsByClassName("toggle-label"), function(e) {
1353                 e.style.display = 'none';
1354             });
1355             onEach(document.getElementsByClassName("toggle-wrapper"), function(e) {
1356                 removeClass(e, "collapsed");
1357             });
1358             onEach(document.getElementsByClassName("collapse-toggle"), function(e) {
1359                 onEveryMatchingChild(e, "inner", function(i_e) {
1360                     i_e.innerHTML = labelForToggleButton(false);
1361                 });
1362             });
1363         } else {
1364             addClass(toggle, "will-expand");
1365             onEveryMatchingChild(toggle, "inner", function(e) {
1366                 e.innerHTML = labelForToggleButton(true);
1367             });
1368             toggle.title = "expand all docs";
1369             onEach(document.getElementsByClassName("docblock"), function(e) {
1370                 e.style.display = 'none';
1371             });
1372             onEach(document.getElementsByClassName("toggle-label"), function(e) {
1373                 e.style.display = 'inline-block';
1374             });
1375             onEach(document.getElementsByClassName("toggle-wrapper"), function(e) {
1376                 addClass(e, "collapsed");
1377             });
1378             onEach(document.getElementsByClassName("collapse-toggle"), function(e) {
1379                 onEveryMatchingChild(e, "inner", function(i_e) {
1380                     i_e.innerHTML = labelForToggleButton(true);
1381                 });
1382             });
1383         }
1384     }
1385
1386     function collapseDocs(toggle) {
1387         if (!toggle || !toggle.parentNode) {
1388             return;
1389         }
1390         var relatedDoc = toggle.parentNode.nextElementSibling;
1391         if (hasClass(relatedDoc, "stability")) {
1392             relatedDoc = relatedDoc.nextElementSibling;
1393         }
1394         if (hasClass(relatedDoc, "docblock")) {
1395             if (!isHidden(relatedDoc)) {
1396                 relatedDoc.style.display = 'none';
1397                 onEach(toggle.childNodes, function(e) {
1398                     if (hasClass(e, 'toggle-label')) {
1399                         e.style.display = 'inline-block';
1400                     }
1401                     if (hasClass(e, 'inner')) {
1402                         e.innerHTML = labelForToggleButton(true);
1403                     }
1404                 });
1405                 addClass(toggle.parentNode, 'collapsed');
1406             } else {
1407                 relatedDoc.style.display = 'block';
1408                 removeClass(toggle.parentNode, 'collapsed');
1409                 onEach(toggle.childNodes, function(e) {
1410                     if (hasClass(e, 'toggle-label')) {
1411                         e.style.display = 'none';
1412                     }
1413                     if (hasClass(e, 'inner')) {
1414                         e.innerHTML = labelForToggleButton(false);
1415                     }
1416                 });
1417             }
1418         }
1419     }
1420
1421     var x = document.getElementById('toggle-all-docs');
1422     if (x) {
1423         x.onclick = toggleAllDocs;
1424     }
1425
1426     function insertAfter(newNode, referenceNode) {
1427         referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
1428     }
1429
1430     var toggle = document.createElement('a');
1431     toggle.href = 'javascript:void(0)';
1432     toggle.className = 'collapse-toggle';
1433     toggle.innerHTML = "[<span class='inner'>"+labelForToggleButton(false)+"</span>]";
1434
1435     var func = function(e) {
1436         var next = e.nextElementSibling;
1437         if (!next) {
1438             return;
1439         }
1440         if (hasClass(next, 'docblock') ||
1441             (hasClass(next, 'stability') &&
1442              hasClass(next.nextElementSibling, 'docblock'))) {
1443             insertAfter(toggle.cloneNode(true), e.childNodes[e.childNodes.length - 1]);
1444         }
1445     }
1446     onEach(document.getElementsByClassName('method'), func);
1447     onEach(document.getElementsByClassName('impl-items'), function(e) {
1448         onEach(e.getElementsByClassName('associatedconstant'), func);
1449     });
1450
1451     function createToggle() {
1452         var span = document.createElement('span');
1453         span.className = 'toggle-label';
1454         span.style.display = 'none';
1455         span.innerHTML = '&nbsp;Expand&nbsp;description';
1456
1457         var mainToggle = toggle.cloneNode(true);
1458         mainToggle.appendChild(span);
1459
1460         var wrapper = document.createElement('div');
1461         wrapper.className = 'toggle-wrapper';
1462         wrapper.appendChild(mainToggle);
1463         return wrapper;
1464     }
1465
1466     onEach(document.getElementById('main').getElementsByClassName('docblock'), function(e) {
1467         if (e.parentNode.id === "main") {
1468             e.parentNode.insertBefore(createToggle(), e);
1469         }
1470     });
1471
1472     onEach(document.getElementsByClassName('docblock'), function(e) {
1473         if (hasClass(e, 'autohide')) {
1474             var wrap = e.previousElementSibling;
1475             if (wrap && hasClass(wrap, 'toggle-wrapper')) {
1476                 var toggle = wrap.childNodes[0];
1477                 if (e.childNodes[0].tagName === 'H3') {
1478                     onEach(toggle.getElementsByClassName('toggle-label'), function(i_e) {
1479                         i_e.innerHTML = " Show " + e.childNodes[0].innerHTML;
1480                     });
1481                 }
1482                 e.style.display = 'none';
1483                 addClass(wrap, 'collapsed');
1484                 onEach(toggle.getElementsByClassName('inner'), function(e) {
1485                     e.innerHTML = labelForToggleButton(true);
1486                 });
1487                 onEach(toggle.getElementsByClassName('toggle-label'), function(e) {
1488                     e.style.display = 'inline-block';
1489                 });
1490             }
1491         }
1492     })
1493
1494     function createToggleWrapper() {
1495         var span = document.createElement('span');
1496         span.className = 'toggle-label';
1497         span.style.display = 'none';
1498         span.innerHTML = '&nbsp;Expand&nbsp;attributes';
1499         toggle.appendChild(span);
1500
1501         var wrapper = document.createElement('div');
1502         wrapper.className = 'toggle-wrapper toggle-attributes';
1503         wrapper.appendChild(toggle);
1504         return wrapper;
1505     }
1506
1507     // In the search display, allows to switch between tabs.
1508     function printTab(nb) {
1509         if (nb === 0 || nb === 1 || nb === 2) {
1510             currentTab = nb;
1511         }
1512         var nb_copy = nb;
1513         onEach(document.getElementById('titles').childNodes, function(elem) {
1514             if (nb_copy === 0) {
1515                 addClass(elem, 'selected');
1516             } else {
1517                 removeClass(elem, 'selected');
1518             }
1519             nb_copy -= 1;
1520         });
1521         onEach(document.getElementById('results').childNodes, function(elem) {
1522             if (nb === 0) {
1523                 elem.style.display = '';
1524             } else {
1525                 elem.style.display = 'none';
1526             }
1527             nb -= 1;
1528         });
1529     }
1530
1531     onEach(document.getElementById('main').getElementsByTagName('pre'), function(e) {
1532         onEach(e.getElementsByClassName('attributes'), function(i_e) {
1533             i_e.parentNode.insertBefore(createToggleWrapper(), i_e);
1534             collapseDocs(i_e.previousSibling.childNodes[0]);
1535         });
1536     });
1537
1538     onEach(document.getElementsByClassName('rust-example-rendered'), function(e) {
1539         if (hasClass(e, 'compile_fail')) {
1540             e.addEventListener("mouseover", function(event) {
1541                 e.previousElementSibling.childNodes[0].style.color = '#f00';
1542             });
1543             e.addEventListener("mouseout", function(event) {
1544                 e.previousElementSibling.childNodes[0].style.color = '';
1545             });
1546         } else if (hasClass(e, 'ignore')) {
1547             e.addEventListener("mouseover", function(event) {
1548                 e.previousElementSibling.childNodes[0].style.color = '#ff9200';
1549             });
1550             e.addEventListener("mouseout", function(event) {
1551                 e.previousElementSibling.childNodes[0].style.color = '';
1552             });
1553         }
1554     });
1555 }());
1556
1557 // Sets the focus on the search bar at the top of the page
1558 function focusSearchBar() {
1559     document.getElementsByClassName('search-input')[0].focus();
1560 }