Skip to content

Disable stylesheet objects rather than link elements #75063

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

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 1 addition & 1 deletion src/librustdoc/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ impl Options {
))
.emit();
}
themes.push(StylePath { path: theme_file, disabled: true });
themes.push(StylePath { path: theme_file });
}
}

Expand Down
17 changes: 5 additions & 12 deletions src/librustdoc/html/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ pub fn render<T: Print, S: Print>(
<meta name=\"keywords\" content=\"{keywords}\">\
<title>{title}</title>\
<link rel=\"stylesheet\" type=\"text/css\" href=\"{static_root_path}normalize{suffix}.css\">\
<link rel=\"stylesheet\" type=\"text/css\" href=\"{static_root_path}rustdoc{suffix}.css\" \
id=\"mainThemeStyle\">\
<link rel=\"stylesheet\" type=\"text/css\" href=\"{static_root_path}rustdoc{suffix}.css\">\
{style_files}\
<script src=\"{static_root_path}storage{suffix}.js\"></script>\
<noscript><link rel=\"stylesheet\" href=\"{static_root_path}noscript{suffix}.css\"></noscript>\
Expand Down Expand Up @@ -171,17 +170,11 @@ pub fn render<T: Print, S: Print>(
krate = layout.krate,
style_files = style_files
.iter()
.filter_map(|t| {
if let Some(stem) = t.path.file_stem() { Some((stem, t.disabled)) } else { None }
})
.filter_map(|t| {
if let Some(path) = t.0.to_str() { Some((path, t.1)) } else { None }
})
.filter_map(|t| t.path.file_stem())
.filter_map(|t| t.to_str())
.map(|t| format!(
r#"<link rel="stylesheet" type="text/css" href="{}.css" {} {}>"#,
Escape(&format!("{}{}{}", static_root_path, t.0, page.resource_suffix)),
if t.1 { "disabled" } else { "" },
if t.0 == "light" { "id=\"themeStyle\"" } else { "" }
r#"<link rel="stylesheet" type="text/css" href="{}.css">"#,
Escape(&format!("{}{}{}", static_root_path, t, page.resource_suffix)),
))
.collect::<String>(),
suffix = page.resource_suffix,
Expand Down
2 changes: 0 additions & 2 deletions src/librustdoc/html/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1148,8 +1148,6 @@ pub struct IdMap {
fn init_id_map() -> FxHashMap<String, usize> {
let mut map = FxHashMap::default();
// This is the list of IDs used by rustdoc templates.
map.insert("mainThemeStyle".to_owned(), 1);
map.insert("themeStyle".to_owned(), 1);
map.insert("theme-picker".to_owned(), 1);
map.insert("theme-choices".to_owned(), 1);
map.insert("settings-menu".to_owned(), 1);
Expand Down
22 changes: 9 additions & 13 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,6 @@ impl Serialize for TypeWithKind {
pub struct StylePath {
/// The path to the theme
pub path: PathBuf,
/// What the `disabled` attribute should be set to in the HTML tag
pub disabled: bool,
}

thread_local!(pub static CURRENT_DEPTH: Cell<usize> = Cell::new(0));
Expand Down Expand Up @@ -472,14 +470,9 @@ impl FormatRenderer for Context {
//
// Note that these must be added before `sources::render` is called
// so that the resulting source pages are styled
//
// `light.css` is not disabled because it is the stylesheet that stays loaded
// by the browser as the theme stylesheet. The theme system (hackily) works by
// changing the href to this stylesheet. All other themes are disabled to
// prevent rule conflicts
scx.style_files.push(StylePath { path: PathBuf::from("light.css"), disabled: false });
scx.style_files.push(StylePath { path: PathBuf::from("dark.css"), disabled: true });
scx.style_files.push(StylePath { path: PathBuf::from("ayu.css"), disabled: true });
scx.style_files.push(StylePath { path: PathBuf::from("light.css") });
scx.style_files.push(StylePath { path: PathBuf::from("dark.css") });
scx.style_files.push(StylePath { path: PathBuf::from("ayu.css") });

let dst = output;
scx.ensure_dir(&dst)?;
Expand Down Expand Up @@ -568,7 +561,7 @@ impl FormatRenderer for Context {

let mut style_files = self.shared.style_files.clone();
let sidebar = "<p class='location'>Settings</p><div class='sidebar-elems'></div>";
style_files.push(StylePath { path: PathBuf::from("settings.css"), disabled: false });
style_files.push(StylePath { path: PathBuf::from("settings.css") });
let v = layout::render(
&self.shared.layout,
&page,
Expand Down Expand Up @@ -808,7 +801,7 @@ themePicker.onblur = handleThemeButtonsBlur;
var but = document.createElement('button');
but.textContent = item;
but.onclick = function(el) {{
switchTheme(currentTheme, mainTheme, item, true);
switchTheme(item, true);
}};
but.onblur = handleThemeButtonsBlur;
themes.appendChild(but);
Expand Down Expand Up @@ -843,8 +836,11 @@ themePicker.onblur = handleThemeButtonsBlur;
&cx.shared.fs,
cx.path("storage.js"),
&format!(
"var resourcesSuffix = \"{}\";{}",
r#"var resourcesSuffix = "{}";
var allThemeNames = {};
{}"#,
cx.shared.resource_suffix,
serde_json::to_string(&themes).unwrap(),
static_files::STORAGE_JS
),
options.enable_minification,
Expand Down
31 changes: 14 additions & 17 deletions src/librustdoc/html/static/storage.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// From rust:
/* global resourcesSuffix */

var currentTheme = document.getElementById("themeStyle");
var mainTheme = document.getElementById("mainThemeStyle");
/* global allThemeNames */

var savedHref = [];

Expand Down Expand Up @@ -87,29 +85,30 @@ function getCurrentValue(name) {
return null;
}

function switchTheme(styleElem, mainStyleElem, newTheme, saveTheme) {
var fullBasicCss = "rustdoc" + resourcesSuffix + ".css";
var fullNewTheme = newTheme + resourcesSuffix + ".css";
var newHref = mainStyleElem.href.replace(fullBasicCss, fullNewTheme);

if (styleElem.href === newHref) {
return;
}
function switchTheme(newTheme, saveTheme) {
// The theme file we are switching to
var newThemeFile = newTheme + resourcesSuffix + ".css";

var found = false;
if (savedHref.length === 0) {
onEachLazy(document.getElementsByTagName("link"), function(el) {
savedHref.push(el.href);
});
}
onEach(savedHref, function(el) {
if (el === newHref) {
onEach(savedHref, function(href) {
if (href.endsWith(newThemeFile)) {
found = true;
return true;
}
});
if (found === true) {
styleElem.href = newHref;
onEach(allThemeNames, function(themeName) {
// The theme file for this theme name
var themeFile = themeName + resourcesSuffix + ".css";
var themeSheet = document.querySelector("[href$='" + themeFile + "']");

themeSheet.disabled = themeName !== newTheme;
});
// If this new value comes from a system setting or from the previously saved theme, no
// need to save it.
if (saveTheme === true) {
Expand All @@ -123,6 +122,4 @@ function getSystemValue() {
return property.replace(/[\"\']/g, "");
}

switchTheme(currentTheme, mainTheme,
getCurrentValue("rustdoc-theme") || getSystemValue() || "light",
false);
switchTheme(getCurrentValue("rustdoc-theme") || getSystemValue() || "light", false);