Skip to content

Commit cbcb74e

Browse files
committed
rustdoc: simplify the codeblock tooltip
This commit moves the tooltip into example-wrap, simplifying allowing several overly-complex things to be fixed: * 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.
1 parent f9da510 commit cbcb74e

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
@@ -352,10 +352,6 @@ img {
352352
max-width: 100%;
353353
}
354354

355-
li {
356-
position: relative;
357-
}
358-
359355
.source .content {
360356
max-width: none;
361357
overflow: visible;
@@ -657,7 +653,7 @@ h2.location a {
657653
position: relative;
658654
}
659655

660-
.docblock > :not(.information):not(.more-examples-toggle) {
656+
.docblock > :not(.more-examples-toggle):not(.example-wrap) {
661657
max-width: 100%;
662658
overflow-x: auto;
663659
}
@@ -1174,12 +1170,12 @@ pre.ignore {
11741170
border-left: 2px solid var(--codeblock-ignore-color);
11751171
}
11761172

1177-
pre.compile_fail:hover, .information:hover + .example-wrap pre.compile_fail,
1178-
pre.should_panic:hover, .information:hover + .example-wrap pre.should_panic {
1173+
.example-wrap:hover pre.compile_fail,
1174+
.example-wrap:hover pre.should_panic {
11791175
border-left: 2px solid var(--codeblock-error-hover-color);
11801176
}
11811177

1182-
pre.ignore:hover, .information:hover + .example-wrap pre.ignore {
1178+
.example-wrap:hover pre.ignore {
11831179
border-left: 2px solid var(--codeblock-ignore-hover-color);
11841180
}
11851181

@@ -1192,12 +1188,12 @@ pre.ignore:hover, .information:hover + .example-wrap pre.ignore {
11921188
color: var(--codeblock-ignore-color);
11931189
}
11941190

1195-
.information > .compile_fail:hover,
1196-
.information > .should_panic:hover {
1191+
.example-wrap:hover .tooltip.compile_fail,
1192+
.example-wrap:hover .tooltip.should_panic {
11971193
color: var(--codeblock-error-hover-color);
11981194
}
11991195

1200-
.information > .ignore:hover {
1196+
.example-wrap:hover .tooltip.ignore {
12011197
color: var(--codeblock-ignore-hover-color);
12021198
}
12031199

@@ -1738,7 +1734,7 @@ in storage.js plus the media query with (max-width: 700px)
17381734
to prevent an overlay between the "collapse toggle" and the information tooltip.
17391735
However, it's not needed with smaller screen width because the doc/code block is always put
17401736
"one line" below. */
1741-
.docblock > .information:first-child > .tooltip {
1737+
.docblock > .example-wrap:first-child > .information > .tooltip {
17421738
margin-top: 16px;
17431739
}
17441740

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)