From ab41e2b6dca05ba325f56f8af0eb811998197949 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Thu, 14 Sep 2023 12:56:15 -0700 Subject: [PATCH] rustdoc: avoid calling `document.write` after the page loads --- src/librustdoc/html/static/js/storage.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/librustdoc/html/static/js/storage.js b/src/librustdoc/html/static/js/storage.js index ec34f5e8bf9..c69641092ab 100644 --- a/src/librustdoc/html/static/js/storage.js +++ b/src/librustdoc/html/static/js/storage.js @@ -131,8 +131,18 @@ function switchTheme(newThemeName, saveTheme) { const newHref = getVar("root-path") + newThemeName + getVar("resource-suffix") + ".css"; if (!window.currentTheme) { - document.write(``); - window.currentTheme = document.getElementById("themeStyle"); + // 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; }