diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js
index f688be89beebc..ba2e58eb47576 100644
--- a/src/librustdoc/html/static/main.js
+++ b/src/librustdoc/html/static/main.js
@@ -96,14 +96,6 @@
}
}
- function onEach(arr, func) {
- if (arr && arr.length > 0 && func) {
- for (var i = 0; i < arr.length; i++) {
- func(arr[i]);
- }
- }
- }
-
function isHidden(elem) {
return (elem.offsetParent === null)
}
diff --git a/src/librustdoc/html/static/rustdoc.css b/src/librustdoc/html/static/rustdoc.css
index cd4f2cfa678e6..a6630902c9794 100644
--- a/src/librustdoc/html/static/rustdoc.css
+++ b/src/librustdoc/html/static/rustdoc.css
@@ -1189,6 +1189,10 @@ kbd {
top: 19px;
}
+.theme-picker button {
+ outline: none;
+}
+
#theme-picker {
padding: 4px;
width: 27px;
diff --git a/src/librustdoc/html/static/storage.js b/src/librustdoc/html/static/storage.js
index 0aa1065b3786a..f21dfc8af9298 100644
--- a/src/librustdoc/html/static/storage.js
+++ b/src/librustdoc/html/static/storage.js
@@ -13,6 +13,18 @@
var currentTheme = document.getElementById("themeStyle");
var mainTheme = document.getElementById("mainThemeStyle");
+var savedHref = [];
+
+function onEach(arr, func) {
+ if (arr && arr.length > 0 && func) {
+ for (var i = 0; i < arr.length; i++) {
+ if (func(arr[i]) === true) {
+ break;
+ }
+ }
+ }
+}
+
function updateLocalStorage(name, value) {
if (typeof(Storage) !== "undefined") {
localStorage[name] = value;
@@ -29,8 +41,24 @@ function getCurrentValue(name) {
}
function switchTheme(styleElem, mainStyleElem, newTheme) {
- styleElem.href = mainStyleElem.href.replace("rustdoc.css", newTheme + ".css");
- updateLocalStorage('theme', newTheme);
+ var newHref = mainStyleElem.href.replace("rustdoc.css", newTheme + ".css");
+ var found = false;
+
+ if (savedHref.length === 0) {
+ onEach(document.getElementsByTagName("link"), function(el) {
+ savedHref.push(el.href);
+ });
+ }
+ onEach(savedHref, function(el) {
+ if (el === newHref) {
+ found = true;
+ return true;
+ }
+ });
+ if (found === true) {
+ styleElem.href = newHref;
+ updateLocalStorage('rustdoc-theme', newTheme);
+ }
}
-switchTheme(currentTheme, mainTheme, getCurrentValue('theme') || 'main');
+switchTheme(currentTheme, mainTheme, getCurrentValue('rustdoc-theme') || 'main');