From 3bf8bcfbe0ebd9527a8ec107c83085cd2d4d6ec1 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Fri, 19 Jul 2024 09:00:50 -0700 Subject: [PATCH] rustdoc: properly handle path wrapping --- src/librustdoc/html/escape.rs | 5 +++++ src/librustdoc/html/escape/tests.rs | 9 ++++++++- src/librustdoc/html/render/print_item.rs | 2 +- src/librustdoc/html/static/css/rustdoc.css | 5 ++++- src/librustdoc/html/templates/sidebar.html | 2 +- tests/rustdoc-gui/label-next-to-symbol.goml | 3 ++- 6 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/librustdoc/html/escape.rs b/src/librustdoc/html/escape.rs index 3e20c5b322b..f8199cd6e0e 100644 --- a/src/librustdoc/html/escape.rs +++ b/src/librustdoc/html/escape.rs @@ -107,12 +107,17 @@ impl<'a> fmt::Display for EscapeBodyTextWithWbr<'a> { let next_is_uppercase = || pk.map_or(true, |(_, t)| t.chars().any(|c| c.is_uppercase())); let next_is_underscore = || pk.map_or(true, |(_, t)| t.contains('_')); + let next_is_colon = || pk.map_or(true, |(_, t)| t.contains(':')); if (i - last > 3 && is_uppercase() && !next_is_uppercase()) || (s.contains('_') && !next_is_underscore()) { EscapeBodyText(&text[last..i]).fmt(fmt)?; fmt.write_str("")?; last = i; + } else if s.contains(':') && !next_is_colon() { + EscapeBodyText(&text[last..i + 1]).fmt(fmt)?; + fmt.write_str("")?; + last = i + 1; } } if last < text.len() { diff --git a/src/librustdoc/html/escape/tests.rs b/src/librustdoc/html/escape/tests.rs index 7933f23eb74..e2d81cf5c27 100644 --- a/src/librustdoc/html/escape/tests.rs +++ b/src/librustdoc/html/escape/tests.rs @@ -6,6 +6,12 @@ fn escape_body_text_with_wbr() { assert_eq!(&E("").to_string(), ""); assert_eq!(&E("a").to_string(), "a"); assert_eq!(&E("A").to_string(), "A"); + assert_eq!(&E("_").to_string(), "_"); + assert_eq!(&E(":").to_string(), ":"); + assert_eq!(&E(" ").to_string(), " "); + assert_eq!(&E("___________").to_string(), "___________"); + assert_eq!(&E(":::::::::::").to_string(), ":::::::::::"); + assert_eq!(&E(" ").to_string(), " "); // real(istic) examples assert_eq!(&E("FirstSecond").to_string(), "FirstSecond"); assert_eq!(&E("First_Second").to_string(), "First_Second"); @@ -15,8 +21,9 @@ fn escape_body_text_with_wbr() { assert_eq!(&E("First SecondThird").to_string(), "First SecondThird"); assert_eq!(&E("First_Second").to_string(), "First<T>_Second"); assert_eq!(&E("first_second").to_string(), "first_second"); + assert_eq!(&E("first:second").to_string(), "first:second"); + assert_eq!(&E("first::second").to_string(), "first::second"); assert_eq!(&E("MY_CONSTANT").to_string(), "MY_CONSTANT"); - assert_eq!(&E("___________").to_string(), "___________"); // a string won't get wrapped if it's less than 8 bytes assert_eq!(&E("HashSet").to_string(), "HashSet"); // an individual word won't get wrapped if it's less than 4 bytes diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index 3f01c082ba9..eec6df9dd20 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -558,7 +558,7 @@ fn extra_info_tags<'a, 'tcx: 'a>( display_fn(move |f| { write!( f, - r#"{contents}"#, + r#"{contents}"#, title = Escape(title), ) }) diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css index e936e1ca07e..cafe5fe4c87 100644 --- a/src/librustdoc/html/static/css/rustdoc.css +++ b/src/librustdoc/html/static/css/rustdoc.css @@ -586,12 +586,15 @@ ul.block, .block li { } .sidebar h2 { + text-wrap: balance; overflow-wrap: anywhere; padding: 0; margin: 0.7rem 0; } .sidebar h3 { + text-wrap: balance; + overflow-wrap: anywhere; font-size: 1.125rem; /* 18px */ padding: 0; margin: 0; @@ -2222,7 +2225,7 @@ in src-script.js and main.js width: 33%; } .item-table > li > div { - word-break: break-all; + overflow-wrap: anywhere; } } diff --git a/src/librustdoc/html/templates/sidebar.html b/src/librustdoc/html/templates/sidebar.html index 025220ab415..0990c2716b8 100644 --- a/src/librustdoc/html/templates/sidebar.html +++ b/src/librustdoc/html/templates/sidebar.html @@ -31,6 +31,6 @@ {% endif %} {% if !path.is_empty() %} -

In {{+ path}}

+

In {{+ path|wrapped|safe}}

{% endif %} diff --git a/tests/rustdoc-gui/label-next-to-symbol.goml b/tests/rustdoc-gui/label-next-to-symbol.goml index 1fa0a120ada..a8363f29dd5 100644 --- a/tests/rustdoc-gui/label-next-to-symbol.goml +++ b/tests/rustdoc-gui/label-next-to-symbol.goml @@ -27,7 +27,8 @@ compare-elements-position-near: ( ".item-name .stab.deprecated", {"y": 2}, ) -compare-elements-position: ( +// "Unix" part is on second line +compare-elements-position-false: ( ".item-name .stab.deprecated", ".item-name .stab.portability", ["y"],