Skip to content

Commit 5b61b61

Browse files
Allow to not switch to a theme if it doesn't exist
1 parent 2d3f31d commit 5b61b61

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

src/librustdoc/html/static/main.js

-8
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,6 @@
9696
}
9797
}
9898

99-
function onEach(arr, func) {
100-
if (arr && arr.length > 0 && func) {
101-
for (var i = 0; i < arr.length; i++) {
102-
func(arr[i]);
103-
}
104-
}
105-
}
106-
10799
function isHidden(elem) {
108100
return (elem.offsetParent === null)
109101
}

src/librustdoc/html/static/storage.js

+31-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@
1313
var currentTheme = document.getElementById("themeStyle");
1414
var mainTheme = document.getElementById("mainThemeStyle");
1515

16+
var savedHref = [];
17+
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) {
22+
break;
23+
}
24+
}
25+
}
26+
}
27+
1628
function updateLocalStorage(name, value) {
1729
if (typeof(Storage) !== "undefined") {
1830
localStorage[name] = value;
@@ -29,8 +41,24 @@ function getCurrentValue(name) {
2941
}
3042

3143
function switchTheme(styleElem, mainStyleElem, newTheme) {
32-
styleElem.href = mainStyleElem.href.replace("rustdoc.css", newTheme + ".css");
33-
updateLocalStorage('rustdoc-theme', newTheme);
44+
var newHref = mainStyleElem.href.replace("rustdoc.css", newTheme + ".css");
45+
var found = false;
46+
47+
if (savedHref.length === 0) {
48+
onEach(document.getElementsByTagName("link"), function(el) {
49+
savedHref.push(el.href);
50+
});
51+
}
52+
onEach(savedHref, function(el) {
53+
if (el === newHref) {
54+
found = true;
55+
return true;
56+
}
57+
});
58+
if (found === true) {
59+
styleElem.href = newHref;
60+
updateLocalStorage('rustdoc-theme', newTheme);
61+
}
3462
}
3563

36-
switchTheme(currentTheme, mainTheme, getCurrentValue('theme') || 'main');
64+
switchTheme(currentTheme, mainTheme, getCurrentValue('rustdoc-theme') || 'main');

0 commit comments

Comments
 (0)