Skip to content

Commit 2e258ce

Browse files
Rollup merge of #101600 - notriddle:notriddle/li, r=GuillaumeGomez
rustdoc: simplify the codeblock tooltip **#101593 needs merged first** This PR moves the tooltip into example-wrap, simplifying several overly-complex aspects of how these tooltips work: * The mousover javascript can be removed, because hovering example-wrap can style the tooltip inside. * The sibling selecor can be removed, because hovering the tooltip also hovers the wrapper, which can hover the codeblock itself. * The relative positioning of the `<li>` tag, which was added in e861efd to fix the positioning of the code tooltip, can now be removed, because example-wrap itself already has relative positioning.
2 parents ff21ccf + cbcb74e commit 2e258ce

File tree

6 files changed

+61
-83
lines changed

6 files changed

+61
-83
lines changed

src/librustdoc/html/highlight.rs

+28-27
Original file line numberDiff line numberDiff line change
@@ -52,35 +52,14 @@ pub(crate) fn render_example_with_highlighting(
5252
tooltip: Tooltip,
5353
playground_button: Option<&str>,
5454
) {
55-
let class = match tooltip {
56-
Tooltip::Ignore => " ignore",
57-
Tooltip::CompileFail => " compile_fail",
58-
Tooltip::ShouldPanic => " should_panic",
59-
Tooltip::Edition(_) => " edition",
60-
Tooltip::None => "",
61-
};
62-
63-
if tooltip != Tooltip::None {
64-
write!(
65-
out,
66-
"<div class='information'><div class='tooltip{}'{}>ⓘ</div></div>",
67-
class,
68-
if let Tooltip::Edition(edition_info) = tooltip {
69-
format!(" data-edition=\"{}\"", edition_info)
70-
} else {
71-
String::new()
72-
},
73-
);
74-
}
75-
76-
write_header(out, &format!("rust-example-rendered{}", class), None);
55+
write_header(out, "rust-example-rendered", None, tooltip);
7756
write_code(out, src, None, None);
7857
write_footer(out, playground_button);
7958
}
8059

8160
/// Highlights `src` as a macro, returning the HTML output.
8261
pub(crate) fn render_macro_with_highlighting(src: &str, out: &mut Buffer) {
83-
write_header(out, "macro", None);
62+
write_header(out, "macro", None, Tooltip::None);
8463
write_code(out, src, None, None);
8564
write_footer(out, None);
8665
}
@@ -93,20 +72,42 @@ pub(crate) fn render_source_with_highlighting(
9372
href_context: HrefContext<'_, '_, '_>,
9473
decoration_info: DecorationInfo,
9574
) {
96-
write_header(out, "", Some(line_numbers));
75+
write_header(out, "", Some(line_numbers), Tooltip::None);
9776
write_code(out, src, Some(href_context), Some(decoration_info));
9877
write_footer(out, None);
9978
}
10079

101-
fn write_header(out: &mut Buffer, class: &str, extra_content: Option<Buffer>) {
80+
fn write_header(out: &mut Buffer, class: &str, extra_content: Option<Buffer>, tooltip: Tooltip) {
10281
write!(out, "<div class=\"example-wrap\">");
82+
83+
let tooltip_class = match tooltip {
84+
Tooltip::Ignore => " ignore",
85+
Tooltip::CompileFail => " compile_fail",
86+
Tooltip::ShouldPanic => " should_panic",
87+
Tooltip::Edition(_) => " edition",
88+
Tooltip::None => "",
89+
};
90+
91+
if tooltip != Tooltip::None {
92+
write!(
93+
out,
94+
"<div class='information'><div class='tooltip{}'{}>ⓘ</div></div>",
95+
tooltip_class,
96+
if let Tooltip::Edition(edition_info) = tooltip {
97+
format!(" data-edition=\"{}\"", edition_info)
98+
} else {
99+
String::new()
100+
},
101+
);
102+
}
103+
103104
if let Some(extra) = extra_content {
104105
out.push_buffer(extra);
105106
}
106-
if class.is_empty() {
107+
if class.is_empty() && tooltip_class.is_empty() {
107108
write!(out, "<pre class=\"rust\">");
108109
} else {
109-
write!(out, "<pre class=\"rust {}\">", class);
110+
write!(out, "<pre class=\"rust {class}{tooltip_class}\">");
110111
}
111112
write!(out, "<code>");
112113
}

src/librustdoc/html/static/css/rustdoc.css

+8-12
Original file line numberDiff line numberDiff line change
@@ -347,10 +347,6 @@ img {
347347
max-width: 100%;
348348
}
349349

350-
li {
351-
position: relative;
352-
}
353-
354350
.source .content {
355351
max-width: none;
356352
overflow: visible;
@@ -652,7 +648,7 @@ h2.location a {
652648
position: relative;
653649
}
654650

655-
.docblock > :not(.information):not(.more-examples-toggle) {
651+
.docblock > :not(.more-examples-toggle):not(.example-wrap) {
656652
max-width: 100%;
657653
overflow-x: auto;
658654
}
@@ -1169,12 +1165,12 @@ pre.ignore {
11691165
border-left: 2px solid var(--codeblock-ignore-color);
11701166
}
11711167

1172-
pre.compile_fail:hover, .information:hover + .example-wrap pre.compile_fail,
1173-
pre.should_panic:hover, .information:hover + .example-wrap pre.should_panic {
1168+
.example-wrap:hover pre.compile_fail,
1169+
.example-wrap:hover pre.should_panic {
11741170
border-left: 2px solid var(--codeblock-error-hover-color);
11751171
}
11761172

1177-
pre.ignore:hover, .information:hover + .example-wrap pre.ignore {
1173+
.example-wrap:hover pre.ignore {
11781174
border-left: 2px solid var(--codeblock-ignore-hover-color);
11791175
}
11801176

@@ -1187,12 +1183,12 @@ pre.ignore:hover, .information:hover + .example-wrap pre.ignore {
11871183
color: var(--codeblock-ignore-color);
11881184
}
11891185

1190-
.information > .compile_fail:hover,
1191-
.information > .should_panic:hover {
1186+
.example-wrap:hover .tooltip.compile_fail,
1187+
.example-wrap:hover .tooltip.should_panic {
11921188
color: var(--codeblock-error-hover-color);
11931189
}
11941190

1195-
.information > .ignore:hover {
1191+
.example-wrap:hover .tooltip.ignore {
11961192
color: var(--codeblock-ignore-hover-color);
11971193
}
11981194

@@ -1727,7 +1723,7 @@ in storage.js plus the media query with (max-width: 700px)
17271723
to prevent an overlay between the "collapse toggle" and the information tooltip.
17281724
However, it's not needed with smaller screen width because the doc/code block is always put
17291725
"one line" below. */
1730-
.docblock > .information:first-child > .tooltip {
1726+
.docblock > .example-wrap:first-child > .information > .tooltip {
17311727
margin-top: 16px;
17321728
}
17331729

src/librustdoc/html/static/js/main.js

+2-21
Original file line numberDiff line numberDiff line change
@@ -699,9 +699,8 @@ function loadCss(cssFileName) {
699699

700700
(function() {
701701
// To avoid checking on "rustdoc-line-numbers" value on every loop...
702-
let lineNumbersFunc = () => {};
703702
if (getSettingValue("line-numbers") === "true") {
704-
lineNumbersFunc = x => {
703+
onEachLazy(document.getElementsByClassName("rust-example-rendered"), x => {
705704
const count = x.textContent.split("\n").length;
706705
const elems = [];
707706
for (let i = 0; i < count; ++i) {
@@ -711,26 +710,8 @@ function loadCss(cssFileName) {
711710
addClass(node, "line-number");
712711
node.innerHTML = elems.join("\n");
713712
x.parentNode.insertBefore(node, x);
714-
};
713+
});
715714
}
716-
onEachLazy(document.getElementsByClassName("rust-example-rendered"), e => {
717-
if (hasClass(e, "compile_fail")) {
718-
e.addEventListener("mouseover", function() {
719-
this.parentElement.previousElementSibling.childNodes[0].style.color = "#f00";
720-
});
721-
e.addEventListener("mouseout", function() {
722-
this.parentElement.previousElementSibling.childNodes[0].style.color = "";
723-
});
724-
} else if (hasClass(e, "ignore")) {
725-
e.addEventListener("mouseover", function() {
726-
this.parentElement.previousElementSibling.childNodes[0].style.color = "#ff9200";
727-
});
728-
e.addEventListener("mouseout", function() {
729-
this.parentElement.previousElementSibling.childNodes[0].style.color = "";
730-
});
731-
}
732-
lineNumbersFunc(e);
733-
});
734715
}());
735716

736717
let oldSidebarScrollPosition = null;

src/test/rustdoc-gui/check_info_sign_position.goml

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ goto: file://|DOC_PATH|/test_docs/index.html
44
goto: ./fn.check_list_code_block.html
55
// If the codeblock is the first element of the docblock, the information tooltip must have
66
// have some top margin to avoid going over the toggle (the "[+]").
7-
assert-css: (".docblock > .information > .compile_fail", { "margin-top": "16px" })
7+
assert-css: (".docblock > .example-wrap > .information > .compile_fail", { "margin-top": "16px" })
88
// Checks that the other codeblocks don't have this top margin.
9-
assert-css: ("ol > li > .information > .compile_fail", { "margin-top": "0px" })
10-
assert-css: ("ol > li > .information > .ignore", { "margin-top": "0px" })
11-
assert-css: (".docblock > .information > .ignore", { "margin-top": "0px" })
9+
assert-css: ("ol > li > .example-wrap > .information > .compile_fail", { "margin-top": "0px" })
10+
assert-css: ("ol > li > .example-wrap > .information > .ignore", { "margin-top": "0px" })
11+
assert-css: (".docblock > .example-wrap > .information > .ignore", { "margin-top": "0px" })

src/test/rustdoc-gui/codeblock-tooltip.goml

+18-18
Original file line numberDiff line numberDiff line change
@@ -8,89 +8,89 @@ reload:
88

99
// compile_fail block
1010
assert-css: (".docblock .information .compile_fail", {"color": "rgba(255, 0, 0, 0.5)"})
11-
assert-css: (".docblock .example-wrap .compile_fail", {"border-left": "2px solid rgba(255, 0, 0, 0.5)"})
11+
assert-css: (".docblock .example-wrap pre.compile_fail", {"border-left": "2px solid rgba(255, 0, 0, 0.5)"})
1212

1313
move-cursor-to: ".docblock .information .compile_fail"
1414

1515
assert-css: (".docblock .information .compile_fail", {"color": "rgb(255, 0, 0)"})
16-
assert-css: (".docblock .example-wrap .compile_fail", {"border-left": "2px solid rgb(255, 0, 0)"})
16+
assert-css: (".docblock .example-wrap pre.compile_fail", {"border-left": "2px solid rgb(255, 0, 0)"})
1717

1818
// should_panic block
1919
assert-css: (".docblock .information .should_panic", {"color": "rgba(255, 0, 0, 0.5)"})
20-
assert-css: (".docblock .example-wrap .should_panic", {"border-left": "2px solid rgba(255, 0, 0, 0.5)"})
20+
assert-css: (".docblock .example-wrap pre.should_panic", {"border-left": "2px solid rgba(255, 0, 0, 0.5)"})
2121

2222
move-cursor-to: ".docblock .information .should_panic"
2323

2424
assert-css: (".docblock .information .should_panic", {"color": "rgb(255, 0, 0)"})
25-
assert-css: (".docblock .example-wrap .should_panic", {"border-left": "2px solid rgb(255, 0, 0)"})
25+
assert-css: (".docblock .example-wrap pre.should_panic", {"border-left": "2px solid rgb(255, 0, 0)"})
2626

2727
// ignore block
2828
assert-css: (".docblock .information .ignore", {"color": "rgba(255, 142, 0, 0.6)"})
29-
assert-css: (".docblock .example-wrap .ignore", {"border-left": "2px solid rgba(255, 142, 0, 0.6)"})
29+
assert-css: (".docblock .example-wrap pre.ignore", {"border-left": "2px solid rgba(255, 142, 0, 0.6)"})
3030

3131
move-cursor-to: ".docblock .information .ignore"
3232

3333
assert-css: (".docblock .information .ignore", {"color": "rgb(255, 142, 0)"})
34-
assert-css: (".docblock .example-wrap .ignore", {"border-left": "2px solid rgb(255, 142, 0)"})
34+
assert-css: (".docblock .example-wrap pre.ignore", {"border-left": "2px solid rgb(255, 142, 0)"})
3535

3636

3737
// Light theme.
3838
local-storage: {"rustdoc-theme": "light"}
3939
reload:
4040

4141
assert-css: (".docblock .information .compile_fail", {"color": "rgba(255, 0, 0, 0.5)"})
42-
assert-css: (".docblock .example-wrap .compile_fail", {"border-left": "2px solid rgba(255, 0, 0, 0.5)"})
42+
assert-css: (".docblock .example-wrap pre.compile_fail", {"border-left": "2px solid rgba(255, 0, 0, 0.5)"})
4343

4444
move-cursor-to: ".docblock .information .compile_fail"
4545

4646
assert-css: (".docblock .information .compile_fail", {"color": "rgb(255, 0, 0)"})
47-
assert-css: (".docblock .example-wrap .compile_fail", {"border-left": "2px solid rgb(255, 0, 0)"})
47+
assert-css: (".docblock .example-wrap pre.compile_fail", {"border-left": "2px solid rgb(255, 0, 0)"})
4848

4949
// should_panic block
5050
assert-css: (".docblock .information .should_panic", {"color": "rgba(255, 0, 0, 0.5)"})
51-
assert-css: (".docblock .example-wrap .should_panic", {"border-left": "2px solid rgba(255, 0, 0, 0.5)"})
51+
assert-css: (".docblock .example-wrap pre.should_panic", {"border-left": "2px solid rgba(255, 0, 0, 0.5)"})
5252

5353
move-cursor-to: ".docblock .information .should_panic"
5454

5555
assert-css: (".docblock .information .should_panic", {"color": "rgb(255, 0, 0)"})
56-
assert-css: (".docblock .example-wrap .should_panic", {"border-left": "2px solid rgb(255, 0, 0)"})
56+
assert-css: (".docblock .example-wrap pre.should_panic", {"border-left": "2px solid rgb(255, 0, 0)"})
5757

5858
// ignore block
5959
assert-css: (".docblock .information .ignore", {"color": "rgba(255, 142, 0, 0.6)"})
60-
assert-css: (".docblock .example-wrap .ignore", {"border-left": "2px solid rgba(255, 142, 0, 0.6)"})
60+
assert-css: (".docblock .example-wrap pre.ignore", {"border-left": "2px solid rgba(255, 142, 0, 0.6)"})
6161

6262
move-cursor-to: ".docblock .information .ignore"
6363

6464
assert-css: (".docblock .information .ignore", {"color": "rgb(255, 142, 0)"})
65-
assert-css: (".docblock .example-wrap .ignore", {"border-left": "2px solid rgb(255, 142, 0)"})
65+
assert-css: (".docblock .example-wrap pre.ignore", {"border-left": "2px solid rgb(255, 142, 0)"})
6666

6767

6868
// Ayu theme.
6969
local-storage: {"rustdoc-theme": "ayu"}
7070
reload:
7171

7272
assert-css: (".docblock .information .compile_fail", {"color": "rgba(255, 0, 0, 0.5)"})
73-
assert-css: (".docblock .example-wrap .compile_fail", {"border-left": "2px solid rgba(255, 0, 0, 0.5)"})
73+
assert-css: (".docblock .example-wrap pre.compile_fail", {"border-left": "2px solid rgba(255, 0, 0, 0.5)"})
7474

7575
move-cursor-to: ".docblock .information .compile_fail"
7676

7777
assert-css: (".docblock .information .compile_fail", {"color": "rgb(255, 0, 0)"})
78-
assert-css: (".docblock .example-wrap .compile_fail", {"border-left": "2px solid rgb(255, 0, 0)"})
78+
assert-css: (".docblock .example-wrap pre.compile_fail", {"border-left": "2px solid rgb(255, 0, 0)"})
7979

8080
// should_panic block
8181
assert-css: (".docblock .information .should_panic", {"color": "rgba(255, 0, 0, 0.5)"})
82-
assert-css: (".docblock .example-wrap .should_panic", {"border-left": "2px solid rgba(255, 0, 0, 0.5)"})
82+
assert-css: (".docblock .example-wrap pre.should_panic", {"border-left": "2px solid rgba(255, 0, 0, 0.5)"})
8383

8484
move-cursor-to: ".docblock .information .should_panic"
8585

8686
assert-css: (".docblock .information .should_panic", {"color": "rgb(255, 0, 0)"})
87-
assert-css: (".docblock .example-wrap .should_panic", {"border-left": "2px solid rgb(255, 0, 0)"})
87+
assert-css: (".docblock .example-wrap pre.should_panic", {"border-left": "2px solid rgb(255, 0, 0)"})
8888

8989
// ignore block
9090
assert-css: (".docblock .information .ignore", {"color": "rgba(255, 142, 0, 0.6)"})
91-
assert-css: (".docblock .example-wrap .ignore", {"border-left": "2px solid rgba(255, 142, 0, 0.6)"})
91+
assert-css: (".docblock .example-wrap pre.ignore", {"border-left": "2px solid rgba(255, 142, 0, 0.6)"})
9292

9393
move-cursor-to: ".docblock .information .ignore"
9494

9595
assert-css: (".docblock .information .ignore", {"color": "rgb(255, 142, 0)"})
96-
assert-css: (".docblock .example-wrap .ignore", {"border-left": "2px solid rgb(255, 142, 0)"})
96+
assert-css: (".docblock .example-wrap pre.ignore", {"border-left": "2px solid rgb(255, 142, 0)"})

src/test/rustdoc-gui/overflow-tooltip-information.goml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// have overflow and max-width CSS rules set because they create a bug in firefox on
33
// mac. For more information: https://github.com/rust-lang/rust/issues/89185
44
goto: file://|DOC_PATH|/test_docs/fn.foo.html
5-
assert-css: (".docblock > .information", {
5+
assert-css: (".docblock > .example-wrap > .information", {
66
"overflow-x": "visible",
77
"max-width": "none"
88
}, ALL)

0 commit comments

Comments
 (0)