about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume.gomez@huawei.com>2022-09-14 18:36:48 +0200
committerGuillaume Gomez <guillaume.gomez@huawei.com>2022-09-14 19:36:03 +0200
commite7d8ad62db3af48ce8fb3844fd44be6251e1cc15 (patch)
tree7f8951d04b2edb63c0923307773af4af4e4e4496
parent0b037c17b867cc8d47c0391b3d681fbf5a66ce80 (diff)
downloadrust-e7d8ad62db3af48ce8fb3844fd44be6251e1cc15.tar.gz
rust-e7d8ad62db3af48ce8fb3844fd44be6251e1cc15.zip
Add check for missing CSS variables
-rw-r--r--src/librustdoc/theme.rs14
-rw-r--r--src/librustdoc/theme/tests.rs52
2 files changed, 56 insertions, 10 deletions
diff --git a/src/librustdoc/theme.rs b/src/librustdoc/theme.rs
index 77f8359bd42..87cfe78e10c 100644
--- a/src/librustdoc/theme.rs
+++ b/src/librustdoc/theme.rs
@@ -202,8 +202,18 @@ pub(crate) fn get_differences(
 ) {
     for (selector, entry) in origin.iter() {
         match against.get(selector) {
-            Some(a) => get_differences(&a.children, &entry.children, v),
-            None => v.push(format!("  Missing rule `{}`", selector)),
+            Some(a) => {
+                get_differences(&entry.children, &a.children, v);
+                if selector == ":root" {
+                    // We need to check that all variables have been set.
+                    for rule in entry.rules.keys() {
+                        if !a.rules.contains_key(rule) {
+                            v.push(format!("  Missing CSS variable `{rule}` in `:root`"));
+                        }
+                    }
+                }
+            }
+            None => v.push(format!("  Missing rule `{selector}`")),
         }
     }
 }
diff --git a/src/librustdoc/theme/tests.rs b/src/librustdoc/theme/tests.rs
index 54226c6cf3b..ce1d77d98d7 100644
--- a/src/librustdoc/theme/tests.rs
+++ b/src/librustdoc/theme/tests.rs
@@ -63,26 +63,26 @@ d {}
 
 #[test]
 fn test_comparison() {
-    let x = r#"
+    let origin = r#"
 @a {
     b {}
+    c {}
 }
 "#;
 
-    let y = r#"
+    let against = r#"
 @a {
     b {}
-    c {}
 }
 "#;
 
-    let against = load_css_paths(y).unwrap();
-    let other = load_css_paths(x).unwrap();
+    let origin = load_css_paths(origin).unwrap();
+    let against = load_css_paths(against).unwrap();
 
     let mut ret = Vec::new();
-    get_differences(&against, &other, &mut ret);
+    get_differences(&against, &origin, &mut ret);
     assert!(ret.is_empty());
-    get_differences(&other, &against, &mut ret);
+    get_differences(&origin, &against, &mut ret);
     assert_eq!(ret, vec!["  Missing rule `c`".to_owned()]);
 }
 
@@ -123,13 +123,49 @@ fn test_media() {
         x: y;
     }
 }
+
+@media (max-width: 1001px) {
+    b {
+        x: y;
+    }
+}
 "#;
 
     let paths = load_css_paths(text).unwrap();
-    eprintln!("{:?}", paths);
     let p = paths.get("@media (min-width:701px)");
     assert!(p.is_some());
     let p = p.unwrap();
     assert!(p.children.get("a:hover").is_some());
     assert!(p.children.get("b").is_some());
+
+    eprintln!("{:?}", paths);
+    let p = paths.get("@media (max-width:1001px)");
+    assert!(p.is_some());
+    let p = p.unwrap();
+    assert!(p.children.get("b").is_some());
+}
+
+#[test]
+fn test_css_variables() {
+    let x = r#"
+:root {
+    --a: #fff;
+}
+"#;
+
+    let y = r#"
+:root {
+    --a: #fff;
+    --b: #fff;
+}
+"#;
+
+    let against = load_css_paths(x).unwrap();
+    let other = load_css_paths(y).unwrap();
+
+    let mut ret = Vec::new();
+    get_differences(&against, &other, &mut ret);
+    assert!(ret.is_empty());
+    get_differences(&other, &against, &mut ret);
+    assert_eq!(ret, vec!["  Missing CSS variable `--b` in `:root`".to_owned()]);
 }