Skip to content

Commit

Permalink
Rollup merge of rust-lang#48381 - GuillaumeGomez:rustdoc-theme-securi…
Browse files Browse the repository at this point in the history
…ties, r=QuietMisdreavus

Rustdoc theme securities rust-lang#48381

Fixes rust-lang#48375.
Fixes rust-lang#48376.
  • Loading branch information
kennytm committed Feb 27, 2018
2 parents 5cd8eb2 + 8d51c33 commit 8bdad67
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
8 changes: 0 additions & 8 deletions src/librustdoc/html/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,6 @@
}
}

function onEach(arr, func) {
if (arr && arr.length > 0 && func) {
for (var i = 0; i < arr.length; i++) {
func(arr[i]);
}
}
}

function isHidden(elem) {
return (elem.offsetParent === null)
}
Expand Down
4 changes: 4 additions & 0 deletions src/librustdoc/html/static/rustdoc.css
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,10 @@ kbd {
top: 19px;
}

.theme-picker button {
outline: none;
}

#theme-picker {
padding: 4px;
width: 27px;
Expand Down
34 changes: 31 additions & 3 deletions src/librustdoc/html/static/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@
var currentTheme = document.getElementById("themeStyle");
var mainTheme = document.getElementById("mainThemeStyle");

var savedHref = [];

function onEach(arr, func) {
if (arr && arr.length > 0 && func) {
for (var i = 0; i < arr.length; i++) {
if (func(arr[i]) === true) {
break;
}
}
}
}

function updateLocalStorage(name, value) {
if (typeof(Storage) !== "undefined") {
localStorage[name] = value;
Expand All @@ -29,8 +41,24 @@ function getCurrentValue(name) {
}

function switchTheme(styleElem, mainStyleElem, newTheme) {
styleElem.href = mainStyleElem.href.replace("rustdoc.css", newTheme + ".css");
updateLocalStorage('theme', newTheme);
var newHref = mainStyleElem.href.replace("rustdoc.css", newTheme + ".css");
var found = false;

if (savedHref.length === 0) {
onEach(document.getElementsByTagName("link"), function(el) {
savedHref.push(el.href);
});
}
onEach(savedHref, function(el) {
if (el === newHref) {
found = true;
return true;
}
});
if (found === true) {
styleElem.href = newHref;
updateLocalStorage('rustdoc-theme', newTheme);
}
}

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

0 comments on commit 8bdad67

Please sign in to comment.