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.
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.
13 /*jslint browser: true, es5: true */
14 /*globals $: true, rootPath: true */
19 // This mapping table should match the discriminants of
20 // `rustdoc::html::item_type::ItemType` type in Rust.
21 var itemTypes = ["mod",
42 // On the search screen, so you remain on the last tab you opened.
44 // 0 for "Types/modules"
45 // 1 for "As parameters"
46 // 2 for "As return value"
49 function hasClass(elem, className) {
50 if (elem && className && elem.className) {
51 var elemClass = elem.className;
52 var start = elemClass.indexOf(className);
55 } else if (elemClass.length == className.length) {
58 if (start > 0 && elemClass[start - 1] != ' ') {
61 var end = start + className.length;
62 if (end < elemClass.length && elemClass[end] != ' ') {
71 function addClass(elem, className) {
72 if (elem && className && !hasClass(elem, className)) {
73 if (elem.className && elem.className.length > 0) {
74 elem.className += ' ' + className;
76 elem.className = className;
81 function removeClass(elem, className) {
82 if (elem && className && elem.className) {
83 elem.className = (" " + elem.className + " ").replace(" " + className + " ", " ")
88 function onEach(arr, func) {
89 if (arr && arr.length > 0 && func) {
90 for (var i = 0; i < arr.length; i++) {
96 function isHidden(elem) {
97 return (elem.offsetParent === null)
100 // used for special search precedence
101 var TY_PRIMITIVE = itemTypes.indexOf("primitive");
103 onEach(document.getElementsByClassName('js-only'), function(e) {
104 removeClass(e, 'js-only');
107 function getQueryStringParams() {
109 window.location.search.substring(1).split("&").
111 var pair = s.split("=");
112 params[decodeURIComponent(pair[0])] =
113 typeof pair[1] === "undefined" ?
114 null : decodeURIComponent(pair[1]);
119 function browserSupportsHistoryApi() {
120 return document.location.protocol != "file:" &&
121 window.history && typeof window.history.pushState === "function";
124 function highlightSourceLines(ev) {
125 var i, from, to, match = window.location.hash.match(/^#?(\d+)(?:-(\d+))?$/);
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);
135 var x = document.getElementById(from);
140 onEach(document.getElementsByClassName('line-numbers'), function(e) {
141 onEach(e.getElementsByTagName('span'), function(i_e) {
142 removeClass(i_e, 'line-highlighted');
145 for (i = from; i <= to; ++i) {
146 addClass(document.getElementById(i), 'line-highlighted');
150 highlightSourceLines(null);
151 window.onhashchange = highlightSourceLines;
153 // Gets the human-readable string for the virtual-key code of the
154 // given KeyboardEvent, ev.
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.
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")
167 var c = ev.charCode || ev.keyCode;
170 return String.fromCharCode(c);
173 function displayHelp(display, ev) {
174 if (display === true) {
175 if (hasClass(help, "hidden")) {
177 removeClass(help, "hidden");
178 addClass(document.body, "blur");
180 } else if (!hasClass(help, "hidden")) {
182 addClass(help, "hidden");
183 removeClass(document.body, "blur");
187 function handleShortcut(ev) {
188 if (document.activeElement.tagName === "INPUT")
191 // Don't interfere with browser shortcuts
192 if (ev.ctrlKey || ev.altKey || ev.metaKey)
195 var help = document.getElementById("help");
196 switch (getVirtualKey(ev)) {
198 var search = document.getElementById("search");
199 if (!hasClass(help, "hidden")) {
200 displayHelp(false, ev);
201 } else if (!hasClass(search, "hidden")) {
203 addClass(search, "hidden");
204 removeClass(document.getElementById("main"), "hidden");
210 displayHelp(false, ev);
223 displayHelp(true, ev);
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')) {
239 var set_fragment = function (name) {
240 if (browserSupportsHistoryApi()) {
241 history.replaceState(null, null, '#' + name);
244 location.replace('#' + name);
248 var cur_id = parseInt(ev.target.id, 10);
250 if (ev.shiftKey && prev_id) {
251 if (prev_id > cur_id) {
257 set_fragment(prev_id + '-' + cur_id);
261 set_fragment(cur_id);
263 } else if (!hasClass(document.getElementById("help"), "hidden")) {
264 addClass(document.getElementById("help"), "hidden");
265 removeClass(document.body, "blur");
269 var x = document.getElementsByClassName('version-selector');
271 x[0].onchange = function() {
273 url = document.location.href,
275 len = rootPath.match(/\.\.\//g).length + 1;
277 for (i = 0; i < len; ++i) {
278 match = url.match(/\/[^\/]*$/);
280 stripped = match[0] + stripped;
282 url = url.substring(0, url.length - match[0].length);
285 url += '/' + document.getElementsByClassName('version-selector')[0].value + stripped;
287 document.location.href = url;
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
298 var levenshtein = (function() {
300 return function(s1, s2) {
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) {
310 while (i2 < s2_len) {
311 c2 = s2.charCodeAt(i2);
315 for (i1 = 0; i1 < s1_len; ++i1) {
316 c = a + (s1.charCodeAt(i1) !== c2 ? 1 : 0);
318 b = b < a ? (b < c ? b + 1 : c) : (a < c ? a + 1 : c);
324 return s1_len + s2_len;
328 function initSearch(rawSearchIndex) {
329 var currentResults, index, searchIndex;
330 var MAX_LEV_DISTANCE = 3;
331 var params = getQueryStringParams();
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 || '';
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
347 * @return {[type]} [A search index of results]
349 function execQuery(query, max, searchWords) {
350 var valLower = query.query.toLowerCase(),
352 typeFilter = itemTypeFromName(query.type),
354 split = valLower.split("::");
356 // remove empty keywords
357 for (var j = 0; j < split.length; ++j) {
358 split[j].toLowerCase();
359 if (split[j] === "") {
371 function nbElements(obj) {
374 if (obj.hasOwnProperty(key)) {
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.
389 lev_distance = min(levenshtein(obj.type.inputs[i].name, val), lev_distance);
390 if (lev_distance === 0) {
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) {
404 lev_distance = min(levenshtein(obj.type.output.name, val));
405 if (lev_distance === 0) {
412 function typePassesFilter(filter, type) {
414 if (filter < 0) return true;
417 if (filter === type) return true;
419 // Match related items
420 var name = itemTypes[type];
421 switch (itemTypes[filter]) {
423 return (name == "associatedconstant");
425 return (name == "method" || name == "tymethod");
427 return (name == "primitive");
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))
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};
447 } else if (findArg(searchIndex[i], val) ||
450 ty.type.output.name === val)) {
451 if (typePassesFilter(typeFilter, searchIndex[i].ty)) {
452 results[ty.path + ty.name] = {
459 if (nbElements(results) === max) {
463 query.inputs = [val];
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];
475 for (var i = 0; i < nSearchWords; ++i) {
476 var type = searchIndex[i].type;
477 var ty = searchIndex[i];
482 // sort index inputs so that order does not matter
483 var typeInputs = type.inputs.map(function (input) {
487 // allow searching for void (no output) functions as well
488 var typeOutput = type.output ? type.output.name : "";
489 if (output === "*" || output == typeOutput) {
491 results[ty.path + ty.name] = {id: i, index: -1, dontValidate: true};
494 for (var it = 0; allFound === true && it < inputs.length; it++) {
496 for (var y = 0; found === false && y < typeInputs.length; y++) {
497 found = typeInputs[y] === inputs[it];
501 if (allFound === true) {
502 results[ty.path + ty.name] = {
511 query.inputs = inputs;
512 query.output = output;
514 query.inputs = [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) {
522 var ty = searchIndex[j];
526 if (searchWords[j].indexOf(split[i]) > -1 ||
527 searchWords[j].indexOf(val) > -1 ||
528 searchWords[j].replace(/_/g, "").indexOf(val) > -1)
530 // filter type: ... queries
531 if (typePassesFilter(typeFilter, searchIndex[j].ty)) {
532 results[ty.path + ty.name] = {
534 index: searchWords[j].replace(/_/g, "").indexOf(val),
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] = {
546 // we want lev results to go lower than others
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] = {
559 // we want lev results to go lower than others
565 (lev_distance = checkReturned(searchIndex[j], val)) <=
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] = {
573 // we want lev results to go lower than others
579 if (nbElements(results) === max) {
587 for (var entry in results) {
588 if (results.hasOwnProperty(entry)) {
589 ar.push(results[entry]);
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] || {};
598 // if there are no results then return to default and fail
599 if (results.length === 0) {
603 results.sort(function sortResults(aaa, bbb) {
606 // Sort by non levenshtein results and then levenshtein results by the distance
607 // (less changes required to match means higher rankings)
610 if (a !== b) { return a - b; }
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; }
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; }
622 // sort by item name length (longer goes later)
625 if (a !== b) { return a - b; }
627 // sort by item name (lexicographically larger goes later)
630 if (a !== b) { return (a > b ? +1 : -1); }
632 // sort by index of keyword in item name (no literal occurrence goes later)
635 if (a !== b) { return a - b; }
636 // (later literal occurrence, if any, goes later)
639 if (a !== b) { return a - b; }
641 // special precedence for primitive pages
642 if ((aaa.item.ty === TY_PRIMITIVE) && (bbb.item.ty !== TY_PRIMITIVE)) {
645 if ((bbb.item.ty === TY_PRIMITIVE) && (aaa.item.ty !== TY_PRIMITIVE)) {
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; }
654 // sort by type (later occurrence in `itemTypes` goes later)
657 if (a !== b) { return a - b; }
659 // sort by path (lexicographically larger goes later)
662 if (a !== b) { return (a > b ? +1 : -1); }
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;
674 // this validation does not make sense when searching by types
675 if (result.dontValidate) {
679 var valid = validateResult(name, path, split, parent);
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
693 * This could be written functionally, but I wanted to minimise
694 * functions on stack.
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]
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
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)) {
721 function getQuery() {
722 var matches, type, query, raw =
723 document.getElementsByClassName('search-input')[0].value;
726 matches = query.match(/^(fn|mod|struct|enum|trait|type|const|macro)\s*:\s*/i);
728 type = matches[1].replace(/^const$/, 'constant');
729 query = query.substring(matches[0].length);
740 function initSearchNav() {
743 var click_func = function(e) {
745 // to retrieve the real "owner" of the event.
746 while (el.tagName !== 'TR') {
749 var dst = e.target.getElementsByTagName('a');
750 if (dst.length < 1) {
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;
760 var mouseover_func = function(e) {
762 // to retrieve the real "owner" of the event.
763 while (el.tagName !== 'TR') {
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');
773 addClass(el, 'highlighted');
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;
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.
790 onEach(document.getElementsByClassName('search-results'), function(e) {
791 onEach(e.getElementsByClassName('highlighted'), function(e) {
792 actives[current].push(e);
797 if (e.which === 38) { // up
798 if (!actives[currentTab].length ||
799 !actives[currentTab][0].previousElementSibling) {
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');
814 } else if (actives[currentTab][0].nextElementSibling) {
815 addClass(actives[currentTab][0].nextElementSibling, 'highlighted');
816 removeClass(actives[currentTab][0], 'highlighted');
818 } else if (e.which === 13) { // return
819 if (actives[currentTab].length) {
820 document.location.href =
821 actives[currentTab][0].getElementsByTagName('a')[0].href;
823 } else if (e.which === 9) { // tab
825 printTab(currentTab > 0 ? currentTab - 1 : 2);
827 printTab(currentTab > 1 ? 0 : currentTab + 1);
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');
838 function escape(content) {
839 var h1 = document.createElement('h1');
840 h1.textContent = content;
844 function addTab(array, query, display) {
846 if (display === false) {
847 extraStyle = ' style="display: none;"';
851 if (array.length > 0) {
852 output = '<table class="search-results"' + extraStyle + '>';
855 array.forEach(function(item) {
856 var name, type, href, displayPath;
858 if (shown.indexOf(item) !== -1) {
864 type = itemTypes[item.ty];
866 if (type === 'mod') {
867 displayPath = item.path + '::';
868 href = rootPath + item.path.replace(/::/g, '/') + '/' +
869 name + '/index.html';
870 } else if (type === "primitive") {
872 href = rootPath + item.path.replace(/::/g, '/') +
873 '/' + type + '.' + name + '.html';
874 } else if (type === "externcrate") {
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 + '::';
884 displayPath = item.path + '::' + myparent.name + '::';
886 href = rootPath + item.path.replace(/::/g, '/') +
888 '.' + myparent.name +
891 displayPath = item.path + '::';
892 href = rootPath + item.path.replace(/::/g, '/') +
893 '/' + type + '.' + name + '.html';
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 ' </span></a></td></tr>';
904 output += '</table>';
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>';
914 function makeTabHeader(tabNb, text) {
915 if (currentTab === tabNb) {
916 return '<div class="selected">' + text + '</div>';
918 return '<div>' + text + '</div>';
921 function showResults(results) {
922 var output, query = getQuery();
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">';
933 output += addTab(results['others'], query);
934 output += addTab(results['in_args'], query, false);
935 output += addTab(results['returned'], query, false);
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');
944 if (tds.length > 0) {
945 td_width = tds[0].offsetWidth;
947 var width = search.offsetWidth - 40 - td_width;
948 onEach(search.getElementsByClassName('desc'), function(e) {
949 e.style.width = width + 'px';
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);
963 results = {"in_args": [], "returned": [], "others": []},
966 var params = getQueryStringParams();
973 if (!query.query || query.id === currentResults) {
977 // Update document title to maintain a meaningful browser history
978 document.title = "Results for " + query.query + " - Rust";
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));
986 history.replaceState(query, "", "?search=" + encodeURIComponent(query.raw));
990 resultIndex = execQuery(query, 20000, index);
991 len = resultIndex.length;
992 for (i = 0; i < len; ++i) {
993 if (resultIndex[i].id > -1) {
995 obj = searchIndex[resultIndex[i].id];
996 filterdata.push([obj.name, obj.ty, obj.path, obj.desc]);
998 if (results['returned'].length < maxResults &&
1000 obj.type.output.name.toLowerCase() === query.output) {
1001 results['returned'].push(obj);
1004 if (results['in_args'].length < maxResults && obj.type.inputs.length > 0) {
1005 var all_founds = true;
1007 all_founds === true && it < query.inputs.length;
1011 found === false && y < obj.type.inputs.length;
1013 found = query.inputs[it] === obj.type.inputs[y].name;
1017 if (all_founds === true) {
1018 results['in_args'].push(obj);
1023 if (results['others'].length < maxResults &&
1024 ((query.search && obj.name.indexOf(query.search) !== -1) ||
1026 results['others'].push(obj);
1029 if (results['others'].length >= maxResults &&
1030 results['in_args'].length >= maxResults &&
1031 results['returned'].length >= maxResults) {
1036 showResults(results);
1039 function itemTypeFromName(typename) {
1040 for (var i = 0; i < itemTypes.length; ++i) {
1041 if (itemTypes[i] === typename) { return i; }
1046 function buildIndex(rawSearchIndex) {
1048 var searchWords = [];
1049 for (var crate in rawSearchIndex) {
1050 if (!rawSearchIndex.hasOwnProperty(crate)) { continue; }
1052 searchWords.push(crate);
1055 ty: 1, // == ExternCrate
1058 desc: rawSearchIndex[crate].doc,
1062 // an array of [(Number) item type,
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,
1071 var paths = rawSearchIndex[crate].paths;
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]};
1079 // convert `items` into an object form, and construct word indices.
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;
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);
1098 searchWords.push("");
1100 lastPath = row.path;
1106 function startSearch() {
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=");
1117 var main = document.getElementById('main');
1118 if (hasClass(main, 'content')) {
1119 removeClass(main, 'hidden');
1121 var search_c = document.getElementById('search');
1122 if (hasClass(search_c, 'content')) {
1123 addClass(search_c, 'hidden');
1126 searchTimeout = setTimeout(search, 500);
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){
1134 clearTimeout(searchTimeout);
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
1143 setTimeout(search, 0);
1145 search_input.onpaste = search_input.onchange;
1147 // Push and pop states are used to add search results to the browser
1149 if (browserSupportsHistoryApi()) {
1150 // Store the previous <title> so we can revert back to it later.
1151 var previousTitle = document.title;
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');
1162 var search_c = document.getElementById('search');
1163 if (hasClass(search_c, 'content')) {
1164 addClass(search_c, 'hidden');
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;
1181 document.getElementsByClassName('search-input')[0].value = '';
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()
1194 index = buildIndex(rawSearchIndex);
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);
1207 for (var crate in rawSearchIndex) {
1208 if (!rawSearchIndex.hasOwnProperty(crate)) { continue; }
1212 for (var i = 0; i < crates.length; ++i) {
1213 var klass = 'crate';
1214 if (crates[i] === window.currentCrate) {
1215 klass += ' current';
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];
1223 var li = document.createElement('li');
1224 li.appendChild(link);
1227 sidebar.appendChild(div);
1231 window.initSearch = initSearch;
1233 // delayed sidebar rendering.
1234 function initSidebarItems(items) {
1235 var sidebar = document.getElementsByClassName('sidebar')[0];
1236 var current = window.sidebarCurrent;
1238 function block(shortty, longty) {
1239 var filtered = items[shortty];
1240 if (!filtered) { return; }
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');
1249 for (var i = 0; i < filtered.length; ++i) {
1250 var item = filtered[i];
1252 var desc = item[1]; // can be null
1254 var klass = shortty;
1255 if (name === current.name && shortty === current.ty) {
1256 klass += ' current';
1259 if (shortty === 'mod') {
1260 path = name + '/index.html';
1262 path = shortty + '.' + name + '.html';
1264 var link = document.createElement('a');
1265 link.href = current.relpath + path;
1267 link.className = klass;
1268 link.textContent = name;
1269 var li = document.createElement('li');
1270 li.appendChild(link);
1273 div.appendChild(ul);
1274 sidebar.appendChild(div);
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");
1290 window.initSidebarItems = initSidebarItems;
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];
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);
1309 var li = document.createElement('li');
1310 li.appendChild(code);
1311 list.appendChild(li);
1315 if (window.pending_implementors) {
1316 window.register_implementors(window.pending_implementors);
1319 function labelForToggleButton(sectionIsCollapsed) {
1320 if (sectionIsCollapsed) {
1321 // button will expand the section
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
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]);
1335 onEveryMatchingChild(elem.childNodes[i], className, func);
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);
1348 toggle.title = "collapse all docs";
1349 onEach(document.getElementsByClassName("docblock"), function(e) {
1350 e.style.display = 'block';
1352 onEach(document.getElementsByClassName("toggle-label"), function(e) {
1353 e.style.display = 'none';
1355 onEach(document.getElementsByClassName("toggle-wrapper"), function(e) {
1356 removeClass(e, "collapsed");
1358 onEach(document.getElementsByClassName("collapse-toggle"), function(e) {
1359 onEveryMatchingChild(e, "inner", function(i_e) {
1360 i_e.innerHTML = labelForToggleButton(false);
1364 addClass(toggle, "will-expand");
1365 onEveryMatchingChild(toggle, "inner", function(e) {
1366 e.innerHTML = labelForToggleButton(true);
1368 toggle.title = "expand all docs";
1369 onEach(document.getElementsByClassName("docblock"), function(e) {
1370 e.style.display = 'none';
1372 onEach(document.getElementsByClassName("toggle-label"), function(e) {
1373 e.style.display = 'inline-block';
1375 onEach(document.getElementsByClassName("toggle-wrapper"), function(e) {
1376 addClass(e, "collapsed");
1378 onEach(document.getElementsByClassName("collapse-toggle"), function(e) {
1379 onEveryMatchingChild(e, "inner", function(i_e) {
1380 i_e.innerHTML = labelForToggleButton(true);
1386 function collapseDocs(toggle) {
1387 if (!toggle || !toggle.parentNode) {
1390 var relatedDoc = toggle.parentNode.nextElementSibling;
1391 if (hasClass(relatedDoc, "stability")) {
1392 relatedDoc = relatedDoc.nextElementSibling;
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';
1401 if (hasClass(e, 'inner')) {
1402 e.innerHTML = labelForToggleButton(true);
1405 addClass(toggle.parentNode, 'collapsed');
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';
1413 if (hasClass(e, 'inner')) {
1414 e.innerHTML = labelForToggleButton(false);
1421 var x = document.getElementById('toggle-all-docs');
1423 x.onclick = toggleAllDocs;
1426 function insertAfter(newNode, referenceNode) {
1427 referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
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>]";
1435 var func = function(e) {
1436 var next = e.nextElementSibling;
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]);
1446 onEach(document.getElementsByClassName('method'), func);
1447 onEach(document.getElementsByClassName('impl-items'), function(e) {
1448 onEach(e.getElementsByClassName('associatedconstant'), func);
1451 function createToggle() {
1452 var span = document.createElement('span');
1453 span.className = 'toggle-label';
1454 span.style.display = 'none';
1455 span.innerHTML = ' Expand description';
1457 var mainToggle = toggle.cloneNode(true);
1458 mainToggle.appendChild(span);
1460 var wrapper = document.createElement('div');
1461 wrapper.className = 'toggle-wrapper';
1462 wrapper.appendChild(mainToggle);
1466 onEach(document.getElementById('main').getElementsByClassName('docblock'), function(e) {
1467 if (e.parentNode.id === "main") {
1468 e.parentNode.insertBefore(createToggle(), e);
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;
1482 e.style.display = 'none';
1483 addClass(wrap, 'collapsed');
1484 onEach(toggle.getElementsByClassName('inner'), function(e) {
1485 e.innerHTML = labelForToggleButton(true);
1487 onEach(toggle.getElementsByClassName('toggle-label'), function(e) {
1488 e.style.display = 'inline-block';
1494 function createToggleWrapper() {
1495 var span = document.createElement('span');
1496 span.className = 'toggle-label';
1497 span.style.display = 'none';
1498 span.innerHTML = ' Expand attributes';
1499 toggle.appendChild(span);
1501 var wrapper = document.createElement('div');
1502 wrapper.className = 'toggle-wrapper toggle-attributes';
1503 wrapper.appendChild(toggle);
1507 // In the search display, allows to switch between tabs.
1508 function printTab(nb) {
1509 if (nb === 0 || nb === 1 || nb === 2) {
1513 onEach(document.getElementById('titles').childNodes, function(elem) {
1514 if (nb_copy === 0) {
1515 addClass(elem, 'selected');
1517 removeClass(elem, 'selected');
1521 onEach(document.getElementById('results').childNodes, function(elem) {
1523 elem.style.display = '';
1525 elem.style.display = 'none';
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]);
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';
1543 e.addEventListener("mouseout", function(event) {
1544 e.previousElementSibling.childNodes[0].style.color = '';
1546 } else if (hasClass(e, 'ignore')) {
1547 e.addEventListener("mouseover", function(event) {
1548 e.previousElementSibling.childNodes[0].style.color = '#ff9200';
1550 e.addEventListener("mouseout", function(event) {
1551 e.previousElementSibling.childNodes[0].style.color = '';
1557 // Sets the focus on the search bar at the top of the page
1558 function focusSearchBar() {
1559 document.getElementsByClassName('search-input')[0].focus();