1
Fork 0

Add isIdentCharacter function to ensure that unexpected characters are handled correctly

This commit is contained in:
Guillaume Gomez 2022-03-23 17:26:32 +01:00
parent f9251eef68
commit 8e29ed43d9
3 changed files with 45 additions and 15 deletions

View file

@ -211,6 +211,21 @@ window.initSearch = function(rawSearchIndex) {
return parserState.userQuery.slice(parserState.pos, parserState.pos + 2) == '->'; return parserState.userQuery.slice(parserState.pos, parserState.pos + 2) == '->';
} }
/**
* Returns `true` if the given `c` character is valid for an ident.
*
* @param {string} c
*
* @return {boolean}
*/
function isIdentCharacter(c) {
return (
c === '_' ||
(c >= '0' && c <= '9') ||
(c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z'));
}
/** /**
* @param {ParsedQuery} query * @param {ParsedQuery} query
* @param {ParserState} parserState * @param {ParserState} parserState
@ -274,18 +289,22 @@ window.initSearch = function(rawSearchIndex) {
} else { } else {
while (parserState.pos < parserState.length) { while (parserState.pos < parserState.length) {
var c = parserState.userQuery[parserState.pos]; var c = parserState.userQuery[parserState.pos];
if (isErrorCharacter(c)) { if (!isIdentCharacter(c)) {
throw new Error(`Unexpected \`${c}\``); if (isErrorCharacter(c)) {
} else if (isStopCharacter(c) || isSpecialStartCharacter(c)) { throw new Error(`Unexpected \`${c}\``);
break; } else if (isStopCharacter(c) || isSpecialStartCharacter(c)) {
}
// If we allow paths ("str::string" for example).
else if (c === ":") {
if (!isPathStart(parserState)) {
break; break;
} }
// Skip current ":". // If we allow paths ("str::string" for example).
parserState.pos += 1; else if (c === ":") {
if (!isPathStart(parserState)) {
break;
}
// Skip current ":".
parserState.pos += 1;
} else {
throw new Error(`Unexpected \`${c}\``);
}
} }
parserState.pos += 1; parserState.pos += 1;
end = parserState.pos; end = parserState.pos;

View file

@ -17,9 +17,10 @@ const QUERY = [
":a", ":a",
"a b:", "a b:",
"a (b:", "a (b:",
"{:", "_:",
"a-bb", "a-bb",
"a>bb", "a>bb",
"ab'",
]; ];
const PARSED = [ const PARSED = [
@ -188,11 +189,11 @@ const PARSED = [
{ {
elems: [], elems: [],
foundElems: 0, foundElems: 0,
original: "{:", original: "_:",
returned: [], returned: [],
typeFilter: -1, typeFilter: -1,
userQuery: "{:", userQuery: "_:",
error: "Unknown type filter `{`", error: "Unknown type filter `_`",
}, },
{ {
elems: [], elems: [],
@ -212,4 +213,13 @@ const PARSED = [
userQuery: "a>bb", userQuery: "a>bb",
error: "Unexpected `>` (did you mean `->`?)", error: "Unexpected `>` (did you mean `->`?)",
}, },
{
elems: [],
foundElems: 0,
original: "ab'",
returned: [],
typeFilter: -1,
userQuery: "ab'",
error: "Unexpected `'`",
},
]; ];

View file

@ -274,7 +274,8 @@ function loadSearchJsAndIndex(searchJs, searchIndex, storageJs, crate) {
"isWhitespace", "isSpecialStartCharacter", "isStopCharacter", "isWhitespace", "isSpecialStartCharacter", "isStopCharacter",
"parseInput", "getItemsBefore", "getNextElem", "createQueryElement", "parseInput", "getItemsBefore", "getNextElem", "createQueryElement",
"isReturnArrow", "isPathStart", "getStringElem", "newParsedQuery", "isReturnArrow", "isPathStart", "getStringElem", "newParsedQuery",
"itemTypeFromName", "isEndCharacter", "isErrorCharacter"]; "itemTypeFromName", "isEndCharacter", "isErrorCharacter",
"isIdentCharacter"];
const functions = ["hasOwnPropertyRustdoc", "onEach"]; const functions = ["hasOwnPropertyRustdoc", "onEach"];
ALIASES = {}; ALIASES = {};