diff --git a/src/librustdoc/html/static/js/main.js b/src/librustdoc/html/static/js/main.js
index 4150c5609a97e..a7ce2bf9048bf 100644
--- a/src/librustdoc/html/static/js/main.js
+++ b/src/librustdoc/html/static/js/main.js
@@ -1101,7 +1101,6 @@ function preLoadCss(cssUrl) {
});
}());
- // @ts-expect-error
window.rustdoc_add_line_numbers_to_examples = () => {
// @ts-expect-error
function generateLine(nb) {
@@ -1123,7 +1122,6 @@ function preLoadCss(cssUrl) {
});
};
- // @ts-expect-error
window.rustdoc_remove_line_numbers_from_examples = () => {
onEachLazy(
document.querySelectorAll(".rustdoc:not(.src) :not(.scraped-example) > .example-wrap"),
@@ -1132,7 +1130,6 @@ function preLoadCss(cssUrl) {
};
if (getSettingValue("line-numbers") === "true") {
- // @ts-expect-error
window.rustdoc_add_line_numbers_to_examples();
}
@@ -1596,7 +1593,7 @@ function preLoadCss(cssUrl) {
/**
* Hide popover menus, clickable tooltips, and the sidebar (if applicable).
*
- * Pass "true" to reset focus for tooltip popovers.
+ * Pass `true` to reset focus for tooltip popovers.
*/
window.hideAllModals = switchFocus => {
hideSidebar();
diff --git a/src/librustdoc/html/static/js/rustdoc.d.ts b/src/librustdoc/html/static/js/rustdoc.d.ts
index 91a58fab86eff..0d2e19e019f34 100644
--- a/src/librustdoc/html/static/js/rustdoc.d.ts
+++ b/src/librustdoc/html/static/js/rustdoc.d.ts
@@ -30,6 +30,8 @@ declare global {
currentCrate: string|null;
/**
* Hide popovers, tooltips, or the mobile sidebar.
+ *
+ * Pass `true` to reset focus for tooltip popovers.
*/
hideAllModals: function(boolean),
/**
@@ -78,6 +80,8 @@ declare global {
pending_implementors?: rustdoc.Implementors,
register_type_impls?: function(rustdoc.TypeImpls): void,
pending_type_impls?: rustdoc.TypeImpls,
+ rustdoc_add_line_numbers_to_examples?: function(),
+ rustdoc_remove_line_numbers_from_examples?: function(),
}
interface HTMLElement {
/** Used by the popover tooltip code. */
@@ -477,4 +481,14 @@ declare namespace rustdoc {
* is a tuple of (filename, subdirs, filenames).
*/
type Dir = [string, rustdoc.Dir[], string[]]
+
+ /**
+ * Indivitual setting object, used in `settings.js`
+ */
+ interface Setting {
+ js_name: string,
+ name: string,
+ options?: string[],
+ default: string | boolean,
+ }
}
diff --git a/src/librustdoc/html/static/js/settings.js b/src/librustdoc/html/static/js/settings.js
index 5f1bbd27328cb..2430b5829b2ba 100644
--- a/src/librustdoc/html/static/js/settings.js
+++ b/src/librustdoc/html/static/js/settings.js
@@ -1,22 +1,39 @@
// Local js definitions:
/* global getSettingValue, updateLocalStorage, updateTheme */
/* global addClass, removeClass, onEach, onEachLazy */
-/* global MAIN_ID, getVar, getSettingsButton, getHelpButton */
-
-// Eventually fix this.
-// @ts-nocheck
+/* global MAIN_ID, getVar, getSettingsButton, getHelpButton, nonnull */
"use strict";
(function() {
const isSettingsPage = window.location.pathname.endsWith("/settings.html");
+ /**
+ * @param {Element} elem
+ * @param {EventTarget|null} target
+ */
+ function elemContainsTarget(elem, target) {
+ if (target instanceof Node) {
+ return elem.contains(target);
+ } else {
+ return false;
+ }
+ }
+
+ /**
+ * @overload {"theme"|"preferred-dark-theme"|"preferred-light-theme"}
+ * @param {string} settingName
+ * @param {string} value
+ * @returns
+ * @param {string} settingName
+ * @param {string|boolean} value
+ */
function changeSetting(settingName, value) {
if (settingName === "theme") {
const useSystem = value === "system preference" ? "true" : "false";
updateLocalStorage("use-system-theme", useSystem);
}
- updateLocalStorage(settingName, value);
+ updateLocalStorage(settingName, "" + value);
switch (settingName) {
case "theme":
@@ -27,9 +44,15 @@
break;
case "line-numbers":
if (value === true) {
- window.rustdoc_add_line_numbers_to_examples();
+ const f = window.rustdoc_add_line_numbers_to_examples;
+ if (f !== undefined) {
+ f();
+ }
} else {
- window.rustdoc_remove_line_numbers_from_examples();
+ const f = window.rustdoc_remove_line_numbers_from_examples;
+ if (f !== undefined) {
+ f();
+ }
}
break;
case "hide-sidebar":
@@ -89,6 +112,9 @@
}
}
+ /**
+ * @param {HTMLElement} settingsElement
+ */
function setEvents(settingsElement) {
updateLightAndDark();
onEachLazy(settingsElement.querySelectorAll("input[type=\"checkbox\"]"), toggle => {
@@ -101,23 +127,27 @@
changeSetting(toggle.id, toggle.checked);
};
});
- onEachLazy(settingsElement.querySelectorAll("input[type=\"radio\"]"), elem => {
- const settingId = elem.name;
- let settingValue = getSettingValue(settingId);
- if (settingId === "theme") {
- const useSystem = getSettingValue("use-system-theme");
- if (useSystem === "true" || settingValue === null) {
- // "light" is the default theme
- settingValue = useSystem === "false" ? "light" : "system preference";
+ onEachLazy(
+ settingsElement.querySelectorAll("input[type=\"radio\"]"),
+ /** @param {HTMLInputElement} elem */
+ elem => {
+ const settingId = elem.name;
+ let settingValue = getSettingValue(settingId);
+ if (settingId === "theme") {
+ const useSystem = getSettingValue("use-system-theme");
+ if (useSystem === "true" || settingValue === null) {
+ // "light" is the default theme
+ settingValue = useSystem === "false" ? "light" : "system preference";
+ }
}
- }
- if (settingValue !== null && settingValue !== "null") {
- elem.checked = settingValue === elem.value;
- }
- elem.addEventListener("change", ev => {
- changeSetting(ev.target.name, ev.target.value);
- });
- });
+ if (settingValue !== null && settingValue !== "null") {
+ elem.checked = settingValue === elem.value;
+ }
+ elem.addEventListener("change", () => {
+ changeSetting(elem.name, elem.value);
+ });
+ },
+ );
}
/**
@@ -125,7 +155,7 @@
* as argument which describes each setting and how to render it. It returns a string
* representing the raw HTML.
*
- * @param {Array