Skip to content

Commit 9a043da

Browse files
authored
Rollup merge of rust-lang#79742 - GuillaumeGomez:move-tooltips-messages-out-of-html, r=Nemo157
Move tooltips messages out of html First thing first: nothing in the output has changed. You still have the "i" on the left of code blocks examples when they have `ignore`, `compile_fail`, `should_panic` and `edition`. The behavior also remains the same: when you hover the "i", you have the corresponding message showing up. So now, why this PR then? I realized recently that we were actually generating those messages into the HTML every time whereas all messages are the same (except for the edition ones, I'll come back to it later). So instead of generating more content, I simply moved it inside the CSS thanks to pseudo elements (`::before` and `::after`). The message is now inside `::after` and we use the `::before` to have the small triangle on the left of the message. So now, we have less HTML generated which is seems pretty nice. So now, back to the `edition` change: the message is globally the same, but the "edition" itself can be different (2015 or 2018 currently, I expect 2021 to arrive not too far in the future). So the only difference for it is that I added a new attribute on the tooltip called `edition` which contains this information. Then, the `::after` uses it inside its `content` (you can get the content of an element's attribute by using `attr` and concat different strings by simply having them after the other). Don't hesitate if a part of my explanations isn't clear. r? ``@jyn514``
2 parents c34c015 + 152d4e7 commit 9a043da

File tree

7 files changed

+52
-71
lines changed

7 files changed

+52
-71
lines changed

Diff for: src/librustdoc/html/highlight.rs

+10-5
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!(" data-edition=\"{}\"", edition_info)
34+
} else {
35+
String::new()
36+
},
3237
)
3338
.unwrap();
3439
}

Diff for: src/librustdoc/html/markdown.rs

+14-47
Original file line numberDiff line numberDiff line change
@@ -284,60 +284,27 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
284284
});
285285

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

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

Diff for: src/librustdoc/html/static/rustdoc.css

+17-7
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(data-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

Diff for: src/librustdoc/html/static/themes/ayu.css

+2-2
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

Diff for: src/librustdoc/html/static/themes/dark.css

+2-2
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

Diff for: src/librustdoc/html/static/themes/light.css

+2-2
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

Diff for: src/test/rustdoc/codeblock-title.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
#![crate_name = "foo"]
22

3-
// ignore-tidy-linelength
4-
5-
// @has foo/fn.bar.html '//*[@class="tooltip compile_fail"]/span' "This example deliberately fails to compile"
6-
// @has foo/fn.bar.html '//*[@class="tooltip ignore"]/span' "This example is not tested"
7-
// @has foo/fn.bar.html '//*[@class="tooltip should_panic"]/span' "This example panics"
3+
// @has foo/fn.bar.html '//*[@class="tooltip compile_fail"]' "ⓘ"
4+
// @has foo/fn.bar.html '//*[@class="tooltip ignore"]' "ⓘ"
5+
// @has foo/fn.bar.html '//*[@class="tooltip should_panic"]' "ⓘ"
6+
// @has foo/fn.bar.html '//*[@data-edition="2018"]' "ⓘ"
87

98
/// foo
109
///
@@ -20,7 +19,7 @@
2019
/// hoo();
2120
/// ```
2221
///
23-
/// ```
22+
/// ```edition2018
2423
/// let x = 0;
2524
/// ```
2625
pub fn bar() -> usize { 2 }

0 commit comments

Comments
 (0)