Skip to content

Commit 38d6899

Browse files
committed
Disable stylesheet objects rather than link elements
1 parent fd4d151 commit 38d6899

File tree

5 files changed

+33
-45
lines changed

5 files changed

+33
-45
lines changed

src/librustdoc/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ impl Options {
428428
))
429429
.emit();
430430
}
431-
themes.push(StylePath { path: theme_file, disabled: true });
431+
themes.push(StylePath { path: theme_file });
432432
}
433433
}
434434

src/librustdoc/html/layout.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ pub fn render<T: Print, S: Print>(
5050
<meta name=\"keywords\" content=\"{keywords}\">\
5151
<title>{title}</title>\
5252
<link rel=\"stylesheet\" type=\"text/css\" href=\"{static_root_path}normalize{suffix}.css\">\
53-
<link rel=\"stylesheet\" type=\"text/css\" href=\"{static_root_path}rustdoc{suffix}.css\" \
54-
id=\"mainThemeStyle\">\
53+
<link rel=\"stylesheet\" type=\"text/css\" href=\"{static_root_path}rustdoc{suffix}.css\">\
5554
{style_files}\
5655
<script src=\"{static_root_path}storage{suffix}.js\"></script>\
5756
<noscript><link rel=\"stylesheet\" href=\"{static_root_path}noscript{suffix}.css\"></noscript>\
@@ -171,17 +170,11 @@ pub fn render<T: Print, S: Print>(
171170
krate = layout.krate,
172171
style_files = style_files
173172
.iter()
174-
.filter_map(|t| {
175-
if let Some(stem) = t.path.file_stem() { Some((stem, t.disabled)) } else { None }
176-
})
177-
.filter_map(|t| {
178-
if let Some(path) = t.0.to_str() { Some((path, t.1)) } else { None }
179-
})
173+
.filter_map(|t| t.path.file_stem())
174+
.filter_map(|t| t.to_str())
180175
.map(|t| format!(
181-
r#"<link rel="stylesheet" type="text/css" href="{}.css" {} {}>"#,
182-
Escape(&format!("{}{}{}", static_root_path, t.0, page.resource_suffix)),
183-
if t.1 { "disabled" } else { "" },
184-
if t.0 == "light" { "id=\"themeStyle\"" } else { "" }
176+
r#"<link rel="stylesheet" type="text/css" href="{}.css">"#,
177+
Escape(&format!("{}{}{}", static_root_path, t, page.resource_suffix)),
185178
))
186179
.collect::<String>(),
187180
suffix = page.resource_suffix,

src/librustdoc/html/markdown.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,8 +1148,6 @@ pub struct IdMap {
11481148
fn init_id_map() -> FxHashMap<String, usize> {
11491149
let mut map = FxHashMap::default();
11501150
// This is the list of IDs used by rustdoc templates.
1151-
map.insert("mainThemeStyle".to_owned(), 1);
1152-
map.insert("themeStyle".to_owned(), 1);
11531151
map.insert("theme-picker".to_owned(), 1);
11541152
map.insert("theme-choices".to_owned(), 1);
11551153
map.insert("settings-menu".to_owned(), 1);

src/librustdoc/html/render/mod.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,6 @@ impl Serialize for TypeWithKind {
344344
pub struct StylePath {
345345
/// The path to the theme
346346
pub path: PathBuf,
347-
/// What the `disabled` attribute should be set to in the HTML tag
348-
pub disabled: bool,
349347
}
350348

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

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

569562
let mut style_files = self.shared.style_files.clone();
570563
let sidebar = "<p class='location'>Settings</p><div class='sidebar-elems'></div>";
571-
style_files.push(StylePath { path: PathBuf::from("settings.css"), disabled: false });
564+
style_files.push(StylePath { path: PathBuf::from("settings.css") });
572565
let v = layout::render(
573566
&self.shared.layout,
574567
&page,
@@ -808,7 +801,7 @@ themePicker.onblur = handleThemeButtonsBlur;
808801
var but = document.createElement('button');
809802
but.textContent = item;
810803
but.onclick = function(el) {{
811-
switchTheme(currentTheme, mainTheme, item, true);
804+
switchTheme(item, true);
812805
}};
813806
but.onblur = handleThemeButtonsBlur;
814807
themes.appendChild(but);
@@ -843,8 +836,11 @@ themePicker.onblur = handleThemeButtonsBlur;
843836
&cx.shared.fs,
844837
cx.path("storage.js"),
845838
&format!(
846-
"var resourcesSuffix = \"{}\";{}",
839+
r#"var resourcesSuffix = "{}";
840+
var allThemeNames = {};
841+
{}"#,
847842
cx.shared.resource_suffix,
843+
serde_json::to_string(&themes).unwrap(),
848844
static_files::STORAGE_JS
849845
),
850846
options.enable_minification,

src/librustdoc/html/static/storage.js

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// From rust:
22
/* global resourcesSuffix */
3-
4-
var currentTheme = document.getElementById("themeStyle");
5-
var mainTheme = document.getElementById("mainThemeStyle");
3+
/* global allThemeNames */
64

75
var savedHref = [];
86

@@ -87,29 +85,34 @@ function getCurrentValue(name) {
8785
return null;
8886
}
8987

90-
function switchTheme(styleElem, mainStyleElem, newTheme, saveTheme) {
91-
var fullBasicCss = "rustdoc" + resourcesSuffix + ".css";
92-
var fullNewTheme = newTheme + resourcesSuffix + ".css";
93-
var newHref = mainStyleElem.href.replace(fullBasicCss, fullNewTheme);
94-
95-
if (styleElem.href === newHref) {
96-
return;
97-
}
88+
function switchTheme(newTheme, saveTheme) {
89+
// The theme file we are switching to
90+
var newThemeFile = newTheme + resourcesSuffix + ".css";
9891

9992
var found = false;
10093
if (savedHref.length === 0) {
10194
onEachLazy(document.getElementsByTagName("link"), function(el) {
10295
savedHref.push(el.href);
10396
});
10497
}
105-
onEach(savedHref, function(el) {
106-
if (el === newHref) {
98+
onEach(savedHref, function(href) {
99+
if (href.endsWith(newThemeFile)) {
107100
found = true;
108101
return true;
109102
}
110103
});
111104
if (found === true) {
112-
styleElem.href = newHref;
105+
onEach(allThemeNames, function(themeName) {
106+
// The theme file for this theme name
107+
var themeFile = themeName + resourcesSuffix + ".css";
108+
var themeSheet = document.querySelector("[href$='" + themeFile + "']");
109+
110+
if (themeName === newTheme) {
111+
themeSheet.disabled = false;
112+
} else {
113+
themeSheet.disabled = true;
114+
}
115+
});
113116
// If this new value comes from a system setting or from the previously saved theme, no
114117
// need to save it.
115118
if (saveTheme === true) {
@@ -123,6 +126,4 @@ function getSystemValue() {
123126
return property.replace(/[\"\']/g, "");
124127
}
125128

126-
switchTheme(currentTheme, mainTheme,
127-
getCurrentValue("rustdoc-theme") || getSystemValue() || "light",
128-
false);
129+
switchTheme(getCurrentValue("rustdoc-theme") || getSystemValue() || "light", false);

0 commit comments

Comments
 (0)