Skip to content

Commit 6fe0972

Browse files
Simplify usage of CSS background-image
1 parent 64c9903 commit 6fe0972

File tree

9 files changed

+57
-24
lines changed

9 files changed

+57
-24
lines changed

src/librustdoc/html/render/write_shared.rs

+41-6
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,13 @@ impl Context<'_> {
126126
emit: &[EmitType],
127127
) -> Result<(), Error> {
128128
let tmp;
129-
let contents =
130-
contents.replace("data-url(minus-17x17.png)", static_files::MINUS_PNG_DATAURL);
131-
let contents = contents.replace("data-url(plus-17x17.png)", static_files::PLUS_PNG_DATAURL);
132129
let contents = if minify {
133130
tmp = if resource.extension() == Some(&OsStr::new("css")) {
134-
minifier::css::minify(&contents).map_err(|e| {
131+
minifier::css::minify(contents).map_err(|e| {
135132
Error::new(format!("failed to minify CSS file: {}", e), resource.path(self))
136133
})?
137134
} else {
138-
minifier::js::minify(&contents)
135+
minifier::js::minify(contents)
139136
};
140137
tmp.as_bytes()
141138
} else {
@@ -178,9 +175,45 @@ pub(super) fn write_shared(
178175
cx.write_shared(SharedResource::InvocationSpecific { basename: p }, content, &options.emit)
179176
};
180177

178+
fn add_background_image_to_css(
179+
cx: &Context<'_>,
180+
css: &mut String,
181+
rule: &str,
182+
file: &'static str,
183+
) {
184+
css.push_str(&format!(
185+
"{} {{ background-image: url({}); }}",
186+
rule,
187+
SharedResource::ToolchainSpecific { basename: file }
188+
.path(cx)
189+
.file_name()
190+
.unwrap()
191+
.to_str()
192+
.unwrap()
193+
))
194+
}
195+
196+
// Add all the static files. These may already exist, but we just
197+
// overwrite them anyway to make sure that they're fresh and up-to-date.
198+
let mut rustdoc_css = static_files::RUSTDOC_CSS.to_owned();
199+
add_background_image_to_css(
200+
cx,
201+
&mut rustdoc_css,
202+
"details.undocumented[open] > summary::before, \
203+
details.rustdoc-toggle[open] > summary::before, \
204+
details.rustdoc-toggle[open] > summary.hideme::before",
205+
"toggle-minus.svg",
206+
);
207+
add_background_image_to_css(
208+
cx,
209+
&mut rustdoc_css,
210+
"details.undocumented > summary::before, details.rustdoc-toggle > summary::before",
211+
"toggle-plus.svg",
212+
);
213+
write_minify("rustdoc.css", &rustdoc_css)?;
214+
181215
// Add all the static files. These may already exist, but we just
182216
// overwrite them anyway to make sure that they're fresh and up-to-date.
183-
write_minify("rustdoc.css", static_files::RUSTDOC_CSS)?;
184217
write_minify("settings.css", static_files::SETTINGS_CSS)?;
185218
write_minify("noscript.css", static_files::NOSCRIPT_CSS)?;
186219

@@ -220,6 +253,8 @@ pub(super) fn write_shared(
220253
write_toolchain("wheel.svg", static_files::WHEEL_SVG)?;
221254
write_toolchain("clipboard.svg", static_files::CLIPBOARD_SVG)?;
222255
write_toolchain("down-arrow.svg", static_files::DOWN_ARROW_SVG)?;
256+
write_toolchain("toggle-minus.svg", static_files::TOGGLE_MINUS_PNG)?;
257+
write_toolchain("toggle-plus.svg", static_files::TOGGLE_PLUS_PNG)?;
223258

224259
let mut themes: Vec<&String> = themes.iter().collect();
225260
themes.sort();

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

+8-12
Original file line numberDiff line numberDiff line change
@@ -1512,7 +1512,8 @@ details.rustdoc-toggle > summary::before {
15121512
cursor: pointer;
15131513
width: 17px;
15141514
height: max(17px, 1.1em);
1515-
background: data-url(plus-17x17.png) no-repeat top left;
1515+
background-repeat: no-repeat;
1516+
background-position: top left;
15161517
display: inline-block;
15171518
vertical-align: middle;
15181519
opacity: .5;
@@ -1533,10 +1534,6 @@ details.rustdoc-toggle > summary.hideme::after {
15331534
content: "";
15341535
}
15351536

1536-
details.rustdoc-toggle > summary:focus::before {
1537-
outline: dotted 1px;
1538-
}
1539-
15401537
details.rustdoc-toggle > summary:focus::before,
15411538
details.rustdoc-toggle > summary:hover::before {
15421539
opacity: 1;
@@ -1591,7 +1588,8 @@ details.rustdoc-toggle[open] > summary::before,
15911588
details.rustdoc-toggle[open] > summary.hideme::before {
15921589
width: 17px;
15931590
height: max(17px, 1.1em);
1594-
background: data-url(minus-17x17.png) no-repeat top left;
1591+
background-repeat: no-repeat;
1592+
background-position: top left;
15951593
display: inline-block;
15961594
content: "";
15971595
}
@@ -1604,18 +1602,15 @@ details.rustdoc-toggle[open] > summary.hideme::after {
16041602
details.undocumented > summary::before {
16051603
padding-left: 17px;
16061604
height: max(17px, 1.1em);
1607-
background: data-url(plus-17x17.png) no-repeat top left;
1605+
background-repeat: no-repeat;
1606+
background-position: top left;
16081607
content: "Show hidden undocumented items";
16091608
cursor: pointer;
16101609
font-size: 16px;
16111610
font-weight: 300;
16121611
opacity: .5;
16131612
}
16141613

1615-
details.undocumented > summary:focus::before {
1616-
outline: dotted 1px;
1617-
}
1618-
16191614
details.undocumented > summary:focus::before,
16201615
details.undocumented > summary:hover::before {
16211616
opacity: 1;
@@ -1624,7 +1619,8 @@ details.undocumented > summary:hover::before {
16241619
details.undocumented[open] > summary::before {
16251620
padding-left: 17px;
16261621
height: max(17px, 1.1em);
1627-
background: data-url(minus-17x17.png) no-repeat top left;
1622+
background-repeat: no-repeat
1623+
background-position: top left;
16281624
content: "Hide undocumented items";
16291625
}
16301626

-3.11 KB
Binary file not shown.

src/librustdoc/html/static/images/minus-17x17.png.url

-1
This file was deleted.
-3.26 KB
Binary file not shown.

src/librustdoc/html/static/images/plus-17x17.png.url

-1
This file was deleted.
Loading
Loading

src/librustdoc/html/static_files.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ crate static CLIPBOARD_SVG: &[u8] = include_bytes!("static/images/clipboard.svg"
4747
/// The file contents of `down-arrow.svg`, the icon used for the crate choice combobox.
4848
crate static DOWN_ARROW_SVG: &[u8] = include_bytes!("static/images/down-arrow.svg");
4949

50+
/// The file contents of `toggle-minus.svg`, the icon used for opened toggles.
51+
crate static TOGGLE_MINUS_PNG: &[u8] = include_bytes!("static/images/toggle-minus.svg");
52+
53+
/// The file contents of `toggle-plus.svg`, the icon used for closed toggles.
54+
crate static TOGGLE_PLUS_PNG: &[u8] = include_bytes!("static/images/toggle-plus.svg");
55+
5056
/// The contents of `COPYRIGHT.txt`, the license listing for files distributed with documentation
5157
/// output.
5258
crate static COPYRIGHT: &[u8] = include_bytes!("static/COPYRIGHT.txt");
@@ -64,10 +70,6 @@ crate static RUST_FAVICON_SVG: &[u8] = include_bytes!("static/images/favicon.svg
6470
crate static RUST_FAVICON_PNG_16: &[u8] = include_bytes!("static/images/favicon-16x16.png");
6571
crate static RUST_FAVICON_PNG_32: &[u8] = include_bytes!("static/images/favicon-32x32.png");
6672

67-
/// The pixel art buttons
68-
crate static MINUS_PNG_DATAURL: &str = include_str!("static/images/minus-17x17.png.url");
69-
crate static PLUS_PNG_DATAURL: &str = include_str!("static/images/plus-17x17.png.url");
70-
7173
crate static PAGE: &str = include_str!("templates/page.html");
7274

7375
/// The built-in themes given to every documentation site.

0 commit comments

Comments
 (0)