From 38d68996532fd108d8b603aaf3530e349ee3fed7 Mon Sep 17 00:00:00 2001 From: Jarek Samic Date: Sun, 2 Aug 2020 13:01:58 -0400 Subject: [PATCH 1/3] Disable stylesheet objects rather than link elements --- src/librustdoc/config.rs | 2 +- src/librustdoc/html/layout.rs | 17 ++++--------- src/librustdoc/html/markdown.rs | 2 -- src/librustdoc/html/render/mod.rs | 22 +++++++---------- src/librustdoc/html/static/storage.js | 35 ++++++++++++++------------- 5 files changed, 33 insertions(+), 45 deletions(-) diff --git a/src/librustdoc/config.rs b/src/librustdoc/config.rs index 4f751decc8090..b9aba11f73015 100644 --- a/src/librustdoc/config.rs +++ b/src/librustdoc/config.rs @@ -428,7 +428,7 @@ impl Options { )) .emit(); } - themes.push(StylePath { path: theme_file, disabled: true }); + themes.push(StylePath { path: theme_file }); } } diff --git a/src/librustdoc/html/layout.rs b/src/librustdoc/html/layout.rs index cc6b38ebcdb7f..014bd8b2e0e93 100644 --- a/src/librustdoc/html/layout.rs +++ b/src/librustdoc/html/layout.rs @@ -50,8 +50,7 @@ pub fn render( \ {title}\ \ - \ + \ {style_files}\ \ \ @@ -171,17 +170,11 @@ pub fn render( 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#""#, - Escape(&format!("{}{}{}", static_root_path, t.0, page.resource_suffix)), - if t.1 { "disabled" } else { "" }, - if t.0 == "light" { "id=\"themeStyle\"" } else { "" } + r#""#, + Escape(&format!("{}{}{}", static_root_path, t, page.resource_suffix)), )) .collect::(), suffix = page.resource_suffix, diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs index 4cfd81ffbce9d..cdf0fe48d79fb 100644 --- a/src/librustdoc/html/markdown.rs +++ b/src/librustdoc/html/markdown.rs @@ -1148,8 +1148,6 @@ pub struct IdMap { fn init_id_map() -> FxHashMap { 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); diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 5fb2d9f6f917c..9b01f616c1121 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -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 = Cell::new(0)); @@ -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)?; @@ -568,7 +561,7 @@ impl FormatRenderer for Context { let mut style_files = self.shared.style_files.clone(); let sidebar = "

Settings

"; - 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, @@ -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); @@ -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, diff --git a/src/librustdoc/html/static/storage.js b/src/librustdoc/html/static/storage.js index 0a2fae274fa87..84c44d500901d 100644 --- a/src/librustdoc/html/static/storage.js +++ b/src/librustdoc/html/static/storage.js @@ -1,8 +1,6 @@ // From rust: /* global resourcesSuffix */ - -var currentTheme = document.getElementById("themeStyle"); -var mainTheme = document.getElementById("mainThemeStyle"); +/* global allThemeNames */ var savedHref = []; @@ -87,14 +85,9 @@ 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) { @@ -102,14 +95,24 @@ function switchTheme(styleElem, mainStyleElem, newTheme, saveTheme) { 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 + "']"); + + if (themeName === newTheme) { + themeSheet.disabled = false; + } else { + themeSheet.disabled = true; + } + }); // If this new value comes from a system setting or from the previously saved theme, no // need to save it. if (saveTheme === true) { @@ -123,6 +126,4 @@ function getSystemValue() { return property.replace(/[\"\']/g, ""); } -switchTheme(currentTheme, mainTheme, - getCurrentValue("rustdoc-theme") || getSystemValue() || "light", - false); +switchTheme(getCurrentValue("rustdoc-theme") || getSystemValue() || "light", false); From eea0e39b223f4296e63ea586aa5c2a1b63cf8aad Mon Sep 17 00:00:00 2001 From: Jarek Samic Date: Sun, 2 Aug 2020 13:48:24 -0400 Subject: [PATCH 2/3] tidy fix --- src/librustdoc/html/static/storage.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustdoc/html/static/storage.js b/src/librustdoc/html/static/storage.js index 84c44d500901d..4f6cce236703f 100644 --- a/src/librustdoc/html/static/storage.js +++ b/src/librustdoc/html/static/storage.js @@ -106,7 +106,7 @@ function switchTheme(newTheme, saveTheme) { // The theme file for this theme name var themeFile = themeName + resourcesSuffix + ".css"; var themeSheet = document.querySelector("[href$='" + themeFile + "']"); - + if (themeName === newTheme) { themeSheet.disabled = false; } else { From c4d0a1b3f640fbfa087161dd806f7490654104ed Mon Sep 17 00:00:00 2001 From: Jarek Samic Date: Mon, 3 Aug 2020 08:46:46 -0400 Subject: [PATCH 3/3] Apply suggestion --- src/librustdoc/html/static/storage.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/librustdoc/html/static/storage.js b/src/librustdoc/html/static/storage.js index 4f6cce236703f..8b3ae3bec2ef3 100644 --- a/src/librustdoc/html/static/storage.js +++ b/src/librustdoc/html/static/storage.js @@ -107,11 +107,7 @@ function switchTheme(newTheme, saveTheme) { var themeFile = themeName + resourcesSuffix + ".css"; var themeSheet = document.querySelector("[href$='" + themeFile + "']"); - if (themeName === newTheme) { - themeSheet.disabled = false; - } else { - themeSheet.disabled = true; - } + themeSheet.disabled = themeName !== newTheme; }); // If this new value comes from a system setting or from the previously saved theme, no // need to save it.