Repository: rust-lang/mdBook (language: rust)

Issue:
Theme popup not opening when localStorage has an invalid theme id stored
### Problem

If `mdbook-theme` in localStorage contains a theme id that's not in the book, an uncaught exception causes theme popup to not function properly

### Steps

1. Create a new project with `--theme`, build and serve the book
2. Switch the theme so `mdbook-theme` gets set in localStorage (may not be needed?)
3. Edit `theme/index.hbs`, find `<ul id="theme-list"` and remove/change the `id` of the button so that the saved theme is no longer in the list
4. Rebuild the book
5. Observe that clicking on the theme button no longer opens the menu


### Possible Solution(s)

The root cause is `updateThemeSelected()` throws if the selected theme is not in the DOM

I think the cleanest fix is to update `get_theme()` to return `default_theme` if the saved theme is invalid
```js
    // cache the valid theme ids
    var themeIds = [];
    themePopup.querySelectorAll('button.theme').forEach(function (el) {
        themeIds.push(el.id);
    });

   // ...

    function get_theme() {
        var theme;
        try { theme = localStorage.getItem('mdbook-theme'); } catch (e) { }
        if (theme === null || theme === undefined || !themeIds.includes(theme)) { // <- add this check
            return default_theme;
        } else {
            return theme;
        }
    }
```
Verified that the solution works

### Notes

_No response_

### Version

mdbook v0.4.40


Discussion / hints:
@ehuss pinging according to the contribution guide. Should I open a PR for this fix?
Seems like a reasonable fix to me, would appreciate a PR!

Fix patch:
diff --git a/src/theme/book.js b/src/theme/book.js
index 18b8f3a4d9..02b1908ddc 100644
--- a/src/theme/book.js
+++ b/src/theme/book.js
@@ -289,6 +289,10 @@ function playground_text(playground, hidden = true) {
     var themeToggleButton = document.getElementById('theme-toggle');
     var themePopup = document.getElementById('theme-list');
     var themeColorMetaTag = document.querySelector('meta[name="theme-color"]');
+    var themeIds = [];
+    themePopup.querySelectorAll('button.theme').forEach(function (el) {
+        themeIds.push(el.id);
+    });
     var stylesheets = {
         ayuHighlight: document.querySelector("[href$='ayu-highlight.css']"),
         tomorrowNight: document.querySelector("[href$='tomorrow-night.css']"),
@@ -317,7 +321,7 @@ function playground_text(playground, hidden = true) {
     function get_theme() {
         var theme;
         try { theme = localStorage.getItem('mdbook-theme'); } catch (e) { }
-        if (theme === null || theme === undefined) {
+        if (theme === null || theme === undefined || !themeIds.includes(theme)) {
             return default_theme;
         } else {
             return theme;


Test patch: