From c28de27a73521c0814e771120bd0aeecde67d93b Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Wed, 29 Nov 2023 10:11:37 -0700 Subject: [PATCH 1/3] rustdoc-search: allow `:: ` and ` ::` This restriction made sense back when spaces separated function parameters, but now that they separate path components, there's no real ambiguity any more. Additionally, the Rust language allows it. --- src/librustdoc/html/static/js/search.js | 13 +++---- tests/rustdoc-js-std/parser-errors.js | 27 +++++--------- tests/rustdoc-js-std/parser-paths.js | 48 +++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 26 deletions(-) diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js index 979aebdb7bed1..dc6cdcd569aa5 100644 --- a/src/librustdoc/html/static/js/search.js +++ b/src/librustdoc/html/static/js/search.js @@ -512,18 +512,15 @@ function initSearch(rawSearchIndex) { bindingName, }; } + const quadcolon = /::\s*::/.exec(path); if (path.startsWith("::")) { throw ["Paths cannot start with ", "::"]; } else if (path.endsWith("::")) { throw ["Paths cannot end with ", "::"]; - } else if (path.includes("::::")) { - throw ["Unexpected ", "::::"]; - } else if (path.includes(" ::")) { - throw ["Unexpected ", " ::"]; - } else if (path.includes(":: ")) { - throw ["Unexpected ", ":: "]; - } - const pathSegments = path.split(/::|\s+/); + } else if (quadcolon !== null) { + throw ["Unexpected ", quadcolon[0]]; + } + const pathSegments = path.split(/(?:::\s*)|(?:\s+(?:::\s*)?)/); // In case we only have something like `

`, there is no name. if (pathSegments.length === 0 || (pathSegments.length === 1 && pathSegments[0] === "")) { if (generics.length > 0 || prevIs(parserState, ">")) { diff --git a/tests/rustdoc-js-std/parser-errors.js b/tests/rustdoc-js-std/parser-errors.js index ab8d72bf71b13..410fe11b9cf0a 100644 --- a/tests/rustdoc-js-std/parser-errors.js +++ b/tests/rustdoc-js-std/parser-errors.js @@ -143,6 +143,15 @@ const PARSED = [ userQuery: "a::::b", error: "Unexpected `::::`", }, + { + query: "a:: ::b", + elems: [], + foundElems: 0, + original: "a:: ::b", + returned: [], + userQuery: "a:: ::b", + error: "Unexpected `:: ::`", + }, { query: "a::b::", elems: [], @@ -314,24 +323,6 @@ const PARSED = [ userQuery: 'a<->', error: 'Unexpected `-` after `<`', }, - { - query: "a:: a", - elems: [], - foundElems: 0, - original: 'a:: a', - returned: [], - userQuery: 'a:: a', - error: 'Unexpected `:: `', - }, - { - query: "a ::a", - elems: [], - foundElems: 0, - original: 'a ::a', - returned: [], - userQuery: 'a ::a', - error: 'Unexpected ` ::`', - }, { query: "a:", elems: [], diff --git a/tests/rustdoc-js-std/parser-paths.js b/tests/rustdoc-js-std/parser-paths.js index 8d4dedf3f46c8..774e5d028cc2c 100644 --- a/tests/rustdoc-js-std/parser-paths.js +++ b/tests/rustdoc-js-std/parser-paths.js @@ -15,6 +15,54 @@ const PARSED = [ userQuery: "a::b", error: null, }, + { + query: "a:: a", + elems: [{ + name: "a:: a", + fullPath: ["a", "a"], + pathWithoutLast: ["a"], + pathLast: "a", + generics: [], + typeFilter: -1, + }], + foundElems: 1, + original: 'a:: a', + returned: [], + userQuery: 'a:: a', + error: null, + }, + { + query: "a ::a", + elems: [{ + name: "a ::a", + fullPath: ["a", "a"], + pathWithoutLast: ["a"], + pathLast: "a", + generics: [], + typeFilter: -1, + }], + foundElems: 1, + original: 'a ::a', + returned: [], + userQuery: 'a ::a', + error: null, + }, + { + query: "a :: a", + elems: [{ + name: "a :: a", + fullPath: ["a", "a"], + pathWithoutLast: ["a"], + pathLast: "a", + generics: [], + typeFilter: -1, + }], + foundElems: 1, + original: 'a :: a', + returned: [], + userQuery: 'a :: a', + error: null, + }, { query: 'A::B,C', elems: [ From 93f17117ed50a972bfabea86762241cd9ac5ccbd Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Wed, 29 Nov 2023 10:23:42 -0700 Subject: [PATCH 2/3] rustdoc-search: removed dead parser code This is already covered by the normal unexpected char path. --- src/librustdoc/html/static/js/search.js | 2 -- tests/rustdoc-js-std/parser-errors.js | 9 +++++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js index dc6cdcd569aa5..37e5276a49494 100644 --- a/src/librustdoc/html/static/js/search.js +++ b/src/librustdoc/html/static/js/search.js @@ -473,8 +473,6 @@ function initSearch(rawSearchIndex) { const path = name.trim(); if (path.length === 0 && generics.length === 0) { throw ["Unexpected ", parserState.userQuery[parserState.pos]]; - } else if (path === "*") { - throw ["Unexpected ", "*"]; } if (query.literalSearch && parserState.totalElems - parserState.genericsElems > 0) { throw ["Cannot have more than one element if you use quotes"]; diff --git a/tests/rustdoc-js-std/parser-errors.js b/tests/rustdoc-js-std/parser-errors.js index 410fe11b9cf0a..cb1654c0fb0ba 100644 --- a/tests/rustdoc-js-std/parser-errors.js +++ b/tests/rustdoc-js-std/parser-errors.js @@ -17,6 +17,15 @@ const PARSED = [ userQuery: "->

", error: "Found generics without a path", }, + { + query: '-> *', + elems: [], + foundElems: 0, + original: "-> *", + returned: [], + userQuery: "-> *", + error: "Unexpected `*`", + }, { query: 'a<"P">', elems: [], From 930cba8061dca5988a58b4e74774489e1b129184 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Wed, 29 Nov 2023 10:54:49 -0700 Subject: [PATCH 3/3] rustdoc-search: replace TAB/NL/LF with SP first This way, most of the parsing code doesn't need to be designed to handle it, since they should always be treated exactly the same anyhow. --- src/librustdoc/html/static/js/search.js | 16 ++++++-------- tests/rustdoc-js-std/parser-errors.js | 9 ++++++++ tests/rustdoc-js-std/parser-separators.js | 22 ++++++++++---------- tests/rustdoc-js-std/parser-weird-queries.js | 4 ++-- 4 files changed, 28 insertions(+), 23 deletions(-) diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js index 37e5276a49494..8f68796ad26b8 100644 --- a/src/librustdoc/html/static/js/search.js +++ b/src/librustdoc/html/static/js/search.js @@ -287,10 +287,6 @@ function initSearch(rawSearchIndex) { } } - function isWhitespace(c) { - return " \t\n\r".indexOf(c) !== -1; - } - function isSpecialStartCharacter(c) { return "<\"".indexOf(c) !== -1; } @@ -408,7 +404,7 @@ function initSearch(rawSearchIndex) { * @return {boolean} */ function isPathSeparator(c) { - return c === ":" || isWhitespace(c); + return c === ":" || c === " "; } /** @@ -425,7 +421,7 @@ function initSearch(rawSearchIndex) { const c = parserState.userQuery[pos - 1]; if (c === lookingFor) { return true; - } else if (!isWhitespace(c)) { + } else if (c !== " ") { break; } pos -= 1; @@ -454,7 +450,7 @@ function initSearch(rawSearchIndex) { function skipWhitespace(parserState) { while (parserState.pos < parserState.userQuery.length) { const c = parserState.userQuery[parserState.pos]; - if (!isWhitespace(c)) { + if (c !== " ") { break; } parserState.pos += 1; @@ -599,7 +595,7 @@ function initSearch(rawSearchIndex) { } else { while (parserState.pos + 1 < parserState.length) { const next_c = parserState.userQuery[parserState.pos + 1]; - if (!isWhitespace(next_c)) { + if (next_c !== " ") { break; } parserState.pos += 1; @@ -953,7 +949,7 @@ function initSearch(rawSearchIndex) { query.literalSearch = false; foundStopChar = true; continue; - } else if (isWhitespace(c)) { + } else if (c === " ") { skipWhitespace(parserState); continue; } @@ -1113,7 +1109,7 @@ function initSearch(rawSearchIndex) { } } } - userQuery = userQuery.trim(); + userQuery = userQuery.trim().replace(/\r|\n|\t/g, " "); const parserState = { length: userQuery.length, pos: 0, diff --git a/tests/rustdoc-js-std/parser-errors.js b/tests/rustdoc-js-std/parser-errors.js index cb1654c0fb0ba..f9f9c4f4de8c6 100644 --- a/tests/rustdoc-js-std/parser-errors.js +++ b/tests/rustdoc-js-std/parser-errors.js @@ -161,6 +161,15 @@ const PARSED = [ userQuery: "a:: ::b", error: "Unexpected `:: ::`", }, + { + query: "a::\t::b", + elems: [], + foundElems: 0, + original: "a:: ::b", + returned: [], + userQuery: "a:: ::b", + error: "Unexpected `:: ::`", + }, { query: "a::b::", elems: [], diff --git a/tests/rustdoc-js-std/parser-separators.js b/tests/rustdoc-js-std/parser-separators.js index 00c489b51a6a2..7f95f61b006bb 100644 --- a/tests/rustdoc-js-std/parser-separators.js +++ b/tests/rustdoc-js-std/parser-separators.js @@ -5,7 +5,7 @@ const PARSED = [ query: 'aaaaaa b', elems: [ { - name: 'aaaaaa\tb', + name: 'aaaaaa b', fullPath: ['aaaaaa', 'b'], pathWithoutLast: ['aaaaaa'], pathLast: 'b', @@ -14,9 +14,9 @@ const PARSED = [ }, ], foundElems: 1, - original: "aaaaaa b", + original: "aaaaaa b", returned: [], - userQuery: "aaaaaa b", + userQuery: "aaaaaa b", error: null, }, { @@ -40,9 +40,9 @@ const PARSED = [ }, ], foundElems: 2, - original: "aaaaaa, b", + original: "aaaaaa, b", returned: [], - userQuery: "aaaaaa, b", + userQuery: "aaaaaa, b", error: null, }, { @@ -93,7 +93,7 @@ const PARSED = [ query: 'a\tb', elems: [ { - name: 'a\tb', + name: 'a b', fullPath: ['a', 'b'], pathWithoutLast: ['a'], pathLast: 'b', @@ -102,9 +102,9 @@ const PARSED = [ }, ], foundElems: 1, - original: "a\tb", + original: "a b", returned: [], - userQuery: "a\tb", + userQuery: "a b", error: null, }, { @@ -176,7 +176,7 @@ const PARSED = [ pathLast: 'a', generics: [ { - name: 'b\tc', + name: 'b c', fullPath: ['b', 'c'], pathWithoutLast: ['b'], pathLast: 'c', @@ -187,9 +187,9 @@ const PARSED = [ }, ], foundElems: 1, - original: "a", + original: "a", returned: [], - userQuery: "a", + userQuery: "a", error: null, }, ]; diff --git a/tests/rustdoc-js-std/parser-weird-queries.js b/tests/rustdoc-js-std/parser-weird-queries.js index 720ef66c16525..ba68c9717c51a 100644 --- a/tests/rustdoc-js-std/parser-weird-queries.js +++ b/tests/rustdoc-js-std/parser-weird-queries.js @@ -92,9 +92,9 @@ const PARSED = [ query: 'mod\t:', elems: [], foundElems: 0, - original: 'mod\t:', + original: 'mod :', returned: [], - userQuery: 'mod\t:', + userQuery: 'mod :', error: "Unexpected `:` (expected path after type filter `mod:`)", }, ];