Add check for missing CSS variables

This commit is contained in:
Guillaume Gomez 2022-09-14 18:36:48 +02:00
parent 0b037c17b8
commit e7d8ad62db
2 changed files with 60 additions and 14 deletions

View File

@ -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}`")),
}
}
}

View File

@ -63,26 +63,26 @@ d {}
#[test]
fn test_comparison() {
let x = r#"
@a {
b {}
}
"#;
let y = r#"
let origin = r#"
@a {
b {}
c {}
}
"#;
let against = load_css_paths(y).unwrap();
let other = load_css_paths(x).unwrap();
let against = r#"
@a {
b {}
}
"#;
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()]);
}