about summary refs log tree commit diff
diff options
context:
space:
mode:
authorkennytm <kennytm@gmail.com>2018-02-28 19:15:31 +0800
committerGitHub <noreply@github.com>2018-02-28 19:15:31 +0800
commit71ce38eb9e9ee34f9e244f7bb57ee51d9a5a6140 (patch)
treedf46713bddcbe56a6769c556235eb4538e285f71
parentc599463cd165d7ec01ba37c6979d1e7109cf9d3a (diff)
parent8d51c331c759210ba320c5662ab3ce3af5e0500b (diff)
downloadrust-71ce38eb9e9ee34f9e244f7bb57ee51d9a5a6140.tar.gz
rust-71ce38eb9e9ee34f9e244f7bb57ee51d9a5a6140.zip
Rollup merge of #48381 - GuillaumeGomez:rustdoc-theme-securities, r=QuietMisdreavus
Rustdoc theme securities

Fixes #48375.
Fixes #48376.

r? @steveklabnik
cc @QuietMisdreavus
-rw-r--r--src/librustdoc/html/static/main.js8
-rw-r--r--src/librustdoc/html/static/rustdoc.css4
-rw-r--r--src/librustdoc/html/static/storage.js34
3 files changed, 35 insertions, 11 deletions
diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js
index aa09e93e61a..960f2f198d8 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 d91e0679ed0..b70dc37fdd5 100644
--- a/src/librustdoc/html/static/rustdoc.css
+++ b/src/librustdoc/html/static/rustdoc.css
@@ -1202,6 +1202,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 0aa1065b378..f21dfc8af92 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');