Skip to content

Commit

Permalink
Simplify usage of CSS background-image
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Aug 3, 2021
1 parent 64c9903 commit 6fe0972
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 24 deletions.
47 changes: 41 additions & 6 deletions src/librustdoc/html/render/write_shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,13 @@ impl Context<'_> {
emit: &[EmitType],
) -> Result<(), Error> {
let tmp;
let contents =
contents.replace("data-url(minus-17x17.png)", static_files::MINUS_PNG_DATAURL);
let contents = contents.replace("data-url(plus-17x17.png)", static_files::PLUS_PNG_DATAURL);
let contents = if minify {
tmp = if resource.extension() == Some(&OsStr::new("css")) {
minifier::css::minify(&contents).map_err(|e| {
minifier::css::minify(contents).map_err(|e| {
Error::new(format!("failed to minify CSS file: {}", e), resource.path(self))
})?
} else {
minifier::js::minify(&contents)
minifier::js::minify(contents)
};
tmp.as_bytes()
} else {
Expand Down Expand Up @@ -178,9 +175,45 @@ pub(super) fn write_shared(
cx.write_shared(SharedResource::InvocationSpecific { basename: p }, content, &options.emit)
};

fn add_background_image_to_css(
cx: &Context<'_>,
css: &mut String,
rule: &str,
file: &'static str,
) {
css.push_str(&format!(
"{} {{ background-image: url({}); }}",
rule,
SharedResource::ToolchainSpecific { basename: file }
.path(cx)
.file_name()
.unwrap()
.to_str()
.unwrap()
))
}

// Add all the static files. These may already exist, but we just
// overwrite them anyway to make sure that they're fresh and up-to-date.
let mut rustdoc_css = static_files::RUSTDOC_CSS.to_owned();
add_background_image_to_css(
cx,
&mut rustdoc_css,
"details.undocumented[open] > summary::before, \
details.rustdoc-toggle[open] > summary::before, \
details.rustdoc-toggle[open] > summary.hideme::before",
"toggle-minus.svg",
);
add_background_image_to_css(
cx,
&mut rustdoc_css,
"details.undocumented > summary::before, details.rustdoc-toggle > summary::before",
"toggle-plus.svg",
);
write_minify("rustdoc.css", &rustdoc_css)?;

// Add all the static files. These may already exist, but we just
// overwrite them anyway to make sure that they're fresh and up-to-date.
write_minify("rustdoc.css", static_files::RUSTDOC_CSS)?;
write_minify("settings.css", static_files::SETTINGS_CSS)?;
write_minify("noscript.css", static_files::NOSCRIPT_CSS)?;

Expand Down Expand Up @@ -220,6 +253,8 @@ pub(super) fn write_shared(
write_toolchain("wheel.svg", static_files::WHEEL_SVG)?;
write_toolchain("clipboard.svg", static_files::CLIPBOARD_SVG)?;
write_toolchain("down-arrow.svg", static_files::DOWN_ARROW_SVG)?;
write_toolchain("toggle-minus.svg", static_files::TOGGLE_MINUS_PNG)?;
write_toolchain("toggle-plus.svg", static_files::TOGGLE_PLUS_PNG)?;

let mut themes: Vec<&String> = themes.iter().collect();
themes.sort();
Expand Down
20 changes: 8 additions & 12 deletions src/librustdoc/html/static/css/rustdoc.css
Original file line number Diff line number Diff line change
Expand Up @@ -1512,7 +1512,8 @@ details.rustdoc-toggle > summary::before {
cursor: pointer;
width: 17px;
height: max(17px, 1.1em);
background: data-url(plus-17x17.png) no-repeat top left;
background-repeat: no-repeat;
background-position: top left;
display: inline-block;
vertical-align: middle;
opacity: .5;
Expand All @@ -1533,10 +1534,6 @@ details.rustdoc-toggle > summary.hideme::after {
content: "";
}

details.rustdoc-toggle > summary:focus::before {
outline: dotted 1px;
}

details.rustdoc-toggle > summary:focus::before,
details.rustdoc-toggle > summary:hover::before {
opacity: 1;
Expand Down Expand Up @@ -1591,7 +1588,8 @@ details.rustdoc-toggle[open] > summary::before,
details.rustdoc-toggle[open] > summary.hideme::before {
width: 17px;
height: max(17px, 1.1em);
background: data-url(minus-17x17.png) no-repeat top left;
background-repeat: no-repeat;
background-position: top left;
display: inline-block;
content: "";
}
Expand All @@ -1604,18 +1602,15 @@ details.rustdoc-toggle[open] > summary.hideme::after {
details.undocumented > summary::before {
padding-left: 17px;
height: max(17px, 1.1em);
background: data-url(plus-17x17.png) no-repeat top left;
background-repeat: no-repeat;
background-position: top left;
content: "Show hidden undocumented items";
cursor: pointer;
font-size: 16px;
font-weight: 300;
opacity: .5;
}

details.undocumented > summary:focus::before {
outline: dotted 1px;
}

details.undocumented > summary:focus::before,
details.undocumented > summary:hover::before {
opacity: 1;
Expand All @@ -1624,7 +1619,8 @@ details.undocumented > summary:hover::before {
details.undocumented[open] > summary::before {
padding-left: 17px;
height: max(17px, 1.1em);
background: data-url(minus-17x17.png) no-repeat top left;
background-repeat: no-repeat
background-position: top left;
content: "Hide undocumented items";
}

Expand Down
Binary file removed src/librustdoc/html/static/images/minus-17x17.png
Binary file not shown.
1 change: 0 additions & 1 deletion src/librustdoc/html/static/images/minus-17x17.png.url

This file was deleted.

Binary file removed src/librustdoc/html/static/images/plus-17x17.png
Binary file not shown.
1 change: 0 additions & 1 deletion src/librustdoc/html/static/images/plus-17x17.png.url

This file was deleted.

1 change: 1 addition & 0 deletions src/librustdoc/html/static/images/toggle-minus.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/librustdoc/html/static/images/toggle-plus.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 6 additions & 4 deletions src/librustdoc/html/static_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ crate static CLIPBOARD_SVG: &[u8] = include_bytes!("static/images/clipboard.svg"
/// The file contents of `down-arrow.svg`, the icon used for the crate choice combobox.
crate static DOWN_ARROW_SVG: &[u8] = include_bytes!("static/images/down-arrow.svg");

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

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

/// The contents of `COPYRIGHT.txt`, the license listing for files distributed with documentation
/// output.
crate static COPYRIGHT: &[u8] = include_bytes!("static/COPYRIGHT.txt");
Expand All @@ -64,10 +70,6 @@ crate static RUST_FAVICON_SVG: &[u8] = include_bytes!("static/images/favicon.svg
crate static RUST_FAVICON_PNG_16: &[u8] = include_bytes!("static/images/favicon-16x16.png");
crate static RUST_FAVICON_PNG_32: &[u8] = include_bytes!("static/images/favicon-32x32.png");

/// The pixel art buttons
crate static MINUS_PNG_DATAURL: &str = include_str!("static/images/minus-17x17.png.url");
crate static PLUS_PNG_DATAURL: &str = include_str!("static/images/plus-17x17.png.url");

crate static PAGE: &str = include_str!("templates/page.html");

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

0 comments on commit 6fe0972

Please sign in to comment.