stylex_css/utils/
pre_rule.rs1use std::cmp::Ordering;
2
3pub fn sort_pseudos(pseudos: &[String]) -> Vec<String> {
4 if pseudos.len() < 2 {
5 return pseudos.to_owned();
6 }
7
8 let mut acc: Vec<Vec<String>> = Vec::new();
9
10 for pseudo in pseudos {
11 if pseudo.starts_with("::") {
12 acc.push(vec![pseudo.to_string()]);
13 } else if let Some(last_element) = acc.last_mut() {
14 if last_element.len() == 1 && !last_element[0].starts_with("::") {
15 last_element.push(pseudo.to_string());
16 } else {
17 acc.push(vec![pseudo.to_string()]);
18 }
19 } else {
20 acc.push(vec![pseudo.to_string()]);
21 }
22 }
23
24 acc
25 .into_iter()
26 .flat_map(|pseudo| {
27 if pseudo.len() > 1 {
28 let mut sorted_pseudo = pseudo.clone();
29 sorted_pseudo.sort();
30 sorted_pseudo
31 } else {
32 pseudo
33 }
34 })
35 .collect()
36}
37
38pub fn sort_at_rules(at_rules: &[String]) -> Vec<String> {
39 let mut unsorted_at_rules = at_rules.to_vec();
40
41 unsorted_at_rules.sort_by(|a, b| string_comparator(a, b));
42
43 unsorted_at_rules
44}
45
46fn string_comparator(a: &str, b: &str) -> std::cmp::Ordering {
49 if a == "default" {
50 return Ordering::Less;
51 }
52 if b == "default" {
53 return Ordering::Greater;
54 }
55 a.cmp(b)
56}