rust/src/librustdoc/html/static/main.js

2155 lines
84 KiB
JavaScript
Raw Normal View History

2017-05-30 06:17:11 +02:00
/*!
* Copyright 2014 The Rust Project Developers. See the COPYRIGHT
* file at the top-level directory of this distribution and at
* http://rust-lang.org/COPYRIGHT.
*
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
* http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
* <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
* option. This file may not be copied, modified, or distributed
* except according to those terms.
*/
/*jslint browser: true, es5: true */
/*globals $: true, rootPath: true */
(function() {
"use strict";
// This mapping table should match the discriminants of
// `rustdoc::html::item_type::ItemType` type in Rust.
var itemTypes = ["mod",
"externcrate",
"import",
"struct",
"enum",
"fn",
"type",
"static",
"trait",
"impl",
"tymethod",
"method",
"structfield",
"variant",
"macro",
"primitive",
"associatedtype",
"constant",
2016-08-10 20:00:17 +02:00
"associatedconstant",
"union",
"foreigntype"];
2018-04-24 20:23:57 +02:00
var search_input = document.getElementsByClassName('search-input')[0];
2017-10-14 16:31:48 +02:00
// On the search screen, so you remain on the last tab you opened.
//
// 0 for "In Names"
// 1 for "In Parameters"
// 2 for "In Return Types"
2017-10-14 16:31:48 +02:00
var currentTab = 0;
2018-01-27 18:58:59 +01:00
var themesWidth = null;
2018-03-23 21:27:15 +01:00
if (!String.prototype.startsWith) {
String.prototype.startsWith = function(searchString, position) {
position = position || 0;
return this.indexOf(searchString, position) === position;
};
}
function getPageId() {
var id = document.location.href.split('#')[1];
if (id) {
return id.split('?')[0].split('&')[0];
}
return null;
}
2017-04-14 16:37:09 +02:00
function hasClass(elem, className) {
if (elem && className && elem.className) {
var elemClass = elem.className;
var start = elemClass.indexOf(className);
2017-11-01 17:38:37 +01:00
if (start === -1) {
2017-04-14 16:37:09 +02:00
return false;
} else if (elemClass.length === className.length) {
2017-04-14 16:37:09 +02:00
return true;
} else {
if (start > 0 && elemClass[start - 1] !== ' ') {
2017-04-14 16:37:09 +02:00
return false;
}
var end = start + className.length;
2018-04-13 22:54:09 +02:00
return !(end < elemClass.length && elemClass[end] !== ' ');
2017-04-14 16:37:09 +02:00
}
2017-11-01 17:38:37 +01:00
if (start > 0 && elemClass[start - 1] !== ' ') {
return false;
}
var end = start + className.length;
2018-04-13 22:54:09 +02:00
return !(end < elemClass.length && elemClass[end] !== ' ');
2017-04-14 16:37:09 +02:00
}
return false;
}
function addClass(elem, className) {
if (elem && className && !hasClass(elem, className)) {
if (elem.className && elem.className.length > 0) {
elem.className += ' ' + className;
} else {
elem.className = className;
}
}
}
function removeClass(elem, className) {
if (elem && className && elem.className) {
elem.className = (" " + elem.className + " ").replace(" " + className + " ", " ")
.trim();
}
}
function isHidden(elem) {
return (elem.offsetParent === null)
}
function showSidebar() {
2017-12-07 22:55:14 +01:00
var elems = document.getElementsByClassName("sidebar-elems")[0];
if (elems) {
2017-12-20 00:44:44 +01:00
addClass(elems, "show-it");
2017-12-07 22:55:14 +01:00
}
var sidebar = document.getElementsByClassName('sidebar')[0];
2017-12-20 00:44:44 +01:00
if (sidebar) {
addClass(sidebar, 'mobile');
var filler = document.getElementById("sidebar-filler");
if (!filler) {
var div = document.createElement("div");
div.id = "sidebar-filler";
sidebar.appendChild(div);
}
}
var themePicker = document.getElementsByClassName("theme-picker");
if (themePicker && themePicker.length > 0) {
themePicker[0].style.display = "none";
}
}
function hideSidebar() {
2017-12-07 22:55:14 +01:00
var elems = document.getElementsByClassName("sidebar-elems")[0];
if (elems) {
2017-12-20 00:44:44 +01:00
removeClass(elems, "show-it");
2017-12-07 22:55:14 +01:00
}
var sidebar = document.getElementsByClassName('sidebar')[0];
2017-12-20 00:44:44 +01:00
removeClass(sidebar, 'mobile');
var filler = document.getElementById("sidebar-filler");
if (filler) {
filler.remove();
}
document.getElementsByTagName("body")[0].style.marginTop = '';
var themePicker = document.getElementsByClassName("theme-picker");
if (themePicker && themePicker.length > 0) {
themePicker[0].style.display = null;
}
}
// used for special search precedence
var TY_PRIMITIVE = itemTypes.indexOf("primitive");
2017-04-14 16:37:09 +02:00
onEach(document.getElementsByClassName('js-only'), function(e) {
removeClass(e, 'js-only');
});
function getQueryStringParams() {
var params = {};
window.location.search.substring(1).split("&").
map(function(s) {
var pair = s.split("=");
params[decodeURIComponent(pair[0])] =
typeof pair[1] === "undefined" ? null : decodeURIComponent(pair[1]);
});
return params;
}
function browserSupportsHistoryApi() {
return document.location.protocol != "file:" &&
window.history && typeof window.history.pushState === "function";
}
function highlightSourceLines(ev) {
// If we're in mobile mode, we should add the sidebar in any case.
hideSidebar();
2017-11-06 21:14:37 +01:00
var search = document.getElementById("search");
var i, from, to, match = window.location.hash.match(/^#?(\d+)(?:-(\d+))?$/);
if (match) {
from = parseInt(match[1], 10);
to = Math.min(50000, parseInt(match[2] || match[1], 10));
from = Math.min(from, to);
2017-04-14 16:37:09 +02:00
var elem = document.getElementById(from);
if (!elem) {
return;
}
2017-04-14 16:37:09 +02:00
if (ev === null) {
var x = document.getElementById(from);
if (x) {
x.scrollIntoView();
}
2017-11-11 23:24:04 +01:00
}
2017-04-14 16:37:09 +02:00
onEach(document.getElementsByClassName('line-numbers'), function(e) {
onEach(e.getElementsByTagName('span'), function(i_e) {
removeClass(i_e, 'line-highlighted');
});
2018-05-11 00:02:05 +02:00
});
for (i = from; i <= to; ++i) {
2017-04-14 16:37:09 +02:00
addClass(document.getElementById(i), 'line-highlighted');
}
2017-11-06 21:14:37 +01:00
} else if (ev !== null && search && !hasClass(search, "hidden") && ev.newURL) {
addClass(search, "hidden");
removeClass(document.getElementById("main"), "hidden");
var hash = ev.newURL.slice(ev.newURL.indexOf('#') + 1);
2017-11-10 19:40:46 +01:00
if (browserSupportsHistoryApi()) {
history.replaceState(hash, "", "?search=#" + hash);
2017-11-10 19:40:46 +01:00
}
2017-11-06 21:14:37 +01:00
var elem = document.getElementById(hash);
if (elem) {
elem.scrollIntoView();
}
}
}
highlightSourceLines(null);
2017-04-14 16:37:09 +02:00
window.onhashchange = highlightSourceLines;
// Gets the human-readable string for the virtual-key code of the
// given KeyboardEvent, ev.
//
// This function is meant as a polyfill for KeyboardEvent#key,
// since it is not supported in Trident. We also test for
// KeyboardEvent#keyCode because the handleShortcut handler is
// also registered for the keydown event, because Blink doesn't fire
// keypress on hitting the Escape key.
//
// So I guess you could say things are getting pretty interoperable.
function getVirtualKey(ev) {
if ("key" in ev && typeof ev.key != "undefined")
return ev.key;
var c = ev.charCode || ev.keyCode;
if (c == 27)
return "Escape";
return String.fromCharCode(c);
}
2017-10-14 18:43:00 +02:00
function displayHelp(display, ev) {
if (display === true) {
if (hasClass(help, "hidden")) {
ev.preventDefault();
removeClass(help, "hidden");
addClass(document.body, "blur");
}
} else if (!hasClass(help, "hidden")) {
ev.preventDefault();
addClass(help, "hidden");
removeClass(document.body, "blur");
}
}
2018-03-18 16:32:41 +01:00
function handleEscape(ev, help) {
hideModal();
var search = document.getElementById("search");
if (!hasClass(help, "hidden")) {
displayHelp(false, ev);
} else if (!hasClass(search, "hidden")) {
ev.preventDefault();
addClass(search, "hidden");
removeClass(document.getElementById("main"), "hidden");
}
2018-03-18 16:32:41 +01:00
defocusSearchBar();
}
2018-03-18 16:32:41 +01:00
function handleShortcut(ev) {
// Don't interfere with browser shortcuts
if (ev.ctrlKey || ev.altKey || ev.metaKey) {
return;
}
2017-04-14 16:37:09 +02:00
var help = document.getElementById("help");
2018-03-18 16:32:41 +01:00
if (document.activeElement.tagName === "INPUT") {
switch (getVirtualKey(ev)) {
case "Escape":
handleEscape(ev, help);
break;
}
2018-03-18 16:32:41 +01:00
} else {
switch (getVirtualKey(ev)) {
case "Escape":
handleEscape(ev, help);
break;
2018-03-18 16:32:41 +01:00
case "s":
case "S":
displayHelp(false, ev);
hideModal();
ev.preventDefault();
focusSearchBar();
break;
2018-03-18 16:32:41 +01:00
case "+":
case "-":
ev.preventDefault();
toggleAllDocs();
break;
2018-03-18 16:32:41 +01:00
case "?":
if (ev.shiftKey) {
hideModal();
displayHelp(true, ev);
}
break;
}
}
}
2017-04-14 16:37:09 +02:00
document.onkeypress = handleShortcut;
document.onkeydown = handleShortcut;
document.onclick = function(ev) {
if (hasClass(ev.target, 'collapse-toggle')) {
collapseDocs(ev.target, "toggle");
2017-04-14 16:37:09 +02:00
} else if (hasClass(ev.target.parentNode, 'collapse-toggle')) {
collapseDocs(ev.target.parentNode, "toggle");
2017-04-14 16:37:09 +02:00
} else if (ev.target.tagName === 'SPAN' && hasClass(ev.target.parentNode, 'line-numbers')) {
var prev_id = 0;
2018-04-13 22:54:09 +02:00
var set_fragment = function(name) {
2017-04-14 16:37:09 +02:00
if (browserSupportsHistoryApi()) {
history.replaceState(null, null, '#' + name);
window.hashchange();
} else {
location.replace('#' + name);
}
};
2017-04-14 16:37:09 +02:00
var cur_id = parseInt(ev.target.id, 10);
2017-04-14 16:37:09 +02:00
if (ev.shiftKey && prev_id) {
if (prev_id > cur_id) {
var tmp = prev_id;
prev_id = cur_id;
cur_id = tmp;
}
set_fragment(prev_id + '-' + cur_id);
} else {
prev_id = cur_id;
set_fragment(cur_id);
}
2017-04-14 16:37:09 +02:00
} else if (!hasClass(document.getElementById("help"), "hidden")) {
addClass(document.getElementById("help"), "hidden");
removeClass(document.body, "blur");
}
2017-04-14 16:37:09 +02:00
};
2017-04-14 16:37:09 +02:00
var x = document.getElementsByClassName('version-selector');
if (x.length > 0) {
x[0].onchange = function() {
var i, match,
url = document.location.href,
stripped = '',
len = rootPath.match(/\.\.\//g).length + 1;
2017-04-14 16:37:09 +02:00
for (i = 0; i < len; ++i) {
match = url.match(/\/[^\/]*$/);
if (i < len - 1) {
stripped = match[0] + stripped;
}
url = url.substring(0, url.length - match[0].length);
}
url += '/' + document.getElementsByClassName('version-selector')[0].value + stripped;
document.location.href = url;
};
}
/**
* A function to compute the Levenshtein distance between two strings
* Licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported
* Full License can be found at http://creativecommons.org/licenses/by-sa/3.0/legalcode
* This code is an unmodified version of the code written by Marco de Wit
* and was found at http://stackoverflow.com/a/18514751/745719
*/
2018-01-07 16:19:44 +01:00
var levenshtein_row2 = [];
function levenshtein(s1, s2) {
if (s1 === s2) {
return 0;
}
var s1_len = s1.length, s2_len = s2.length;
if (s1_len && s2_len) {
var i1 = 0, i2 = 0, a, b, c, c2, row = levenshtein_row2;
while (i1 < s1_len) {
row[i1] = ++i1;
}
2018-01-07 16:19:44 +01:00
while (i2 < s2_len) {
c2 = s2.charCodeAt(i2);
a = i2;
++i2;
b = i2;
for (i1 = 0; i1 < s1_len; ++i1) {
c = a + (s1.charCodeAt(i1) !== c2 ? 1 : 0);
a = row[i1];
b = b < a ? (b < c ? b + 1 : c) : (a < c ? a + 1 : c);
row[i1] = b;
}
}
2018-01-07 16:19:44 +01:00
return b;
}
return s1_len + s2_len;
}
function initSearch(rawSearchIndex) {
var currentResults, index, searchIndex;
var MAX_LEV_DISTANCE = 3;
2017-11-22 00:17:30 +01:00
var MAX_RESULTS = 200;
var params = getQueryStringParams();
// Populate search bar with query string search term when provided,
// but only if the input bar is empty. This avoid the obnoxious issue
// where you start trying to do a search, and the index loads, and
// suddenly your search is gone!
2018-04-24 20:23:57 +02:00
if (search_input.value === "") {
search_input.value = params.search || '';
}
/**
* Executes the query and builds an index of results
* @param {[Object]} query [The user query]
* @param {[type]} searchWords [The list of search words to query
* against]
* @return {[type]} [A search index of results]
*/
2018-01-07 16:19:44 +01:00
function execQuery(query, searchWords) {
function itemTypeFromName(typename) {
for (var i = 0; i < itemTypes.length; ++i) {
if (itemTypes[i] === typename) {
return i;
}
}
return -1;
}
var valLower = query.query.toLowerCase(),
val = valLower,
typeFilter = itemTypeFromName(query.type),
2017-11-22 00:17:30 +01:00
results = {}, results_in_args = {}, results_returned = {},
split = valLower.split("::");
2017-11-18 17:30:19 +01:00
for (var z = 0; z < split.length; ++z) {
if (split[z] === "") {
2017-11-19 13:41:56 +01:00
split.splice(z, 1);
2017-11-18 17:30:19 +01:00
z -= 1;
}
}
2017-11-22 00:17:30 +01:00
function transformResults(results, isType) {
var out = [];
for (i = 0; i < results.length; ++i) {
if (results[i].id > -1) {
var obj = searchIndex[results[i].id];
obj.lev = results[i].lev;
if (isType !== true || obj.type) {
out.push(obj);
}
}
if (out.length >= MAX_RESULTS) {
break;
}
}
return out;
}
function sortResults(results, isType) {
var ar = [];
for (var entry in results) {
if (results.hasOwnProperty(entry)) {
ar.push(results[entry]);
}
}
results = ar;
var nresults = results.length;
for (var i = 0; i < nresults; ++i) {
results[i].word = searchWords[results[i].id];
results[i].item = searchIndex[results[i].id] || {};
}
// if there are no results then return to default and fail
if (results.length === 0) {
return [];
}
results.sort(function(aaa, bbb) {
var a, b;
// Sort by non levenshtein results and then levenshtein results by the distance
// (less changes required to match means higher rankings)
a = (aaa.lev);
b = (bbb.lev);
if (a !== b) { return a - b; }
// sort by crate (non-current crate goes later)
a = (aaa.item.crate !== window.currentCrate);
b = (bbb.item.crate !== window.currentCrate);
if (a !== b) { return a - b; }
// sort by exact match (mismatch goes later)
a = (aaa.word !== valLower);
b = (bbb.word !== valLower);
if (a !== b) { return a - b; }
// sort by item name length (longer goes later)
a = aaa.word.length;
b = bbb.word.length;
if (a !== b) { return a - b; }
// sort by item name (lexicographically larger goes later)
a = aaa.word;
b = bbb.word;
if (a !== b) { return (a > b ? +1 : -1); }
// sort by index of keyword in item name (no literal occurrence goes later)
a = (aaa.index < 0);
b = (bbb.index < 0);
if (a !== b) { return a - b; }
// (later literal occurrence, if any, goes later)
a = aaa.index;
b = bbb.index;
if (a !== b) { return a - b; }
// special precedence for primitive pages
if ((aaa.item.ty === TY_PRIMITIVE) && (bbb.item.ty !== TY_PRIMITIVE)) {
return -1;
}
if ((bbb.item.ty === TY_PRIMITIVE) && (aaa.item.ty !== TY_PRIMITIVE)) {
return 1;
}
// sort by description (no description goes later)
a = (aaa.item.desc === '');
b = (bbb.item.desc === '');
if (a !== b) { return a - b; }
// sort by type (later occurrence in `itemTypes` goes later)
a = aaa.item.ty;
b = bbb.item.ty;
if (a !== b) { return a - b; }
// sort by path (lexicographically larger goes later)
a = aaa.item.path;
b = bbb.item.path;
if (a !== b) { return (a > b ? +1 : -1); }
// que sera, sera
return 0;
});
for (var i = 0; i < results.length; ++i) {
var result = results[i];
// this validation does not make sense when searching by types
if (result.dontValidate) {
continue;
}
var name = result.item.name.toLowerCase(),
path = result.item.path.toLowerCase(),
parent = result.item.parent;
if (isType !== true &&
validateResult(name, path, split, parent) === false)
{
result.id = -1;
}
}
return transformResults(results);
}
2017-11-01 17:38:37 +01:00
function extractGenerics(val) {
val = val.toLowerCase();
if (val.indexOf('<') !== -1) {
var values = val.substring(val.indexOf('<') + 1, val.lastIndexOf('>'));
return {
name: val.substring(0, val.indexOf('<')),
generics: values.split(/\s*,\s*/),
};
}
return {
name: val,
generics: [],
};
}
function checkGenerics(obj, val) {
// The names match, but we need to be sure that all generics kinda
// match as well.
var lev_distance = MAX_LEV_DISTANCE + 1;
2017-11-01 17:38:37 +01:00
if (val.generics.length > 0) {
2017-11-22 00:17:30 +01:00
if (obj.generics && obj.generics.length >= val.generics.length) {
2017-11-01 17:38:37 +01:00
var elems = obj.generics.slice(0);
2017-11-22 00:17:30 +01:00
var total = 0;
var done = 0;
// We need to find the type that matches the most to remove it in order
// to move forward.
2017-11-18 17:30:19 +01:00
for (var y = 0; y < val.generics.length; ++y) {
2017-11-01 17:38:37 +01:00
var lev = { pos: -1, lev: MAX_LEV_DISTANCE + 1};
for (var x = 0; x < elems.length; ++x) {
var tmp_lev = levenshtein(elems[x], val.generics[y]);
if (tmp_lev < lev.lev) {
lev.lev = tmp_lev;
lev.pos = x;
}
}
if (lev.pos !== -1) {
elems.splice(lev.pos, 1);
2017-11-17 21:59:06 +01:00
lev_distance = Math.min(lev.lev, lev_distance);
2017-11-22 00:17:30 +01:00
total += lev.lev;
done += 1;
2017-11-01 17:38:37 +01:00
} else {
return MAX_LEV_DISTANCE + 1;
}
}
2017-11-22 00:17:30 +01:00
return lev_distance;//Math.ceil(total / done);
2017-11-01 17:38:37 +01:00
}
}
return MAX_LEV_DISTANCE + 1;
}
// Check for type name and type generics (if any).
function checkType(obj, val, literalSearch) {
var lev_distance = MAX_LEV_DISTANCE + 1;
if (obj.name === val.name) {
if (literalSearch === true) {
2018-01-22 22:49:44 +01:00
if (val.generics && val.generics.length !== 0) {
2017-11-01 17:38:37 +01:00
if (obj.generics && obj.length >= val.generics.length) {
var elems = obj.generics.slice(0);
var allFound = true;
var x;
for (var y = 0; allFound === true && y < val.generics.length; ++y) {
allFound = false;
for (x = 0; allFound === false && x < elems.length; ++x) {
allFound = elems[x] === val.generics[y];
}
if (allFound === true) {
elems.splice(x - 1, 1);
}
}
if (allFound === true) {
return true;
}
} else {
return false;
}
}
return true;
}
2017-11-11 23:53:37 +01:00
// If the type has generics but don't match, then it won't return at this point.
// Otherwise, `checkGenerics` will return 0 and it'll return.
2017-11-22 00:17:30 +01:00
if (obj.generics && obj.generics.length !== 0) {
var tmp_lev = checkGenerics(obj, val);
if (tmp_lev <= MAX_LEV_DISTANCE) {
return tmp_lev;
}
} else {
return 0;
2017-11-01 17:38:37 +01:00
}
}
// Names didn't match so let's check if one of the generic types could.
if (literalSearch === true) {
2018-01-22 22:49:44 +01:00
if (obj.generics && obj.generics.length > 0) {
2017-11-01 17:38:37 +01:00
for (var x = 0; x < obj.generics.length; ++x) {
if (obj.generics[x] === val.name) {
return true;
}
}
}
return false;
}
2017-11-22 00:17:30 +01:00
var lev_distance = Math.min(levenshtein(obj.name, val.name), lev_distance);
if (lev_distance <= MAX_LEV_DISTANCE) {
lev_distance = Math.min(checkGenerics(obj, val), lev_distance);
2017-11-01 17:38:37 +01:00
} else if (obj.generics && obj.generics.length > 0) {
2017-11-22 00:17:30 +01:00
// We can check if the type we're looking for is inside the generics!
2017-11-01 17:38:37 +01:00
for (var x = 0; x < obj.generics.length; ++x) {
2017-11-17 21:59:06 +01:00
lev_distance = Math.min(levenshtein(obj.generics[x], val.name),
lev_distance);
2017-11-01 17:38:37 +01:00
}
}
2017-11-11 23:53:37 +01:00
// Now whatever happens, the returned distance is "less good" so we should mark it
// as such, and so we add 1 to the distance to make it "less good".
return lev_distance + 1;
2017-11-01 17:38:37 +01:00
}
function findArg(obj, val, literalSearch) {
var lev_distance = MAX_LEV_DISTANCE + 1;
if (obj && obj.type && obj.type.inputs.length > 0) {
for (var i = 0; i < obj.type.inputs.length; i++) {
2017-11-01 17:38:37 +01:00
var tmp = checkType(obj.type.inputs[i], val, literalSearch);
2017-11-11 23:53:37 +01:00
if (literalSearch === true && tmp === true) {
2017-11-01 17:38:37 +01:00
return true;
}
2017-11-17 21:59:06 +01:00
lev_distance = Math.min(tmp, lev_distance);
if (lev_distance === 0) {
return 0;
}
}
}
2017-11-01 17:38:37 +01:00
return literalSearch === true ? false : lev_distance;
}
2017-11-01 17:38:37 +01:00
function checkReturned(obj, val, literalSearch) {
var lev_distance = MAX_LEV_DISTANCE + 1;
2017-11-01 17:38:37 +01:00
if (obj && obj.type && obj.type.output) {
2017-11-01 17:38:37 +01:00
var tmp = checkType(obj.type.output, val, literalSearch);
2017-11-11 23:53:37 +01:00
if (literalSearch === true && tmp === true) {
2017-11-01 17:38:37 +01:00
return true;
}
2017-11-17 21:59:06 +01:00
lev_distance = Math.min(tmp, lev_distance);
if (lev_distance === 0) {
return 0;
}
}
2017-11-01 17:38:37 +01:00
return literalSearch === true ? false : lev_distance;
2017-10-29 13:29:07 +01:00
}
2017-11-19 13:41:56 +01:00
function checkPath(startsWith, lastElem, ty) {
2017-12-13 00:20:45 +01:00
if (startsWith.length === 0) {
return 0;
}
2017-11-19 13:41:56 +01:00
var ret_lev = MAX_LEV_DISTANCE + 1;
2017-11-18 17:30:19 +01:00
var path = ty.path.split("::");
2017-11-19 13:41:56 +01:00
if (ty.parent && ty.parent.name) {
path.push(ty.parent.name.toLowerCase());
}
2017-11-18 17:30:19 +01:00
if (startsWith.length > path.length) {
return MAX_LEV_DISTANCE + 1;
}
2017-11-19 13:41:56 +01:00
for (var i = 0; i < path.length; ++i) {
if (i + startsWith.length > path.length) {
break;
}
var lev_total = 0;
var aborted = false;
for (var x = 0; x < startsWith.length; ++x) {
var lev = levenshtein(path[i + x], startsWith[x]);
if (lev > MAX_LEV_DISTANCE) {
aborted = true;
break;
}
lev_total += lev;
}
if (aborted === false) {
2017-12-13 00:20:45 +01:00
ret_lev = Math.min(ret_lev, Math.round(lev_total / startsWith.length));
2017-11-18 17:30:19 +01:00
}
}
2017-11-19 13:41:56 +01:00
return ret_lev;
2017-11-18 17:30:19 +01:00
}
2015-08-04 17:40:23 +02:00
function typePassesFilter(filter, type) {
// No filter
if (filter < 0) return true;
// Exact match
if (filter === type) return true;
// Match related items
var name = itemTypes[type];
switch (itemTypes[filter]) {
case "constant":
return (name == "associatedconstant");
case "fn":
return (name == "method" || name == "tymethod");
case "type":
return (name == "primitive");
}
// No match
return false;
}
2017-12-01 20:55:25 +01:00
function generateId(ty) {
if (ty.parent && ty.parent.name) {
return itemTypes[ty.ty] + ty.path + ty.parent.name + ty.name;
}
return itemTypes[ty.ty] + ty.path + ty.name;
}
// quoted values mean literal search
var nSearchWords = searchWords.length;
if ((val.charAt(0) === "\"" || val.charAt(0) === "'") &&
val.charAt(val.length - 1) === val.charAt(0))
{
2017-11-01 17:38:37 +01:00
val = extractGenerics(val.substr(1, val.length - 2));
for (var i = 0; i < nSearchWords; ++i) {
2017-11-12 13:19:15 +01:00
var in_args = findArg(searchIndex[i], val, true);
2017-11-01 17:38:37 +01:00
var returned = checkReturned(searchIndex[i], val, true);
var ty = searchIndex[i];
2017-12-01 20:55:25 +01:00
var fullId = generateId(ty);
2017-11-17 21:59:06 +01:00
2017-11-01 17:38:37 +01:00
if (searchWords[i] === val.name) {
// filter type: ... queries
2017-11-01 17:38:37 +01:00
if (typePassesFilter(typeFilter, searchIndex[i].ty) &&
2017-11-17 21:59:06 +01:00
results[fullId] === undefined)
2017-11-11 23:24:04 +01:00
{
2017-11-17 21:59:06 +01:00
results[fullId] = {id: i, index: -1};
}
2017-11-12 13:19:15 +01:00
} else if ((in_args === true || returned === true) &&
2017-11-01 17:38:37 +01:00
typePassesFilter(typeFilter, searchIndex[i].ty)) {
2017-11-22 00:17:30 +01:00
if (in_args === true || returned === true) {
if (in_args === true) {
results_in_args[fullId] = {
id: i,
index: -1,
dontValidate: true,
};
}
if (returned === true) {
results_returned[fullId] = {
id: i,
index: -1,
dontValidate: true,
};
}
} else {
2017-11-17 21:59:06 +01:00
results[fullId] = {
id: i,
index: -1,
dontValidate: true,
};
}
}
}
query.inputs = [val];
query.output = val;
query.search = val;
// searching by type
} else if (val.search("->") > -1) {
2018-04-13 22:54:09 +02:00
var trimmer = function(s) { return s.trim(); };
var parts = val.split("->").map(trimmer);
var input = parts[0];
// sort inputs so that order does not matter
var inputs = input.split(",").map(trimmer).sort();
2017-11-11 23:24:04 +01:00
for (var i = 0; i < inputs.length; ++i) {
2017-11-01 17:38:37 +01:00
inputs[i] = extractGenerics(inputs[i]);
}
var output = extractGenerics(parts[1]);
for (var i = 0; i < nSearchWords; ++i) {
var type = searchIndex[i].type;
var ty = searchIndex[i];
if (!type) {
continue;
}
2017-12-01 20:55:25 +01:00
var fullId = generateId(ty);
// allow searching for void (no output) functions as well
var typeOutput = type.output ? type.output.name : "";
2017-11-01 17:38:37 +01:00
var returned = checkReturned(ty, output, true);
if (output.name === "*" || returned === true) {
2017-11-12 13:19:15 +01:00
var in_args = false;
2017-11-01 17:38:37 +01:00
var module = false;
if (input === "*") {
2017-11-01 17:38:37 +01:00
module = true;
} else {
var allFound = true;
for (var it = 0; allFound === true && it < inputs.length; it++) {
2017-11-01 17:38:37 +01:00
allFound = checkType(type, inputs[it], true);
}
2017-11-12 13:19:15 +01:00
in_args = allFound;
2017-11-01 17:38:37 +01:00
}
2017-11-22 00:17:30 +01:00
if (in_args === true) {
results_in_args[fullId] = {
id: i,
index: -1,
dontValidate: true,
};
}
if (returned === true) {
results_returned[fullId] = {
id: i,
index: -1,
dontValidate: true,
};
}
if (module === true) {
results[fullId] = {
id: i,
index: -1,
dontValidate: true,
};
}
}
}
2017-11-01 17:38:37 +01:00
query.inputs = inputs.map(function(input) {
return input.name;
});
query.output = output.name;
} else {
query.inputs = [val];
query.output = val;
query.search = val;
// gather matching search results up to a certain maximum
val = val.replace(/\_/g, "");
2017-11-18 17:30:19 +01:00
var valGenerics = extractGenerics(val);
var paths = valLower.split("::");
var j;
for (j = 0; j < paths.length; ++j) {
if (paths[j] === "") {
paths.splice(j, 1);
j -= 1;
}
}
val = paths[paths.length - 1];
var startsWith = paths.slice(0, paths.length > 1 ? paths.length - 1 : 1);
for (j = 0; j < nSearchWords; ++j) {
var lev_distance;
var ty = searchIndex[j];
if (!ty) {
continue;
}
var lev_add = 0;
if (paths.length > 1) {
2017-11-19 13:41:56 +01:00
var lev = checkPath(startsWith, paths[paths.length - 1], ty);
2017-11-18 17:30:19 +01:00
if (lev > MAX_LEV_DISTANCE) {
continue;
2017-11-18 17:30:19 +01:00
} else if (lev > 0) {
lev_add = 1;
}
2017-11-18 17:30:19 +01:00
}
2017-11-22 00:17:30 +01:00
var returned = MAX_LEV_DISTANCE + 1;
var in_args = MAX_LEV_DISTANCE + 1;
2017-11-18 17:30:19 +01:00
var index = -1;
// we want lev results to go lower than others
var lev = MAX_LEV_DISTANCE + 1;
2017-12-01 20:55:25 +01:00
var fullId = generateId(ty);
2017-11-18 17:30:19 +01:00
2017-11-22 00:17:30 +01:00
if (searchWords[j].indexOf(split[i]) > -1 ||
searchWords[j].indexOf(val) > -1 ||
2017-11-18 17:30:19 +01:00
searchWords[j].replace(/_/g, "").indexOf(val) > -1)
{
// filter type: ... queries
2017-12-12 00:19:36 +01:00
if (typePassesFilter(typeFilter, ty.ty) && results[fullId] === undefined) {
2017-11-18 17:30:19 +01:00
index = searchWords[j].replace(/_/g, "").indexOf(val);
2017-11-01 17:38:37 +01:00
}
2017-11-18 17:30:19 +01:00
}
2017-11-22 00:17:30 +01:00
if ((lev = levenshtein(searchWords[j], val)) <= MAX_LEV_DISTANCE) {
2017-12-12 00:19:36 +01:00
if (typePassesFilter(typeFilter, ty.ty) === false) {
2017-11-22 00:17:30 +01:00
lev = MAX_LEV_DISTANCE + 1;
} else {
lev += 1;
2017-11-01 17:38:37 +01:00
}
2017-11-18 17:30:19 +01:00
}
2017-11-22 00:17:30 +01:00
if ((in_args = findArg(ty, valGenerics)) <= MAX_LEV_DISTANCE) {
2017-12-12 00:19:36 +01:00
if (typePassesFilter(typeFilter, ty.ty) === false) {
2017-11-22 00:17:30 +01:00
in_args = MAX_LEV_DISTANCE + 1;
2017-11-01 17:38:37 +01:00
}
2017-11-18 17:30:19 +01:00
}
2017-11-22 00:17:30 +01:00
if ((returned = checkReturned(ty, valGenerics)) <= MAX_LEV_DISTANCE) {
2017-12-12 00:19:36 +01:00
if (typePassesFilter(typeFilter, ty.ty) === false) {
2017-11-22 00:17:30 +01:00
returned = MAX_LEV_DISTANCE + 1;
2017-11-01 17:38:37 +01:00
}
2017-11-18 17:30:19 +01:00
}
2017-11-22 00:17:30 +01:00
2017-11-18 17:30:19 +01:00
lev += lev_add;
2017-12-15 15:42:38 +01:00
if (lev > 0 && val.length > 3 && searchWords[j].startsWith(val)) {
if (val.length < 6) {
lev -= 1;
} else {
lev = 0;
}
2017-12-13 00:20:45 +01:00
}
2017-11-22 00:17:30 +01:00
if (in_args <= MAX_LEV_DISTANCE) {
if (results_in_args[fullId] === undefined) {
results_in_args[fullId] = {
id: j,
index: index,
lev: in_args,
};
}
results_in_args[fullId].lev =
Math.min(results_in_args[fullId].lev, in_args);
}
if (returned <= MAX_LEV_DISTANCE) {
if (results_returned[fullId] === undefined) {
results_returned[fullId] = {
id: j,
index: index,
lev: returned,
};
}
results_returned[fullId].lev =
Math.min(results_returned[fullId].lev, returned);
}
if (index !== -1 || lev <= MAX_LEV_DISTANCE) {
2018-05-03 22:20:57 +02:00
if (index !== -1 && paths.length < 2) {
2017-11-22 00:17:30 +01:00
lev = 0;
}
2017-11-18 17:30:19 +01:00
if (results[fullId] === undefined) {
results[fullId] = {
id: j,
index: index,
lev: lev,
};
}
2017-11-22 00:17:30 +01:00
results[fullId].lev = Math.min(results[fullId].lev, lev);
}
}
}
2018-04-19 18:23:12 +02:00
var ret = {
2017-11-22 00:17:30 +01:00
'in_args': sortResults(results_in_args, true),
'returned': sortResults(results_returned, true),
'others': sortResults(results),
};
if (ALIASES && ALIASES[window.currentCrate] &&
ALIASES[window.currentCrate][query.raw]) {
2018-04-19 18:23:12 +02:00
var aliases = ALIASES[window.currentCrate][query.raw];
for (var i = 0; i < aliases.length; ++i) {
ret['others'].unshift(aliases[i]);
if (ret['others'].length > MAX_RESULTS) {
ret['others'].pop();
}
}
}
return ret;
}
/**
* Validate performs the following boolean logic. For example:
* "File::open" will give IF A PARENT EXISTS => ("file" && "open")
* exists in (name || path || parent) OR => ("file" && "open") exists in
* (name || path )
*
* This could be written functionally, but I wanted to minimise
* functions on stack.
*
* @param {[string]} name [The name of the result]
* @param {[string]} path [The path of the result]
* @param {[string]} keys [The keys to be used (["file", "open"])]
* @param {[object]} parent [The parent of the result]
* @return {[boolean]} [Whether the result is valid or not]
*/
function validateResult(name, path, keys, parent) {
for (var i = 0; i < keys.length; ++i) {
// each check is for validation so we negate the conditions and invalidate
if (!(
// check for an exact name match
2017-11-01 17:38:37 +01:00
name.indexOf(keys[i]) > -1 ||
// then an exact path match
2017-11-01 17:38:37 +01:00
path.indexOf(keys[i]) > -1 ||
// next if there is a parent, check for exact parent match
(parent !== undefined &&
parent.name.toLowerCase().indexOf(keys[i]) > -1) ||
// lastly check to see if the name was a levenshtein match
2017-11-01 17:38:37 +01:00
levenshtein(name, keys[i]) <= MAX_LEV_DISTANCE)) {
return false;
}
}
return true;
}
2018-01-07 16:19:44 +01:00
function getQuery(raw) {
var matches, type, query;
query = raw;
2015-08-04 17:40:23 +02:00
matches = query.match(/^(fn|mod|struct|enum|trait|type|const|macro)\s*:\s*/i);
if (matches) {
2015-08-04 17:40:23 +02:00
type = matches[1].replace(/^const$/, 'constant');
query = query.substring(matches[0].length);
}
return {
raw: raw,
query: query,
type: type,
id: query + type
};
}
function initSearchNav() {
2017-04-14 16:37:09 +02:00
var hoverTimeout;
2017-04-14 16:37:09 +02:00
var click_func = function(e) {
var el = e.target;
// to retrieve the real "owner" of the event.
while (el.tagName !== 'TR') {
el = el.parentNode;
}
var dst = e.target.getElementsByTagName('a');
if (dst.length < 1) {
return;
}
dst = dst[0];
if (window.location.pathname === dst.pathname) {
2017-04-14 16:37:09 +02:00
addClass(document.getElementById('search'), 'hidden');
removeClass(document.getElementById('main'), 'hidden');
document.location.href = dst.href;
}
2017-04-14 16:37:09 +02:00
};
var mouseover_func = function(e) {
var el = e.target;
// to retrieve the real "owner" of the event.
while (el.tagName !== 'TR') {
el = el.parentNode;
}
clearTimeout(hoverTimeout);
hoverTimeout = setTimeout(function() {
2017-04-14 16:37:09 +02:00
onEach(document.getElementsByClassName('search-results'), function(e) {
onEach(e.getElementsByClassName('result'), function(i_e) {
removeClass(i_e, 'highlighted');
});
});
addClass(el, 'highlighted');
}, 20);
2017-04-14 16:37:09 +02:00
};
onEach(document.getElementsByClassName('search-results'), function(e) {
onEach(e.getElementsByClassName('result'), function(i_e) {
i_e.onclick = click_func;
i_e.onmouseover = mouseover_func;
});
});
2017-04-14 16:37:09 +02:00
search_input.onkeydown = function(e) {
// "actives" references the currently highlighted item in each search tab.
// Each array in "actives" represents a tab.
var actives = [[], [], []];
// "current" is used to know which tab we're looking into.
var current = 0;
2017-04-14 16:37:09 +02:00
onEach(document.getElementsByClassName('search-results'), function(e) {
onEach(e.getElementsByClassName('highlighted'), function(e) {
actives[current].push(e);
2017-04-14 16:37:09 +02:00
});
current += 1;
2017-04-14 16:37:09 +02:00
});
if (e.which === 38) { // up
if (!actives[currentTab].length ||
!actives[currentTab][0].previousElementSibling) {
return;
}
addClass(actives[currentTab][0].previousElementSibling, 'highlighted');
removeClass(actives[currentTab][0], 'highlighted');
} else if (e.which === 40) { // down
if (!actives[currentTab].length) {
2017-04-14 16:37:09 +02:00
var results = document.getElementsByClassName('search-results');
if (results.length > 0) {
var res = results[currentTab].getElementsByClassName('result');
2017-04-14 16:37:09 +02:00
if (res.length > 0) {
addClass(res[0], 'highlighted');
}
}
} else if (actives[currentTab][0].nextElementSibling) {
addClass(actives[currentTab][0].nextElementSibling, 'highlighted');
removeClass(actives[currentTab][0], 'highlighted');
}
} else if (e.which === 13) { // return
if (actives[currentTab].length) {
document.location.href =
actives[currentTab][0].getElementsByTagName('a')[0].href;
}
} else if (e.which === 9) { // tab
if (e.shiftKey) {
printTab(currentTab > 0 ? currentTab - 1 : 2);
} else {
printTab(currentTab > 1 ? 0 : currentTab + 1);
}
e.preventDefault();
} else if (e.which === 16) { // shift
// Does nothing, it's just to avoid losing "focus" on the highlighted element.
2018-01-05 01:14:10 +01:00
} else if (e.which === 27) { // escape
removeClass(actives[currentTab][0], 'highlighted');
2018-04-24 20:23:57 +02:00
search_input.value = '';
2018-01-05 01:14:10 +01:00
defocusSearchBar();
} else if (actives[currentTab].length > 0) {
removeClass(actives[currentTab][0], 'highlighted');
}
2017-04-14 16:37:09 +02:00
};
}
function escape(content) {
var h1 = document.createElement('h1');
2017-04-14 16:37:09 +02:00
h1.textContent = content;
return h1.innerHTML;
}
2018-03-27 10:33:31 +02:00
function pathSplitter(path) {
return '<span>' + path.replace(/::/g, '::</span><span>');
}
function buildHrefAndPath(item) {
var displayPath;
var href;
var type = itemTypes[item.ty];
var name = item.name;
if (type === 'mod') {
displayPath = item.path + '::';
href = rootPath + item.path.replace(/::/g, '/') + '/' +
name + '/index.html';
} else if (type === "primitive") {
displayPath = "";
href = rootPath + item.path.replace(/::/g, '/') +
'/' + type + '.' + name + '.html';
} else if (type === "externcrate") {
displayPath = "";
href = rootPath + name + '/index.html';
} else if (item.parent !== undefined) {
var myparent = item.parent;
var anchor = '#' + type + '.' + name;
var parentType = itemTypes[myparent.ty];
if (parentType === "primitive") {
displayPath = myparent.name + '::';
} else {
displayPath = item.path + '::' + myparent.name + '::';
}
href = rootPath + item.path.replace(/::/g, '/') +
'/' + parentType +
'.' + myparent.name +
'.html' + anchor;
} else {
displayPath = item.path + '::';
href = rootPath + item.path.replace(/::/g, '/') +
'/' + type + '.' + name + '.html';
}
return [displayPath, href];
}
function addTab(array, query, display) {
var extraStyle = '';
if (display === false) {
extraStyle = ' style="display: none;"';
}
var output = '';
if (array.length > 0) {
2017-10-14 16:31:48 +02:00
output = '<table class="search-results"' + extraStyle + '>';
var shown = [];
array.forEach(function(item) {
var name, type, href, displayPath;
2018-04-19 19:59:45 +02:00
var id_ty = item.ty + item.path + item.name;
if (shown.indexOf(id_ty) !== -1) {
return;
}
2018-04-19 19:59:45 +02:00
shown.push(id_ty);
name = item.name;
type = itemTypes[item.ty];
var res = buildHrefAndPath(item);
var href = res[1];
var displayPath = res[0];
output += '<tr class="' + type + ' result"><td>' +
'<a href="' + href + '">' +
2018-03-27 10:33:31 +02:00
pathSplitter(displayPath) + '<span class="' + type + '">' +
name + '</span></a></td><td>' +
'<a href="' + href + '">' +
'<span class="desc">' + escape(item.desc) +
'&nbsp;</span></a></td></tr>';
});
output += '</table>';
} else {
2017-10-14 16:31:48 +02:00
output = '<div class="search-failed"' + extraStyle + '>No results :(<br/>' +
'Try on <a href="https://duckduckgo.com/?q=' +
encodeURIComponent('rust ' + query.query) +
'">DuckDuckGo</a>?</div>';
}
return output;
}
2017-11-02 01:01:51 +01:00
function makeTabHeader(tabNb, text, nbElems) {
2017-10-14 16:31:48 +02:00
if (currentTab === tabNb) {
2017-11-02 01:01:51 +01:00
return '<div class="selected">' + text +
' <div class="count">(' + nbElems + ')</div></div>';
2017-10-14 16:31:48 +02:00
}
2017-11-02 01:01:51 +01:00
return '<div>' + text + ' <div class="count">(' + nbElems + ')</div></div>';
2017-10-14 16:31:48 +02:00
}
function showResults(results) {
if (results['others'].length === 1 &&
getCurrentValue('rustdoc-go-to-only-result') === "true") {
var elem = document.createElement('a');
var res = buildHrefAndPath(results['others'][0]);
elem.href = res[1];
elem.style.display = 'none';
// For firefox, we need the element to be in the DOM so it can be clicked.
document.body.appendChild(elem);
elem.click();
}
2018-04-24 20:23:57 +02:00
var output, query = getQuery(search_input.value);
currentResults = query.id;
output = '<h1>Results for ' + escape(query.query) +
(query.type ? ' (type: ' + escape(query.type) + ')' : '') + '</h1>' +
'<div id="titles">' +
makeTabHeader(0, "In Names", results['others'].length) +
makeTabHeader(1, "In Parameters", results['in_args'].length) +
makeTabHeader(2, "In Return Types", results['returned'].length) +
2017-10-14 16:31:48 +02:00
'</div><div id="results">';
output += addTab(results['others'], query);
output += addTab(results['in_args'], query, false);
output += addTab(results['returned'], query, false);
output += '</div>';
2017-04-14 16:37:09 +02:00
addClass(document.getElementById('main'), 'hidden');
var search = document.getElementById('search');
removeClass(search, 'hidden');
search.innerHTML = output;
var tds = search.getElementsByTagName('td');
var td_width = 0;
if (tds.length > 0) {
td_width = tds[0].offsetWidth;
}
var width = search.offsetWidth - 40 - td_width;
onEach(search.getElementsByClassName('desc'), function(e) {
e.style.width = width + 'px';
});
initSearchNav();
var elems = document.getElementById('titles').childNodes;
elems[0].onclick = function() { printTab(0); };
elems[1].onclick = function() { printTab(1); };
elems[2].onclick = function() { printTab(2); };
printTab(currentTab);
}
2018-04-14 17:01:28 +02:00
function execSearch(query, searchWords) {
var queries = query.raw.split(",");
var results = {
'in_args': [],
'returned': [],
'others': [],
};
for (var i = 0; i < queries.length; ++i) {
var query = queries[i].trim();
if (query.length !== 0) {
var tmp = execQuery(getQuery(query), searchWords);
results['in_args'].push(tmp['in_args']);
results['returned'].push(tmp['returned']);
results['others'].push(tmp['others']);
}
}
if (queries.length > 1) {
function getSmallest(arrays, positions) {
var start = null;
for (var it = 0; it < positions.length; ++it) {
if (arrays[it].length > positions[it] &&
(start === null || start > arrays[it][positions[it]].lev)) {
start = arrays[it][positions[it]].lev;
}
}
return start;
}
function mergeArrays(arrays) {
var ret = [];
var positions = [];
for (var x = 0; x < arrays.length; ++x) {
positions.push(0);
}
while (ret.length < MAX_RESULTS) {
var smallest = getSmallest(arrays, positions);
if (smallest === null) {
break;
}
for (x = 0; x < arrays.length && ret.length < MAX_RESULTS; ++x) {
if (arrays[x].length > positions[x] &&
arrays[x][positions[x]].lev === smallest) {
ret.push(arrays[x][positions[x]]);
positions[x] += 1;
}
}
}
return ret;
}
return {
'in_args': mergeArrays(results['in_args']),
'returned': mergeArrays(results['returned']),
'others': mergeArrays(results['others']),
};
} else {
return {
'in_args': results['in_args'][0],
'returned': results['returned'][0],
'others': results['others'][0],
};
}
}
function search(e) {
var params = getQueryStringParams();
2018-04-20 16:42:44 +02:00
var query = getQuery(search_input.value.trim());
if (e) {
e.preventDefault();
}
2018-04-24 20:23:57 +02:00
if (query.query.length === 0 || query.id === currentResults) {
2018-04-20 16:42:44 +02:00
if (query.query.length > 0) {
putBackSearch(search_input);
}
return;
}
// Update document title to maintain a meaningful browser history
2017-04-14 16:37:09 +02:00
document.title = "Results for " + query.query + " - Rust";
// Because searching is incremental by character, only the most
// recent search query is added to the browser history.
if (browserSupportsHistoryApi()) {
if (!history.state && !params.search) {
2017-04-14 16:37:09 +02:00
history.pushState(query, "", "?search=" + encodeURIComponent(query.raw));
} else {
2017-04-14 16:37:09 +02:00
history.replaceState(query, "", "?search=" + encodeURIComponent(query.raw));
}
}
2018-04-14 17:01:28 +02:00
showResults(execSearch(query, index));
}
function buildIndex(rawSearchIndex) {
searchIndex = [];
var searchWords = [];
for (var crate in rawSearchIndex) {
if (!rawSearchIndex.hasOwnProperty(crate)) { continue; }
2016-02-16 20:00:57 +01:00
searchWords.push(crate);
searchIndex.push({
crate: crate,
2016-02-16 20:00:57 +01:00
ty: 1, // == ExternCrate
name: crate,
path: "",
desc: rawSearchIndex[crate].doc,
type: null,
});
// an array of [(Number) item type,
// (String) name,
// (String) full path or empty string for previous path,
// (String) description,
// (Number | null) the parent path index to `paths`]
// (Object | null) the type of the function (if any)
var items = rawSearchIndex[crate].items;
// an array of [(Number) item type,
// (String) name]
var paths = rawSearchIndex[crate].paths;
// convert `paths` into an object form
var len = paths.length;
for (var i = 0; i < len; ++i) {
paths[i] = {ty: paths[i][0], name: paths[i][1]};
}
// convert `items` into an object form, and construct word indices.
//
// before any analysis is performed lets gather the search terms to
// search against apart from the rest of the data. This is a quick
// operation that is cached for the life of the page state so that
// all other search operations have access to this cached data for
// faster analysis operations
var len = items.length;
var lastPath = "";
for (var i = 0; i < len; ++i) {
var rawRow = items[i];
var row = {crate: crate, ty: rawRow[0], name: rawRow[1],
path: rawRow[2] || lastPath, desc: rawRow[3],
parent: paths[rawRow[4]], type: rawRow[5]};
searchIndex.push(row);
if (typeof row.name === "string") {
var word = row.name.toLowerCase();
searchWords.push(word);
} else {
searchWords.push("");
}
lastPath = row.path;
}
}
return searchWords;
}
function startSearch() {
var searchTimeout;
2017-04-14 16:37:09 +02:00
var callback = function() {
clearTimeout(searchTimeout);
2017-04-14 16:37:09 +02:00
if (search_input.value.length === 0) {
if (browserSupportsHistoryApi()) {
history.replaceState("", "std - Rust", "?search=");
}
2017-04-14 16:37:09 +02:00
var main = document.getElementById('main');
if (hasClass(main, 'content')) {
removeClass(main, 'hidden');
}
var search_c = document.getElementById('search');
if (hasClass(search_c, 'content')) {
addClass(search_c, 'hidden');
}
} else {
searchTimeout = setTimeout(search, 500);
}
2017-04-14 16:37:09 +02:00
};
search_input.onkeyup = callback;
search_input.oninput = callback;
2017-11-01 17:38:37 +01:00
document.getElementsByClassName("search-form")[0].onsubmit = function(e) {
e.preventDefault();
clearTimeout(searchTimeout);
search();
2017-04-14 16:37:09 +02:00
};
search_input.onchange = function(e) {
// Do NOT e.preventDefault() here. It will prevent pasting.
clearTimeout(searchTimeout);
// zero-timeout necessary here because at the time of event handler execution the
// pasted content is not in the input field yet. Shouldnt make any difference for
// change, though.
setTimeout(search, 0);
2017-04-14 16:37:09 +02:00
};
search_input.onpaste = search_input.onchange;
// Push and pop states are used to add search results to the browser
// history.
if (browserSupportsHistoryApi()) {
// Store the previous <title> so we can revert back to it later.
2017-04-14 16:37:09 +02:00
var previousTitle = document.title;
2017-04-14 16:37:09 +02:00
window.onpopstate = function(e) {
var params = getQueryStringParams();
// When browsing back from search results the main page
// visibility must be reset.
if (!params.search) {
2017-04-14 16:37:09 +02:00
var main = document.getElementById('main');
if (hasClass(main, 'content')) {
removeClass(main, 'hidden');
}
var search_c = document.getElementById('search');
if (hasClass(search_c, 'content')) {
addClass(search_c, 'hidden');
2017-04-14 16:37:09 +02:00
}
}
// Revert to the previous title manually since the History
// API ignores the title parameter.
2017-04-14 16:37:09 +02:00
document.title = previousTitle;
// When browsing forward to search results the previous
// search will be repeated, so the currentResults are
// cleared to ensure the search is successful.
currentResults = null;
// Synchronize search bar with query string state and
// perform the search. This will empty the bar if there's
// nothing there, which lets you really go back to a
// previous state with nothing in the bar.
if (params.search) {
2018-04-24 20:23:57 +02:00
search_input.value = params.search;
} else {
2018-04-24 20:23:57 +02:00
search_input.value = '';
}
// Some browsers fire 'onpopstate' for every page load
// (Chrome), while others fire the event only when actually
// popping a state (Firefox), which is why search() is
// called both here and at the end of the startSearch()
// function.
search();
2017-04-14 16:37:09 +02:00
};
}
search();
}
index = buildIndex(rawSearchIndex);
startSearch();
// Draw a convenient sidebar of known crates if we have a listing
2018-04-13 22:54:09 +02:00
if (rootPath === '../' || rootPath === "./") {
var sidebar = document.getElementsByClassName('sidebar-elems')[0];
if (sidebar) {
var div = document.createElement('div');
div.className = 'block crate';
div.innerHTML = '<h3>Crates</h3>';
var ul = document.createElement('ul');
div.appendChild(ul);
var crates = [];
for (var crate in rawSearchIndex) {
if (!rawSearchIndex.hasOwnProperty(crate)) {
continue;
}
crates.push(crate);
2017-11-01 17:38:37 +01:00
}
crates.sort();
for (var i = 0; i < crates.length; ++i) {
var klass = 'crate';
2018-04-13 22:54:09 +02:00
if (rootPath !== "./" && crates[i] === window.currentCrate) {
klass += ' current';
}
var link = document.createElement('a');
2018-04-13 22:54:09 +02:00
link.href = rootPath + crates[i] + '/index.html';
link.title = rawSearchIndex[crates[i]].doc;
link.className = klass;
link.textContent = crates[i];
var li = document.createElement('li');
li.appendChild(link);
ul.appendChild(li);
}
sidebar.appendChild(div);
}
}
}
window.initSearch = initSearch;
// delayed sidebar rendering.
function initSidebarItems(items) {
var sidebar = document.getElementsByClassName('sidebar-elems')[0];
var current = window.sidebarCurrent;
function block(shortty, longty) {
var filtered = items[shortty];
if (!filtered) { return; }
2017-04-14 16:37:09 +02:00
var div = document.createElement('div');
div.className = 'block ' + shortty;
var h3 = document.createElement('h3');
h3.textContent = longty;
div.appendChild(h3);
var ul = document.createElement('ul');
for (var i = 0; i < filtered.length; ++i) {
var item = filtered[i];
var name = item[0];
var desc = item[1]; // can be null
var klass = shortty;
if (name === current.name && shortty === current.ty) {
klass += ' current';
}
var path;
if (shortty === 'mod') {
path = name + '/index.html';
} else {
path = shortty + '.' + name + '.html';
}
2017-04-14 16:37:09 +02:00
var link = document.createElement('a');
link.href = current.relpath + path;
link.title = desc;
link.className = klass;
link.textContent = name;
var li = document.createElement('li');
li.appendChild(link);
ul.appendChild(li);
}
2017-04-14 16:37:09 +02:00
div.appendChild(ul);
if (sidebar) {
sidebar.appendChild(div);
}
}
block("primitive", "Primitive Types");
block("mod", "Modules");
block("macro", "Macros");
block("struct", "Structs");
block("enum", "Enums");
block("union", "Unions");
block("constant", "Constants");
block("static", "Statics");
block("trait", "Traits");
block("fn", "Functions");
block("type", "Type Definitions");
block("foreigntype", "Foreign Types");
}
window.initSidebarItems = initSidebarItems;
window.register_implementors = function(imp) {
Generate documentation for auto-trait impls A new section is added to both both struct and trait doc pages. On struct/enum pages, a new 'Auto Trait Implementations' section displays any synthetic implementations for auto traits. Currently, this is only done for Send and Sync. On trait pages, a new 'Auto Implementors' section displays all types which automatically implement the trait. Effectively, this is a list of all public types in the standard library. Synthesized impls for a particular auto trait ('synthetic impls') take into account generic bounds. For example, a type 'struct Foo<T>(T)' will have 'impl<T> Send for Foo<T> where T: Send' generated for it. Manual implementations of auto traits are also taken into account. If we have the following types: 'struct Foo<T>(T)' 'struct Wrapper<T>(Foo<T>)' 'unsafe impl<T> Send for Wrapper<T>' // pretend that Wrapper<T> makes this sound somehow Then Wrapper will have the following impl generated: 'impl<T> Send for Wrapper<T>' reflecting the fact that 'T: Send' need not hold for 'Wrapper<T>: Send' to hold Lifetimes, HRTBS, and projections (e.g. '<T as Iterator>::Item') are taken into account by synthetic impls However, if a type can *never* implement a particular auto trait (e.g. 'struct MyStruct<T>(*const T)'), then a negative impl will be generated (in this case, 'impl<T> !Send for MyStruct<T>') All of this means that a user should be able to copy-paste a synthetic impl into their code, without any observable changes in behavior (assuming the rest of the program remains unchanged).
2017-11-22 22:16:55 +01:00
var implementors = document.getElementById('implementors-list');
var synthetic_implementors = document.getElementById('synthetic-implementors-list');
2018-02-16 00:45:52 +01:00
var libs = Object.getOwnPropertyNames(imp);
for (var i = 0; i < libs.length; ++i) {
if (libs[i] === currentCrate) { continue; }
var structs = imp[libs[i]];
2018-02-10 20:34:46 +01:00
struct_loop:
for (var j = 0; j < structs.length; ++j) {
Generate documentation for auto-trait impls A new section is added to both both struct and trait doc pages. On struct/enum pages, a new 'Auto Trait Implementations' section displays any synthetic implementations for auto traits. Currently, this is only done for Send and Sync. On trait pages, a new 'Auto Implementors' section displays all types which automatically implement the trait. Effectively, this is a list of all public types in the standard library. Synthesized impls for a particular auto trait ('synthetic impls') take into account generic bounds. For example, a type 'struct Foo<T>(T)' will have 'impl<T> Send for Foo<T> where T: Send' generated for it. Manual implementations of auto traits are also taken into account. If we have the following types: 'struct Foo<T>(T)' 'struct Wrapper<T>(Foo<T>)' 'unsafe impl<T> Send for Wrapper<T>' // pretend that Wrapper<T> makes this sound somehow Then Wrapper will have the following impl generated: 'impl<T> Send for Wrapper<T>' reflecting the fact that 'T: Send' need not hold for 'Wrapper<T>: Send' to hold Lifetimes, HRTBS, and projections (e.g. '<T as Iterator>::Item') are taken into account by synthetic impls However, if a type can *never* implement a particular auto trait (e.g. 'struct MyStruct<T>(*const T)'), then a negative impl will be generated (in this case, 'impl<T> !Send for MyStruct<T>') All of this means that a user should be able to copy-paste a synthetic impl into their code, without any observable changes in behavior (assuming the rest of the program remains unchanged).
2017-11-22 22:16:55 +01:00
var struct = structs[j];
2018-02-16 00:45:52 +01:00
Generate documentation for auto-trait impls A new section is added to both both struct and trait doc pages. On struct/enum pages, a new 'Auto Trait Implementations' section displays any synthetic implementations for auto traits. Currently, this is only done for Send and Sync. On trait pages, a new 'Auto Implementors' section displays all types which automatically implement the trait. Effectively, this is a list of all public types in the standard library. Synthesized impls for a particular auto trait ('synthetic impls') take into account generic bounds. For example, a type 'struct Foo<T>(T)' will have 'impl<T> Send for Foo<T> where T: Send' generated for it. Manual implementations of auto traits are also taken into account. If we have the following types: 'struct Foo<T>(T)' 'struct Wrapper<T>(Foo<T>)' 'unsafe impl<T> Send for Wrapper<T>' // pretend that Wrapper<T> makes this sound somehow Then Wrapper will have the following impl generated: 'impl<T> Send for Wrapper<T>' reflecting the fact that 'T: Send' need not hold for 'Wrapper<T>: Send' to hold Lifetimes, HRTBS, and projections (e.g. '<T as Iterator>::Item') are taken into account by synthetic impls However, if a type can *never* implement a particular auto trait (e.g. 'struct MyStruct<T>(*const T)'), then a negative impl will be generated (in this case, 'impl<T> !Send for MyStruct<T>') All of this means that a user should be able to copy-paste a synthetic impl into their code, without any observable changes in behavior (assuming the rest of the program remains unchanged).
2017-11-22 22:16:55 +01:00
var list = struct.synthetic ? synthetic_implementors : implementors;
2018-02-16 00:45:52 +01:00
if (struct.synthetic) {
for (var k = 0; k < struct.types.length; k++) {
if (window.inlined_types.has(struct.types[k])) {
continue struct_loop;
}
window.inlined_types.add(struct.types[k]);
Generate documentation for auto-trait impls A new section is added to both both struct and trait doc pages. On struct/enum pages, a new 'Auto Trait Implementations' section displays any synthetic implementations for auto traits. Currently, this is only done for Send and Sync. On trait pages, a new 'Auto Implementors' section displays all types which automatically implement the trait. Effectively, this is a list of all public types in the standard library. Synthesized impls for a particular auto trait ('synthetic impls') take into account generic bounds. For example, a type 'struct Foo<T>(T)' will have 'impl<T> Send for Foo<T> where T: Send' generated for it. Manual implementations of auto traits are also taken into account. If we have the following types: 'struct Foo<T>(T)' 'struct Wrapper<T>(Foo<T>)' 'unsafe impl<T> Send for Wrapper<T>' // pretend that Wrapper<T> makes this sound somehow Then Wrapper will have the following impl generated: 'impl<T> Send for Wrapper<T>' reflecting the fact that 'T: Send' need not hold for 'Wrapper<T>: Send' to hold Lifetimes, HRTBS, and projections (e.g. '<T as Iterator>::Item') are taken into account by synthetic impls However, if a type can *never* implement a particular auto trait (e.g. 'struct MyStruct<T>(*const T)'), then a negative impl will be generated (in this case, 'impl<T> !Send for MyStruct<T>') All of this means that a user should be able to copy-paste a synthetic impl into their code, without any observable changes in behavior (assuming the rest of the program remains unchanged).
2017-11-22 22:16:55 +01:00
}
}
2017-04-14 16:37:09 +02:00
var code = document.createElement('code');
Generate documentation for auto-trait impls A new section is added to both both struct and trait doc pages. On struct/enum pages, a new 'Auto Trait Implementations' section displays any synthetic implementations for auto traits. Currently, this is only done for Send and Sync. On trait pages, a new 'Auto Implementors' section displays all types which automatically implement the trait. Effectively, this is a list of all public types in the standard library. Synthesized impls for a particular auto trait ('synthetic impls') take into account generic bounds. For example, a type 'struct Foo<T>(T)' will have 'impl<T> Send for Foo<T> where T: Send' generated for it. Manual implementations of auto traits are also taken into account. If we have the following types: 'struct Foo<T>(T)' 'struct Wrapper<T>(Foo<T>)' 'unsafe impl<T> Send for Wrapper<T>' // pretend that Wrapper<T> makes this sound somehow Then Wrapper will have the following impl generated: 'impl<T> Send for Wrapper<T>' reflecting the fact that 'T: Send' need not hold for 'Wrapper<T>: Send' to hold Lifetimes, HRTBS, and projections (e.g. '<T as Iterator>::Item') are taken into account by synthetic impls However, if a type can *never* implement a particular auto trait (e.g. 'struct MyStruct<T>(*const T)'), then a negative impl will be generated (in this case, 'impl<T> !Send for MyStruct<T>') All of this means that a user should be able to copy-paste a synthetic impl into their code, without any observable changes in behavior (assuming the rest of the program remains unchanged).
2017-11-22 22:16:55 +01:00
code.innerHTML = struct.text;
2017-04-14 16:37:09 +02:00
var x = code.getElementsByTagName('a');
for (var k = 0; k < x.length; k++) {
var href = x[k].getAttribute('href');
if (href && href.indexOf('http') !== 0) {
x[k].setAttribute('href', rootPath + href);
2014-06-01 19:17:30 +02:00
}
2017-04-14 16:37:09 +02:00
}
var li = document.createElement('li');
li.appendChild(code);
list.appendChild(li);
}
}
};
if (window.pending_implementors) {
window.register_implementors(window.pending_implementors);
}
function labelForToggleButton(sectionIsCollapsed) {
if (sectionIsCollapsed) {
// button will expand the section
return "+";
}
// button will collapse the section
// note that this text is also set in the HTML template in render.rs
return "\u2212"; // "\u2212" is '' minus sign
}
2017-04-14 16:37:09 +02:00
function onEveryMatchingChild(elem, className, func) {
if (elem && className && func) {
for (var i = 0; i < elem.childNodes.length; i++) {
if (hasClass(elem.childNodes[i], className)) {
func(elem.childNodes[i]);
} else {
onEveryMatchingChild(elem.childNodes[i], className, func);
}
}
}
}
function toggleAllDocs(pageId) {
2017-04-14 16:37:09 +02:00
var toggle = document.getElementById("toggle-all-docs");
if (!toggle) {
return;
}
2017-04-14 16:37:09 +02:00
if (hasClass(toggle, "will-expand")) {
updateLocalStorage("rustdoc-collapse", "false");
2017-04-14 16:37:09 +02:00
removeClass(toggle, "will-expand");
onEveryMatchingChild(toggle, "inner", function(e) {
e.innerHTML = labelForToggleButton(false);
});
toggle.title = "collapse all docs";
onEach(document.getElementsByClassName("collapse-toggle"), function(e) {
collapseDocs(e, "show");
2017-04-14 16:37:09 +02:00
});
} else {
updateLocalStorage("rustdoc-collapse", "true");
2017-04-14 16:37:09 +02:00
addClass(toggle, "will-expand");
onEveryMatchingChild(toggle, "inner", function(e) {
e.innerHTML = labelForToggleButton(true);
});
toggle.title = "expand all docs";
2017-04-14 16:37:09 +02:00
onEach(document.getElementsByClassName("collapse-toggle"), function(e) {
collapseDocs(e, "hide", pageId);
2017-04-14 16:37:09 +02:00
});
}
}
function collapseDocs(toggle, mode, pageId) {
2017-04-14 16:37:09 +02:00
if (!toggle || !toggle.parentNode) {
return;
}
function adjustToggle(arg) {
return function(e) {
if (hasClass(e, 'toggle-label')) {
if (arg) {
2017-04-14 16:37:09 +02:00
e.style.display = 'inline-block';
} else {
2017-04-14 16:37:09 +02:00
e.style.display = 'none';
}
}
if (hasClass(e, 'inner')) {
e.innerHTML = labelForToggleButton(arg);
}
};
};
if (!hasClass(toggle.parentNode, "impl")) {
var relatedDoc = toggle.parentNode.nextElementSibling;
if (hasClass(relatedDoc, "stability")) {
relatedDoc = relatedDoc.nextElementSibling;
}
if (hasClass(relatedDoc, "docblock")) {
var action = mode;
if (action === "toggle") {
if (hasClass(relatedDoc, "hidden-by-usual-hider")) {
action = "show";
} else {
action = "hide";
2017-04-14 16:37:09 +02:00
}
}
if (action === "hide") {
addClass(relatedDoc, "hidden-by-usual-hider");
onEach(toggle.childNodes, adjustToggle(true));
addClass(toggle.parentNode, 'collapsed');
} else if (action === "show") {
removeClass(relatedDoc, "hidden-by-usual-hider");
removeClass(toggle.parentNode, 'collapsed');
onEach(toggle.childNodes, adjustToggle(false));
}
}
} else {
// we are collapsing the impl block
function implHider(addOrRemove) {
return function(n) {
var is_method = hasClass(n, "method");
if (is_method || hasClass(n, "type")) {
if (is_method === true) {
if (addOrRemove) {
addClass(n, "hidden-by-impl-hider");
} else {
removeClass(n, "hidden-by-impl-hider");
}
}
var ns = n.nextElementSibling;
while (true) {
if (ns && (
hasClass(ns, "docblock") ||
hasClass(ns, "stability"))) {
if (addOrRemove) {
addClass(ns, "hidden-by-impl-hider");
} else {
removeClass(ns, "hidden-by-impl-hider");
}
ns = ns.nextElementSibling;
continue;
}
break;
}
2017-04-14 16:37:09 +02:00
}
}
}
var parentElem = toggle.parentNode;
var relatedDoc = parentElem;
var docblock = relatedDoc.nextElementSibling;
while (!hasClass(relatedDoc, "impl-items")) {
relatedDoc = relatedDoc.nextElementSibling;
}
if ((!relatedDoc && !hasClass(docblock, "docblock")) ||
(pageId && onEach(relatedDoc.childNodes, function(e) {
return e.id === pageId;
}) === true)) {
return;
}
// Hide all functions, but not associated types/consts
var action = mode;
if (action === "toggle") {
if (hasClass(relatedDoc, "fns-now-collapsed") ||
hasClass(docblock, "hidden-by-impl-hider")) {
action = "show";
} else {
action = "hide";
}
}
if (action === "show") {
removeClass(relatedDoc, "fns-now-collapsed");
removeClass(docblock, "hidden-by-usual-hider");
onEach(toggle.childNodes, adjustToggle(false));
onEach(relatedDoc.childNodes, implHider(false));
} else if (action === "hide") {
addClass(relatedDoc, "fns-now-collapsed");
addClass(docblock, "hidden-by-usual-hider");
onEach(toggle.childNodes, adjustToggle(true));
onEach(relatedDoc.childNodes, implHider(true));
}
}
2016-11-06 21:06:20 +01:00
}
function autoCollapseAllImpls(pageId) {
// Automatically minimize all non-inherent impls
onEach(document.getElementsByClassName('impl'), function(n) {
// inherent impl ids are like 'impl' or impl-<number>'
var inherent = (n.id.match(/^impl(?:-\d+)?$/) !== null);
if (!inherent) {
onEach(n.childNodes, function(m) {
if (hasClass(m, "collapse-toggle")) {
collapseDocs(m, "hide", pageId);
2017-04-14 16:37:09 +02:00
}
});
}
});
2016-11-06 21:06:20 +01:00
}
2017-04-14 16:37:09 +02:00
var x = document.getElementById('toggle-all-docs');
if (x) {
x.onclick = toggleAllDocs;
}
2016-11-06 21:06:20 +01:00
2017-04-14 16:37:09 +02:00
function insertAfter(newNode, referenceNode) {
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}
function checkIfThereAreMethods(elems) {
var areThereMethods = false;
onEach(elems, function(e) {
if (hasClass(e, "method")) {
areThereMethods = true;
return true;
}
});
return areThereMethods;
}
2017-04-14 16:37:09 +02:00
var toggle = document.createElement('a');
toggle.href = 'javascript:void(0)';
toggle.className = 'collapse-toggle';
toggle.innerHTML = "[<span class='inner'>" + labelForToggleButton(false) + "</span>]";
2017-04-14 16:37:09 +02:00
var func = function(e) {
var next = e.nextElementSibling;
if (hasClass(e, 'impl') && next && hasClass(next, 'docblock')) {
next = next.nextElementSibling;
}
2017-04-14 16:37:09 +02:00
if (!next) {
return;
}
if ((checkIfThereAreMethods(next.childNodes) || hasClass(e, 'method')) &&
(hasClass(next, 'docblock') ||
hasClass(e, 'impl') ||
(hasClass(next, 'stability') &&
hasClass(next.nextElementSibling, 'docblock')))) {
insertAfter(toggle.cloneNode(true), e.childNodes[e.childNodes.length - 1]);
}
2018-05-11 00:02:05 +02:00
};
2017-04-14 16:37:09 +02:00
onEach(document.getElementsByClassName('method'), func);
onEach(document.getElementsByClassName('impl'), func);
2017-04-14 16:37:09 +02:00
onEach(document.getElementsByClassName('impl-items'), function(e) {
onEach(e.getElementsByClassName('associatedconstant'), func);
});
2018-03-27 11:57:00 +02:00
function createToggle(otherMessage) {
var span = document.createElement('span');
span.className = 'toggle-label';
span.style.display = 'none';
2018-03-27 11:57:00 +02:00
if (!otherMessage) {
span.innerHTML = '&nbsp;Expand&nbsp;description';
} else {
span.innerHTML = otherMessage;
span.style.fontSize = '20px';
}
var mainToggle = toggle.cloneNode(true);
mainToggle.appendChild(span);
var wrapper = document.createElement('div');
wrapper.className = 'toggle-wrapper';
wrapper.appendChild(mainToggle);
return wrapper;
}
2017-04-14 16:37:09 +02:00
onEach(document.getElementsByClassName('docblock'), function(e) {
if (hasClass(e, 'autohide')) {
var wrap = e.previousElementSibling;
if (wrap && hasClass(wrap, 'toggle-wrapper')) {
var toggle = wrap.childNodes[0];
2018-04-24 20:23:57 +02:00
var extra = false;
2017-04-14 16:37:09 +02:00
if (e.childNodes[0].tagName === 'H3') {
2018-04-24 20:23:57 +02:00
extra = true;
}
2017-04-14 16:37:09 +02:00
e.style.display = 'none';
addClass(wrap, 'collapsed');
onEach(toggle.getElementsByClassName('inner'), function(e) {
e.innerHTML = labelForToggleButton(true);
});
onEach(toggle.getElementsByClassName('toggle-label'), function(e) {
e.style.display = 'inline-block';
2018-04-24 20:23:57 +02:00
if (extra === true) {
i_e.innerHTML = " Show " + e.childNodes[0].innerHTML;
}
2017-04-14 16:37:09 +02:00
});
}
2017-04-14 16:37:09 +02:00
}
2018-04-24 20:23:57 +02:00
if (e.parentNode.id === "main") {
var otherMessage;
if (hasClass(e, "type-decl")) {
otherMessage = '&nbsp;Show&nbsp;declaration';
2018-04-24 20:23:57 +02:00
}
e.parentNode.insertBefore(createToggle(otherMessage), e);
if (otherMessage && getCurrentValue('rustdoc-item-declarations') !== "false") {
collapseDocs(e.previousSibling.childNodes[0], "toggle");
}
}
});
2017-04-14 16:37:09 +02:00
autoCollapseAllImpls(getPageId());
function createToggleWrapper() {
var span = document.createElement('span');
span.className = 'toggle-label';
span.style.display = 'none';
span.innerHTML = '&nbsp;Expand&nbsp;attributes';
toggle.appendChild(span);
var wrapper = document.createElement('div');
wrapper.className = 'toggle-wrapper toggle-attributes';
wrapper.appendChild(toggle);
return wrapper;
}
2017-04-14 16:37:09 +02:00
// In the search display, allows to switch between tabs.
function printTab(nb) {
2017-10-14 16:31:48 +02:00
if (nb === 0 || nb === 1 || nb === 2) {
currentTab = nb;
}
var nb_copy = nb;
onEach(document.getElementById('titles').childNodes, function(elem) {
if (nb_copy === 0) {
addClass(elem, 'selected');
} else {
removeClass(elem, 'selected');
}
nb_copy -= 1;
});
onEach(document.getElementById('results').childNodes, function(elem) {
if (nb === 0) {
elem.style.display = '';
} else {
elem.style.display = 'none';
}
nb -= 1;
});
}
2017-04-14 16:37:09 +02:00
onEach(document.getElementById('main').getElementsByTagName('pre'), function(e) {
onEach(e.getElementsByClassName('attributes'), function(i_e) {
i_e.parentNode.insertBefore(createToggleWrapper(), i_e);
2018-04-13 22:54:09 +02:00
if (getCurrentValue("rustdoc-item-attributes") !== "false") {
collapseDocs(i_e.previousSibling.childNodes[0], "toggle");
}
2017-04-14 16:37:09 +02:00
});
});
2017-09-09 15:33:57 +02:00
onEach(document.getElementsByClassName('rust-example-rendered'), function(e) {
if (hasClass(e, 'compile_fail')) {
e.addEventListener("mouseover", function(event) {
e.previousElementSibling.childNodes[0].style.color = '#f00';
});
e.addEventListener("mouseout", function(event) {
e.previousElementSibling.childNodes[0].style.color = '';
});
} else if (hasClass(e, 'ignore')) {
e.addEventListener("mouseover", function(event) {
e.previousElementSibling.childNodes[0].style.color = '#ff9200';
});
e.addEventListener("mouseout", function(event) {
e.previousElementSibling.childNodes[0].style.color = '';
});
}
});
2017-11-10 19:40:46 +01:00
2017-11-15 20:04:02 +01:00
function showModal(content) {
var modal = document.createElement('div');
modal.id = "important";
2017-11-15 20:04:02 +01:00
addClass(modal, 'modal');
modal.innerHTML = '<div class="modal-content"><div class="close" id="modal-close">✕</div>' +
'<div class="whiter"></div><span class="docblock">' + content +
'</span></div>';
2017-11-15 20:04:02 +01:00
document.getElementsByTagName('body')[0].appendChild(modal);
document.getElementById('modal-close').onclick = hideModal;
modal.onclick = hideModal;
}
function hideModal() {
var modal = document.getElementById("important");
if (modal) {
modal.parentNode.removeChild(modal);
}
2017-11-15 20:04:02 +01:00
}
onEach(document.getElementsByClassName('important-traits'), function(e) {
e.onclick = function() {
showModal(e.lastElementChild.innerHTML);
2017-11-15 20:04:02 +01:00
};
});
2018-04-20 16:42:44 +02:00
function putBackSearch(search_input) {
if (search_input.value !== "") {
addClass(document.getElementById("main"), "hidden");
removeClass(document.getElementById("search"), "hidden");
if (browserSupportsHistoryApi()) {
history.replaceState(search_input.value,
"",
"?search=" + encodeURIComponent(search_input.value));
}
}
}
2017-11-10 19:40:46 +01:00
if (search_input) {
search_input.onfocus = function() {
2018-04-20 16:42:44 +02:00
putBackSearch(this);
2017-11-10 19:40:46 +01:00
};
}
var params = getQueryStringParams();
if (params && params.search) {
addClass(document.getElementById("main"), "hidden");
var search = document.getElementById("search");
removeClass(search, "hidden");
search.innerHTML = '<h3 style="text-align: center;">Loading search results...</h3>';
}
var sidebar_menu = document.getElementsByClassName("sidebar-menu")[0];
if (sidebar_menu) {
sidebar_menu.onclick = function() {
var sidebar = document.getElementsByClassName('sidebar')[0];
2017-12-20 00:44:44 +01:00
if (hasClass(sidebar, "mobile") === true) {
hideSidebar();
} else {
showSidebar();
}
};
}
window.onresize = function() {
hideSidebar();
};
if (getCurrentValue("rustdoc-collapse") === "true") {
toggleAllDocs(getPageId());
}
}());
2015-07-14 02:56:31 +02:00
// Sets the focus on the search bar at the top of the page
function focusSearchBar() {
2017-04-14 16:37:09 +02:00
document.getElementsByClassName('search-input')[0].focus();
2015-07-14 02:56:31 +02:00
}
// Removes the focus from the search bar
function defocusSearchBar() {
document.getElementsByClassName('search-input')[0].blur();
}