Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 8e29ed4

Browse files
Add isIdentCharacter function to ensure that unexpected characters are handled correctly
1 parent f9251ee commit 8e29ed4

File tree

3 files changed

+45
-15
lines changed

3 files changed

+45
-15
lines changed

src/librustdoc/html/static/js/search.js

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,21 @@ window.initSearch = function(rawSearchIndex) {
211211
return parserState.userQuery.slice(parserState.pos, parserState.pos + 2) == '->';
212212
}
213213

214+
/**
215+
* Returns `true` if the given `c` character is valid for an ident.
216+
*
217+
* @param {string} c
218+
*
219+
* @return {boolean}
220+
*/
221+
function isIdentCharacter(c) {
222+
return (
223+
c === '_' ||
224+
(c >= '0' && c <= '9') ||
225+
(c >= 'a' && c <= 'z') ||
226+
(c >= 'A' && c <= 'Z'));
227+
}
228+
214229
/**
215230
* @param {ParsedQuery} query
216231
* @param {ParserState} parserState
@@ -274,18 +289,22 @@ window.initSearch = function(rawSearchIndex) {
274289
} else {
275290
while (parserState.pos < parserState.length) {
276291
var c = parserState.userQuery[parserState.pos];
277-
if (isErrorCharacter(c)) {
278-
throw new Error(`Unexpected \`${c}\``);
279-
} else if (isStopCharacter(c) || isSpecialStartCharacter(c)) {
280-
break;
281-
}
282-
// If we allow paths ("str::string" for example).
283-
else if (c === ":") {
284-
if (!isPathStart(parserState)) {
292+
if (!isIdentCharacter(c)) {
293+
if (isErrorCharacter(c)) {
294+
throw new Error(`Unexpected \`${c}\``);
295+
} else if (isStopCharacter(c) || isSpecialStartCharacter(c)) {
285296
break;
286297
}
287-
// Skip current ":".
288-
parserState.pos += 1;
298+
// If we allow paths ("str::string" for example).
299+
else if (c === ":") {
300+
if (!isPathStart(parserState)) {
301+
break;
302+
}
303+
// Skip current ":".
304+
parserState.pos += 1;
305+
} else {
306+
throw new Error(`Unexpected \`${c}\``);
307+
}
289308
}
290309
parserState.pos += 1;
291310
end = parserState.pos;

src/test/rustdoc-js-std/parser-errors.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ const QUERY = [
1717
":a",
1818
"a b:",
1919
"a (b:",
20-
"{:",
20+
"_:",
2121
"a-bb",
2222
"a>bb",
23+
"ab'",
2324
];
2425

2526
const PARSED = [
@@ -188,11 +189,11 @@ const PARSED = [
188189
{
189190
elems: [],
190191
foundElems: 0,
191-
original: "{:",
192+
original: "_:",
192193
returned: [],
193194
typeFilter: -1,
194-
userQuery: "{:",
195-
error: "Unknown type filter `{`",
195+
userQuery: "_:",
196+
error: "Unknown type filter `_`",
196197
},
197198
{
198199
elems: [],
@@ -212,4 +213,13 @@ const PARSED = [
212213
userQuery: "a>bb",
213214
error: "Unexpected `>` (did you mean `->`?)",
214215
},
216+
{
217+
elems: [],
218+
foundElems: 0,
219+
original: "ab'",
220+
returned: [],
221+
typeFilter: -1,
222+
userQuery: "ab'",
223+
error: "Unexpected `'`",
224+
},
215225
];

src/tools/rustdoc-js/tester.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,8 @@ function loadSearchJsAndIndex(searchJs, searchIndex, storageJs, crate) {
274274
"isWhitespace", "isSpecialStartCharacter", "isStopCharacter",
275275
"parseInput", "getItemsBefore", "getNextElem", "createQueryElement",
276276
"isReturnArrow", "isPathStart", "getStringElem", "newParsedQuery",
277-
"itemTypeFromName", "isEndCharacter", "isErrorCharacter"];
277+
"itemTypeFromName", "isEndCharacter", "isErrorCharacter",
278+
"isIdentCharacter"];
278279

279280
const functions = ["hasOwnPropertyRustdoc", "onEach"];
280281
ALIASES = {};

0 commit comments

Comments
 (0)