From c23a457e72abe608715ac76f076f47dc42af07a5 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Thu, 30 May 2024 20:31:44 +0200 Subject: Merging upstream version 1.74.1+dfsg1. Signed-off-by: Daniel Baumann --- src/librustdoc/html/static/js/storage.js | 36 ++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 11 deletions(-) (limited to 'src/librustdoc/html/static/js/storage.js') diff --git a/src/librustdoc/html/static/js/storage.js b/src/librustdoc/html/static/js/storage.js index af3ca42a6..c69641092 100644 --- a/src/librustdoc/html/static/js/storage.js +++ b/src/librustdoc/html/static/js/storage.js @@ -5,6 +5,7 @@ // the page, so we don't see major layout changes during the load of the page. "use strict"; +const builtinThemes = ["light", "dark", "ayu"]; const darkThemes = ["dark", "ayu"]; window.currentTheme = document.getElementById("themeStyle"); @@ -119,19 +120,32 @@ function switchTheme(newThemeName, saveTheme) { updateLocalStorage("theme", newThemeName); } - let newHref; + document.documentElement.setAttribute("data-theme", newThemeName); - if (newThemeName === "light" || newThemeName === "dark" || newThemeName === "ayu") { - newHref = getVar("static-root-path") + getVar("theme-" + newThemeName + "-css"); + if (builtinThemes.indexOf(newThemeName) !== -1) { + if (window.currentTheme) { + window.currentTheme.parentNode.removeChild(window.currentTheme); + window.currentTheme = null; + } } else { - newHref = getVar("root-path") + newThemeName + getVar("resource-suffix") + ".css"; - } - - if (!window.currentTheme) { - document.write(``); - window.currentTheme = document.getElementById("themeStyle"); - } else if (newHref !== window.currentTheme.href) { - window.currentTheme.href = newHref; + const newHref = getVar("root-path") + newThemeName + + getVar("resource-suffix") + ".css"; + if (!window.currentTheme) { + // If we're in the middle of loading, document.write blocks + // rendering, but if we are done, it would blank the page. + if (document.readyState === "loading") { + document.write(``); + window.currentTheme = document.getElementById("themeStyle"); + } else { + window.currentTheme = document.createElement("link"); + window.currentTheme.rel = "stylesheet"; + window.currentTheme.id = "themeStyle"; + window.currentTheme.href = newHref; + document.documentElement.appendChild(window.currentTheme); + } + } else if (newHref !== window.currentTheme.href) { + window.currentTheme.href = newHref; + } } } -- cgit v1.2.3