diff options
| author | Guillaume Gomez <guillaume.gomez@huawei.com> | 2022-03-23 17:26:32 +0100 |
|---|---|---|
| committer | Guillaume Gomez <guillaume.gomez@huawei.com> | 2022-04-18 20:59:09 +0200 |
| commit | 8e29ed43d9943b2cf705e3511e54ba01d2feaa93 (patch) | |
| tree | 3aa1edd8d84885ed20d6e8c98652de8c2bde6f26 | |
| parent | f9251eef688590cd4770baaa01241bbb060c2eba (diff) | |
| download | rust-8e29ed43d9943b2cf705e3511e54ba01d2feaa93.tar.gz rust-8e29ed43d9943b2cf705e3511e54ba01d2feaa93.zip | |
Add isIdentCharacter function to ensure that unexpected characters are handled correctly
| -rw-r--r-- | src/librustdoc/html/static/js/search.js | 39 | ||||
| -rw-r--r-- | src/test/rustdoc-js-std/parser-errors.js | 18 | ||||
| -rw-r--r-- | src/tools/rustdoc-js/tester.js | 3 |
3 files changed, 45 insertions, 15 deletions
diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js index 421e8f31e97..8beb0768dcf 100644 --- a/src/librustdoc/html/static/js/search.js +++ b/src/librustdoc/html/static/js/search.js @@ -212,6 +212,21 @@ window.initSearch = function(rawSearchIndex) { } /** + * 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 {ParserState} parserState * @param {string} name - Name of the query element. @@ -274,18 +289,22 @@ window.initSearch = function(rawSearchIndex) { } else { while (parserState.pos < parserState.length) { var c = parserState.userQuery[parserState.pos]; - if (isErrorCharacter(c)) { - throw new Error(`Unexpected \`${c}\``); - } else if (isStopCharacter(c) || isSpecialStartCharacter(c)) { - break; - } - // If we allow paths ("str::string" for example). - else if (c === ":") { - if (!isPathStart(parserState)) { + if (!isIdentCharacter(c)) { + if (isErrorCharacter(c)) { + throw new Error(`Unexpected \`${c}\``); + } else if (isStopCharacter(c) || isSpecialStartCharacter(c)) { break; } - // Skip current ":". - parserState.pos += 1; + // If we allow paths ("str::string" for example). + else if (c === ":") { + if (!isPathStart(parserState)) { + break; + } + // Skip current ":". + parserState.pos += 1; + } else { + throw new Error(`Unexpected \`${c}\``); + } } parserState.pos += 1; end = parserState.pos; diff --git a/src/test/rustdoc-js-std/parser-errors.js b/src/test/rustdoc-js-std/parser-errors.js index 080f82b41ec..ae5145e8322 100644 --- a/src/test/rustdoc-js-std/parser-errors.js +++ b/src/test/rustdoc-js-std/parser-errors.js @@ -17,9 +17,10 @@ const QUERY = [ ":a", "a b:", "a (b:", - "{:", + "_:", "a-bb", "a>bb", + "ab'", ]; const PARSED = [ @@ -188,11 +189,11 @@ const PARSED = [ { elems: [], foundElems: 0, - original: "{:", + original: "_:", returned: [], typeFilter: -1, - userQuery: "{:", - error: "Unknown type filter `{`", + userQuery: "_:", + error: "Unknown type filter `_`", }, { elems: [], @@ -212,4 +213,13 @@ const PARSED = [ userQuery: "a>bb", error: "Unexpected `>` (did you mean `->`?)", }, + { + elems: [], + foundElems: 0, + original: "ab'", + returned: [], + typeFilter: -1, + userQuery: "ab'", + error: "Unexpected `'`", + }, ]; diff --git a/src/tools/rustdoc-js/tester.js b/src/tools/rustdoc-js/tester.js index afd878423c0..d5209089892 100644 --- a/src/tools/rustdoc-js/tester.js +++ b/src/tools/rustdoc-js/tester.js @@ -274,7 +274,8 @@ function loadSearchJsAndIndex(searchJs, searchIndex, storageJs, crate) { "isWhitespace", "isSpecialStartCharacter", "isStopCharacter", "parseInput", "getItemsBefore", "getNextElem", "createQueryElement", "isReturnArrow", "isPathStart", "getStringElem", "newParsedQuery", - "itemTypeFromName", "isEndCharacter", "isErrorCharacter"]; + "itemTypeFromName", "isEndCharacter", "isErrorCharacter", + "isIdentCharacter"]; const functions = ["hasOwnPropertyRustdoc", "onEach"]; ALIASES = {}; |
