Skip to content

Commit 6f35475

Browse files
Parse idents the same way in both quote string elements and "normal" elements
1 parent a6051c7 commit 6f35475

File tree

2 files changed

+45
-33
lines changed

2 files changed

+45
-33
lines changed

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

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,11 @@ window.initSearch = function(rawSearchIndex) {
176176
throw new Error("Cannot use literal search when there is more than one element");
177177
}
178178
parserState.pos += 1;
179-
while (parserState.pos < parserState.length &&
180-
parserState.userQuery[parserState.pos] !== "\"")
181-
{
182-
parserState.pos += 1;
183-
}
179+
var end = getIdentEndPosition(parserState);
184180
if (parserState.pos >= parserState.length) {
185181
throw new Error("Unclosed `\"`");
182+
} else if (parserState.userQuery[end] !== "\"") {
183+
throw new Error(`Unexpected \`${parserState.userQuery[end]}\` in a string element`);
186184
}
187185
// To skip the quote at the end.
188186
parserState.pos += 1;
@@ -284,6 +282,45 @@ window.initSearch = function(rawSearchIndex) {
284282
};
285283
}
286284

285+
/**
286+
* This function goes through all characters until it reaches an invalid ident character or the
287+
* end of the query. It returns the position of the last character of the ident.
288+
*
289+
* @param {ParserState} parserState
290+
*
291+
* @return {integer}
292+
*/
293+
function getIdentEndPosition(parserState) {
294+
var end = parserState.pos;
295+
while (parserState.pos < parserState.length) {
296+
var c = parserState.userQuery[parserState.pos];
297+
if (!isIdentCharacter(c)) {
298+
if (isErrorCharacter(c)) {
299+
throw new Error(`Unexpected \`${c}\``);
300+
} else if (
301+
isStopCharacter(c) ||
302+
isSpecialStartCharacter(c) ||
303+
isSeparatorCharacter(c))
304+
{
305+
break;
306+
}
307+
// If we allow paths ("str::string" for example).
308+
else if (c === ":") {
309+
if (!isPathStart(parserState)) {
310+
break;
311+
}
312+
// Skip current ":".
313+
parserState.pos += 1;
314+
} else {
315+
throw new Error(`Unexpected \`${c}\``);
316+
}
317+
}
318+
parserState.pos += 1;
319+
end = parserState.pos;
320+
}
321+
return end;
322+
}
323+
287324
/**
288325
* @param {ParsedQuery} query
289326
* @param {ParserState} parserState
@@ -294,39 +331,14 @@ window.initSearch = function(rawSearchIndex) {
294331
var generics = [];
295332

296333
var start = parserState.pos;
297-
var end = start;
334+
var end;
298335
// We handle the strings on their own mostly to make code easier to follow.
299336
if (parserState.userQuery[parserState.pos] === "\"") {
300337
start += 1;
301338
getStringElem(query, parserState, isInGenerics);
302339
end = parserState.pos - 1;
303340
} else {
304-
while (parserState.pos < parserState.length) {
305-
var c = parserState.userQuery[parserState.pos];
306-
if (!isIdentCharacter(c)) {
307-
if (isErrorCharacter(c)) {
308-
throw new Error(`Unexpected \`${c}\``);
309-
} else if (
310-
isStopCharacter(c) ||
311-
isSpecialStartCharacter(c) ||
312-
isSeparatorCharacter(c))
313-
{
314-
break;
315-
}
316-
// If we allow paths ("str::string" for example).
317-
else if (c === ":") {
318-
if (!isPathStart(parserState)) {
319-
break;
320-
}
321-
// Skip current ":".
322-
parserState.pos += 1;
323-
} else {
324-
throw new Error(`Unexpected \`${c}\``);
325-
}
326-
}
327-
parserState.pos += 1;
328-
end = parserState.pos;
329-
}
341+
end = getIdentEndPosition(parserState);
330342
}
331343
if (parserState.pos < parserState.length &&
332344
parserState.userQuery[parserState.pos] === "<")

src/tools/rustdoc-js/tester.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ function loadSearchJsAndIndex(searchJs, searchIndex, storageJs, crate) {
275275
"parseInput", "getItemsBefore", "getNextElem", "createQueryElement",
276276
"isReturnArrow", "isPathStart", "getStringElem", "newParsedQuery",
277277
"itemTypeFromName", "isEndCharacter", "isErrorCharacter",
278-
"isIdentCharacter", "isSeparatorCharacter"];
278+
"isIdentCharacter", "isSeparatorCharacter", "getIdentEndPosition"];
279279

280280
const functions = ["hasOwnPropertyRustdoc", "onEach"];
281281
ALIASES = {};

0 commit comments

Comments
 (0)