From 2edc54c830bb3925967c77486fef32707b4321ce Mon Sep 17 00:00:00 2001
From: Guillaume Gomez <guillaume.gomez@huawei.com>
Date: Wed, 3 Apr 2024 14:11:01 +0200
Subject: [PATCH 1/5] Default to light theme is JS is enabled but not working

---
 src/librustdoc/html/static/css/noscript.css | 4 ++--
 src/librustdoc/html/static/css/rustdoc.css  | 8 +++++++-
 2 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/src/librustdoc/html/static/css/noscript.css b/src/librustdoc/html/static/css/noscript.css
index f425f3ec95c..ccb97d7df4c 100644
--- a/src/librustdoc/html/static/css/noscript.css
+++ b/src/librustdoc/html/static/css/noscript.css
@@ -34,7 +34,7 @@ nav.sub {
 	in rustdoc.css */
 
 /* Begin theme: light */
-:root {
+:root, :root:not([data-theme]) {
 	--main-background-color: white;
 	--main-color: black;
 	--settings-input-color: #2196f3;
@@ -140,7 +140,7 @@ nav.sub {
 
 @media (prefers-color-scheme: dark) {
 	/* Begin theme: dark */
-	:root {
+	:root, :root:not([data-theme]) {
 		--main-background-color: #353535;
 		--main-color: #ddd;
 		--settings-input-color: #2196f3;
diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css
index 9993dfb1d8c..0bb073b1cea 100644
--- a/src/librustdoc/html/static/css/rustdoc.css
+++ b/src/librustdoc/html/static/css/rustdoc.css
@@ -2315,8 +2315,14 @@ in src-script.js and main.js
 	tooling to ensure different themes all define all the variables. Do not
 	alter their formatting. */
 
+/*
+About `:root:not([data-theme])`: if for any reason the JS is enabled but cannot be loaded,
+`noscript` won't be enabled and the doc will have no color applied. To do around this, we
+add a selector check that if `data-theme` is not defined, then we apply the light theme
+by default.
+*/
 /* Begin theme: light */
-:root[data-theme="light"] {
+:root[data-theme="light"], :root:not([data-theme]) {
 	--main-background-color: white;
 	--main-color: black;
 	--settings-input-color: #2196f3;

From 8f9d93bf57cb1c74cd52581a980bc3accd01e5c4 Mon Sep 17 00:00:00 2001
From: Guillaume Gomez <guillaume.gomez@huawei.com>
Date: Wed, 3 Apr 2024 14:11:23 +0200
Subject: [PATCH 2/5] Add GUI test to ensure there is always a theme applied if
 JS is disabled

---
 tests/rustdoc-gui/javascript-disabled.goml | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/tests/rustdoc-gui/javascript-disabled.goml b/tests/rustdoc-gui/javascript-disabled.goml
index a0872d553af..cc1a925fbc9 100644
--- a/tests/rustdoc-gui/javascript-disabled.goml
+++ b/tests/rustdoc-gui/javascript-disabled.goml
@@ -3,4 +3,9 @@
 javascript: false
 
 go-to: "file://" + |DOC_PATH| + "/test_docs/struct.Foo.html"
+show-text: true
 assert-css: (".sub", {"display": "none"})
+
+// Even though JS is disabled, we should still have themes applied. Links are never black-colored
+// if styles are applied so we check that they are not.
+assert-css-false: ("a.src", {"color": "#000"})

From 2815edc671357649438dfac1bf3aca3aba07b71b Mon Sep 17 00:00:00 2001
From: Guillaume Gomez <guillaume.gomez@huawei.com>
Date: Wed, 3 Apr 2024 14:27:11 +0200
Subject: [PATCH 3/5] Update `rustdoc_css_themes.rs` to take into account new
 selectors

---
 src/tools/tidy/src/rustdoc_css_themes.rs | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/src/tools/tidy/src/rustdoc_css_themes.rs b/src/tools/tidy/src/rustdoc_css_themes.rs
index 852d6e14e91..af36f9ba58e 100644
--- a/src/tools/tidy/src/rustdoc_css_themes.rs
+++ b/src/tools/tidy/src/rustdoc_css_themes.rs
@@ -74,8 +74,11 @@ fn compare_themes<'a>(
         (noscript_css_line_number, noscript_css_line),
     ) in rustdoc_css_lines.zip(noscript_css_lines)
     {
-        if noscript_css_line.starts_with(":root {")
-            && rustdoc_css_line.starts_with(&format!(r#":root[data-theme="{name}"] {{"#))
+        if noscript_css_line.starts_with(":root, :root:not([data-theme]) {")
+            && (rustdoc_css_line.starts_with(&format!(r#":root[data-theme="{name}"] {{"#))
+                || rustdoc_css_line.starts_with(&format!(
+                    r#":root[data-theme="{name}"], :root:not([data-theme]) {{"#
+                )))
         {
             // selectors are different between rustdoc.css and noscript.css
             // that's why they both exist: one uses JS, the other uses media queries

From f2ff9c903548d88211df90cd4e1673577ff58ad5 Mon Sep 17 00:00:00 2001
From: Guillaume Gomez <guillaume.gomez@huawei.com>
Date: Thu, 4 Apr 2024 23:48:35 +0200
Subject: [PATCH 4/5] Update browser-ui-test version to 0.17.1

---
 .../docker/host-x86_64/x86_64-gnu-tools/browser-ui-test.version | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/ci/docker/host-x86_64/x86_64-gnu-tools/browser-ui-test.version b/src/ci/docker/host-x86_64/x86_64-gnu-tools/browser-ui-test.version
index 07feb823492..14a8c245756 100644
--- a/src/ci/docker/host-x86_64/x86_64-gnu-tools/browser-ui-test.version
+++ b/src/ci/docker/host-x86_64/x86_64-gnu-tools/browser-ui-test.version
@@ -1 +1 @@
-0.17.0
\ No newline at end of file
+0.17.1
\ No newline at end of file

From a815b97850e487f5c668edf83357cf871b3db57a Mon Sep 17 00:00:00 2001
From: Guillaume Gomez <guillaume.gomez@huawei.com>
Date: Thu, 4 Apr 2024 23:49:34 +0200
Subject: [PATCH 5/5] Add regression test to ensure that even if JS is enabled
 but not working, a theme will still get applied

---
 tests/rustdoc-gui/javascript-disabled.goml | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/tests/rustdoc-gui/javascript-disabled.goml b/tests/rustdoc-gui/javascript-disabled.goml
index cc1a925fbc9..a7579ef7ec1 100644
--- a/tests/rustdoc-gui/javascript-disabled.goml
+++ b/tests/rustdoc-gui/javascript-disabled.goml
@@ -9,3 +9,12 @@ assert-css: (".sub", {"display": "none"})
 // Even though JS is disabled, we should still have themes applied. Links are never black-colored
 // if styles are applied so we check that they are not.
 assert-css-false: ("a.src", {"color": "#000"})
+
+javascript: true
+fail-on-request-error: false
+block-network-request: "*.js"
+reload:
+
+// JS is enabled but wasn't loaded, we should still have the light theme applied. Links are never
+// black-colored if styles are applied so we check that they are not.
+assert-css-false: ("a.src", {"color": "#000"})