From 1f4435a5940da9c54e23fd863feee99038a8f2c9 Mon Sep 17 00:00:00 2001 From: binarycat Date: Thu, 5 Jun 2025 18:42:54 -0500 Subject: [PATCH 1/2] rustdoc: make srcIndex no longer a global variable this is one-time initialization data, it can just be a function parameter. we also move the json parsing into createSrcSidebar to save a few bytes. --- src/librustdoc/html/render/write_shared.rs | 6 +----- .../html/render/write_shared/tests.rs | 18 +++--------------- src/librustdoc/html/static/js/rustdoc.d.ts | 6 ++---- src/librustdoc/html/static/js/src-script.js | 10 ++++++---- tests/rustdoc-gui/globals.goml | 3 --- 5 files changed, 12 insertions(+), 31 deletions(-) diff --git a/src/librustdoc/html/render/write_shared.rs b/src/librustdoc/html/render/write_shared.rs index 4f6e9abdbca06..7fd600f142eb4 100644 --- a/src/librustdoc/html/render/write_shared.rs +++ b/src/librustdoc/html/render/write_shared.rs @@ -477,11 +477,7 @@ impl SourcesPart { // This needs to be `var`, not `const`. // This variable needs declared in the current global scope so that if // src-script.js loads first, it can pick it up. - SortedTemplate::from_before_after( - r"var srcIndex = new Map(JSON.parse('[", - r"]')); -createSrcSidebar();", - ) + SortedTemplate::from_before_after(r"createSrcSidebar('[", r"]');") } fn get(cx: &Context<'_>, crate_name: &OrderedJson) -> Result, Error> { diff --git a/src/librustdoc/html/render/write_shared/tests.rs b/src/librustdoc/html/render/write_shared/tests.rs index a235f1d37243a..6f185e85345bc 100644 --- a/src/librustdoc/html/render/write_shared/tests.rs +++ b/src/librustdoc/html/render/write_shared/tests.rs @@ -22,23 +22,11 @@ fn but_last_line(s: &str) -> &str { #[test] fn sources_template() { let mut template = SourcesPart::blank(); - assert_eq!( - but_last_line(&template.to_string()), - r"var srcIndex = new Map(JSON.parse('[]')); -createSrcSidebar();" - ); + assert_eq!(but_last_line(&template.to_string()), r"createSrcSidebar('[]');"); template.append(EscapedJson::from(OrderedJson::serialize("u").unwrap()).to_string()); - assert_eq!( - but_last_line(&template.to_string()), - r#"var srcIndex = new Map(JSON.parse('["u"]')); -createSrcSidebar();"# - ); + assert_eq!(but_last_line(&template.to_string()), r#"createSrcSidebar('["u"]');"#); template.append(EscapedJson::from(OrderedJson::serialize("v").unwrap()).to_string()); - assert_eq!( - but_last_line(&template.to_string()), - r#"var srcIndex = new Map(JSON.parse('["u","v"]')); -createSrcSidebar();"# - ); + assert_eq!(but_last_line(&template.to_string()), r#"createSrcSidebar('["u","v"]');"#); } #[test] diff --git a/src/librustdoc/html/static/js/rustdoc.d.ts b/src/librustdoc/html/static/js/rustdoc.d.ts index 0d2e19e019f34..6af16441de88b 100644 --- a/src/librustdoc/html/static/js/rustdoc.d.ts +++ b/src/librustdoc/html/static/js/rustdoc.d.ts @@ -4,8 +4,6 @@ /* eslint-disable */ declare global { - /** Map from crate name to directory structure, for source view */ - declare var srcIndex: Map; /** Defined and documented in `storage.js` */ declare function nonnull(x: T|null, msg: string|undefined); /** Defined and documented in `storage.js` */ @@ -64,7 +62,7 @@ declare global { * create's the sidebar in source code view. * called in generated `src-files.js`. */ - createSrcSidebar?: function(), + createSrcSidebar?: function(string), /** * Set up event listeners for a scraped source example. */ @@ -129,7 +127,7 @@ declare namespace rustdoc { /** * A single parsed "atom" in a search query. For example, - * + * * std::fmt::Formatter, Write -> Result<()> * ┏━━━━━━━━━━━━━━━━━━ ┌──── ┏━━━━━┅┅┅┅┄┄┄┄┄┄┄┄┄┄┄┄┄┄┐ * ┃ │ ┗ QueryElement { ┊ diff --git a/src/librustdoc/html/static/js/src-script.js b/src/librustdoc/html/static/js/src-script.js index b9ab6e85603bc..f23504dbec763 100644 --- a/src/librustdoc/html/static/js/src-script.js +++ b/src/librustdoc/html/static/js/src-script.js @@ -1,6 +1,3 @@ -// From rust: -/* global srcIndex */ - // Local js definitions: /* global addClass, onEachLazy, removeClass, browserSupportsHistoryApi */ /* global updateLocalStorage, getVar, nonnull */ @@ -100,11 +97,16 @@ window.rustdocToggleSrcSidebar = () => { // This function is called from "src-files.js", generated in `html/render/write_shared.rs`. // eslint-disable-next-line no-unused-vars -function createSrcSidebar() { +/** + * @param {string} srcIndexStr - strinified json map from crate name to dir structure + */ +function createSrcSidebar(srcIndexStr) { + console.log(srcIndexStr); const container = nonnull(document.querySelector("nav.sidebar")); const sidebar = document.createElement("div"); sidebar.id = "src-sidebar"; + const srcIndex = new Map(JSON.parse(srcIndexStr)); let hasFoundFile = false; diff --git a/tests/rustdoc-gui/globals.goml b/tests/rustdoc-gui/globals.goml index f8c495ec18a69..7a0e2b9eb7462 100644 --- a/tests/rustdoc-gui/globals.goml +++ b/tests/rustdoc-gui/globals.goml @@ -6,7 +6,6 @@ go-to: "file://" + |DOC_PATH| + "/test_docs/index.html?search=sa'%3Bda'%3Bds" wait-for: "#search-tabs" assert-window-property-false: {"searchIndex": null} -assert-window-property: {"srcIndex": null} // Form input go-to: "file://" + |DOC_PATH| + "/test_docs/index.html" @@ -14,11 +13,9 @@ write-into: (".search-input", "Foo") press-key: 'Enter' wait-for: "#search-tabs" assert-window-property-false: {"searchIndex": null} -assert-window-property: {"srcIndex": null} // source sidebar go-to: "file://" + |DOC_PATH| + "/src/test_docs/lib.rs.html" click: "#sidebar-button" wait-for: "#src-sidebar details" -assert-window-property-false: {"srcIndex": null} assert-window-property: {"searchIndex": null} From 6cecd764013e592a9bb1707072cea75d0939e1ee Mon Sep 17 00:00:00 2001 From: lolbinarycat Date: Fri, 6 Jun 2025 10:48:22 -0500 Subject: [PATCH 2/2] src-script.js: remove debug print Co-authored-by: erikdesjardins <7673145+erikdesjardins@users.noreply.github.com> --- src/librustdoc/html/static/js/src-script.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/librustdoc/html/static/js/src-script.js b/src/librustdoc/html/static/js/src-script.js index f23504dbec763..0c6afbeed22e2 100644 --- a/src/librustdoc/html/static/js/src-script.js +++ b/src/librustdoc/html/static/js/src-script.js @@ -101,7 +101,6 @@ window.rustdocToggleSrcSidebar = () => { * @param {string} srcIndexStr - strinified json map from crate name to dir structure */ function createSrcSidebar(srcIndexStr) { - console.log(srcIndexStr); const container = nonnull(document.querySelector("nav.sidebar")); const sidebar = document.createElement("div");