Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustdoc: improve <wbr>-insertion for SCREAMING_CAMEL_CASE #131370

Merged
merged 2 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/librustdoc/html/escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,17 @@ impl<'a> fmt::Display for EscapeBodyTextWithWbr<'a> {
|| 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() {
// Check for CamelCase.
//
// `i - last > 3` avoids turning FmRadio into Fm<wbr>Radio, which is technically
// correct, but needlessly bloated.
//
// is_uppercase && !next_is_uppercase checks for camelCase. HTTPSProxy,
// for example, should become HTTPS<wbr>Proxy.
//
// !next_is_underscore avoids turning TEST_RUN into TEST<wbr>_<wbr>RUN, which is also
// needlessly bloated.
if i - last > 3 && is_uppercase() && !next_is_uppercase() && !next_is_underscore() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this code is now complex enough to deserve a small comment (at least on this if) to explain what it's checking (with some examples to make it even simpler).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. No problem.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks!

EscapeBodyText(&text[last..i]).fmt(fmt)?;
fmt.write_str("<wbr>")?;
last = i;
Expand Down
4 changes: 4 additions & 0 deletions src/librustdoc/html/escape/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ fn escape_body_text_with_wbr() {
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("_SIDD_MASKED_NEGATIVE_POLARITY").to_string(),
"_SIDD_<wbr>MASKED_<wbr>NEGATIVE_<wbr>POLARITY"
);
// 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
Loading