2 /* global resourcesSuffix */
4 var currentTheme = document.getElementById("themeStyle");
5 var mainTheme = document.getElementById("mainThemeStyle");
9 function hasClass(elem, className) {
10 return elem && elem.classList && elem.classList.contains(className);
13 function addClass(elem, className) {
14 if (!elem || !elem.classList) {
17 elem.classList.add(className);
20 function removeClass(elem, className) {
21 if (!elem || !elem.classList) {
24 elem.classList.remove(className);
27 function onEach(arr, func, reversed) {
28 if (arr && arr.length > 0 && func) {
29 var length = arr.length;
30 if (reversed !== true) {
31 for (var i = 0; i < length; ++i) {
32 if (func(arr[i]) === true) {
37 for (var i = length - 1; i >= 0; --i) {
38 if (func(arr[i]) === true) {
47 function onEachLazy(lazyArray, func, reversed) {
49 Array.prototype.slice.call(lazyArray),
54 function usableLocalStorage() {
55 // Check if the browser supports localStorage at all:
56 if (typeof Storage === "undefined") {
59 // Check if we can access it; this access will fail if the browser
60 // preferences deny access to localStorage, e.g., to prevent storage of
61 // "cookies" (or cookie-likes, as is the case here).
63 return window.localStorage !== null && window.localStorage !== undefined;
65 // Storage is supported, but browser preferences deny access to it.
70 function updateLocalStorage(name, value) {
71 if (usableLocalStorage()) {
72 localStorage[name] = value;
74 // No Web Storage support so we do nothing
78 function getCurrentValue(name) {
79 if (usableLocalStorage() && localStorage[name] !== undefined) {
80 return localStorage[name];
85 function switchTheme(styleElem, mainStyleElem, newTheme, saveTheme) {
86 var fullBasicCss = "rustdoc" + resourcesSuffix + ".css";
87 var fullNewTheme = newTheme + resourcesSuffix + ".css";
88 var newHref = mainStyleElem.href.replace(fullBasicCss, fullNewTheme);
90 if (styleElem.href === newHref) {
95 if (savedHref.length === 0) {
96 onEachLazy(document.getElementsByTagName("link"), function(el) {
97 savedHref.push(el.href);
100 onEach(savedHref, function(el) {
101 if (el === newHref) {
106 if (found === true) {
107 styleElem.href = newHref;
108 // If this new value comes from a system setting or from the previously saved theme, no
110 if (saveTheme === true) {
111 updateLocalStorage("rustdoc-theme", newTheme);
116 function getSystemValue() {
117 var property = getComputedStyle(document.documentElement).getPropertyValue('content');
118 return property.replace(/[\"\']/g, "");
121 switchTheme(currentTheme, mainTheme,
122 getCurrentValue("rustdoc-theme") || getSystemValue() || "light",