diff --git a/src/utils/html.rs b/src/utils/html.rs index be3d52c3f..2f015ae63 100644 --- a/src/utils/html.rs +++ b/src/utils/html.rs @@ -68,8 +68,18 @@ pub(crate) fn rewrite_lol( element!("body", body_handler), // Append `vendored.css` before `rustdoc.css`, so that the duplicate copy of // `normalize.css` will be overridden by the later version. + // + // Later rustdoc has `#mainThemeStyle` that could be used, but pre-2018 docs + // don't have this: + // + // https://github.com/rust-lang/rust/commit/003b2bc1c65251ec2fc80b78ed91c43fb35402ec + // + // Pre-2018 rustdoc also didn't have the resource suffix, but docs.rs was using a fork + // that had implemented it already then, so we can assume the css files are + // `/rustdoc-.css` and use the `-` to distinguish from the + // `rustdoc.static` path. element!( - "link[rel='stylesheet'][href*='rustdoc']", + "link[rel='stylesheet'][href*='rustdoc-']", |rustdoc_css: &mut Element| { rustdoc_css.before(&tera_vendored_css, ContentType::Html); Ok(()) @@ -95,3 +105,50 @@ pub(crate) fn rewrite_lol( Ok(buffer) } + +#[cfg(test)] +mod test { + use crate::test::wrapper; + + #[test] + fn rewriting_only_injects_css_once() { + wrapper(|env| { + env.fake_release() + .name("testing") + .version("0.1.0") + // A somewhat representative rustdoc html file from 2016 + .rustdoc_file_with("2016/index.html", br#" + + + + + + + + "#) + // A somewhat representative rustdoc html file from late 2022 + .rustdoc_file_with("2022/index.html", br#" + + + + + + + + + + + + "#) + .create()?; + + let output = env.frontend().get("/testing/0.1.0/2016/").send()?.text()?; + assert_eq!(output.matches(r#"href="/-/static/vendored.css"#).count(), 1); + + let output = env.frontend().get("/testing/0.1.0/2022/").send()?.text()?; + assert_eq!(output.matches(r#"href="/-/static/vendored.css"#).count(), 1); + + Ok(()) + }); + } +}