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;
31 if (reversed !== true) {
32 for (i = 0; i < length; ++i) {
33 if (func(arr[i]) === true) {
38 for (i = length - 1; i >= 0; --i) {
39 if (func(arr[i]) === true) {
48 function onEachLazy(lazyArray, func, reversed) {
50 Array.prototype.slice.call(lazyArray),
55 function hasOwnProperty(obj, property) {
56 return Object.prototype.hasOwnProperty.call(obj, property);
59 function usableLocalStorage() {
60 // Check if the browser supports localStorage at all:
61 if (typeof Storage === "undefined") {
64 // Check if we can access it; this access will fail if the browser
65 // preferences deny access to localStorage, e.g., to prevent storage of
66 // "cookies" (or cookie-likes, as is the case here).
68 return window.localStorage !== null && window.localStorage !== undefined;
70 // Storage is supported, but browser preferences deny access to it.
75 function updateLocalStorage(name, value) {
76 if (usableLocalStorage()) {
77 localStorage[name] = value;
79 // No Web Storage support so we do nothing
83 function getCurrentValue(name) {
84 if (usableLocalStorage() && localStorage[name] !== undefined) {
85 return localStorage[name];
90 function switchTheme(styleElem, mainStyleElem, newTheme, saveTheme) {
91 var fullBasicCss = "rustdoc" + resourcesSuffix + ".css";
92 var fullNewTheme = newTheme + resourcesSuffix + ".css";
93 var newHref = mainStyleElem.href.replace(fullBasicCss, fullNewTheme);
95 if (styleElem.href === newHref) {
100 if (savedHref.length === 0) {
101 onEachLazy(document.getElementsByTagName("link"), function(el) {
102 savedHref.push(el.href);
105 onEach(savedHref, function(el) {
106 if (el === newHref) {
111 if (found === true) {
112 styleElem.href = newHref;
113 // If this new value comes from a system setting or from the previously saved theme, no
115 if (saveTheme === true) {
116 updateLocalStorage("rustdoc-theme", newTheme);
121 function getSystemValue() {
122 var property = getComputedStyle(document.documentElement).getPropertyValue('content');
123 return property.replace(/[\"\']/g, "");
126 switchTheme(currentTheme, mainTheme,
127 getCurrentValue("rustdoc-theme") || getSystemValue() || "light",