Skip to content

Commit 8c83058

Browse files
authored
Rollup merge of #109542 - notriddle:notriddle/storage-js, r=GuillaumeGomez
rustdoc: clean up `storage.js`
2 parents 15ea227 + 95ef91c commit 8c83058

File tree

3 files changed

+19
-77
lines changed

3 files changed

+19
-77
lines changed

src/librustdoc/html/static/css/rustdoc.css

+1-16
Original file line numberDiff line numberDiff line change
@@ -87,21 +87,6 @@
8787
box-sizing: border-box;
8888
}
8989

90-
/* This part handles the "default" theme being used depending on the system one. */
91-
html {
92-
content: "";
93-
}
94-
@media (prefers-color-scheme: light) {
95-
html {
96-
content: "light";
97-
}
98-
}
99-
@media (prefers-color-scheme: dark) {
100-
html {
101-
content: "dark";
102-
}
103-
}
104-
10590
/* General structure and fonts */
10691

10792
body {
@@ -1538,7 +1523,7 @@ However, it's not needed with smaller screen width because the doc/code block is
15381523
/*
15391524
WARNING: RUSTDOC_MOBILE_BREAKPOINT MEDIA QUERY
15401525
If you update this line, then you also need to update the line with the same warning
1541-
in storage.js
1526+
in main.js
15421527
*/
15431528
@media (max-width: 700px) {
15441529
/* When linking to an item with an `id` (for instance, by clicking a link in the sidebar,

src/librustdoc/html/static/js/main.js

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
"use strict";
66

7+
// WARNING: RUSTDOC_MOBILE_BREAKPOINT MEDIA QUERY
8+
// If you update this line, then you also need to update the media query with the same
9+
// warning in rustdoc.css
10+
window.RUSTDOC_MOBILE_BREAKPOINT = 700;
11+
712
// Given a basename (e.g. "storage") and an extension (e.g. ".js"), return a URL
813
// for a resource under the root-path, with the resource-suffix.
914
function resourcePath(basename, extension) {

src/librustdoc/html/static/js/storage.js

+13-61
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,22 @@
88
const darkThemes = ["dark", "ayu"];
99
window.currentTheme = document.getElementById("themeStyle");
1010

11-
// WARNING: RUSTDOC_MOBILE_BREAKPOINT MEDIA QUERY
12-
// If you update this line, then you also need to update the media query with the same
13-
// warning in rustdoc.css
14-
window.RUSTDOC_MOBILE_BREAKPOINT = 700;
15-
1611
const settingsDataset = (function() {
1712
const settingsElement = document.getElementById("default-settings");
18-
if (settingsElement === null) {
19-
return null;
20-
}
21-
const dataset = settingsElement.dataset;
22-
if (dataset === undefined) {
23-
return null;
24-
}
25-
return dataset;
13+
return settingsElement && settingsElement.dataset ? settingsElement.dataset : null;
2614
})();
2715

2816
function getSettingValue(settingName) {
2917
const current = getCurrentValue(settingName);
30-
if (current !== null) {
31-
return current;
32-
}
33-
if (settingsDataset !== null) {
18+
if (current === null && settingsDataset !== null) {
3419
// See the comment for `default_settings.into_iter()` etc. in
3520
// `Options::from_matches` in `librustdoc/config.rs`.
3621
const def = settingsDataset[settingName.replace(/-/g,"_")];
3722
if (def !== undefined) {
3823
return def;
3924
}
4025
}
41-
return null;
26+
return current;
4227
}
4328

4429
const localStoredTheme = getSettingValue("theme");
@@ -49,18 +34,16 @@ function hasClass(elem, className) {
4934
}
5035

5136
function addClass(elem, className) {
52-
if (!elem || !elem.classList) {
53-
return;
37+
if (elem && elem.classList) {
38+
elem.classList.add(className);
5439
}
55-
elem.classList.add(className);
5640
}
5741

5842
// eslint-disable-next-line no-unused-vars
5943
function removeClass(elem, className) {
60-
if (!elem || !elem.classList) {
61-
return;
44+
if (elem && elem.classList) {
45+
elem.classList.remove(className);
6246
}
63-
elem.classList.remove(className);
6447
}
6548

6649
/**
@@ -127,11 +110,7 @@ function getCurrentValue(name) {
127110
// Rust to the JS. If there is no such element, return null.
128111
const getVar = (function getVar(name) {
129112
const el = document.getElementById("rustdoc-vars");
130-
if (el) {
131-
return el.attributes["data-" + name].value;
132-
} else {
133-
return null;
134-
}
113+
return el ? el.attributes["data-" + name].value : null;
135114
});
136115

137116
function switchTheme(newThemeName, saveTheme) {
@@ -158,6 +137,9 @@ function switchTheme(newThemeName, saveTheme) {
158137
}
159138

160139
const updateTheme = (function() {
140+
// only listen to (prefers-color-scheme: dark) because light is the default
141+
const mql = window.matchMedia("(prefers-color-scheme: dark)");
142+
161143
/**
162144
* Update the current theme to match whatever the current combination of
163145
* * the preference for using the system theme
@@ -177,7 +159,7 @@ const updateTheme = (function() {
177159
const lightTheme = getSettingValue("preferred-light-theme") || "light";
178160
const darkTheme = getSettingValue("preferred-dark-theme") || "dark";
179161

180-
if (isDarkMode()) {
162+
if (mql.matches) {
181163
use(darkTheme, true);
182164
} else {
183165
// prefers a light theme, or has no preference
@@ -191,37 +173,7 @@ const updateTheme = (function() {
191173
}
192174
}
193175

194-
// This is always updated below to a function () => bool.
195-
let isDarkMode;
196-
197-
// Determine the function for isDarkMode, and if we have
198-
// `window.matchMedia`, set up an event listener on the preferred color
199-
// scheme.
200-
//
201-
// Otherwise, fall back to the prefers-color-scheme value CSS captured in
202-
// the "content" property.
203-
if (window.matchMedia) {
204-
// only listen to (prefers-color-scheme: dark) because light is the default
205-
const mql = window.matchMedia("(prefers-color-scheme: dark)");
206-
207-
isDarkMode = () => mql.matches;
208-
209-
if (mql.addEventListener) {
210-
mql.addEventListener("change", updateTheme);
211-
} else {
212-
// This is deprecated, see:
213-
// https://developer.mozilla.org/en-US/docs/Web/API/MediaQueryList/addListener
214-
mql.addListener(updateTheme);
215-
}
216-
} else {
217-
// fallback to the CSS computed value
218-
const cssContent = getComputedStyle(document.documentElement)
219-
.getPropertyValue("content");
220-
// (Note: the double-quotes come from that this is a CSS value, which
221-
// might be a length, string, etc.)
222-
const cssColorScheme = cssContent || "\"light\"";
223-
isDarkMode = () => (cssColorScheme === "\"dark\"");
224-
}
176+
mql.addEventListener("change", updateTheme);
225177

226178
return updateTheme;
227179
})();

0 commit comments

Comments
 (0)