]>
git.proxmox.com Git - rustc.git/blob - src/librustdoc/html/static/storage.js
e10e330402f5edf3ca56fae7f527ab7bad8cdcb2
2 * Copyright 2018 The Rust Project Developers. See the COPYRIGHT
3 * file at the top-level directory of this distribution and at
4 * http://rust-lang.org/COPYRIGHT.
6 * Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
7 * http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
8 * <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
9 * option. This file may not be copied, modified, or distributed
10 * except according to those terms.
13 var currentTheme
= document
.getElementById("themeStyle");
14 var mainTheme
= document
.getElementById("mainThemeStyle");
18 function onEach(arr
, func
) {
19 if (arr
&& arr
.length
> 0 && func
) {
20 for (var i
= 0; i
< arr
.length
; i
++) {
21 if (func(arr
[i
]) === true) {
29 function usableLocalStorage() {
30 // Check if the browser supports localStorage at all:
31 if (typeof(Storage
) === "undefined") {
34 // Check if we can access it; this access will fail if the browser
35 // preferences deny access to localStorage, e.g., to prevent storage of
36 // "cookies" (or cookie-likes, as is the case here).
40 // Storage is supported, but browser preferences deny access to it.
47 function updateLocalStorage(name
, value
) {
48 if (usableLocalStorage()) {
49 localStorage
[name
] = value
;
51 // No Web Storage support so we do nothing
55 function getCurrentValue(name
) {
56 if (usableLocalStorage() && localStorage
[name
] !== undefined) {
57 return localStorage
[name
];
62 function switchTheme(styleElem
, mainStyleElem
, newTheme
) {
63 var fullBasicCss
= "rustdoc" + resourcesSuffix
+ ".css";
64 var fullNewTheme
= newTheme
+ resourcesSuffix
+ ".css";
65 var newHref
= mainStyleElem
.href
.replace(fullBasicCss
, fullNewTheme
);
67 if (styleElem
.href
=== newHref
) {
72 if (savedHref
.length
=== 0) {
73 onEach(document
.getElementsByTagName("link"), function(el
) {
74 savedHref
.push(el
.href
);
77 onEach(savedHref
, function(el
) {
84 styleElem
.href
= newHref
;
85 updateLocalStorage('rustdoc-theme', newTheme
);
89 switchTheme(currentTheme
, mainTheme
, getCurrentValue('rustdoc-theme') || 'light');