Skip to content

Commit

Permalink
rustdoc: load icons from css instead of inline
Browse files Browse the repository at this point in the history
This cuts the HTML overhead for a page by about 1KiB,
significantly reducing the overall size of the docs bundle.
  • Loading branch information
notriddle committed Apr 10, 2024
1 parent dac788f commit 13235dc
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/librustdoc/html/render/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ impl<'tcx> Context<'tcx> {
rust_logo: has_doc_flag(self.tcx(), LOCAL_CRATE.as_def_id(), sym::rust_logo),
};
let mut page_buffer = Buffer::html();
print_item(self, it, &mut page_buffer, &page);
print_item(self, it, &mut page_buffer);
layout::render(
&clone_shared.layout,
&page,
Expand Down
14 changes: 2 additions & 12 deletions src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ use crate::html::format::{
display_fn, join_with_double_colon, print_abi_with_space, print_constness_with_space,
print_where_clause, visibility_print_with_space, Buffer, Ending, PrintWithSpace,
};
use crate::html::layout::Page;
use crate::html::highlight;
use crate::html::markdown::{HeadingOffset, MarkdownSummaryLine};
use crate::html::render::{document_full, document_item_info};
use crate::html::url_parts_builder::UrlPartsBuilder;
use crate::html::{highlight, static_files};

use askama::Template;
use itertools::Itertools;
Expand Down Expand Up @@ -157,8 +156,6 @@ struct PathComponent {
#[derive(Template)]
#[template(path = "print_item.html")]
struct ItemVars<'a> {
static_root_path: &'a str,
clipboard_svg: &'static static_files::StaticFile,
typ: &'a str,
name: &'a str,
item_type: &'a str,
Expand All @@ -178,12 +175,7 @@ fn print_where_clause_and_check<'a, 'tcx: 'a>(
len_before != buffer.len()
}

pub(super) fn print_item(
cx: &mut Context<'_>,
item: &clean::Item,
buf: &mut Buffer,
page: &Page<'_>,
) {
pub(super) fn print_item(cx: &mut Context<'_>, item: &clean::Item, buf: &mut Buffer) {
debug_assert!(!item.is_stripped());
let typ = match *item.kind {
clean::ModuleItem(_) => {
Expand Down Expand Up @@ -252,8 +244,6 @@ pub(super) fn print_item(
};

let item_vars = ItemVars {
static_root_path: &page.get_static_root_path(),
clipboard_svg: &static_files::STATIC_FILES.clipboard_svg,
typ,
name: item.name.as_ref().unwrap().as_str(),
item_type: &item.type_().to_string(),
Expand Down
20 changes: 18 additions & 2 deletions src/librustdoc/html/static/css/rustdoc.css
Original file line number Diff line number Diff line change
Expand Up @@ -1603,6 +1603,16 @@ a.tooltip:hover::after {
border-color: var(--settings-button-border-focus);
}

#settings-menu > a {
line-height: 0;
font-size: 0;
}
#settings-menu > a:before {
content: url('wheel-63255fc4502dca9a.svg');
width: 22px;
height: 22px;
}

#sidebar-button > a:before {
content: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 22 22" \
fill="none" stroke="black">\
Expand All @@ -1622,11 +1632,17 @@ a.tooltip:hover::after {
padding-left: 2px;
border: 0;
width: 33px;
line-height: 0;
font-size: 0;
}
#copy-path > img {

#copy-path:before {
filter: var(--copy-path-img-filter);
content: url('clipboard-24048e6d87f63d07.svg');
width: 19px;
height: 18px;
}
#copy-path:hover > img {
#copy-path:hover:before {
filter: var(--copy-path-img-hover-filter);
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/static/images/clipboard.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/librustdoc/html/static/images/wheel.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 1 addition & 2 deletions src/librustdoc/html/templates/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ <h2>Files</h2> {# #}
</div> {# #}
<div id="settings-menu" tabindex="-1"> {# #}
<a href="{{page.root_path|safe}}settings.html" title="settings"> {# #}
<img width="22" height="22" alt="Change settings" {#+ #}
src="{{static_root_path|safe}}{{files.wheel_svg}}"> {# #}
Settings {# #}
</a> {# #}
</div> {# #}
</form> {# #}
Expand Down
4 changes: 1 addition & 3 deletions src/librustdoc/html/templates/print_item.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ <h1>
{% endfor %}
<a class="{{item_type}}" href="#">{{name}}</a> {# #}
<button id="copy-path" title="Copy item path to clipboard"> {# #}
<img src="{{static_root_path|safe}}{{clipboard_svg}}" {#+ #}
width="19" height="18" {#+ #}
alt="Copy item path"> {# #}
Copy item path {# #}
</button> {# #}
</h1> {# #}
<span class="out-of-band">
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/templates/sidebar.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ <h2 class="location"> {# #}
{% endif %}
<div class="sidebar-elems">
{% if is_crate %}
<ul class="block">
<ul class="block"> {# #}
<li><a id="all-types" href="all.html">All Items</a></li> {# #}
</ul>
{% endif %}
Expand Down

0 comments on commit 13235dc

Please sign in to comment.