about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume.gomez@huawei.com>2022-04-17 12:05:31 +0200
committerGuillaume Gomez <guillaume.gomez@huawei.com>2022-04-18 21:43:19 +0200
commit299e8ee25ec170cbcc5a6829b2638361875c6f1f (patch)
tree4c969d4465a73e0861a50066b6433603e9ee9ee9
parentd7d538a8c493b52ffc76ac1aa325ad4e516bb3e9 (diff)
downloadrust-299e8ee25ec170cbcc5a6829b2638361875c6f1f.tar.gz
rust-299e8ee25ec170cbcc5a6829b2638361875c6f1f.zip
Add an extra check over filter type
-rw-r--r--src/librustdoc/html/static/js/search.js33
-rw-r--r--src/test/rustdoc-js-std/parser-errors.js40
-rw-r--r--src/test/rustdoc-js-std/parser-weird-queries.js20
-rw-r--r--src/tools/rustdoc-js/tester.js3
4 files changed, 92 insertions, 4 deletions
diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js
index a5b4847f99c..4d78a50e4d0 100644
--- a/src/librustdoc/html/static/js/search.js
+++ b/src/librustdoc/html/static/js/search.js
@@ -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";
     }
 
     /**
@@ -425,6 +436,22 @@ window.initSearch = function(rawSearchIndex) {
     }
 
     /**
+     * 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.
      *
@@ -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;
diff --git a/src/test/rustdoc-js-std/parser-errors.js b/src/test/rustdoc-js-std/parser-errors.js
index 383c8996c1e..9d92f5ddfd3 100644
--- a/src/test/rustdoc-js-std/parser-errors.js
+++ b/src/test/rustdoc-js-std/parser-errors.js
@@ -30,6 +30,10 @@ const QUERY = [
     "a<->",
     "a:: a",
     "a ::a",
+    "a<a>:",
+    "a<>:",
+    "a,:",
+    "  a<>  :",
 ];
 
 const PARSED = [
@@ -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',
+    },
 ];
diff --git a/src/test/rustdoc-js-std/parser-weird-queries.js b/src/test/rustdoc-js-std/parser-weird-queries.js
index d249e0bfdad..a3d85aeca5e 100644
--- a/src/test/rustdoc-js-std/parser-weird-queries.js
+++ b/src/test/rustdoc-js-std/parser-weird-queries.js
@@ -7,6 +7,8 @@ const QUERY = [
     'a,b(c)',
     'aaa,a',
     ',,,,',
+    'mod    :',
+    'mod\t:',
 ];
 
 const PARSED = [
@@ -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,
+    },
 ];
diff --git a/src/tools/rustdoc-js/tester.js b/src/tools/rustdoc-js/tester.js
index 3aeeee0d8f0..17362338355 100644
--- a/src/tools/rustdoc-js/tester.js
+++ b/src/tools/rustdoc-js/tester.js
@@ -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 = {};