Skip to content

Commit

Permalink
rustdoc: avoid calling document.write after the page loads
Browse files Browse the repository at this point in the history
  • Loading branch information
notriddle committed Sep 15, 2023
1 parent 7e86fd6 commit ab41e2b
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/librustdoc/html/static/js/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,18 @@ function switchTheme(newThemeName, saveTheme) {
const newHref = getVar("root-path") + newThemeName +
getVar("resource-suffix") + ".css";
if (!window.currentTheme) {
document.write(`<link rel="stylesheet" id="themeStyle" href="${newHref}">`);
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(`<link rel="stylesheet" id="themeStyle" href="${newHref}">`);
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;
}
Expand Down

0 comments on commit ab41e2b

Please sign in to comment.