Skip to content

Commit

Permalink
Add an extra check over filter type
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Apr 18, 2022
1 parent d7d538a commit 299e8ee
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 4 deletions.
33 changes: 30 additions & 3 deletions src/librustdoc/html/static/js/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,18 @@ window.initSearch = function(rawSearchIndex) {
* @return {boolean}
*/
function isSeparatorCharacter(c) {
return ", \t".indexOf(c) !== -1;
return c === "," || isWhitespaceCharacter(c);
}

/**
* Returns `true` if the given `c` character is a whitespace.
*
* @param {string} c
*
* @return {boolean}
*/
function isWhitespaceCharacter(c) {
return c === " " || c === "\t";
}

/**
Expand Down Expand Up @@ -424,6 +435,22 @@ window.initSearch = function(rawSearchIndex) {
parserState.pos += 1;
}

/**
* Checks that the type filter doesn't have unwanted characters like `<>` (which are ignored
* if empty).
*
* @param {ParserState} parserState
*/
function checkExtraTypeFilterCharacters(parserState) {
var query = parserState.userQuery;

for (var pos = 0; pos < parserState.pos; ++pos) {
if (!isIdentCharacter(query[pos]) && !isWhitespaceCharacter(query[pos])) {
throw new Error(`Unexpected \`${query[pos]}\` in type filter`);
}
}
}

/**
* Parses the provided `query` input to fill `parserState`. If it encounters an error while
* parsing `query`, it'll throw an error.
Expand Down Expand Up @@ -457,10 +484,10 @@ window.initSearch = function(rawSearchIndex) {
throw new Error("Expected type filter before `:`");
} else if (query.elems.length !== 1 || parserState.totalElems !== 1) {
throw new Error("Unexpected `:`");
}
if (query.literalSearch) {
} else if (query.literalSearch) {
throw new Error("You cannot use quotes on type filter");
}
checkExtraTypeFilterCharacters(parserState);
// The type filter doesn't count as an element since it's a modifier.
parserState.typeFilter = query.elems.pop().name;
parserState.pos += 1;
Expand Down
40 changes: 40 additions & 0 deletions src/test/rustdoc-js-std/parser-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ const QUERY = [
"a<->",
"a:: a",
"a ::a",
"a<a>:",
"a<>:",
"a,:",
" a<> :",
];

const PARSED = [
Expand Down Expand Up @@ -312,4 +316,40 @@ const PARSED = [
userQuery: 'a ::a',
error: 'Paths cannot start with `::`',
},
{
elems: [],
foundElems: 0,
original: "a<a>:",
returned: [],
typeFilter: -1,
userQuery: "a<a>:",
error: 'Unexpected `:`',
},
{
elems: [],
foundElems: 0,
original: "a<>:",
returned: [],
typeFilter: -1,
userQuery: "a<>:",
error: 'Unexpected `<` in type filter',
},
{
elems: [],
foundElems: 0,
original: "a,:",
returned: [],
typeFilter: -1,
userQuery: "a,:",
error: 'Unexpected `,` in type filter',
},
{
elems: [],
foundElems: 0,
original: "a<> :",
returned: [],
typeFilter: -1,
userQuery: "a<> :",
error: 'Unexpected `<` in type filter',
},
];
20 changes: 20 additions & 0 deletions src/test/rustdoc-js-std/parser-weird-queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const QUERY = [
'a,b(c)',
'aaa,a',
',,,,',
'mod :',
'mod\t:',
];

const PARSED = [
Expand Down Expand Up @@ -100,4 +102,22 @@ const PARSED = [
userQuery: ",,,,",
error: null,
},
{
elems: [],
foundElems: 0,
original: 'mod :',
returned: [],
typeFilter: 0,
userQuery: 'mod :',
error: null,
},
{
elems: [],
foundElems: 0,
original: 'mod\t:',
returned: [],
typeFilter: 0,
userQuery: 'mod\t:',
error: null,
},
];
3 changes: 2 additions & 1 deletion src/tools/rustdoc-js/tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,8 @@ function loadSearchJsAndIndex(searchJs, searchIndex, storageJs, crate) {
"parseInput", "getItemsBefore", "getNextElem", "createQueryElement",
"isReturnArrow", "isPathStart", "getStringElem", "newParsedQuery",
"itemTypeFromName", "isEndCharacter", "isErrorCharacter",
"isIdentCharacter", "isSeparatorCharacter", "getIdentEndPosition"];
"isIdentCharacter", "isSeparatorCharacter", "getIdentEndPosition",
"checkExtraTypeFilterCharacters", "isWhitespaceCharacter"];

const functions = ["hasOwnPropertyRustdoc", "onEach"];
ALIASES = {};
Expand Down

0 comments on commit 299e8ee

Please sign in to comment.