From a4518ea7217465aee04860529527f9ecc9f9c79b Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Wed, 24 Jul 2024 13:34:18 -0700 Subject: [PATCH] rustdoc: add test case for modnav position when TOC is off --- src/librustdoc/html/render/context.rs | 7 +-- src/librustdoc/html/render/sidebar.rs | 11 +++-- .../rustdoc-gui/sidebar-modnav-position.goml | 44 +++++++++++++++++++ 3 files changed, 51 insertions(+), 11 deletions(-) create mode 100644 tests/rustdoc-gui/sidebar-modnav-position.goml diff --git a/src/librustdoc/html/render/context.rs b/src/librustdoc/html/render/context.rs index e83c8d2c3eb2e..2e74a33594980 100644 --- a/src/librustdoc/html/render/context.rs +++ b/src/librustdoc/html/render/context.rs @@ -15,12 +15,9 @@ use rustc_span::{sym, FileName, Symbol}; use super::print_item::{full_path, item_path, print_item}; use super::search_index::build_index; +use super::sidebar::{print_sidebar, sidebar_module_like, ModuleLike, Sidebar}; use super::write_shared::write_shared; -use super::{ - collect_spans_and_sources, scrape_examples_help, - sidebar::{print_sidebar, sidebar_module_like, ModuleLike, Sidebar}, - AllTypes, LinkFromSrc, StylePath, -}; +use super::{collect_spans_and_sources, scrape_examples_help, AllTypes, LinkFromSrc, StylePath}; use crate::clean::types::ExternalLocation; use crate::clean::utils::has_doc_flag; use crate::clean::{self, ExternalCrate}; diff --git a/src/librustdoc/html/render/sidebar.rs b/src/librustdoc/html/render/sidebar.rs index 351a23b880d40..fe64739731c52 100644 --- a/src/librustdoc/html/render/sidebar.rs +++ b/src/librustdoc/html/render/sidebar.rs @@ -7,13 +7,12 @@ use rustc_hir::def::CtorKind; use rustc_hir::def_id::DefIdSet; use rustc_middle::ty::{self, TyCtxt}; -use crate::{ - clean, - formats::{item_type::ItemType, Impl}, - html::{format::Buffer, markdown::IdMap, markdown::MarkdownWithToc}, -}; - use super::{item_ty_to_section, Context, ItemSection}; +use crate::clean; +use crate::formats::item_type::ItemType; +use crate::formats::Impl; +use crate::html::format::Buffer; +use crate::html::markdown::{IdMap, MarkdownWithToc}; #[derive(Clone, Copy)] pub(crate) enum ModuleLike { diff --git a/tests/rustdoc-gui/sidebar-modnav-position.goml b/tests/rustdoc-gui/sidebar-modnav-position.goml new file mode 100644 index 0000000000000..eb86d118ab22d --- /dev/null +++ b/tests/rustdoc-gui/sidebar-modnav-position.goml @@ -0,0 +1,44 @@ +// Verifies that, when TOC is hidden, modnav is always in exactly the same spot +// This is driven by a reasonably common use case: +// +// - There are three or more items that might meet my needs. +// - I open the first one, decide it's not what I want, switch to the second one using the sidebar. +// - The second one also doesn't meet my needs, so I switch to the third. +// - The third also doesn't meet my needs, so... +// +// because the sibling module nav is in exactly the same place every time, +// it's very easy to find and switch between pages that way. + +go-to: "file://" + |DOC_PATH| + "/test_docs/enum.WhoLetTheDogOut.html" +show-text: true +set-local-storage: {"rustdoc-hide-toc": "true"} + +define-function: ( + "check-positions", + [url], + block { + go-to: "file://" + |DOC_PATH| + |url| + // Checking results colors. + assert-position: ("#rustdoc-modnav > h2", {"x": |h2_x|, "y": |h2_y|}) + assert-position: ( + "#rustdoc-modnav > ul:first-of-type > li:first-of-type", + {"x": |x|, "y": |y|} + ) + }, +) + +// First, at test_docs root +go-to: "file://" + |DOC_PATH| + "/test_docs/enum.WhoLetTheDogOut.html" +store-position: ("#rustdoc-modnav > h2", {"x": h2_x, "y": h2_y}) +store-position: ("#rustdoc-modnav > ul:first-of-type > li:first-of-type", {"x": x, "y": y}) +call-function: ("check-positions", {"url": "/test_docs/enum.WhoLetTheDogOut.html"}) +call-function: ("check-positions", {"url": "/test_docs/struct.StructWithPublicUndocumentedFields.html"}) +call-function: ("check-positions", {"url": "/test_docs/codeblock_sub/index.html"}) + +// Now in a submodule +go-to: "file://" + |DOC_PATH| + "/test_docs/fields/struct.Struct.html" +store-position: ("#rustdoc-modnav > h2", {"x": h2_x, "y": h2_y}) +store-position: ("#rustdoc-modnav > ul:first-of-type > li:first-of-type", {"x": x, "y": y}) +call-function: ("check-positions", {"url": "/test_docs/fields/struct.Struct.html"}) +call-function: ("check-positions", {"url": "/test_docs/fields/union.Union.html"}) +call-function: ("check-positions", {"url": "/test_docs/fields/enum.Enum.html"})