From 27dcebeb3e88b97420a6d8f9279102892e27c775 Mon Sep 17 00:00:00 2001 From: Jacob Hoffman-Andrews Date: Tue, 14 Jun 2022 23:25:51 -0700 Subject: [PATCH] Improve loading of crates.js and sidebar-items.js Now that the "All Crates" dropdown is only rendered on the search results page, there is no need to load crates.js on most pages. Load it only on crate pages. Also, add the `defer` attribute so it does not block page rendering. For sidebar-items.js, move the script tag to ``. Since it already has the defer attribute it won't block loading. The defer attribute does preserve ordering between scripts, so instead of the callback on load, it can set a global variable on load, which is slightly simpler. Also, since it is required to finish rendering the page, beginning its load earlier is better. Remove generation and handling of sidebar-vars. Everything there can be computed with information available in JS via other means. Remove the "other" wrapper in the sidebar. It was unnecessary. Remove excess script fields --- src/librustdoc/html/layout.rs | 2 - src/librustdoc/html/render/context.rs | 6 +- src/librustdoc/html/render/mod.rs | 19 +-- src/librustdoc/html/render/write_shared.rs | 2 - src/librustdoc/html/sources.rs | 2 - src/librustdoc/html/static/js/main.js | 130 ++++++++---------- src/librustdoc/html/static/js/search.js | 5 +- .../html/static/js/source-script.js | 41 ++---- src/librustdoc/html/templates/page.html | 15 +- .../rustdoc-gui/duplicate-macro-reexport.goml | 6 +- src/test/rustdoc-gui/headings.goml | 4 +- 11 files changed, 91 insertions(+), 141 deletions(-) diff --git a/src/librustdoc/html/layout.rs b/src/librustdoc/html/layout.rs index de54347a0f74e..7d6d4b71e2eaf 100644 --- a/src/librustdoc/html/layout.rs +++ b/src/librustdoc/html/layout.rs @@ -31,8 +31,6 @@ pub(crate) struct Page<'a> { pub(crate) description: &'a str, pub(crate) keywords: &'a str, pub(crate) resource_suffix: &'a str, - pub(crate) extra_scripts: &'a [&'a str], - pub(crate) static_extra_scripts: &'a [&'a str], } impl<'a> Page<'a> { diff --git a/src/librustdoc/html/render/context.rs b/src/librustdoc/html/render/context.rs index 68e2f0cf9c06c..bfdc44c7e4534 100644 --- a/src/librustdoc/html/render/context.rs +++ b/src/librustdoc/html/render/context.rs @@ -211,8 +211,6 @@ impl<'tcx> Context<'tcx> { description: &desc, keywords: &keywords, resource_suffix: &clone_shared.resource_suffix, - extra_scripts: &[], - static_extra_scripts: &[], }; let mut page_buffer = Buffer::html(); print_item(self, it, &mut page_buffer, &page); @@ -568,8 +566,6 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> { description: "List of all items in this crate", keywords: BASIC_KEYWORDS, resource_suffix: &shared.resource_suffix, - extra_scripts: &[], - static_extra_scripts: &[], }; let sidebar = if shared.cache.crate_version.is_some() { format!("

Crate {}

", crate_name) @@ -693,7 +689,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> { else { unreachable!() }; let items = self.build_sidebar_items(module); let js_dst = self.dst.join(&format!("sidebar-items{}.js", self.shared.resource_suffix)); - let v = format!("initSidebarItems({});", serde_json::to_string(&items).unwrap()); + let v = format!("window.SIDEBAR_ITEMS = {};", serde_json::to_string(&items).unwrap()); self.shared.fs.write(js_dst, v)?; } Ok(()) diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index cb887d16906a1..3f426ee93e77e 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -56,7 +56,7 @@ use rustc_middle::middle::stability; use rustc_middle::ty; use rustc_middle::ty::TyCtxt; use rustc_span::{ - symbol::{kw, sym, Symbol}, + symbol::{sym, Symbol}, BytePos, FileName, RealFileName, }; use serde::ser::SerializeSeq; @@ -1738,8 +1738,6 @@ pub(crate) fn render_impl_summary( } fn print_sidebar(cx: &Context<'_>, it: &clean::Item, buffer: &mut Buffer) { - let parentlen = cx.current.len() - if it.is_mod() { 1 } else { 0 }; - if it.is_struct() || it.is_trait() || it.is_primitive() @@ -1800,21 +1798,6 @@ fn print_sidebar(cx: &Context<'_>, it: &clean::Item, buffer: &mut Buffer) { write!(buffer, "

In {}

", path); } - // Sidebar refers to the enclosing module, not this module. - let relpath = if it.is_mod() && parentlen != 0 { "./" } else { "" }; - write!( - buffer, - "
\ -
", - name = it.name.unwrap_or(kw::Empty), - ty = it.type_(), - path = relpath - ); - write!( - buffer, - "", - relpath, cx.shared.resource_suffix - ); // Closes sidebar-elems div. buffer.write_str(""); } diff --git a/src/librustdoc/html/render/write_shared.rs b/src/librustdoc/html/render/write_shared.rs index 9bddee199c7bf..27ad91d09e064 100644 --- a/src/librustdoc/html/render/write_shared.rs +++ b/src/librustdoc/html/render/write_shared.rs @@ -475,8 +475,6 @@ if (typeof exports !== 'undefined') {exports.searchIndex = searchIndex}; description: "List of crates", keywords: BASIC_KEYWORDS, resource_suffix: &shared.resource_suffix, - extra_scripts: &[], - static_extra_scripts: &[], }; let content = format!( diff --git a/src/librustdoc/html/sources.rs b/src/librustdoc/html/sources.rs index 524c90e1f4d64..1971d08e5bef5 100644 --- a/src/librustdoc/html/sources.rs +++ b/src/librustdoc/html/sources.rs @@ -203,8 +203,6 @@ impl SourceCollector<'_, '_> { description: &desc, keywords: BASIC_KEYWORDS, resource_suffix: &shared.resource_suffix, - extra_scripts: &[&format!("source-files{}", shared.resource_suffix)], - static_extra_scripts: &[&format!("source-script{}", shared.resource_suffix)], }; let v = layout::render( &shared.layout, diff --git a/src/librustdoc/html/static/js/main.js b/src/librustdoc/html/static/js/main.js index 414bca850e3dc..b320db9104612 100644 --- a/src/librustdoc/html/static/js/main.js +++ b/src/librustdoc/html/static/js/main.js @@ -66,26 +66,18 @@ function showMain() { (function() { window.rootPath = getVar("root-path"); window.currentCrate = getVar("current-crate"); - window.searchJS = resourcePath("search", ".js"); - window.searchIndexJS = resourcePath("search-index", ".js"); - window.settingsJS = resourcePath("settings", ".js"); - const sidebarVars = document.getElementById("sidebar-vars"); - if (sidebarVars) { - window.sidebarCurrent = { - name: sidebarVars.attributes["data-name"].value, - ty: sidebarVars.attributes["data-ty"].value, - relpath: sidebarVars.attributes["data-relpath"].value, - }; - // FIXME: It would be nicer to generate this text content directly in HTML, - // but with the current code it's hard to get the right information in the right place. - const mobileLocationTitle = document.querySelector(".mobile-topbar h2.location"); - const locationTitle = document.querySelector(".sidebar h2.location"); - if (mobileLocationTitle && locationTitle) { - mobileLocationTitle.innerHTML = locationTitle.innerHTML; - } - } }()); +function setMobileTopbar() { + // FIXME: It would be nicer to generate this text content directly in HTML, + // but with the current code it's hard to get the right information in the right place. + const mobileLocationTitle = document.querySelector(".mobile-topbar h2.location"); + const locationTitle = document.querySelector(".sidebar h2.location"); + if (mobileLocationTitle && locationTitle) { + mobileLocationTitle.innerHTML = locationTitle.innerHTML; + } +} + // Gets the human-readable string for the virtual-key code of the // given KeyboardEvent, ev. // @@ -227,7 +219,7 @@ function loadCss(cssFileName) { // Sending request for the CSS and the JS files at the same time so it will // hopefully be loaded when the JS will generate the settings content. loadCss("settings"); - loadScript(window.settingsJS); + loadScript(resourcePath("settings", ".js")); }; window.searchState = { @@ -304,8 +296,8 @@ function loadCss(cssFileName) { function loadSearch() { if (!searchLoaded) { searchLoaded = true; - loadScript(window.searchJS); - loadScript(window.searchIndexJS); + loadScript(resourcePath("search", ".js")); + loadScript(resourcePath("search-index", ".js")); } } @@ -485,40 +477,11 @@ function loadCss(cssFileName) { document.addEventListener("keypress", handleShortcut); document.addEventListener("keydown", handleShortcut); - // delayed sidebar rendering. - window.initSidebarItems = items => { - const sidebar = document.getElementsByClassName("sidebar-elems")[0]; - let others; - const current = window.sidebarCurrent; - - function addSidebarCrates(crates) { - if (!hasClass(document.body, "crate")) { - // We only want to list crates on the crate page. - return; - } - // Draw a convenient sidebar of known crates if we have a listing - const div = document.createElement("div"); - div.className = "block crate"; - div.innerHTML = "

Crates

"; - const ul = document.createElement("ul"); - div.appendChild(ul); - - for (const crate of crates) { - let klass = "crate"; - if (window.rootPath !== "./" && crate === window.currentCrate) { - klass += " current"; - } - const link = document.createElement("a"); - link.href = window.rootPath + crate + "/index.html"; - link.className = klass; - link.textContent = crate; - - const li = document.createElement("li"); - li.appendChild(link); - ul.appendChild(li); - } - others.appendChild(div); + function addSidebarItems() { + if (!window.SIDEBAR_ITEMS) { + return; } + const sidebar = document.getElementsByClassName("sidebar-elems")[0]; /** * Append to the sidebar a "block" of links - a heading along with a list (`