]> git.proxmox.com Git - rustc.git/blob - src/librustdoc/html/static/storage.js
New upstream version 1.41.1+dfsg1
[rustc.git] / src / librustdoc / html / static / storage.js
1 // From rust:
2 /* global resourcesSuffix */
3
4 var currentTheme = document.getElementById("themeStyle");
5 var mainTheme = document.getElementById("mainThemeStyle");
6
7 var savedHref = [];
8
9 function hasClass(elem, className) {
10 return elem && elem.classList && elem.classList.contains(className);
11 }
12
13 function addClass(elem, className) {
14 if (!elem || !elem.classList) {
15 return;
16 }
17 elem.classList.add(className);
18 }
19
20 function removeClass(elem, className) {
21 if (!elem || !elem.classList) {
22 return;
23 }
24 elem.classList.remove(className);
25 }
26
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) {
33 return true;
34 }
35 }
36 } else {
37 for (var i = length - 1; i >= 0; --i) {
38 if (func(arr[i]) === true) {
39 return true;
40 }
41 }
42 }
43 }
44 return false;
45 }
46
47 function onEachLazy(lazyArray, func, reversed) {
48 return onEach(
49 Array.prototype.slice.call(lazyArray),
50 func,
51 reversed);
52 }
53
54 function usableLocalStorage() {
55 // Check if the browser supports localStorage at all:
56 if (typeof Storage === "undefined") {
57 return false;
58 }
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).
62 try {
63 return window.localStorage !== null && window.localStorage !== undefined;
64 } catch(err) {
65 // Storage is supported, but browser preferences deny access to it.
66 return false;
67 }
68 }
69
70 function updateLocalStorage(name, value) {
71 if (usableLocalStorage()) {
72 localStorage[name] = value;
73 } else {
74 // No Web Storage support so we do nothing
75 }
76 }
77
78 function getCurrentValue(name) {
79 if (usableLocalStorage() && localStorage[name] !== undefined) {
80 return localStorage[name];
81 }
82 return null;
83 }
84
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);
89
90 if (styleElem.href === newHref) {
91 return;
92 }
93
94 var found = false;
95 if (savedHref.length === 0) {
96 onEachLazy(document.getElementsByTagName("link"), function(el) {
97 savedHref.push(el.href);
98 });
99 }
100 onEach(savedHref, function(el) {
101 if (el === newHref) {
102 found = true;
103 return true;
104 }
105 });
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
109 // need to save it.
110 if (saveTheme === true) {
111 updateLocalStorage("rustdoc-theme", newTheme);
112 }
113 }
114 }
115
116 function getSystemValue() {
117 var property = getComputedStyle(document.documentElement).getPropertyValue('content');
118 return property.replace(/[\"\']/g, "");
119 }
120
121 switchTheme(currentTheme, mainTheme,
122 getCurrentValue("rustdoc-theme") || getSystemValue() || "light",
123 false);