Skip to content

Commit db8ddbf

Browse files
Move tooltips messages to CSS instead of inside HTML
1 parent c7cff21 commit db8ddbf

File tree

6 files changed

+47
-65
lines changed

6 files changed

+47
-65
lines changed

src/librustdoc/html/highlight.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use std::fmt::{Display, Write};
1111
use std::iter::Peekable;
1212

1313
use rustc_lexer::{LiteralKind, TokenKind};
14+
use rustc_span::edition::Edition;
1415
use rustc_span::symbol::Ident;
1516
use rustc_span::with_default_session_globals;
1617

@@ -19,16 +20,20 @@ crate fn render_with_highlighting(
1920
src: String,
2021
class: Option<&str>,
2122
playground_button: Option<&str>,
22-
tooltip: Option<(&str, &str)>,
23+
tooltip: Option<(Option<Edition>, &str)>,
2324
) -> String {
2425
debug!("highlighting: ================\n{}\n==============", src);
2526
let mut out = String::with_capacity(src.len());
26-
if let Some((tooltip, class)) = tooltip {
27+
if let Some((edition_info, class)) = tooltip {
2728
write!(
2829
out,
29-
"<div class='information'><div class='tooltip {}'>ⓘ<span \
30-
class='tooltiptext'>{}</span></div></div>",
31-
class, tooltip
30+
"<div class='information'><div class='tooltip {}'{}>ⓘ</div></div>",
31+
class,
32+
if let Some(edition_info) = edition_info {
33+
format!(" edition=\"{}\"", edition_info)
34+
} else {
35+
String::new()
36+
},
3237
)
3338
.unwrap();
3439
}

src/librustdoc/html/markdown.rs

Lines changed: 14 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -286,60 +286,27 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
286286
});
287287

288288
let tooltip = if ignore != Ignore::None {
289-
Some(("This example is not tested".to_owned(), "ignore"))
289+
Some((None, "ignore"))
290290
} else if compile_fail {
291-
Some(("This example deliberately fails to compile".to_owned(), "compile_fail"))
291+
Some((None, "compile_fail"))
292292
} else if should_panic {
293-
Some(("This example panics".to_owned(), "should_panic"))
293+
Some((None, "should_panic"))
294294
} else if explicit_edition {
295-
Some((format!("This code runs with edition {}", edition), "edition"))
295+
Some((Some(edition), "edition"))
296296
} else {
297297
None
298298
};
299299

300-
if let Some((s1, s2)) = tooltip {
301-
s.push_str(&highlight::render_with_highlighting(
302-
text,
303-
Some(&format!(
304-
"rust-example-rendered{}",
305-
if ignore != Ignore::None {
306-
" ignore"
307-
} else if compile_fail {
308-
" compile_fail"
309-
} else if should_panic {
310-
" should_panic"
311-
} else if explicit_edition {
312-
" edition "
313-
} else {
314-
""
315-
}
316-
)),
317-
playground_button.as_deref(),
318-
Some((s1.as_str(), s2)),
319-
));
320-
Some(Event::Html(s.into()))
321-
} else {
322-
s.push_str(&highlight::render_with_highlighting(
323-
text,
324-
Some(&format!(
325-
"rust-example-rendered{}",
326-
if ignore != Ignore::None {
327-
" ignore"
328-
} else if compile_fail {
329-
" compile_fail"
330-
} else if should_panic {
331-
" should_panic"
332-
} else if explicit_edition {
333-
" edition "
334-
} else {
335-
""
336-
}
337-
)),
338-
playground_button.as_deref(),
339-
None,
340-
));
341-
Some(Event::Html(s.into()))
342-
}
300+
s.push_str(&highlight::render_with_highlighting(
301+
text,
302+
Some(&format!(
303+
"rust-example-rendered{}",
304+
if let Some((_, class)) = tooltip { format!(" {}", class) } else { String::new() }
305+
)),
306+
playground_button.as_deref(),
307+
tooltip,
308+
));
309+
Some(Event::Html(s.into()))
343310
}
344311
}
345312

src/librustdoc/html/static/rustdoc.css

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,30 +1079,40 @@ h3 > .collapse-toggle, h4 > .collapse-toggle {
10791079
cursor: pointer;
10801080
}
10811081

1082-
.tooltip .tooltiptext {
1083-
width: 120px;
1082+
.tooltip::after {
10841083
display: none;
10851084
text-align: center;
10861085
padding: 5px 3px 3px 3px;
10871086
border-radius: 6px;
10881087
margin-left: 5px;
1089-
top: -5px;
1090-
left: 105%;
1091-
z-index: 10;
10921088
font-size: 16px;
10931089
}
10941090

1095-
.tooltip .tooltiptext::after {
1091+
.tooltip.ignore::after {
1092+
content: "This example is not tested";
1093+
}
1094+
.tooltip.compile_fail::after {
1095+
content: "This example deliberately fails to compile";
1096+
}
1097+
.tooltip.should_panic::after {
1098+
content: "This example panics";
1099+
}
1100+
.tooltip.edition::after {
1101+
content: "This code runs with edition " attr(edition);
1102+
}
1103+
1104+
.tooltip::before {
10961105
content: " ";
10971106
position: absolute;
10981107
top: 50%;
10991108
left: 16px;
11001109
margin-top: -5px;
11011110
border-width: 5px;
11021111
border-style: solid;
1112+
display: none;
11031113
}
11041114

1105-
.tooltip:hover .tooltiptext {
1115+
.tooltip:hover::before, .tooltip:hover::after {
11061116
display: inline;
11071117
}
11081118

src/librustdoc/html/static/themes/ayu.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,13 +388,13 @@ pre.ignore:hover, .information:hover + pre.ignore {
388388
color: #39AFD7;
389389
}
390390

391-
.tooltip .tooltiptext {
391+
.tooltip::after {
392392
background-color: #314559;
393393
color: #c5c5c5;
394394
border: 1px solid #5c6773;
395395
}
396396

397-
.tooltip .tooltiptext::after {
397+
.tooltip::before {
398398
border-color: transparent #314559 transparent transparent;
399399
}
400400

src/librustdoc/html/static/themes/dark.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,13 +337,13 @@ pre.ignore:hover, .information:hover + pre.ignore {
337337
color: #0089ff;
338338
}
339339

340-
.tooltip .tooltiptext {
340+
.tooltip::after {
341341
background-color: #000;
342342
color: #fff;
343343
border-color: #000;
344344
}
345345

346-
.tooltip .tooltiptext::after {
346+
.tooltip::before {
347347
border-color: transparent black transparent transparent;
348348
}
349349

src/librustdoc/html/static/themes/light.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,12 +329,12 @@ pre.ignore:hover, .information:hover + pre.ignore {
329329
color: #0089ff;
330330
}
331331

332-
.tooltip .tooltiptext {
332+
.tooltip::after {
333333
background-color: #000;
334334
color: #fff;
335335
}
336336

337-
.tooltip .tooltiptext::after {
337+
.tooltip::before {
338338
border-color: transparent black transparent transparent;
339339
}
340340

0 commit comments

Comments
 (0)