rustdoc: avoid redundant HTML when there's already line breaks

This commit is contained in:
Michael Howell 2024-07-06 19:05:28 -07:00
parent 0d0e18e7f6
commit 9186001f34
2 changed files with 10 additions and 0 deletions

View File

@ -97,6 +97,12 @@ impl<'a> fmt::Display for EscapeBodyTextWithWbr<'a> {
let _ = it.next(); // don't insert wbr before first char
while let Some((i, s)) = it.next() {
let pk = it.peek();
if s.chars().all(|c| c.is_whitespace()) {
// don't need "First <wbr>Second"; the space is enough
EscapeBodyText(&text[last..i]).fmt(fmt)?;
last = i;
continue;
}
let is_uppercase = || s.chars().any(|c| c.is_uppercase());
let next_is_uppercase =
|| pk.map_or(true, |(_, t)| t.chars().any(|c| c.is_uppercase()));

View File

@ -9,6 +9,10 @@ fn escape_body_text_with_wbr() {
// real(istic) examples
assert_eq!(&E("FirstSecond").to_string(), "First<wbr>Second");
assert_eq!(&E("First_Second").to_string(), "First<wbr>_Second");
assert_eq!(&E("First Second").to_string(), "First Second");
assert_eq!(&E("First HSecond").to_string(), "First HSecond");
assert_eq!(&E("First HTTPSecond").to_string(), "First HTTP<wbr>Second");
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("MY_CONSTANT").to_string(), "MY<wbr>_CONSTANT");