Skip to content

Commit 30168bd

Browse files
Simplify DOM for codeblocks tooltips
1 parent cedd26b commit 30168bd

File tree

2 files changed

+86
-83
lines changed

2 files changed

+86
-83
lines changed

src/librustdoc/html/highlight.rs

+14-13
Original file line numberDiff line numberDiff line change
@@ -78,21 +78,22 @@ pub(crate) fn render_source_with_highlighting(
7878
}
7979

8080
fn write_header(out: &mut Buffer, class: &str, extra_content: Option<Buffer>, tooltip: Tooltip) {
81-
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-
};
81+
write!(
82+
out,
83+
"<div class=\"example-wrap{}\">",
84+
match tooltip {
85+
Tooltip::Ignore => " ignore",
86+
Tooltip::CompileFail => " compile_fail",
87+
Tooltip::ShouldPanic => " should_panic",
88+
Tooltip::Edition(_) => " edition",
89+
Tooltip::None => "",
90+
},
91+
);
9092

9193
if tooltip != Tooltip::None {
9294
write!(
9395
out,
94-
"<div class='information'><div class='tooltip{}'{}>ⓘ</div></div>",
95-
tooltip_class,
96+
"<div class='tooltip'{}>ⓘ</div>",
9697
if let Tooltip::Edition(edition_info) = tooltip {
9798
format!(" data-edition=\"{}\"", edition_info)
9899
} else {
@@ -104,10 +105,10 @@ fn write_header(out: &mut Buffer, class: &str, extra_content: Option<Buffer>, to
104105
if let Some(extra) = extra_content {
105106
out.push_buffer(extra);
106107
}
107-
if class.is_empty() && tooltip_class.is_empty() {
108+
if class.is_empty() {
108109
write!(out, "<pre class=\"rust\">");
109110
} else {
110-
write!(out, "<pre class=\"rust {class}{tooltip_class}\">");
111+
write!(out, "<pre class=\"rust {class}\">");
111112
}
112113
write!(out, "<code>");
113114
}

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

+72-70
Original file line numberDiff line numberDiff line change
@@ -1156,42 +1156,100 @@ pre.rust .question-mark {
11561156
font-weight: bold;
11571157
}
11581158

1159-
pre.compile_fail,
1160-
pre.should_panic {
1159+
.example-wrap.compile_fail,
1160+
.example-wrap.should_panic {
11611161
border-left: 2px solid var(--codeblock-error-color);
11621162
}
11631163

1164-
pre.ignore {
1164+
.ignore.example-wrap {
11651165
border-left: 2px solid var(--codeblock-ignore-color);
11661166
}
11671167

1168-
.example-wrap:hover pre.compile_fail,
1169-
.example-wrap:hover pre.should_panic {
1168+
.example-wrap.compile_fail:hover,
1169+
.example-wrap.should_panic:hover {
11701170
border-left: 2px solid var(--codeblock-error-hover-color);
11711171
}
11721172

1173-
.example-wrap:hover pre.ignore {
1173+
.example-wrap.ignore:hover {
11741174
border-left: 2px solid var(--codeblock-ignore-hover-color);
11751175
}
11761176

1177-
.tooltip.compile_fail,
1178-
.tooltip.should_panic {
1177+
.example-wrap.compile_fail .tooltip,
1178+
.example-wrap.should_panic .tooltip {
11791179
color: var(--codeblock-error-color);
11801180
}
11811181

1182-
.tooltip.ignore {
1182+
.example-wrap.ignore .tooltip {
11831183
color: var(--codeblock-ignore-color);
11841184
}
11851185

1186-
.example-wrap:hover .tooltip.compile_fail,
1187-
.example-wrap:hover .tooltip.should_panic {
1186+
.example-wrap.compile_fail:hover .tooltip,
1187+
.example-wrap.should_panic:hover .tooltip {
11881188
color: var(--codeblock-error-hover-color);
11891189
}
11901190

1191-
.example-wrap:hover .tooltip.ignore {
1191+
.example-wrap.ignore:hover .tooltip {
11921192
color: var(--codeblock-ignore-hover-color);
11931193
}
11941194

1195+
.example-wrap .tooltip {
1196+
position: absolute;
1197+
display: block;
1198+
cursor: pointer;
1199+
left: -25px;
1200+
top: 5px;
1201+
}
1202+
1203+
.example-wrap .tooltip::after {
1204+
display: none;
1205+
text-align: center;
1206+
padding: 5px 3px 3px 3px;
1207+
border-radius: 6px;
1208+
margin-left: 5px;
1209+
font-size: 1rem;
1210+
border: 1px solid var(--border-color);
1211+
position: absolute;
1212+
width: max-content;
1213+
top: -2px;
1214+
z-index: 1;
1215+
}
1216+
1217+
.example-wrap .tooltip::before {
1218+
content: " ";
1219+
position: absolute;
1220+
top: 50%;
1221+
left: 16px;
1222+
margin-top: -5px;
1223+
border-width: 5px;
1224+
border-style: solid;
1225+
display: none;
1226+
z-index: 1;
1227+
}
1228+
1229+
.example-wrap.ignore .tooltip::after {
1230+
content: "This example is not tested";
1231+
}
1232+
.example-wrap.compile_fail .tooltip::after {
1233+
content: "This example deliberately fails to compile";
1234+
}
1235+
.example-wrap.should_panic .tooltip::after {
1236+
content: "This example panics";
1237+
}
1238+
.example-wrap.edition .tooltip::after {
1239+
content: "This code runs with edition " attr(data-edition);
1240+
}
1241+
1242+
.example-wrap .tooltip:hover::before, .example-wrap .tooltip:hover::after {
1243+
display: inline;
1244+
}
1245+
1246+
.example-wrap.compile_fail .tooltip,
1247+
.example-wrap.should_panic .tooltip,
1248+
.example-wrap.ignore .tooltip {
1249+
font-weight: bold;
1250+
font-size: 1.25rem;
1251+
}
1252+
11951253
a.test-arrow {
11961254
display: inline-block;
11971255
visibility: hidden;
@@ -1257,62 +1315,6 @@ h3.variant {
12571315
padding-right: 3px;
12581316
}
12591317

1260-
.information {
1261-
position: absolute;
1262-
left: -25px;
1263-
margin-top: 7px;
1264-
z-index: 1;
1265-
}
1266-
1267-
.tooltip {
1268-
position: relative;
1269-
display: inline-block;
1270-
cursor: pointer;
1271-
}
1272-
1273-
.tooltip::after {
1274-
display: none;
1275-
text-align: center;
1276-
padding: 5px 3px 3px 3px;
1277-
border-radius: 6px;
1278-
margin-left: 5px;
1279-
font-size: 1rem;
1280-
border: 1px solid var(--border-color);
1281-
}
1282-
1283-
.tooltip.ignore::after {
1284-
content: "This example is not tested";
1285-
}
1286-
.tooltip.compile_fail::after {
1287-
content: "This example deliberately fails to compile";
1288-
}
1289-
.tooltip.should_panic::after {
1290-
content: "This example panics";
1291-
}
1292-
.tooltip.edition::after {
1293-
content: "This code runs with edition " attr(data-edition);
1294-
}
1295-
1296-
.tooltip::before {
1297-
content: " ";
1298-
position: absolute;
1299-
top: 50%;
1300-
left: 16px;
1301-
margin-top: -5px;
1302-
border-width: 5px;
1303-
border-style: solid;
1304-
display: none;
1305-
}
1306-
1307-
.tooltip:hover::before, .tooltip:hover::after {
1308-
display: inline;
1309-
}
1310-
1311-
.tooltip.compile_fail, .tooltip.should_panic, .tooltip.ignore {
1312-
font-weight: bold;
1313-
font-size: 1.25rem;
1314-
}
1315-
13161318
.notable-traits-tooltip {
13171319
display: inline-block;
13181320
cursor: pointer;
@@ -1354,7 +1356,7 @@ h3.variant {
13541356
display: block;
13551357
}
13561358

1357-
.notable-traits .docblock code.content{
1359+
.notable-traits .docblock code.content {
13581360
margin: 0;
13591361
padding: 0;
13601362
font-size: 1.25rem;
@@ -1723,7 +1725,7 @@ in storage.js plus the media query with (max-width: 700px)
17231725
to prevent an overlay between the "collapse toggle" and the information tooltip.
17241726
However, it's not needed with smaller screen width because the doc/code block is always put
17251727
"one line" below. */
1726-
.docblock > .example-wrap:first-child > .information > .tooltip {
1728+
.docblock > .example-wrap:first-child .tooltip {
17271729
margin-top: 16px;
17281730
}
17291731

0 commit comments

Comments
 (0)