1
- /* global addClass, getNakedUrl, getSettingValue, hasOwnPropertyRustdoc, initSearch, onEach */
2
- /* global onEachLazy, removeClass, searchState, browserSupportsHistoryApi */
1
+ /* global addClass, getNakedUrl, getSettingValue */
2
+ /* global onEachLazy, removeClass, searchState, browserSupportsHistoryApi, exports */
3
3
4
4
"use strict" ;
5
5
@@ -38,6 +38,11 @@ const itemTypes = [
38
38
// used for special search precedence
39
39
const TY_PRIMITIVE = itemTypes . indexOf ( "primitive" ) ;
40
40
const TY_KEYWORD = itemTypes . indexOf ( "keyword" ) ;
41
+ const ROOT_PATH = typeof window !== "undefined" ? window . rootPath : "../" ;
42
+
43
+ function hasOwnPropertyRustdoc ( obj , property ) {
44
+ return Object . prototype . hasOwnProperty . call ( obj , property ) ;
45
+ }
41
46
42
47
// In the search display, allows to switch between tabs.
43
48
function printTab ( nb ) {
@@ -106,7 +111,7 @@ function levenshtein(s1, s2) {
106
111
return s1_len + s2_len ;
107
112
}
108
113
109
- window . initSearch = rawSearchIndex => {
114
+ function initSearch ( rawSearchIndex ) {
110
115
const MAX_LEV_DISTANCE = 3 ;
111
116
const MAX_RESULTS = 200 ;
112
117
const GENERICS_DATA = 2 ;
@@ -120,15 +125,6 @@ window.initSearch = rawSearchIndex => {
120
125
let searchIndex ;
121
126
let currentResults ;
122
127
const ALIASES = Object . create ( null ) ;
123
- const params = searchState . getQueryStringParams ( ) ;
124
-
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 || "" ;
131
- }
132
128
133
129
function isWhitespace ( c ) {
134
130
return " \t\n\r" . indexOf ( c ) !== - 1 ;
@@ -726,10 +722,11 @@ window.initSearch = rawSearchIndex => {
726
722
* @param {ParsedQuery } parsedQuery - The parsed user query
727
723
* @param {Object } searchWords - The list of search words to query against
728
724
* @param {Object } [filterCrates] - Crate to search in if defined
725
+ * @param {Object } [currentCrate] - Current crate, to rank results from this crate higher
729
726
*
730
727
* @return {ResultsTable }
731
728
*/
732
- function execQuery ( parsedQuery , searchWords , filterCrates ) {
729
+ function execQuery ( parsedQuery , searchWords , filterCrates , currentCrate ) {
733
730
const results_others = { } , results_in_args = { } , results_returned = { } ;
734
731
735
732
function transformResults ( results ) {
@@ -761,7 +758,7 @@ window.initSearch = rawSearchIndex => {
761
758
return out ;
762
759
}
763
760
764
- function sortResults ( results , isType ) {
761
+ function sortResults ( results , isType , preferredCrate ) {
765
762
const userQuery = parsedQuery . userQuery ;
766
763
const ar = [ ] ;
767
764
for ( const entry in results ) {
@@ -796,9 +793,9 @@ window.initSearch = rawSearchIndex => {
796
793
return a - b ;
797
794
}
798
795
799
- // sort by crate (non- current crate goes later )
800
- a = ( aaa . item . crate !== window . currentCrate ) ;
801
- b = ( bbb . item . crate !== window . currentCrate ) ;
796
+ // sort by crate (current crate comes first )
797
+ a = ( aaa . item . crate !== preferredCrate ) ;
798
+ b = ( bbb . item . crate !== preferredCrate ) ;
802
799
if ( a !== b ) {
803
800
return a - b ;
804
801
}
@@ -1178,7 +1175,7 @@ window.initSearch = rawSearchIndex => {
1178
1175
} ;
1179
1176
}
1180
1177
1181
- function handleAliases ( ret , query , filterCrates ) {
1178
+ function handleAliases ( ret , query , filterCrates , currentCrate ) {
1182
1179
const lowerQuery = query . toLowerCase ( ) ;
1183
1180
// We separate aliases and crate aliases because we want to have current crate
1184
1181
// aliases to be before the others in the displayed results.
@@ -1194,7 +1191,7 @@ window.initSearch = rawSearchIndex => {
1194
1191
} else {
1195
1192
Object . keys ( ALIASES ) . forEach ( crate => {
1196
1193
if ( ALIASES [ crate ] [ lowerQuery ] ) {
1197
- const pushTo = crate === window . currentCrate ? crateAliases : aliases ;
1194
+ const pushTo = crate === currentCrate ? crateAliases : aliases ;
1198
1195
const query_aliases = ALIASES [ crate ] [ lowerQuery ] ;
1199
1196
for ( const alias of query_aliases ) {
1200
1197
pushTo . push ( createAliasFromItem ( searchIndex [ alias ] ) ) ;
@@ -1226,8 +1223,9 @@ window.initSearch = rawSearchIndex => {
1226
1223
ret . others . pop ( ) ;
1227
1224
}
1228
1225
} ;
1229
- onEach ( aliases , pushFunc ) ;
1230
- onEach ( crateAliases , pushFunc ) ;
1226
+
1227
+ aliases . forEach ( pushFunc ) ;
1228
+ crateAliases . forEach ( pushFunc ) ;
1231
1229
}
1232
1230
1233
1231
/**
@@ -1444,11 +1442,11 @@ window.initSearch = rawSearchIndex => {
1444
1442
}
1445
1443
1446
1444
const ret = createQueryResults (
1447
- sortResults ( results_in_args , true ) ,
1448
- sortResults ( results_returned , true ) ,
1449
- sortResults ( results_others , false ) ,
1445
+ sortResults ( results_in_args , true , currentCrate ) ,
1446
+ sortResults ( results_returned , true , currentCrate ) ,
1447
+ sortResults ( results_others , false , currentCrate ) ,
1450
1448
parsedQuery ) ;
1451
- handleAliases ( ret , parsedQuery . original . replace ( / " / g, "" ) , filterCrates ) ;
1449
+ handleAliases ( ret , parsedQuery . original . replace ( / " / g, "" ) , filterCrates , currentCrate ) ;
1452
1450
if ( parsedQuery . error !== null && ret . others . length !== 0 ) {
1453
1451
// It means some doc aliases were found so let's "remove" the error!
1454
1452
ret . query . error = null ;
@@ -1521,18 +1519,18 @@ window.initSearch = rawSearchIndex => {
1521
1519
1522
1520
if ( type === "mod" ) {
1523
1521
displayPath = path + "::" ;
1524
- href = window . rootPath + path . replace ( / : : / g, "/" ) + "/" +
1525
- name + "/index.html" ;
1522
+ href = ROOT_PATH + path . replace ( / : : / g, "/" ) + "/" +
1523
+ name + "/index.html" ;
1526
1524
} else if ( type === "import" ) {
1527
1525
displayPath = item . path + "::" ;
1528
- href = window . rootPath + item . path . replace ( / : : / g, "/" ) + "/index.html#reexport." + name ;
1526
+ href = ROOT_PATH + item . path . replace ( / : : / g, "/" ) + "/index.html#reexport." + name ;
1529
1527
} else if ( type === "primitive" || type === "keyword" ) {
1530
1528
displayPath = "" ;
1531
- href = window . rootPath + path . replace ( / : : / g, "/" ) +
1532
- "/" + type + "." + name + ".html" ;
1529
+ href = ROOT_PATH + path . replace ( / : : / g, "/" ) +
1530
+ "/" + type + "." + name + ".html" ;
1533
1531
} else if ( type === "externcrate" ) {
1534
1532
displayPath = "" ;
1535
- href = window . rootPath + name + "/index.html" ;
1533
+ href = ROOT_PATH + name + "/index.html" ;
1536
1534
} else if ( item . parent !== undefined ) {
1537
1535
const myparent = item . parent ;
1538
1536
let anchor = "#" + type + "." + name ;
@@ -1555,14 +1553,14 @@ window.initSearch = rawSearchIndex => {
1555
1553
} else {
1556
1554
displayPath = path + "::" + myparent . name + "::" ;
1557
1555
}
1558
- href = window . rootPath + path . replace ( / : : / g, "/" ) +
1559
- "/" + pageType +
1560
- "." + pageName +
1561
- ".html" + anchor ;
1556
+ href = ROOT_PATH + path . replace ( / : : / g, "/" ) +
1557
+ "/" + pageType +
1558
+ "." + pageName +
1559
+ ".html" + anchor ;
1562
1560
} else {
1563
1561
displayPath = item . path + "::" ;
1564
- href = window . rootPath + item . path . replace ( / : : / g, "/" ) +
1565
- "/" + type + "." + name + ".html" ;
1562
+ href = ROOT_PATH + item . path . replace ( / : : / g, "/" ) +
1563
+ "/" + type + "." + name + ".html" ;
1566
1564
}
1567
1565
return [ displayPath , href ] ;
1568
1566
}
@@ -1835,7 +1833,7 @@ window.initSearch = rawSearchIndex => {
1835
1833
}
1836
1834
1837
1835
showResults (
1838
- execQuery ( query , searchWords , filterCrates ) ,
1836
+ execQuery ( query , searchWords , filterCrates , window . currentCrate ) ,
1839
1837
params . go_to_first ,
1840
1838
filterCrates ) ;
1841
1839
}
@@ -2015,6 +2013,16 @@ window.initSearch = rawSearchIndex => {
2015
2013
}
2016
2014
2017
2015
function registerSearchEvents ( ) {
2016
+ const params = searchState . getQueryStringParams ( ) ;
2017
+
2018
+ // Populate search bar with query string search term when provided,
2019
+ // but only if the input bar is empty. This avoid the obnoxious issue
2020
+ // where you start trying to do a search, and the index loads, and
2021
+ // suddenly your search is gone!
2022
+ if ( searchState . input . value === "" ) {
2023
+ searchState . input . value = params . search || "" ;
2024
+ }
2025
+
2018
2026
const searchAfter500ms = ( ) => {
2019
2027
searchState . clearInputTimeout ( ) ;
2020
2028
if ( searchState . input . value . length === 0 ) {
@@ -2167,20 +2175,32 @@ window.initSearch = rawSearchIndex => {
2167
2175
* @type {Array<string> }
2168
2176
*/
2169
2177
const searchWords = buildIndex ( rawSearchIndex ) ;
2170
- registerSearchEvents ( ) ;
2171
-
2172
- function runSearchIfNeeded ( ) {
2178
+ if ( typeof window !== "undefined" ) {
2179
+ registerSearchEvents ( ) ;
2173
2180
// If there's a search term in the URL, execute the search now.
2174
- if ( searchState . getQueryStringParams ( ) . search ) {
2181
+ if ( window . searchState . getQueryStringParams ( ) . search ) {
2175
2182
search ( ) ;
2176
2183
}
2177
2184
}
2178
2185
2179
- runSearchIfNeeded ( ) ;
2180
- } ;
2186
+ if ( typeof exports !== "undefined" ) {
2187
+ exports . initSearch = initSearch ;
2188
+ exports . execQuery = execQuery ;
2189
+ exports . parseQuery = parseQuery ;
2190
+ }
2191
+ return searchWords ;
2192
+ }
2181
2193
2182
- if ( window . searchIndex !== undefined ) {
2183
- initSearch ( window . searchIndex ) ;
2194
+ if ( typeof window !== "undefined" ) {
2195
+ window . initSearch = initSearch ;
2196
+ if ( window . searchIndex !== undefined ) {
2197
+ initSearch ( window . searchIndex ) ;
2198
+ }
2199
+ } else {
2200
+ // Running in Node, not a browser. Run initSearch just to produce the
2201
+ // exports.
2202
+ initSearch ( { } ) ;
2184
2203
}
2185
2204
2205
+
2186
2206
} ) ( ) ;
0 commit comments