Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rustdoc theme securities #48381

Merged
merged 3 commits into from
Feb 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kinda want to see a fallback to "main" here, in case there is something amiss in local storage? Right now it just... does nothing, if this function is called with a name that doesn't match any of the available themes. To the user, this is the same as defaulting to "main", but i'd like to see that codified here as well.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is already the case. The last imported style is the main one. So if we do nothing, the main one will remain.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. What i was really after was "overwrite invalid local storage value with 'main'" but i think i'll just let it slide, in case there's a collision with some other local storage key, or with different versions in case we add new themes later on.

styleElem.href = newHref;
updateLocalStorage('rustdoc-theme', newTheme);
}
}

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