Skip to content

Commit 01bb215

Browse files
authored
Rollup merge of #110030 - notriddle:notriddle/clean-up-js, r=GuillaumeGomez
rustdoc: clean up JS * Stop checking `func` in `onEach`. It's always hard-coded right at the call site, so there's no point. * Use the ternary operator in a few spots where it makes sense. * No point in making `onEach` store `arr.length` in a variable if it's only used once anyway.
2 parents 131211a + 815f5bb commit 01bb215

File tree

3 files changed

+9
-28
lines changed

3 files changed

+9
-28
lines changed

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

+2-8
Original file line numberDiff line numberDiff line change
@@ -332,13 +332,7 @@ function preLoadCss(cssUrl) {
332332
};
333333

334334
function getPageId() {
335-
if (window.location.hash) {
336-
const tmp = window.location.hash.replace(/^#/, "");
337-
if (tmp.length > 0) {
338-
return tmp;
339-
}
340-
}
341-
return null;
335+
return window.location.hash.replace(/^#/, "");
342336
}
343337

344338
const toggleAllDocsId = "toggle-all-docs";
@@ -707,7 +701,7 @@ function preLoadCss(cssUrl) {
707701
});
708702

709703
const pageId = getPageId();
710-
if (pageId !== null) {
704+
if (pageId !== "") {
711705
expandSection(pageId);
712706
}
713707
}());

src/librustdoc/html/static/js/settings.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,8 @@
8686
if (settingId === "theme") {
8787
const useSystem = getSettingValue("use-system-theme");
8888
if (useSystem === "true" || settingValue === null) {
89-
if (useSystem !== "false") {
90-
settingValue = "system preference";
91-
} else {
92-
// This is the default theme.
93-
settingValue = "light";
94-
}
89+
// "light" is the default theme
90+
settingValue = useSystem === "false" ? "light" : "system preference";
9591
}
9692
}
9793
if (settingValue !== null && settingValue !== "null") {

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

+5-14
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,9 @@ function removeClass(elem, className) {
5353
* @param {boolean} [reversed] - Whether to iterate in reverse
5454
*/
5555
function onEach(arr, func, reversed) {
56-
if (arr && arr.length > 0 && func) {
56+
if (arr && arr.length > 0) {
5757
if (reversed) {
58-
const length = arr.length;
59-
for (let i = length - 1; i >= 0; --i) {
58+
for (let i = arr.length - 1; i >= 0; --i) {
6059
if (func(arr[i])) {
6160
return true;
6261
}
@@ -150,26 +149,18 @@ const updateTheme = (function() {
150149
* … dictates that it should be.
151150
*/
152151
function updateTheme() {
153-
const use = (theme, saveTheme) => {
154-
switchTheme(theme, saveTheme);
155-
};
156-
157152
// maybe the user has disabled the setting in the meantime!
158153
if (getSettingValue("use-system-theme") !== "false") {
159154
const lightTheme = getSettingValue("preferred-light-theme") || "light";
160155
const darkTheme = getSettingValue("preferred-dark-theme") || "dark";
161156

162-
if (mql.matches) {
163-
use(darkTheme, true);
164-
} else {
165-
// prefers a light theme, or has no preference
166-
use(lightTheme, true);
167-
}
157+
// use light theme if user prefers it, or has no preference
158+
switchTheme(mql.matches ? darkTheme : lightTheme, true);
168159
// note: we save the theme so that it doesn't suddenly change when
169160
// the user disables "use-system-theme" and reloads the page or
170161
// navigates to another page
171162
} else {
172-
use(getSettingValue("theme"), false);
163+
switchTheme(getSettingValue("theme"), false);
173164
}
174165
}
175166

0 commit comments

Comments
 (0)