diff options
| author | Michael Howell <michael@notriddle.com> | 2023-09-14 12:56:15 -0700 |
|---|---|---|
| committer | Michael Howell <michael@notriddle.com> | 2023-09-15 07:40:17 -0700 |
| commit | ab41e2b6dca05ba325f56f8af0eb811998197949 (patch) | |
| tree | 9ca63524cd863d767dc420934e6bc1d616a325a2 | |
| parent | 7e86fd61e83717fc23b6141d2ba728aefbe5e168 (diff) | |
| download | rust-ab41e2b6dca05ba325f56f8af0eb811998197949.tar.gz rust-ab41e2b6dca05ba325f56f8af0eb811998197949.zip | |
rustdoc: avoid calling `document.write` after the page loads
| -rw-r--r-- | src/librustdoc/html/static/js/storage.js | 14 |
1 files 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(`<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; } |
