From c8e567ddc82bed8aac617741b799d24184358fcd Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 12 Feb 2020 14:39:22 +0100 Subject: [PATCH 1/2] Minify CSS rules to be able to handle minified theme files as well --- src/librustdoc/theme.rs | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/librustdoc/theme.rs b/src/librustdoc/theme.rs index af1c50acb0a35..eb8eabe1c03eb 100644 --- a/src/librustdoc/theme.rs +++ b/src/librustdoc/theme.rs @@ -179,20 +179,23 @@ fn get_previous_positions(events: &[Events], mut pos: usize) -> Vec { } fn build_rule(v: &[u8], positions: &[usize]) -> String { - positions - .chunks(2) - .map(|x| ::std::str::from_utf8(&v[x[0]..x[1]]).unwrap_or("")) - .collect::() - .trim() - .replace("\n", " ") - .replace("/", "") - .replace("\t", " ") - .replace("{", "") - .replace("}", "") - .split(' ') - .filter(|s| s.len() > 0) - .collect::>() - .join(" ") + minifier::css::minify( + &positions + .chunks(2) + .map(|x| ::std::str::from_utf8(&v[x[0]..x[1]]).unwrap_or("")) + .collect::() + .trim() + .replace("\n", " ") + .replace("/", "") + .replace("\t", " ") + .replace("{", "") + .replace("}", "") + .split(' ') + .filter(|s| s.len() > 0) + .collect::>() + .join(" "), + ) + .unwrap_or_else(|_| String::new()) } fn inner(v: &[u8], events: &[Events], pos: &mut usize) -> FxHashSet { From 109260f8f0d97d6f1aacefdee806103eac5144eb Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 12 Feb 2020 14:39:40 +0100 Subject: [PATCH 2/2] Add test to check if minified theme are handled correctly --- src/librustdoc/theme/tests.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/librustdoc/theme/tests.rs b/src/librustdoc/theme/tests.rs index 9ead1d28007c1..b924215733d55 100644 --- a/src/librustdoc/theme/tests.rs +++ b/src/librustdoc/theme/tests.rs @@ -102,3 +102,16 @@ fn check_invalid_css() { let events = load_css_events(b"*"); assert_eq!(events.len(), 0); } + +#[test] +fn test_with_minification() { + let text = include_str!("../html/static/themes/dark.css"); + let minified = minifier::css::minify(&text).expect("CSS minification failed"); + + let against = load_css_paths(text.as_bytes()); + let other = load_css_paths(minified.as_bytes()); + + let mut ret = Vec::new(); + get_differences(&against, &other, &mut ret); + assert!(ret.is_empty()); +}