1 /* global addClass, getNakedUrl, getSettingValue, hasOwnPropertyRustdoc, initSearch, onEach */
2 /* global onEachLazy, removeClass, searchState, browserSupportsHistoryApi */
7 // This mapping table should match the discriminants of
8 // `rustdoc::formats::item_type::ItemType` type in Rust.
38 // used for special search precedence
39 const TY_PRIMITIVE = itemTypes.indexOf("primitive");
40 const TY_KEYWORD = itemTypes.indexOf("keyword");
42 // In the search display, allows to switch between tabs.
43 function printTab(nb) {
45 let foundCurrentTab = false;
46 let foundCurrentResultSet = false;
47 onEachLazy(document.getElementById("titles").childNodes, elem => {
49 addClass(elem, "selected");
50 foundCurrentTab = true;
52 removeClass(elem, "selected");
57 onEachLazy(document.getElementById("results").childNodes, elem => {
59 addClass(elem, "active");
60 foundCurrentResultSet = true;
62 removeClass(elem, "active");
66 if (foundCurrentTab && foundCurrentResultSet) {
67 searchState.currentTab = nb;
74 * A function to compute the Levenshtein distance between two strings
75 * Licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported
76 * Full License can be found at http://creativecommons.org/licenses/by-sa/3.0/legalcode
77 * This code is an unmodified version of the code written by Marco de Wit
78 * and was found at https://stackoverflow.com/a/18514751/745719
80 const levenshtein_row2 = [];
81 function levenshtein(s1, s2) {
85 const s1_len = s1.length, s2_len = s2.length;
86 if (s1_len && s2_len) {
87 let i1 = 0, i2 = 0, a, b, c, c2;
88 const row = levenshtein_row2;
93 c2 = s2.charCodeAt(i2);
97 for (i1 = 0; i1 < s1_len; ++i1) {
98 c = a + (s1.charCodeAt(i1) !== c2 ? 1 : 0);
100 b = b < a ? (b < c ? b + 1 : c) : (a < c ? a + 1 : c);
106 return s1_len + s2_len;
109 window.initSearch = rawSearchIndex => {
110 const MAX_LEV_DISTANCE = 3;
111 const MAX_RESULTS = 200;
112 const GENERICS_DATA = 2;
114 const INPUTS_DATA = 0;
115 const OUTPUT_DATA = 1;
116 const NO_TYPE_FILTER = -1;
122 const ALIASES = Object.create(null);
123 const params = searchState.getQueryStringParams();
125 // Populate search bar with query string search term when provided,
126 // but only if the input bar is empty. This avoid the obnoxious issue
127 // where you start trying to do a search, and the index loads, and
128 // suddenly your search is gone!
129 if (searchState.input.value === "") {
130 searchState.input.value = params.search || "";
133 function isWhitespace(c) {
134 return " \t\n\r".indexOf(c) !== -1;
137 function isSpecialStartCharacter(c) {
138 return "<\"".indexOf(c) !== -1;
141 function isEndCharacter(c) {
142 return ",>-".indexOf(c) !== -1;
145 function isStopCharacter(c) {
146 return isWhitespace(c) || isEndCharacter(c);
149 function isErrorCharacter(c) {
150 return "()".indexOf(c) !== -1;
153 function itemTypeFromName(typename) {
154 for (let i = 0, len = itemTypes.length; i < len; ++i) {
155 if (itemTypes[i] === typename) {
160 throw new Error("Unknown type filter `" + typename + "`");
164 * If we encounter a `"`, then we try to extract the string from it until we find another `"`.
166 * This function will throw an error in the following cases:
167 * * There is already another string element.
168 * * We are parsing a generic argument.
169 * * There is more than one element.
170 * * There is no closing `"`.
172 * @param {ParsedQuery} query
173 * @param {ParserState} parserState
174 * @param {boolean} isInGenerics
176 function getStringElem(query, parserState, isInGenerics) {
178 throw new Error("`\"` cannot be used in generics");
179 } else if (query.literalSearch) {
180 throw new Error("Cannot have more than one literal search element");
181 } else if (parserState.totalElems - parserState.genericsElems > 0) {
182 throw new Error("Cannot use literal search when there is more than one element");
184 parserState.pos += 1;
185 const start = parserState.pos;
186 const end = getIdentEndPosition(parserState);
187 if (parserState.pos >= parserState.length) {
188 throw new Error("Unclosed `\"`");
189 } else if (parserState.userQuery[end] !== "\"") {
190 throw new Error(`Unexpected \`${parserState.userQuery[end]}\` in a string element`);
191 } else if (start === end) {
192 throw new Error("Cannot have empty string element");
194 // To skip the quote at the end.
195 parserState.pos += 1;
196 query.literalSearch = true;
200 * Returns `true` if the current parser position is starting with "::".
202 * @param {ParserState} parserState
206 function isPathStart(parserState) {
207 return parserState.userQuery.slice(parserState.pos, parserState.pos + 2) == "::";
211 * Returns `true` if the current parser position is starting with "->".
213 * @param {ParserState} parserState
217 function isReturnArrow(parserState) {
218 return parserState.userQuery.slice(parserState.pos, parserState.pos + 2) == "->";
222 * Returns `true` if the given `c` character is valid for an ident.
228 function isIdentCharacter(c) {
231 (c >= "0" && c <= "9") ||
232 (c >= "a" && c <= "z") ||
233 (c >= "A" && c <= "Z"));
237 * Returns `true` if the given `c` character is a separator.
243 function isSeparatorCharacter(c) {
244 return c === "," || isWhitespaceCharacter(c);
248 * Returns `true` if the given `c` character is a whitespace.
254 function isWhitespaceCharacter(c) {
255 return c === " " || c === "\t";
259 * @param {ParsedQuery} query
260 * @param {ParserState} parserState
261 * @param {string} name - Name of the query element.
262 * @param {Array<QueryElement>} generics - List of generics of this query element.
264 * @return {QueryElement} - The newly created `QueryElement`.
266 function createQueryElement(query, parserState, name, generics, isInGenerics) {
267 if (name === "*" || (name.length === 0 && generics.length === 0)) {
270 if (query.literalSearch && parserState.totalElems - parserState.genericsElems > 0) {
271 throw new Error("You cannot have more than one element if you use quotes");
273 const pathSegments = name.split("::");
274 if (pathSegments.length > 1) {
275 for (let i = 0, len = pathSegments.length; i < len; ++i) {
276 const pathSegment = pathSegments[i];
278 if (pathSegment.length === 0) {
280 throw new Error("Paths cannot start with `::`");
281 } else if (i + 1 === len) {
282 throw new Error("Paths cannot end with `::`");
284 throw new Error("Unexpected `::::`");
288 // In case we only have something like `<p>`, there is no name.
289 if (pathSegments.length === 0 || (pathSegments.length === 1 && pathSegments[0] === "")) {
290 throw new Error("Found generics without a path");
292 parserState.totalElems += 1;
294 parserState.genericsElems += 1;
298 fullPath: pathSegments,
299 pathWithoutLast: pathSegments.slice(0, pathSegments.length - 1),
300 pathLast: pathSegments[pathSegments.length - 1],
306 * This function goes through all characters until it reaches an invalid ident character or the
307 * end of the query. It returns the position of the last character of the ident.
309 * @param {ParserState} parserState
313 function getIdentEndPosition(parserState) {
314 let end = parserState.pos;
315 let foundExclamation = false;
316 while (parserState.pos < parserState.length) {
317 const c = parserState.userQuery[parserState.pos];
318 if (!isIdentCharacter(c)) {
320 if (foundExclamation) {
321 throw new Error("Cannot have more than one `!` in an ident");
322 } else if (parserState.pos + 1 < parserState.length &&
323 isIdentCharacter(parserState.userQuery[parserState.pos + 1]))
325 throw new Error("`!` can only be at the end of an ident");
327 foundExclamation = true;
328 } else if (isErrorCharacter(c)) {
329 throw new Error(`Unexpected \`${c}\``);
331 isStopCharacter(c) ||
332 isSpecialStartCharacter(c) ||
333 isSeparatorCharacter(c))
337 // If we allow paths ("str::string" for example).
338 else if (c === ":") {
339 if (!isPathStart(parserState)) {
343 parserState.pos += 1;
344 foundExclamation = false;
346 throw new Error(`Unexpected \`${c}\``);
349 parserState.pos += 1;
350 end = parserState.pos;
356 * @param {ParsedQuery} query
357 * @param {ParserState} parserState
358 * @param {Array<QueryElement>} elems - This is where the new {QueryElement} will be added.
359 * @param {boolean} isInGenerics
361 function getNextElem(query, parserState, elems, isInGenerics) {
364 let start = parserState.pos;
366 // We handle the strings on their own mostly to make code easier to follow.
367 if (parserState.userQuery[parserState.pos] === "\"") {
369 getStringElem(query, parserState, isInGenerics);
370 end = parserState.pos - 1;
372 end = getIdentEndPosition(parserState);
374 if (parserState.pos < parserState.length &&
375 parserState.userQuery[parserState.pos] === "<")
378 throw new Error("Unexpected `<` after `<`");
379 } else if (start >= end) {
380 throw new Error("Found generics without a path");
382 parserState.pos += 1;
383 getItemsBefore(query, parserState, generics, ">");
385 if (start >= end && generics.length === 0) {
392 parserState.userQuery.slice(start, end),
400 * This function parses the next query element until it finds `endChar`, calling `getNextElem`
401 * to collect each element.
403 * If there is no `endChar`, this function will implicitly stop at the end without raising an
406 * @param {ParsedQuery} query
407 * @param {ParserState} parserState
408 * @param {Array<QueryElement>} elems - This is where the new {QueryElement} will be added.
409 * @param {string} endChar - This function will stop when it'll encounter this
412 function getItemsBefore(query, parserState, elems, endChar) {
413 let foundStopChar = true;
415 while (parserState.pos < parserState.length) {
416 const c = parserState.userQuery[parserState.pos];
419 } else if (isSeparatorCharacter(c)) {
420 parserState.pos += 1;
421 foundStopChar = true;
423 } else if (c === ":" && isPathStart(parserState)) {
424 throw new Error("Unexpected `::`: paths cannot start with `::`");
425 } else if (c === ":" || isEndCharacter(c)) {
427 if (endChar === ">") {
429 } else if (endChar === "") {
432 throw new Error("Unexpected `" + c + "` after " + extra);
434 if (!foundStopChar) {
435 if (endChar !== "") {
436 throw new Error(`Expected \`,\`, \` \` or \`${endChar}\`, found \`${c}\``);
438 throw new Error(`Expected \`,\` or \` \`, found \`${c}\``);
440 const posBefore = parserState.pos;
441 getNextElem(query, parserState, elems, endChar === ">");
442 // This case can be encountered if `getNextElem` encounted a "stop character" right from
443 // the start. For example if you have `,,` or `<>`. In this case, we simply move up the
444 // current position to continue the parsing.
445 if (posBefore === parserState.pos) {
446 parserState.pos += 1;
448 foundStopChar = false;
450 // We are either at the end of the string or on the `endChar`` character, let's move forward
452 parserState.pos += 1;
456 * Checks that the type filter doesn't have unwanted characters like `<>` (which are ignored
459 * @param {ParserState} parserState
461 function checkExtraTypeFilterCharacters(parserState) {
462 const query = parserState.userQuery;
464 for (let pos = 0; pos < parserState.pos; ++pos) {
465 if (!isIdentCharacter(query[pos]) && !isWhitespaceCharacter(query[pos])) {
466 throw new Error(`Unexpected \`${query[pos]}\` in type filter`);
472 * Parses the provided `query` input to fill `parserState`. If it encounters an error while
473 * parsing `query`, it'll throw an error.
475 * @param {ParsedQuery} query
476 * @param {ParserState} parserState
478 function parseInput(query, parserState) {
480 let foundStopChar = true;
482 while (parserState.pos < parserState.length) {
483 c = parserState.userQuery[parserState.pos];
484 if (isStopCharacter(c)) {
485 foundStopChar = true;
486 if (isSeparatorCharacter(c)) {
487 parserState.pos += 1;
489 } else if (c === "-" || c === ">") {
490 if (isReturnArrow(parserState)) {
493 throw new Error(`Unexpected \`${c}\` (did you mean \`->\`?)`);
495 throw new Error(`Unexpected \`${c}\``);
496 } else if (c === ":" && !isPathStart(parserState)) {
497 if (parserState.typeFilter !== null) {
498 throw new Error("Unexpected `:`");
500 if (query.elems.length === 0) {
501 throw new Error("Expected type filter before `:`");
502 } else if (query.elems.length !== 1 || parserState.totalElems !== 1) {
503 throw new Error("Unexpected `:`");
504 } else if (query.literalSearch) {
505 throw new Error("You cannot use quotes on type filter");
507 checkExtraTypeFilterCharacters(parserState);
508 // The type filter doesn't count as an element since it's a modifier.
509 parserState.typeFilter = query.elems.pop().name;
510 parserState.pos += 1;
511 parserState.totalElems = 0;
512 query.literalSearch = false;
513 foundStopChar = true;
516 if (!foundStopChar) {
517 if (parserState.typeFilter !== null) {
518 throw new Error(`Expected \`,\`, \` \` or \`->\`, found \`${c}\``);
520 throw new Error(`Expected \`,\`, \` \`, \`:\` or \`->\`, found \`${c}\``);
522 before = query.elems.length;
523 getNextElem(query, parserState, query.elems, false);
524 if (query.elems.length === before) {
525 // Nothing was added, weird... Let's increase the position to not remain stuck.
526 parserState.pos += 1;
528 foundStopChar = false;
530 while (parserState.pos < parserState.length) {
531 c = parserState.userQuery[parserState.pos];
532 if (isReturnArrow(parserState)) {
533 parserState.pos += 2;
534 // Get returned elements.
535 getItemsBefore(query, parserState, query.returned, "");
536 // Nothing can come afterward!
537 if (query.returned.length === 0) {
538 throw new Error("Expected at least one item after `->`");
542 parserState.pos += 1;
548 * Takes the user search input and returns an empty `ParsedQuery`.
550 * @param {string} userQuery
552 * @return {ParsedQuery}
554 function newParsedQuery(userQuery) {
557 userQuery: userQuery.toLowerCase(),
558 typeFilter: NO_TYPE_FILTER,
561 // Total number of "top" elements (does not include generics).
563 literalSearch: false,
569 * Build an URL with search parameters.
571 * @param {string} search - The current search being performed.
572 * @param {string|null} filterCrates - The current filtering crate (if any).
576 function buildUrl(search, filterCrates) {
577 let extra = "?search=" + encodeURIComponent(search);
579 if (filterCrates !== null) {
580 extra += "&filter-crate=" + encodeURIComponent(filterCrates);
582 return getNakedUrl() + extra + window.location.hash;
586 * Return the filtering crate or `null` if there is none.
588 * @return {string|null}
590 function getFilterCrates() {
591 const elem = document.getElementById("crate-search");
594 elem.value !== "All crates" &&
595 hasOwnPropertyRustdoc(rawSearchIndex, elem.value))
605 * The supported syntax by this parser is as follow:
607 * ident = *(ALPHA / DIGIT / "_") [!]
608 * path = ident *(DOUBLE-COLON ident)
609 * arg = path [generics]
610 * arg-without-generic = path
611 * type-sep = COMMA/WS *(COMMA/WS)
612 * nonempty-arg-list = *(type-sep) arg *(type-sep arg) *(type-sep)
613 * nonempty-arg-list-without-generics = *(type-sep) arg-without-generic
614 * *(type-sep arg-without-generic) *(type-sep)
615 * generics = OPEN-ANGLE-BRACKET [ nonempty-arg-list-without-generics ] *(type-sep)
616 * CLOSE-ANGLE-BRACKET/EOF
617 * return-args = RETURN-ARROW *(type-sep) nonempty-arg-list
619 * exact-search = [type-filter *WS COLON] [ RETURN-ARROW ] *WS QUOTE ident QUOTE [ generics ]
620 * type-search = [type-filter *WS COLON] [ nonempty-arg-list ] [ return-args ]
622 * query = *WS (exact-search / type-search) *WS
643 * "associatedconstant" /
652 * OPEN-ANGLE-BRACKET = "<"
653 * CLOSE-ANGLE-BRACKET = ">"
655 * DOUBLE-COLON = "::"
658 * RETURN-ARROW = "->"
660 * ALPHA = %x41-5A / %x61-7A ; A-Z / a-z
664 * @param {string} val - The user query
666 * @return {ParsedQuery} - The parsed query
668 function parseQuery(userQuery) {
669 userQuery = userQuery.trim();
670 const parserState = {
671 length: userQuery.length,
673 // Total number of elements (includes generics).
677 userQuery: userQuery.toLowerCase(),
679 let query = newParsedQuery(userQuery);
682 parseInput(query, parserState);
683 if (parserState.typeFilter !== null) {
684 let typeFilter = parserState.typeFilter;
685 if (typeFilter === "const") {
686 typeFilter = "constant";
688 query.typeFilter = itemTypeFromName(typeFilter);
691 query = newParsedQuery(userQuery);
692 query.error = err.message;
693 query.typeFilter = -1;
697 if (!query.literalSearch) {
698 // If there is more than one element in the query, we switch to literalSearch in any
700 query.literalSearch = parserState.totalElems > 1;
702 query.foundElems = query.elems.length + query.returned.length;
707 * Creates the query results.
709 * @param {Array<Result>} results_in_args
710 * @param {Array<Result>} results_returned
711 * @param {Array<Result>} results_in_args
712 * @param {ParsedQuery} parsedQuery
714 * @return {ResultsTable}
716 function createQueryResults(results_in_args, results_returned, results_others, parsedQuery) {
718 "in_args": results_in_args,
719 "returned": results_returned,
720 "others": results_others,
721 "query": parsedQuery,
726 * Executes the parsed query and builds a {ResultsTable}.
728 * @param {ParsedQuery} parsedQuery - The parsed user query
729 * @param {Object} searchWords - The list of search words to query against
730 * @param {Object} [filterCrates] - Crate to search in if defined
732 * @return {ResultsTable}
734 function execQuery(parsedQuery, searchWords, filterCrates) {
735 const results_others = {}, results_in_args = {}, results_returned = {};
737 function transformResults(results) {
738 const duplicates = {};
741 for (const result of results) {
742 if (result.id > -1) {
743 const obj = searchIndex[result.id];
744 obj.lev = result.lev;
745 const res = buildHrefAndPath(obj);
746 obj.displayPath = pathSplitter(res[0]);
747 obj.fullPath = obj.displayPath + obj.name;
748 // To be sure than it some items aren't considered as duplicate.
749 obj.fullPath += "|" + obj.ty;
751 if (duplicates[obj.fullPath]) {
754 duplicates[obj.fullPath] = true;
758 if (out.length >= MAX_RESULTS) {
766 function sortResults(results, isType) {
767 const userQuery = parsedQuery.userQuery;
769 for (const entry in results) {
770 if (hasOwnPropertyRustdoc(results, entry)) {
771 const result = results[entry];
772 result.word = searchWords[result.id];
773 result.item = searchIndex[result.id] || {};
778 // if there are no results then return to default and fail
779 if (results.length === 0) {
783 results.sort((aaa, bbb) => {
786 // sort by exact match with regard to the last word (mismatch goes later)
787 a = (aaa.word !== userQuery);
788 b = (bbb.word !== userQuery);
789 if (a !== b) { return a - b; }
791 // Sort by non levenshtein results and then levenshtein results by the distance
792 // (less changes required to match means higher rankings)
795 if (a !== b) { return a - b; }
797 // sort by crate (non-current crate goes later)
798 a = (aaa.item.crate !== window.currentCrate);
799 b = (bbb.item.crate !== window.currentCrate);
800 if (a !== b) { return a - b; }
802 // sort by item name length (longer goes later)
805 if (a !== b) { return a - b; }
807 // sort by item name (lexicographically larger goes later)
810 if (a !== b) { return (a > b ? +1 : -1); }
812 // sort by index of keyword in item name (no literal occurrence goes later)
815 if (a !== b) { return a - b; }
816 // (later literal occurrence, if any, goes later)
819 if (a !== b) { return a - b; }
821 // special precedence for primitive and keyword pages
822 if ((aaa.item.ty === TY_PRIMITIVE && bbb.item.ty !== TY_KEYWORD) ||
823 (aaa.item.ty === TY_KEYWORD && bbb.item.ty !== TY_PRIMITIVE)) {
826 if ((bbb.item.ty === TY_PRIMITIVE && aaa.item.ty !== TY_PRIMITIVE) ||
827 (bbb.item.ty === TY_KEYWORD && aaa.item.ty !== TY_KEYWORD)) {
831 // sort by description (no description goes later)
832 a = (aaa.item.desc === "");
833 b = (bbb.item.desc === "");
834 if (a !== b) { return a - b; }
836 // sort by type (later occurrence in `itemTypes` goes later)
839 if (a !== b) { return a - b; }
841 // sort by path (lexicographically larger goes later)
844 if (a !== b) { return (a > b ? +1 : -1); }
850 let nameSplit = null;
851 if (parsedQuery.elems.length === 1) {
852 const hasPath = typeof parsedQuery.elems[0].path === "undefined";
853 nameSplit = hasPath ? null : parsedQuery.elems[0].path;
856 for (const result of results) {
857 // this validation does not make sense when searching by types
858 if (result.dontValidate) {
861 const name = result.item.name.toLowerCase(),
862 path = result.item.path.toLowerCase(),
863 parent = result.item.parent;
865 if (!isType && !validateResult(name, path, nameSplit, parent)) {
869 return transformResults(results);
873 * This function checks if the object (`row`) generics match the given type (`elem`)
874 * generics. If there are no generics on `row`, `defaultLev` is returned.
876 * @param {Row} row - The object to check.
877 * @param {QueryElement} elem - The element from the parsed query.
878 * @param {integer} defaultLev - This is the value to return in case there are no generics.
880 * @return {integer} - Returns the best match (if any) or `MAX_LEV_DISTANCE + 1`.
882 function checkGenerics(row, elem, defaultLev) {
883 if (row.length <= GENERICS_DATA || row[GENERICS_DATA].length === 0) {
884 return elem.generics.length === 0 ? defaultLev : MAX_LEV_DISTANCE + 1;
885 } else if (row[GENERICS_DATA].length > 0 && row[GENERICS_DATA][0][NAME] === "") {
886 if (row.length > GENERICS_DATA) {
887 return checkGenerics(row[GENERICS_DATA][0], elem, defaultLev);
889 return elem.generics.length === 0 ? defaultLev : MAX_LEV_DISTANCE + 1;
891 // The names match, but we need to be sure that all generics kinda
894 if (elem.generics.length > 0 && row[GENERICS_DATA].length >= elem.generics.length) {
895 const elems = Object.create(null);
896 for (const entry of row[GENERICS_DATA]) {
897 elem_name = entry[NAME];
898 if (elem_name === "") {
899 // Pure generic, needs to check into it.
900 if (checkGenerics(entry, elem, MAX_LEV_DISTANCE + 1) !== 0) {
901 return MAX_LEV_DISTANCE + 1;
905 if (elems[elem_name] === undefined) {
906 elems[elem_name] = 0;
908 elems[elem_name] += 1;
910 // We need to find the type that matches the most to remove it in order
912 for (const generic of elem.generics) {
914 if (elems[generic.name]) {
915 match = generic.name;
917 for (elem_name in elems) {
918 if (!hasOwnPropertyRustdoc(elems, elem_name)) {
921 if (elem_name === generic) {
927 if (match === null) {
928 return MAX_LEV_DISTANCE + 1;
931 if (elems[match] === 0) {
937 return MAX_LEV_DISTANCE + 1;
941 * This function checks if the object (`row`) matches the given type (`elem`) and its
945 * @param {QueryElement} elem - The element from the parsed query.
947 * @return {integer} - Returns a Levenshtein distance to the best match.
949 function checkIfInGenerics(row, elem) {
950 let lev = MAX_LEV_DISTANCE + 1;
951 for (const entry of row[GENERICS_DATA]) {
952 lev = Math.min(checkType(entry, elem, true), lev);
961 * This function checks if the object (`row`) matches the given type (`elem`) and its
965 * @param {QueryElement} elem - The element from the parsed query.
966 * @param {boolean} literalSearch
968 * @return {integer} - Returns a Levenshtein distance to the best match. If there is
969 * no match, returns `MAX_LEV_DISTANCE + 1`.
971 function checkType(row, elem, literalSearch) {
972 if (row[NAME].length === 0) {
973 // This is a pure "generic" search, no need to run other checks.
974 if (row.length > GENERICS_DATA) {
975 return checkIfInGenerics(row, elem);
977 return MAX_LEV_DISTANCE + 1;
980 let lev = levenshtein(row[NAME], elem.name);
983 // The name didn't match, let's try to check if the generics do.
984 if (elem.generics.length === 0) {
985 const checkGeneric = (row.length > GENERICS_DATA &&
986 row[GENERICS_DATA].length > 0);
987 if (checkGeneric && row[GENERICS_DATA]
988 .findIndex(tmp_elem => tmp_elem[NAME] === elem.name) !== -1) {
992 return MAX_LEV_DISTANCE + 1;
993 } else if (elem.generics.length > 0) {
994 return checkGenerics(row, elem, MAX_LEV_DISTANCE + 1);
997 } else if (row.length > GENERICS_DATA) {
998 if (elem.generics.length === 0) {
1002 // The name didn't match so we now check if the type we're looking for is inside
1004 lev = checkIfInGenerics(row, elem);
1005 // Now whatever happens, the returned distance is "less good" so we should mark
1006 // it as such, and so we add 0.5 to the distance to make it "less good".
1008 } else if (lev > MAX_LEV_DISTANCE) {
1009 // So our item's name doesn't match at all and has generics.
1011 // Maybe it's present in a sub generic? For example "f<A<B<C>>>()", if we're
1012 // looking for "B<C>", we'll need to go down.
1013 return checkIfInGenerics(row, elem);
1015 // At this point, the name kinda match and we have generics to check, so
1017 const tmp_lev = checkGenerics(row, elem, lev);
1018 if (tmp_lev > MAX_LEV_DISTANCE) {
1019 return MAX_LEV_DISTANCE + 1;
1021 // We compute the median value of both checks and return it.
1022 return (tmp_lev + lev) / 2;
1024 } else if (elem.generics.length > 0) {
1025 // In this case, we were expecting generics but there isn't so we simply reject this
1027 return MAX_LEV_DISTANCE + 1;
1029 // No generics on our query or on the target type so we can return without doing
1035 * This function checks if the object (`row`) has an argument with the given type (`elem`).
1038 * @param {QueryElement} elem - The element from the parsed query.
1039 * @param {integer} typeFilter
1041 * @return {integer} - Returns a Levenshtein distance to the best match. If there is no
1042 * match, returns `MAX_LEV_DISTANCE + 1`.
1044 function findArg(row, elem, typeFilter) {
1045 let lev = MAX_LEV_DISTANCE + 1;
1047 if (row && row.type && row.type[INPUTS_DATA] && row.type[INPUTS_DATA].length > 0) {
1048 for (const input of row.type[INPUTS_DATA]) {
1049 if (!typePassesFilter(typeFilter, input[1])) {
1052 lev = Math.min(lev, checkType(input, elem, parsedQuery.literalSearch));
1058 return parsedQuery.literalSearch ? MAX_LEV_DISTANCE + 1 : lev;
1062 * This function checks if the object (`row`) returns the given type (`elem`).
1065 * @param {QueryElement} elem - The element from the parsed query.
1066 * @param {integer} typeFilter
1068 * @return {integer} - Returns a Levenshtein distance to the best match. If there is no
1069 * match, returns `MAX_LEV_DISTANCE + 1`.
1071 function checkReturned(row, elem, typeFilter) {
1072 let lev = MAX_LEV_DISTANCE + 1;
1074 if (row && row.type && row.type.length > OUTPUT_DATA) {
1075 let ret = row.type[OUTPUT_DATA];
1076 if (typeof ret[0] === "string") {
1079 for (const ret_ty of ret) {
1080 if (!typePassesFilter(typeFilter, ret_ty[1])) {
1083 lev = Math.min(lev, checkType(ret_ty, elem, parsedQuery.literalSearch));
1089 return parsedQuery.literalSearch ? MAX_LEV_DISTANCE + 1 : lev;
1092 function checkPath(contains, ty) {
1093 if (contains.length === 0) {
1096 let ret_lev = MAX_LEV_DISTANCE + 1;
1097 const path = ty.path.split("::");
1099 if (ty.parent && ty.parent.name) {
1100 path.push(ty.parent.name.toLowerCase());
1103 const length = path.length;
1104 const clength = contains.length;
1105 if (clength > length) {
1106 return MAX_LEV_DISTANCE + 1;
1108 for (let i = 0; i < length; ++i) {
1109 if (i + clength > length) {
1113 let aborted = false;
1114 for (let x = 0; x < clength; ++x) {
1115 const lev = levenshtein(path[i + x], contains[x]);
1116 if (lev > MAX_LEV_DISTANCE) {
1123 ret_lev = Math.min(ret_lev, Math.round(lev_total / clength));
1129 function typePassesFilter(filter, type) {
1130 // No filter or Exact mach
1131 if (filter <= NO_TYPE_FILTER || filter === type) return true;
1133 // Match related items
1134 const name = itemTypes[type];
1135 switch (itemTypes[filter]) {
1137 return name === "associatedconstant";
1139 return name === "method" || name === "tymethod";
1141 return name === "primitive" || name === "associatedtype";
1143 return name === "traitalias";
1150 function createAliasFromItem(item) {
1157 parent: item.parent,
1163 function handleAliases(ret, query, filterCrates) {
1164 const lowerQuery = query.toLowerCase();
1165 // We separate aliases and crate aliases because we want to have current crate
1166 // aliases to be before the others in the displayed results.
1168 const crateAliases = [];
1169 if (filterCrates !== null) {
1170 if (ALIASES[filterCrates] && ALIASES[filterCrates][lowerQuery]) {
1171 const query_aliases = ALIASES[filterCrates][lowerQuery];
1172 for (const alias of query_aliases) {
1173 aliases.push(createAliasFromItem(searchIndex[alias]));
1177 Object.keys(ALIASES).forEach(crate => {
1178 if (ALIASES[crate][lowerQuery]) {
1179 const pushTo = crate === window.currentCrate ? crateAliases : aliases;
1180 const query_aliases = ALIASES[crate][lowerQuery];
1181 for (const alias of query_aliases) {
1182 pushTo.push(createAliasFromItem(searchIndex[alias]));
1188 const sortFunc = (aaa, bbb) => {
1189 if (aaa.path < bbb.path) {
1191 } else if (aaa.path === bbb.path) {
1196 crateAliases.sort(sortFunc);
1197 aliases.sort(sortFunc);
1199 const pushFunc = alias => {
1200 alias.alias = query;
1201 const res = buildHrefAndPath(alias);
1202 alias.displayPath = pathSplitter(res[0]);
1203 alias.fullPath = alias.displayPath + alias.name;
1204 alias.href = res[1];
1206 ret.others.unshift(alias);
1207 if (ret.others.length > MAX_RESULTS) {
1211 onEach(aliases, pushFunc);
1212 onEach(crateAliases, pushFunc);
1216 * This function adds the given result into the provided `results` map if it matches the
1217 * following condition:
1219 * * If it is a "literal search" (`parsedQuery.literalSearch`), then `lev` must be 0.
1220 * * If it is not a "literal search", `lev` must be <= `MAX_LEV_DISTANCE`.
1222 * The `results` map contains information which will be used to sort the search results:
1224 * * `fullId` is a `string`` used as the key of the object we use for the `results` map.
1225 * * `id` is the index in both `searchWords` and `searchIndex` arrays for this element.
1226 * * `index` is an `integer`` used to sort by the position of the word in the item's name.
1227 * * `lev` is the main metric used to sort the search results.
1229 * @param {Results} results
1230 * @param {string} fullId
1231 * @param {integer} id
1232 * @param {integer} index
1233 * @param {integer} lev
1235 function addIntoResults(results, fullId, id, index, lev) {
1236 if (lev === 0 || (!parsedQuery.literalSearch && lev <= MAX_LEV_DISTANCE)) {
1237 if (results[fullId] !== undefined) {
1238 const result = results[fullId];
1239 if (result.dontValidate || result.lev <= lev) {
1246 dontValidate: parsedQuery.literalSearch,
1253 * This function is called in case the query is only one element (with or without generics).
1254 * This element will be compared to arguments' and returned values' items and also to items.
1256 * Other important thing to note: since there is only one element, we use levenshtein
1257 * distance for name comparisons.
1260 * @param {integer} pos - Position in the `searchIndex`.
1261 * @param {QueryElement} elem - The element from the parsed query.
1262 * @param {Results} results_others - Unqualified results (not in arguments nor in
1264 * @param {Results} results_in_args - Matching arguments results.
1265 * @param {Results} results_returned - Matching returned arguments results.
1267 function handleSingleArg(
1275 if (!row || (filterCrates !== null && row.crate !== filterCrates)) {
1278 let lev, lev_add = 0, index = -1;
1279 const fullId = row.id;
1281 const in_args = findArg(row, elem, parsedQuery.typeFilter);
1282 const returned = checkReturned(row, elem, parsedQuery.typeFilter);
1284 addIntoResults(results_in_args, fullId, pos, index, in_args);
1285 addIntoResults(results_returned, fullId, pos, index, returned);
1287 if (!typePassesFilter(parsedQuery.typeFilter, row.ty)) {
1290 const searchWord = searchWords[pos];
1292 if (parsedQuery.literalSearch) {
1293 if (searchWord === elem.name) {
1294 addIntoResults(results_others, fullId, pos, -1, 0);
1299 // No need to check anything else if it's a "pure" generics search.
1300 if (elem.name.length === 0) {
1301 if (row.type !== null) {
1302 lev = checkGenerics(row.type, elem, MAX_LEV_DISTANCE + 1);
1303 addIntoResults(results_others, fullId, pos, index, lev);
1308 if (elem.fullPath.length > 1) {
1309 lev = checkPath(elem.pathWithoutLast, row);
1310 if (lev > MAX_LEV_DISTANCE || (parsedQuery.literalSearch && lev !== 0)) {
1312 } else if (lev > 0) {
1317 if (searchWord.indexOf(elem.pathLast) > -1 ||
1318 row.normalizedName.indexOf(elem.pathLast) > -1)
1320 // filter type: ... queries
1321 if (!results_others[fullId] !== undefined) {
1322 index = row.normalizedName.indexOf(elem.pathLast);
1325 lev = levenshtein(searchWord, elem.pathLast);
1326 if (lev > 0 && elem.pathLast.length > 2 && searchWord.indexOf(elem.pathLast) > -1)
1328 if (elem.pathLast.length < 6) {
1335 if (lev > MAX_LEV_DISTANCE) {
1337 } else if (index !== -1 && elem.fullPath.length < 2) {
1343 addIntoResults(results_others, fullId, pos, index, lev);
1347 * This function is called in case the query has more than one element. In this case, it'll
1348 * try to match the items which validates all the elements. For `aa -> bb` will look for
1349 * functions which have a parameter `aa` and has `bb` in its returned values.
1352 * @param {integer} pos - Position in the `searchIndex`.
1353 * @param {Object} results
1355 function handleArgs(row, pos, results) {
1356 if (!row || (filterCrates !== null && row.crate !== filterCrates)) {
1363 // If the result is too "bad", we return false and it ends this search.
1364 function checkArgs(elems, callback) {
1365 for (const elem of elems) {
1366 // There is more than one parameter to the query so all checks should be "exact"
1367 const lev = callback(row, elem, NO_TYPE_FILTER);
1377 if (!checkArgs(parsedQuery.elems, findArg)) {
1380 if (!checkArgs(parsedQuery.returned, checkReturned)) {
1387 const lev = Math.round(totalLev / nbLev);
1388 addIntoResults(results, row.id, pos, 0, lev);
1391 function innerRunQuery() {
1392 let elem, i, nSearchWords, in_returned, row;
1394 if (parsedQuery.foundElems === 1) {
1395 if (parsedQuery.elems.length === 1) {
1396 elem = parsedQuery.elems[0];
1397 for (i = 0, nSearchWords = searchWords.length; i < nSearchWords; ++i) {
1398 // It means we want to check for this element everywhere (in names, args and
1409 } else if (parsedQuery.returned.length === 1) {
1410 // We received one returned argument to check, so looking into returned values.
1411 elem = parsedQuery.returned[0];
1412 for (i = 0, nSearchWords = searchWords.length; i < nSearchWords; ++i) {
1413 row = searchIndex[i];
1414 in_returned = checkReturned(row, elem, parsedQuery.typeFilter);
1415 addIntoResults(results_others, row.id, i, -1, in_returned);
1418 } else if (parsedQuery.foundElems > 0) {
1419 for (i = 0, nSearchWords = searchWords.length; i < nSearchWords; ++i) {
1420 handleArgs(searchIndex[i], i, results_others);
1425 if (parsedQuery.error === null) {
1429 const ret = createQueryResults(
1430 sortResults(results_in_args, true),
1431 sortResults(results_returned, true),
1432 sortResults(results_others, false),
1434 handleAliases(ret, parsedQuery.original.replace(/"/g, ""), filterCrates);
1435 if (parsedQuery.error !== null && ret.others.length !== 0) {
1436 // It means some doc aliases were found so let's "remove" the error!
1437 ret.query.error = null;
1443 * Validate performs the following boolean logic. For example:
1444 * "File::open" will give IF A PARENT EXISTS => ("file" && "open")
1445 * exists in (name || path || parent) OR => ("file" && "open") exists in
1448 * This could be written functionally, but I wanted to minimise
1449 * functions on stack.
1451 * @param {string} name - The name of the result
1452 * @param {string} path - The path of the result
1453 * @param {string} keys - The keys to be used (["file", "open"])
1454 * @param {Object} parent - The parent of the result
1456 * @return {boolean} - Whether the result is valid or not
1458 function validateResult(name, path, keys, parent) {
1459 if (!keys || !keys.length) {
1462 for (const key of keys) {
1463 // each check is for validation so we negate the conditions and invalidate
1465 // check for an exact name match
1466 name.indexOf(key) > -1 ||
1467 // then an exact path match
1468 path.indexOf(key) > -1 ||
1469 // next if there is a parent, check for exact parent match
1470 (parent !== undefined && parent.name !== undefined &&
1471 parent.name.toLowerCase().indexOf(key) > -1) ||
1472 // lastly check to see if the name was a levenshtein match
1473 levenshtein(name, key) <= MAX_LEV_DISTANCE)) {
1480 function nextTab(direction) {
1481 const next = (searchState.currentTab + direction + 3) % searchState.focusedByTab.length;
1482 searchState.focusedByTab[searchState.currentTab] = document.activeElement;
1484 focusSearchResult();
1487 // Focus the first search result on the active tab, or the result that
1488 // was focused last time this tab was active.
1489 function focusSearchResult() {
1490 const target = searchState.focusedByTab[searchState.currentTab] ||
1491 document.querySelectorAll(".search-results.active a").item(0) ||
1492 document.querySelectorAll("#titles > button").item(searchState.currentTab);
1498 function buildHrefAndPath(item) {
1501 const type = itemTypes[item.ty];
1502 const name = item.name;
1503 let path = item.path;
1505 if (type === "mod") {
1506 displayPath = path + "::";
1507 href = window.rootPath + path.replace(/::/g, "/") + "/" +
1508 name + "/index.html";
1509 } else if (type === "import") {
1510 displayPath = item.path + "::";
1511 href = window.rootPath + item.path.replace(/::/g, "/") + "/index.html#reexport." + name;
1512 } else if (type === "primitive" || type === "keyword") {
1514 href = window.rootPath + path.replace(/::/g, "/") +
1515 "/" + type + "." + name + ".html";
1516 } else if (type === "externcrate") {
1518 href = window.rootPath + name + "/index.html";
1519 } else if (item.parent !== undefined) {
1520 const myparent = item.parent;
1521 let anchor = "#" + type + "." + name;
1522 const parentType = itemTypes[myparent.ty];
1523 let pageType = parentType;
1524 let pageName = myparent.name;
1526 if (parentType === "primitive") {
1527 displayPath = myparent.name + "::";
1528 } else if (type === "structfield" && parentType === "variant") {
1529 // Structfields belonging to variants are special: the
1530 // final path element is the enum name.
1531 const enumNameIdx = item.path.lastIndexOf("::");
1532 const enumName = item.path.substr(enumNameIdx + 2);
1533 path = item.path.substr(0, enumNameIdx);
1534 displayPath = path + "::" + enumName + "::" + myparent.name + "::";
1535 anchor = "#variant." + myparent.name + ".field." + name;
1537 pageName = enumName;
1539 displayPath = path + "::" + myparent.name + "::";
1541 href = window.rootPath + path.replace(/::/g, "/") +
1546 displayPath = item.path + "::";
1547 href = window.rootPath + item.path.replace(/::/g, "/") +
1548 "/" + type + "." + name + ".html";
1550 return [displayPath, href];
1553 function escape(content) {
1554 const h1 = document.createElement("h1");
1555 h1.textContent = content;
1556 return h1.innerHTML;
1559 function pathSplitter(path) {
1560 const tmp = "<span>" + path.replace(/::/g, "::</span><span>");
1561 if (tmp.endsWith("<span>")) {
1562 return tmp.slice(0, tmp.length - 6);
1568 * Render a set of search results for a single tab.
1569 * @param {Array<?>} array - The search results for this tab
1570 * @param {ParsedQuery} query
1571 * @param {boolean} display - True if this is the active tab
1573 function addTab(array, query, display) {
1574 let extraClass = "";
1575 if (display === true) {
1576 extraClass = " active";
1579 const output = document.createElement("div");
1581 if (array.length > 0) {
1582 output.className = "search-results " + extraClass;
1584 array.forEach(item => {
1585 const name = item.name;
1586 const type = itemTypes[item.ty];
1591 if (type === "primitive") {
1592 extra = " <i>(primitive type)</i>";
1593 } else if (type === "keyword") {
1594 extra = " <i>(keyword)</i>";
1597 const link = document.createElement("a");
1598 link.className = "result-" + type;
1599 link.href = item.href;
1601 const wrapper = document.createElement("div");
1602 const resultName = document.createElement("div");
1603 resultName.className = "result-name";
1605 if (item.is_alias) {
1606 const alias = document.createElement("span");
1607 alias.className = "alias";
1609 const bold = document.createElement("b");
1610 bold.innerText = item.alias;
1611 alias.appendChild(bold);
1613 alias.insertAdjacentHTML(
1615 "<span class=\"grey\"><i> - see </i></span>");
1617 resultName.appendChild(alias);
1619 resultName.insertAdjacentHTML(
1621 item.displayPath + "<span class=\"" + type + "\">" + name + extra + "</span>");
1622 wrapper.appendChild(resultName);
1624 const description = document.createElement("div");
1625 description.className = "desc";
1626 const spanDesc = document.createElement("span");
1627 spanDesc.insertAdjacentHTML("beforeend", item.desc);
1629 description.appendChild(spanDesc);
1630 wrapper.appendChild(description);
1631 link.appendChild(wrapper);
1632 output.appendChild(link);
1634 } else if (query.error === null) {
1635 output.className = "search-failed" + extraClass;
1636 output.innerHTML = "No results :(<br/>" +
1637 "Try on <a href=\"https://duckduckgo.com/?q=" +
1638 encodeURIComponent("rust " + query.userQuery) +
1639 "\">DuckDuckGo</a>?<br/><br/>" +
1640 "Or try looking in one of these:<ul><li>The <a " +
1641 "href=\"https://doc.rust-lang.org/reference/index.html\">Rust Reference</a> " +
1642 " for technical details about the language.</li><li><a " +
1643 "href=\"https://doc.rust-lang.org/rust-by-example/index.html\">Rust By " +
1644 "Example</a> for expository code examples.</a></li><li>The <a " +
1645 "href=\"https://doc.rust-lang.org/book/index.html\">Rust Book</a> for " +
1646 "introductions to language features and the language itself.</li><li><a " +
1647 "href=\"https://docs.rs\">Docs.rs</a> for documentation of crates released on" +
1648 " <a href=\"https://crates.io/\">crates.io</a>.</li></ul>";
1650 return [output, length];
1653 function makeTabHeader(tabNb, text, nbElems) {
1654 if (searchState.currentTab === tabNb) {
1655 return "<button class=\"selected\">" + text +
1656 " <div class=\"count\">(" + nbElems + ")</div></button>";
1658 return "<button>" + text + " <div class=\"count\">(" + nbElems + ")</div></button>";
1662 * @param {ResultsTable} results
1663 * @param {boolean} go_to_first
1664 * @param {string} filterCrates
1666 function showResults(results, go_to_first, filterCrates) {
1667 const search = searchState.outputElement();
1668 if (go_to_first || (results.others.length === 1
1669 && getSettingValue("go-to-only-result") === "true"
1670 // By default, the search DOM element is "empty" (meaning it has no children not
1671 // text content). Once a search has been run, it won't be empty, even if you press
1672 // ESC or empty the search input (which also "cancels" the search).
1673 && (!search.firstChild || search.firstChild.innerText !== searchState.loadingText)))
1675 const elem = document.createElement("a");
1676 elem.href = results.others[0].href;
1677 removeClass(elem, "active");
1678 // For firefox, we need the element to be in the DOM so it can be clicked.
1679 document.body.appendChild(elem);
1683 if (results.query === undefined) {
1684 results.query = parseQuery(searchState.input.value);
1687 currentResults = results.query.userQuery;
1689 const ret_others = addTab(results.others, results.query, true);
1690 const ret_in_args = addTab(results.in_args, results.query, false);
1691 const ret_returned = addTab(results.returned, results.query, false);
1693 // Navigate to the relevant tab if the current tab is empty, like in case users search
1694 // for "-> String". If they had selected another tab previously, they have to click on
1696 let currentTab = searchState.currentTab;
1697 if ((currentTab === 0 && ret_others[1] === 0) ||
1698 (currentTab === 1 && ret_in_args[1] === 0) ||
1699 (currentTab === 2 && ret_returned[1] === 0)) {
1700 if (ret_others[1] !== 0) {
1702 } else if (ret_in_args[1] !== 0) {
1704 } else if (ret_returned[1] !== 0) {
1710 if (window.ALL_CRATES.length > 1) {
1711 crates = " in <select id=\"crate-search\"><option value=\"All crates\">" +
1712 "All crates</option>";
1713 for (const c of window.ALL_CRATES) {
1714 crates += `<option value="${c}" ${c == filterCrates && "selected"}>${c}</option>`;
1716 crates += "</select>";
1719 let typeFilter = "";
1720 if (results.query.typeFilter !== NO_TYPE_FILTER) {
1721 typeFilter = " (type: " + escape(itemTypes[results.query.typeFilter]) + ")";
1724 let output = "<div id=\"search-settings\">" +
1725 `<h1 class="search-results-title">Results for ${escape(results.query.userQuery)}` +
1726 `${typeFilter}</h1> in ${crates} </div>`;
1727 if (results.query.error !== null) {
1728 output += `<h3>Query parser error: "${results.query.error}".</h3>`;
1729 output += "<div id=\"titles\">" +
1730 makeTabHeader(0, "In Names", ret_others[1]) +
1733 } else if (results.query.foundElems <= 1 && results.query.returned.length === 0) {
1734 output += "<div id=\"titles\">" +
1735 makeTabHeader(0, "In Names", ret_others[1]) +
1736 makeTabHeader(1, "In Parameters", ret_in_args[1]) +
1737 makeTabHeader(2, "In Return Types", ret_returned[1]) +
1740 const signatureTabTitle =
1741 results.query.elems.length === 0 ? "In Function Return Types" :
1742 results.query.returned.length === 0 ? "In Function Parameters" :
1743 "In Function Signatures";
1744 output += "<div id=\"titles\">" +
1745 makeTabHeader(0, signatureTabTitle, ret_others[1]) +
1750 const resultsElem = document.createElement("div");
1751 resultsElem.id = "results";
1752 resultsElem.appendChild(ret_others[0]);
1753 resultsElem.appendChild(ret_in_args[0]);
1754 resultsElem.appendChild(ret_returned[0]);
1756 search.innerHTML = output;
1757 const crateSearch = document.getElementById("crate-search");
1759 crateSearch.addEventListener("input", updateCrate);
1761 search.appendChild(resultsElem);
1762 // Reset focused elements.
1763 searchState.showResults(search);
1764 const elems = document.getElementById("titles").childNodes;
1765 searchState.focusedByTab = [];
1767 for (const elem of elems) {
1769 elem.onclick = () => { printTab(j); };
1770 searchState.focusedByTab.push(null);
1773 printTab(currentTab);
1777 * Perform a search based on the current state of the search input element
1778 * and display the results.
1779 * @param {Event} [e] - The event that triggered this search, if any
1780 * @param {boolean} [forced]
1782 function search(e, forced) {
1783 const params = searchState.getQueryStringParams();
1784 const query = parseQuery(searchState.input.value.trim());
1790 if (!forced && query.userQuery === currentResults) {
1791 if (query.userQuery.length > 0) {
1797 let filterCrates = getFilterCrates();
1799 // In case we have no information about the saved crate and there is a URL query parameter,
1800 // we override it with the URL query parameter.
1801 if (filterCrates === null && params["filter-crate"] !== undefined) {
1802 filterCrates = params["filter-crate"];
1805 // Update document title to maintain a meaningful browser history
1806 searchState.title = "Results for " + query.original + " - Rust";
1808 // Because searching is incremental by character, only the most
1809 // recent search query is added to the browser history.
1810 if (browserSupportsHistoryApi()) {
1811 const newURL = buildUrl(query.original, filterCrates);
1813 if (!history.state && !params.search) {
1814 history.pushState(null, "", newURL);
1816 history.replaceState(null, "", newURL);
1821 execQuery(query, searchWords, filterCrates),
1826 function buildIndex(rawSearchIndex) {
1829 * @type {Array<string>}
1831 const searchWords = [];
1833 let currentIndex = 0;
1836 for (const crate in rawSearchIndex) {
1837 if (!hasOwnPropertyRustdoc(rawSearchIndex, crate)) {
1844 * The raw search data for a given crate. `n`, `t`, `d`, and `q`, `i`, and `f`
1845 * are arrays with the same length. n[i] contains the name of an item.
1846 * t[i] contains the type of that item (as a small integer that represents an
1847 * offset in `itemTypes`). d[i] contains the description of that item.
1849 * q[i] contains the full path of the item, or an empty string indicating
1852 * i[i], f[i] are a mystery.
1854 * `a` defines aliases with an Array of pairs: [name, offset], where `offset`
1855 * points into the n/t/d/q/i/f arrays.
1857 * `doc` contains the description of the crate.
1859 * `p` is a mystery and isn't the same length as n/t/d/q/i/f.
1869 * f: Array<Array<?>>,
1873 const crateCorpus = rawSearchIndex[crate];
1875 searchWords.push(crate);
1876 // This object should have exactly the same set of fields as the "row"
1877 // object defined below. Your JavaScript runtime will thank you.
1878 // https://mathiasbynens.be/notes/shapes-ics
1881 ty: 1, // == ExternCrate
1884 desc: crateCorpus.doc,
1888 normalizedName: crate.indexOf("_") === -1 ? crate : crate.replace(/_/g, ""),
1891 searchIndex.push(crateRow);
1894 // an array of (Number) item types
1895 const itemTypes = crateCorpus.t;
1896 // an array of (String) item names
1897 const itemNames = crateCorpus.n;
1898 // an array of (String) full paths (or empty string for previous path)
1899 const itemPaths = crateCorpus.q;
1900 // an array of (String) descriptions
1901 const itemDescs = crateCorpus.d;
1902 // an array of (Number) the parent path index + 1 to `paths`, or 0 if none
1903 const itemParentIdxs = crateCorpus.i;
1904 // an array of (Object | null) the type of the function, if any
1905 const itemFunctionSearchTypes = crateCorpus.f;
1906 // an array of [(Number) item type,
1908 const paths = crateCorpus.p;
1909 // an array of [(String) alias name
1910 // [Number] index to items]
1911 const aliases = crateCorpus.a;
1913 // convert `rawPaths` entries into object form
1914 let len = paths.length;
1915 for (i = 0; i < len; ++i) {
1916 paths[i] = {ty: paths[i][0], name: paths[i][1]};
1919 // convert `item*` into an object form, and construct word indices.
1921 // before any analysis is performed lets gather the search terms to
1922 // search against apart from the rest of the data. This is a quick
1923 // operation that is cached for the life of the page state so that
1924 // all other search operations have access to this cached data for
1925 // faster analysis operations
1926 len = itemTypes.length;
1928 for (i = 0; i < len; ++i) {
1929 // This object should have exactly the same set of fields as the "crateRow"
1930 // object defined above.
1931 if (typeof itemNames[i] === "string") {
1932 word = itemNames[i].toLowerCase();
1933 searchWords.push(word);
1936 searchWords.push("");
1942 path: itemPaths[i] ? itemPaths[i] : lastPath,
1944 parent: itemParentIdxs[i] > 0 ? paths[itemParentIdxs[i] - 1] : undefined,
1945 type: itemFunctionSearchTypes[i],
1947 normalizedName: word.indexOf("_") === -1 ? word : word.replace(/_/g, ""),
1950 searchIndex.push(row);
1951 lastPath = row.path;
1956 ALIASES[crate] = Object.create(null);
1957 for (const alias_name in aliases) {
1958 if (!hasOwnPropertyRustdoc(aliases, alias_name)) {
1962 if (!hasOwnPropertyRustdoc(ALIASES[crate], alias_name)) {
1963 ALIASES[crate][alias_name] = [];
1965 for (const local_alias of aliases[alias_name]) {
1966 ALIASES[crate][alias_name].push(local_alias + currentIndex);
1970 currentIndex += crateSize;
1976 * Callback for when the search form is submitted.
1977 * @param {Event} [e] - The event that triggered this call, if any
1979 function onSearchSubmit(e) {
1981 searchState.clearInputTimeout();
1985 function putBackSearch() {
1986 const search_input = searchState.input;
1987 if (!searchState.input) {
1990 if (search_input.value !== "" && !searchState.isDisplayed()) {
1991 searchState.showResults();
1992 if (browserSupportsHistoryApi()) {
1993 history.replaceState(null, "",
1994 buildUrl(search_input.value, getFilterCrates()));
1996 document.title = searchState.title;
2000 function registerSearchEvents() {
2001 const searchAfter500ms = () => {
2002 searchState.clearInputTimeout();
2003 if (searchState.input.value.length === 0) {
2004 if (browserSupportsHistoryApi()) {
2005 history.replaceState(null, window.currentCrate + " - Rust",
2006 getNakedUrl() + window.location.hash);
2008 searchState.hideResults();
2010 searchState.timeout = setTimeout(search, 500);
2013 searchState.input.onkeyup = searchAfter500ms;
2014 searchState.input.oninput = searchAfter500ms;
2015 document.getElementsByClassName("search-form")[0].onsubmit = onSearchSubmit;
2016 searchState.input.onchange = e => {
2017 if (e.target !== document.activeElement) {
2018 // To prevent doing anything when it's from a blur event.
2021 // Do NOT e.preventDefault() here. It will prevent pasting.
2022 searchState.clearInputTimeout();
2023 // zero-timeout necessary here because at the time of event handler execution the
2024 // pasted content is not in the input field yet. Shouldn’t make any difference for
2026 setTimeout(search, 0);
2028 searchState.input.onpaste = searchState.input.onchange;
2030 searchState.outputElement().addEventListener("keydown", e => {
2031 // We only handle unmodified keystrokes here. We don't want to interfere with,
2032 // for instance, alt-left and alt-right for history navigation.
2033 if (e.altKey || e.ctrlKey || e.shiftKey || e.metaKey) {
2036 // up and down arrow select next/previous search result, or the
2037 // search box if we're already at the top.
2038 if (e.which === 38) { // up
2039 const previous = document.activeElement.previousElementSibling;
2043 searchState.focus();
2046 } else if (e.which === 40) { // down
2047 const next = document.activeElement.nextElementSibling;
2051 const rect = document.activeElement.getBoundingClientRect();
2052 if (window.innerHeight - rect.bottom < rect.height) {
2053 window.scrollBy(0, rect.height);
2056 } else if (e.which === 37) { // left
2059 } else if (e.which === 39) { // right
2065 searchState.input.addEventListener("keydown", e => {
2066 if (e.which === 40) { // down
2067 focusSearchResult();
2072 searchState.input.addEventListener("focus", () => {
2076 searchState.input.addEventListener("blur", () => {
2077 searchState.input.placeholder = searchState.input.origPlaceholder;
2080 // Push and pop states are used to add search results to the browser
2082 if (browserSupportsHistoryApi()) {
2083 // Store the previous <title> so we can revert back to it later.
2084 const previousTitle = document.title;
2086 window.addEventListener("popstate", e => {
2087 const params = searchState.getQueryStringParams();
2088 // Revert to the previous title manually since the History
2089 // API ignores the title parameter.
2090 document.title = previousTitle;
2091 // When browsing forward to search results the previous
2092 // search will be repeated, so the currentResults are
2093 // cleared to ensure the search is successful.
2094 currentResults = null;
2095 // Synchronize search bar with query string state and
2096 // perform the search. This will empty the bar if there's
2097 // nothing there, which lets you really go back to a
2098 // previous state with nothing in the bar.
2099 if (params.search && params.search.length > 0) {
2100 searchState.input.value = params.search;
2101 // Some browsers fire "onpopstate" for every page load
2102 // (Chrome), while others fire the event only when actually
2103 // popping a state (Firefox), which is why search() is
2104 // called both here and at the end of the startSearch()
2108 searchState.input.value = "";
2109 // When browsing back from search results the main page
2110 // visibility must be reset.
2111 searchState.hideResults();
2116 // This is required in firefox to avoid this problem: Navigating to a search result
2117 // with the keyboard, hitting enter, and then hitting back would take you back to
2118 // the doc page, rather than the search that should overlay it.
2119 // This was an interaction between the back-forward cache and our handlers
2120 // that try to sync state between the URL and the search input. To work around it,
2121 // do a small amount of re-init on page show.
2122 window.onpageshow = () => {
2123 const qSearch = searchState.getQueryStringParams().search;
2124 if (searchState.input.value === "" && qSearch) {
2125 searchState.input.value = qSearch;
2131 function updateCrate(ev) {
2132 if (ev.target.value === "All crates") {
2133 // If we don't remove it from the URL, it'll be picked up again by the search.
2134 const params = searchState.getQueryStringParams();
2135 const query = searchState.input.value.trim();
2136 if (!history.state && !params.search) {
2137 history.pushState(null, "", buildUrl(query, null));
2139 history.replaceState(null, "", buildUrl(query, null));
2142 // In case you "cut" the entry from the search input, then change the crate filter
2143 // before paste back the previous search, you get the old search results without
2144 // the filter. To prevent this, we need to remove the previous results.
2145 currentResults = null;
2146 search(undefined, true);
2150 * @type {Array<string>}
2152 const searchWords = buildIndex(rawSearchIndex);
2153 registerSearchEvents();
2155 function runSearchIfNeeded() {
2156 // If there's a search term in the URL, execute the search now.
2157 if (searchState.getQueryStringParams().search) {
2162 runSearchIfNeeded();
2165 if (window.searchIndex !== undefined) {
2166 initSearch(window.searchIndex);