From 624885d242fb8ecefa5993e834f429a635b86178 Mon Sep 17 00:00:00 2001
From: Michael Howell <michael@notriddle.com>
Date: Tue, 26 Dec 2023 13:48:56 -0700
Subject: [PATCH] rustdoc: treat query string `+` as space

Fixes #119219
---
 src/librustdoc/html/static/js/main.js       | 3 ++-
 tests/rustdoc-gui/search-form-elements.goml | 9 +++++++++
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/src/librustdoc/html/static/js/main.js b/src/librustdoc/html/static/js/main.js
index 63ab56053af..e205ba46dbf 100644
--- a/src/librustdoc/html/static/js/main.js
+++ b/src/librustdoc/html/static/js/main.js
@@ -279,7 +279,8 @@ function preLoadCss(cssUrl) {
             const params = {};
             window.location.search.substring(1).split("&").
                 map(s => {
-                    const pair = s.split("=");
+                    // https://github.com/rust-lang/rust/issues/119219
+                    const pair = s.split("=").map(x => x.replace(/\+/g, " "));
                     params[decodeURIComponent(pair[0])] =
                         typeof pair[1] === "undefined" ? null : decodeURIComponent(pair[1]);
                 });
diff --git a/tests/rustdoc-gui/search-form-elements.goml b/tests/rustdoc-gui/search-form-elements.goml
index a4e22364859..0ea61a4f0eb 100644
--- a/tests/rustdoc-gui/search-form-elements.goml
+++ b/tests/rustdoc-gui/search-form-elements.goml
@@ -137,3 +137,12 @@ call-function: (
         "menu_a_color": "#3873ad",
     }
 )
+
+// Check that search input correctly decodes form encoding.
+go-to: "file://" + |DOC_PATH| + "/test_docs/index.html?search=a+b"
+wait-for: "#search-tabs" // Waiting for the search.js to load.
+assert-property: (".search-input", { "value": "a b" })
+// Check that literal + is not treated as space.
+go-to: "file://" + |DOC_PATH| + "/test_docs/index.html?search=a%2Bb"
+wait-for: "#search-tabs" // Waiting for the search.js to load.
+assert-property: (".search-input", { "value": "a+b" })