Skip to content

Commit d891849

Browse files
committed
Inline some SVG icons into rustdoc.css
This reduces the number of requests made when loading rustdoc off the web, without any significant bloat (they aren't base64-ed, and they aren't inlined into every single HTML file, either). This is currently three icons, but if we switch to font-awesome, it will probably be more.
1 parent 4459e72 commit d891849

File tree

4 files changed

+41
-20
lines changed

4 files changed

+41
-20
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4602,6 +4602,7 @@ dependencies = [
46024602
"expect-test",
46034603
"itertools 0.9.0",
46044604
"minifier",
4605+
"percent-encoding 2.1.0",
46054606
"pulldown-cmark",
46064607
"rayon",
46074608
"regex",

src/librustdoc/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ rustdoc-json-types = { path = "../rustdoc-json-types" }
2121
tracing = "0.1"
2222
tracing-tree = "0.1.9"
2323
tera = { version = "1.10.0", default-features = false }
24+
percent-encoding = "2.1"
2425

2526
[dependencies.tracing-subscriber]
2627
version = "0.2.13"

src/librustdoc/html/render/write_shared.rs

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -181,18 +181,26 @@ pub(super) fn write_shared(
181181
cx.write_shared(SharedResource::InvocationSpecific { basename: p }, content, &options.emit)
182182
};
183183

184-
// Given "foo.svg", return e.g. "url(\"foo1.58.0.svg\")"
185-
fn ver_url(cx: &Context<'_>, basename: &'static str) -> String {
186-
format!(
187-
"url(\"{}\")",
188-
SharedResource::ToolchainSpecific { basename }
189-
.path(cx)
190-
.file_name()
191-
.unwrap()
192-
.to_str()
193-
.unwrap()
194-
)
195-
}
184+
// Given "foo.svg", either include it directly in the CSS as a data URL,
185+
// or link to it, depending on the options.
186+
let css_icon_url = |basename: &'static str, icon: &[u8]| -> String {
187+
if basename.ends_with(".svg") && options.enable_minification {
188+
use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS};
189+
const DATA_URI: &AsciiSet = &CONTROLS.add(b'\'').add(b'#').add(b'?');
190+
let icn = utf8_percent_encode(std::str::from_utf8(icon).unwrap(), DATA_URI);
191+
format!("url('data:image/svg+xml,{}')", icn,)
192+
} else {
193+
format!(
194+
"url(\"{}\")",
195+
SharedResource::ToolchainSpecific { basename }
196+
.path(cx)
197+
.file_name()
198+
.unwrap()
199+
.to_str()
200+
.unwrap()
201+
)
202+
}
203+
};
196204

197205
// We use the AUTOREPLACE mechanism to inject into our static JS and CSS certain
198206
// values that are only known at doc build time. Since this mechanism is somewhat
@@ -202,10 +210,16 @@ pub(super) fn write_shared(
202210
static_files::RUSTDOC_CSS
203211
.replace(
204212
"/* AUTOREPLACE: */url(\"toggle-minus.svg\")",
205-
&ver_url(cx, "toggle-minus.svg"),
213+
&css_icon_url("toggle-minus.svg", static_files::TOGGLE_MINUS_SVG),
214+
)
215+
.replace(
216+
"/* AUTOREPLACE: */url(\"toggle-plus.svg\")",
217+
&css_icon_url("toggle-plus.svg", static_files::TOGGLE_PLUS_SVG),
206218
)
207-
.replace("/* AUTOREPLACE: */url(\"toggle-plus.svg\")", &ver_url(cx, "toggle-plus.svg"))
208-
.replace("/* AUTOREPLACE: */url(\"down-arrow.svg\")", &ver_url(cx, "down-arrow.svg")),
219+
.replace(
220+
"/* AUTOREPLACE: */url(\"down-arrow.svg\")",
221+
&css_icon_url("down-arrow.svg", static_files::DOWN_ARROW_SVG),
222+
),
209223
cx,
210224
options,
211225
)?;
@@ -250,9 +264,14 @@ pub(super) fn write_shared(
250264
write_toolchain("brush.svg", static_files::BRUSH_SVG)?;
251265
write_toolchain("wheel.svg", static_files::WHEEL_SVG)?;
252266
write_toolchain("clipboard.svg", static_files::CLIPBOARD_SVG)?;
253-
write_toolchain("down-arrow.svg", static_files::DOWN_ARROW_SVG)?;
254-
write_toolchain("toggle-minus.svg", static_files::TOGGLE_MINUS_PNG)?;
255-
write_toolchain("toggle-plus.svg", static_files::TOGGLE_PLUS_PNG)?;
267+
268+
// The following icons are embeded in the CSS file,
269+
// unless we're in tweak-the-theme-mode.
270+
if !options.enable_minification {
271+
write_toolchain("down-arrow.svg", static_files::DOWN_ARROW_SVG)?;
272+
write_toolchain("toggle-minus.svg", static_files::TOGGLE_MINUS_SVG)?;
273+
write_toolchain("toggle-plus.svg", static_files::TOGGLE_PLUS_SVG)?;
274+
}
256275

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

src/librustdoc/html/static_files.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ crate static CLIPBOARD_SVG: &[u8] = include_bytes!("static/images/clipboard.svg"
5252
crate static DOWN_ARROW_SVG: &[u8] = include_bytes!("static/images/down-arrow.svg");
5353

5454
/// The file contents of `toggle-minus.svg`, the icon used for opened toggles.
55-
crate static TOGGLE_MINUS_PNG: &[u8] = include_bytes!("static/images/toggle-minus.svg");
55+
crate static TOGGLE_MINUS_SVG: &[u8] = include_bytes!("static/images/toggle-minus.svg");
5656

5757
/// The file contents of `toggle-plus.svg`, the icon used for closed toggles.
58-
crate static TOGGLE_PLUS_PNG: &[u8] = include_bytes!("static/images/toggle-plus.svg");
58+
crate static TOGGLE_PLUS_SVG: &[u8] = include_bytes!("static/images/toggle-plus.svg");
5959

6060
/// The contents of `COPYRIGHT.txt`, the license listing for files distributed with documentation
6161
/// output.

0 commit comments

Comments
 (0)