Skip to content

Commit d9afca5

Browse files
committed
Consistentize the system for image URLs in CSS.
1 parent f0683f9 commit d9afca5

File tree

6 files changed

+36
-37
lines changed

6 files changed

+36
-37
lines changed

src/librustdoc/html/layout.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,12 @@ crate fn render<T: Print, S: Print>(
6767
) -> String {
6868
let static_root_path = page.get_static_root_path();
6969
let krate_with_trailing_slash = ensure_trailing_slash(&layout.krate).to_string();
70-
let themes = style_files
70+
let mut themes: Vec<String> = style_files
7171
.iter()
7272
.map(StylePath::basename)
7373
.collect::<Result<_, Error>>()
7474
.unwrap_or_default();
75+
themes.sort();
7576
let rustdoc_version = rustc_interface::util::version_str().unwrap_or("unknown version");
7677
let content = Buffer::html().to_display(t); // Note: This must happen before making the sidebar.
7778
let sidebar = Buffer::html().to_display(sidebar);

src/librustdoc/html/render/write_shared.rs

+20-28
Original file line numberDiff line numberDiff line change
@@ -181,42 +181,34 @@ pub(super) fn write_shared(
181181
cx.write_shared(SharedResource::InvocationSpecific { basename: p }, content, &options.emit)
182182
};
183183

184-
fn add_background_image_to_css(
185-
cx: &Context<'_>,
186-
css: &mut String,
187-
rule: &str,
188-
file: &'static str,
189-
) {
190-
css.push_str(&format!(
191-
"{} {{ background-image: url({}); }}",
192-
rule,
193-
SharedResource::ToolchainSpecific { basename: file }
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 }
194189
.path(cx)
195190
.file_name()
196191
.unwrap()
197192
.to_str()
198193
.unwrap()
199-
))
194+
)
200195
}
201196

202-
// Add all the static files. These may already exist, but we just
203-
// overwrite them anyway to make sure that they're fresh and up-to-date.
204-
let mut rustdoc_css = static_files::RUSTDOC_CSS.to_owned();
205-
add_background_image_to_css(
206-
cx,
207-
&mut rustdoc_css,
208-
"details.undocumented[open] > summary::before, \
209-
details.rustdoc-toggle[open] > summary::before, \
210-
details.rustdoc-toggle[open] > summary.hideme::before",
211-
"toggle-minus.svg",
212-
);
213-
add_background_image_to_css(
197+
// We use the AUTOREPLACE mechanism to inject into our static JS and CSS certain
198+
// values that are only known at doc build time. Since this mechanism is somewhat
199+
// surprising when reading the code, please limit it to rustdoc.css.
200+
write_minify(
201+
"rustdoc.css",
202+
static_files::RUSTDOC_CSS
203+
.replace(
204+
"/* AUTOREPLACE: */url(\"toggle-minus.svg\")",
205+
&ver_url(cx, "toggle-minus.svg"),
206+
)
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")),
214209
cx,
215-
&mut rustdoc_css,
216-
"details.undocumented > summary::before, details.rustdoc-toggle > summary::before",
217-
"toggle-plus.svg",
218-
);
219-
write_minify("rustdoc.css", rustdoc_css, cx, options)?;
210+
options,
211+
)?;
220212

221213
// Add all the static files. These may already exist, but we just
222214
// overwrite them anyway to make sure that they're fresh and up-to-date.

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

+11
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,7 @@ h2.small-section-header > .anchor {
824824
background-color: transparent;
825825
background-size: 20px;
826826
background-position: calc(100% - 1px) 56%;
827+
background-image: /* AUTOREPLACE: */url("down-arrow.svg");
827828
}
828829
.search-container > .top-button {
829830
position: absolute;
@@ -1604,6 +1605,16 @@ details.rustdoc-toggle[open] > summary.hideme > span {
16041605
display: none;
16051606
}
16061607

1608+
details.undocumented[open] > summary::before,
1609+
details.rustdoc-toggle[open] > summary::before,
1610+
details.rustdoc-toggle[open] > summary.hideme::before {
1611+
background-image: /* AUTOREPLACE: */url("toggle-minus.svg");
1612+
}
1613+
1614+
details.undocumented > summary::before, details.rustdoc-toggle > summary::before {
1615+
background-image: /* AUTOREPLACE: */url("toggle-plus.svg");
1616+
}
1617+
16071618
details.rustdoc-toggle[open] > summary::before,
16081619
details.rustdoc-toggle[open] > summary.hideme::before {
16091620
width: 17px;

src/librustdoc/html/static/js/main.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ function resourcePath(basename, extension) {
5858
(function () {
5959
window.rootPath = getVar("root-path");
6060
window.currentCrate = getVar("current-crate");
61-
window.searchJS = resourcePath("search", "js");
62-
window.searchIndexJS = resourcePath("search-index", "js");
61+
window.searchJS = resourcePath("search", ".js");
62+
window.searchIndexJS = resourcePath("search-index", ".js");
6363
var sidebarVars = document.getElementById("sidebar-vars");
6464
if (sidebarVars) {
6565
window.sidebarCurrent = {

src/librustdoc/html/templates/page.html

-5
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,6 @@
5858
href="{{static_root_path | safe}}favicon{{page.resource_suffix}}.svg"> {#- -#}
5959
{%- endif -%}
6060
{{- layout.external_html.in_header | safe -}}
61-
<style type="text/css"> {#- -#}
62-
#crate-search{ {#- -#}
63-
background-image:url("{{static_root_path | safe}}down-arrow{{page.resource_suffix}}.svg"); {#- -#}
64-
} {#- -#}
65-
</style> {#- -#}
6661
</head> {#- -#}
6762
<body class="rustdoc {{page.css_class}}"> {#- -#}
6863
<!--[if lte IE 11]> {#- -#}

src/test/rustdoc/static-root-path.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// @has static_root_path/struct.SomeStruct.html
44
// @matches - '"/cache/main\.js"'
55
// @!matches - '"\.\./main\.js"'
6-
// @matches - '"\.\./search-index\.js"'
6+
// @matches - 'data-root-path="\.\./"'
77
// @!matches - '"/cache/search-index\.js"'
88
pub struct SomeStruct;
99

0 commit comments

Comments
 (0)