Skip to content

Commit e7d8ad6

Browse files
Add check for missing CSS variables
1 parent 0b037c1 commit e7d8ad6

File tree

2 files changed

+56
-10
lines changed

2 files changed

+56
-10
lines changed

src/librustdoc/theme.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,18 @@ pub(crate) fn get_differences(
202202
) {
203203
for (selector, entry) in origin.iter() {
204204
match against.get(selector) {
205-
Some(a) => get_differences(&a.children, &entry.children, v),
206-
None => v.push(format!(" Missing rule `{}`", selector)),
205+
Some(a) => {
206+
get_differences(&entry.children, &a.children, v);
207+
if selector == ":root" {
208+
// We need to check that all variables have been set.
209+
for rule in entry.rules.keys() {
210+
if !a.rules.contains_key(rule) {
211+
v.push(format!(" Missing CSS variable `{rule}` in `:root`"));
212+
}
213+
}
214+
}
215+
}
216+
None => v.push(format!(" Missing rule `{selector}`")),
207217
}
208218
}
209219
}

src/librustdoc/theme/tests.rs

+44-8
Original file line numberDiff line numberDiff line change
@@ -63,26 +63,26 @@ d {}
6363

6464
#[test]
6565
fn test_comparison() {
66-
let x = r#"
66+
let origin = r#"
6767
@a {
6868
b {}
69+
c {}
6970
}
7071
"#;
7172

72-
let y = r#"
73+
let against = r#"
7374
@a {
7475
b {}
75-
c {}
7676
}
7777
"#;
7878

79-
let against = load_css_paths(y).unwrap();
80-
let other = load_css_paths(x).unwrap();
79+
let origin = load_css_paths(origin).unwrap();
80+
let against = load_css_paths(against).unwrap();
8181

8282
let mut ret = Vec::new();
83-
get_differences(&against, &other, &mut ret);
83+
get_differences(&against, &origin, &mut ret);
8484
assert!(ret.is_empty());
85-
get_differences(&other, &against, &mut ret);
85+
get_differences(&origin, &against, &mut ret);
8686
assert_eq!(ret, vec![" Missing rule `c`".to_owned()]);
8787
}
8888

@@ -123,13 +123,49 @@ fn test_media() {
123123
x: y;
124124
}
125125
}
126+
127+
@media (max-width: 1001px) {
128+
b {
129+
x: y;
130+
}
131+
}
126132
"#;
127133

128134
let paths = load_css_paths(text).unwrap();
129-
eprintln!("{:?}", paths);
130135
let p = paths.get("@media (min-width:701px)");
131136
assert!(p.is_some());
132137
let p = p.unwrap();
133138
assert!(p.children.get("a:hover").is_some());
134139
assert!(p.children.get("b").is_some());
140+
141+
eprintln!("{:?}", paths);
142+
let p = paths.get("@media (max-width:1001px)");
143+
assert!(p.is_some());
144+
let p = p.unwrap();
145+
assert!(p.children.get("b").is_some());
146+
}
147+
148+
#[test]
149+
fn test_css_variables() {
150+
let x = r#"
151+
:root {
152+
--a: #fff;
153+
}
154+
"#;
155+
156+
let y = r#"
157+
:root {
158+
--a: #fff;
159+
--b: #fff;
160+
}
161+
"#;
162+
163+
let against = load_css_paths(x).unwrap();
164+
let other = load_css_paths(y).unwrap();
165+
166+
let mut ret = Vec::new();
167+
get_differences(&against, &other, &mut ret);
168+
assert!(ret.is_empty());
169+
get_differences(&other, &against, &mut ret);
170+
assert_eq!(ret, vec![" Missing CSS variable `--b` in `:root`".to_owned()]);
135171
}

0 commit comments

Comments
 (0)