Skip to content

Commit

Permalink
rustdoc: properly handle path wrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
notriddle committed Jul 29, 2024
1 parent 1d339b0 commit 3bf8bcf
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/librustdoc/html/escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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("<wbr>")?;
last = i;
} else if s.contains(':') && !next_is_colon() {
EscapeBodyText(&text[last..i + 1]).fmt(fmt)?;
fmt.write_str("<wbr>")?;
last = i + 1;
}
}
if last < text.len() {
Expand Down
9 changes: 8 additions & 1 deletion src/librustdoc/html/escape/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(), "First<wbr>Second");
assert_eq!(&E("First_Second").to_string(), "First<wbr>_Second");
Expand All @@ -15,8 +21,9 @@ fn escape_body_text_with_wbr() {
assert_eq!(&E("First SecondThird").to_string(), "First Second<wbr>Third");
assert_eq!(&E("First<T>_Second").to_string(), "First&lt;<wbr>T&gt;<wbr>_Second");
assert_eq!(&E("first_second").to_string(), "first<wbr>_second");
assert_eq!(&E("first:second").to_string(), "first:<wbr>second");
assert_eq!(&E("first::second").to_string(), "first::<wbr>second");
assert_eq!(&E("MY_CONSTANT").to_string(), "MY<wbr>_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
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ fn extra_info_tags<'a, 'tcx: 'a>(
display_fn(move |f| {
write!(
f,
r#"<span class="stab {class}" title="{title}">{contents}</span>"#,
r#"<wbr><span class="stab {class}" title="{title}">{contents}</span>"#,
title = Escape(title),
)
})
Expand Down
5 changes: 4 additions & 1 deletion src/librustdoc/html/static/css/rustdoc.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -2222,7 +2225,7 @@ in src-script.js and main.js
width: 33%;
}
.item-table > li > div {
word-break: break-all;
overflow-wrap: anywhere;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/templates/sidebar.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ <h3><a href="#{{block.heading.href|safe}}"> {# #}
</section>
{% endif %}
{% if !path.is_empty() %}
<h2><a href="{% if is_mod %}../{% endif %}index.html">In {{+ path}}</a></h2>
<h2><a href="{% if is_mod %}../{% endif %}index.html">In {{+ path|wrapped|safe}}</a></h2>
{% endif %}
</div>
3 changes: 2 additions & 1 deletion tests/rustdoc-gui/label-next-to-symbol.goml
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down

0 comments on commit 3bf8bcf

Please sign in to comment.