| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
use crate::list::{CELL_PART_CLASSES, ROW_PART_CLASSES}; |
| 38 |
use crate::{Emit, option_class}; |
| 39 |
use makeover_layout::Selector; |
| 40 |
use std::collections::BTreeSet; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
#[must_use] |
| 50 |
pub fn vocabulary(opts: &Emit) -> BTreeSet<String> { |
| 51 |
classes_in_css(&crate::stylesheet(opts)) |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
#[must_use] |
| 61 |
pub fn names(opts: &Emit) -> BTreeSet<String> { |
| 62 |
let mut all = vocabulary(opts); |
| 63 |
all.extend( |
| 64 |
ROW_PART_CLASSES |
| 65 |
.iter() |
| 66 |
.chain(CELL_PART_CLASSES) |
| 67 |
.map(|name| crate::class(name, opts)), |
| 68 |
); |
| 69 |
all.extend( |
| 70 |
[Selector::Tabs, Selector::Segmented, Selector::Toggle] |
| 71 |
.into_iter() |
| 72 |
.map(|s| crate::class(option_class(s), opts)), |
| 73 |
); |
| 74 |
all |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
#[must_use] |
| 90 |
pub fn classes_in_css(css: &str) -> BTreeSet<String> { |
| 91 |
let mut found = BTreeSet::new(); |
| 92 |
|
| 93 |
|
| 94 |
let mut blocks: Vec<bool> = Vec::new(); |
| 95 |
|
| 96 |
|
| 97 |
let mut prelude = String::new(); |
| 98 |
|
| 99 |
let mut chars = css.chars().peekable(); |
| 100 |
while let Some(c) = chars.next() { |
| 101 |
match c { |
| 102 |
'/' if chars.peek() == Some(&'*') => { |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
chars.next(); |
| 107 |
let mut star = false; |
| 108 |
for c in chars.by_ref() { |
| 109 |
if star && c == '/' { |
| 110 |
break; |
| 111 |
} |
| 112 |
star = c == '*'; |
| 113 |
} |
| 114 |
prelude.clear(); |
| 115 |
} |
| 116 |
'"' | '\'' => { |
| 117 |
|
| 118 |
|
| 119 |
let quote = c; |
| 120 |
let mut escaped = false; |
| 121 |
for c in chars.by_ref() { |
| 122 |
if escaped { |
| 123 |
escaped = false; |
| 124 |
} else if c == '\\' { |
| 125 |
escaped = true; |
| 126 |
} else if c == quote { |
| 127 |
break; |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
'{' => { |
| 132 |
let declarations = !prelude.trim_start().starts_with('@'); |
| 133 |
if declarations { |
| 134 |
found.extend(classes_in_selector(&prelude)); |
| 135 |
} |
| 136 |
blocks.push(declarations); |
| 137 |
prelude.clear(); |
| 138 |
} |
| 139 |
'}' => { |
| 140 |
blocks.pop(); |
| 141 |
prelude.clear(); |
| 142 |
} |
| 143 |
';' => prelude.clear(), |
| 144 |
|
| 145 |
|
| 146 |
_ if !blocks.last().copied().unwrap_or(false) => prelude.push(c), |
| 147 |
_ => {} |
| 148 |
} |
| 149 |
} |
| 150 |
found |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
fn classes_in_selector(selector: &str) -> Vec<String> { |
| 155 |
let chars: Vec<char> = selector.chars().collect(); |
| 156 |
let mut names = Vec::new(); |
| 157 |
let mut i = 0; |
| 158 |
while i < chars.len() { |
| 159 |
|
| 160 |
|
| 161 |
if chars[i] == '.' |
| 162 |
&& chars |
| 163 |
.get(i + 1) |
| 164 |
.is_some_and(|c| c.is_alphabetic() || *c == '_') |
| 165 |
{ |
| 166 |
let start = i + 1; |
| 167 |
let mut end = start; |
| 168 |
while end < chars.len() |
| 169 |
&& (chars[end].is_alphanumeric() || chars[end] == '-' || chars[end] == '_') |
| 170 |
{ |
| 171 |
end += 1; |
| 172 |
} |
| 173 |
names.push(chars[start..end].iter().collect()); |
| 174 |
i = end; |
| 175 |
} else { |
| 176 |
i += 1; |
| 177 |
} |
| 178 |
} |
| 179 |
names |
| 180 |
} |
| 181 |
|
| 182 |
#[cfg(test)] |
| 183 |
mod tests { |
| 184 |
use super::*; |
| 185 |
use crate::list::{cell_part_class, part_class}; |
| 186 |
use makeover_layout::{CellPart, RowPart}; |
| 187 |
|
| 188 |
#[test] |
| 189 |
fn the_scrape_finds_the_components_the_sheet_is_built_from() { |
| 190 |
let v = vocabulary(&Emit::default()); |
| 191 |
assert!( |
| 192 |
v.len() > 20, |
| 193 |
"scraped {} classes, which reads as a parser failure rather than a small sheet", |
| 194 |
v.len() |
| 195 |
); |
| 196 |
for name in ["card", "tab", "table-heading", "cell-value", "chosen"] { |
| 197 |
assert!( |
| 198 |
v.contains(name), |
| 199 |
"the sheet defines .{name} and the scan missed it" |
| 200 |
); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
#[test] |
| 205 |
fn every_name_a_caller_can_ask_for_is_one_this_crate_admits_to() { |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
let opts = Emit::default(); |
| 211 |
let all = names(&opts); |
| 212 |
|
| 213 |
for selector in [Selector::Tabs, Selector::Segmented, Selector::Toggle] { |
| 214 |
let name = option_class(selector); |
| 215 |
assert!( |
| 216 |
all.contains(name), |
| 217 |
"option_class({selector:?}) is .{name}, which nothing admits to" |
| 218 |
); |
| 219 |
} |
| 220 |
for part in [ |
| 221 |
RowPart::Primary, |
| 222 |
RowPart::Secondary, |
| 223 |
RowPart::Meta, |
| 224 |
RowPart::Actions, |
| 225 |
RowPart::Tokens, |
| 226 |
RowPart::Proportion, |
| 227 |
] { |
| 228 |
let name = part_class(part); |
| 229 |
assert!( |
| 230 |
all.contains(name), |
| 231 |
"part_class({part:?}) is .{name}, which nothing admits to" |
| 232 |
); |
| 233 |
} |
| 234 |
for part in [ |
| 235 |
CellPart::Value, |
| 236 |
CellPart::Tokens, |
| 237 |
CellPart::Actions, |
| 238 |
CellPart::Link, |
| 239 |
] { |
| 240 |
let name = cell_part_class(part); |
| 241 |
assert!( |
| 242 |
all.contains(name), |
| 243 |
"cell_part_class({part:?}) is .{name}, which nothing admits to" |
| 244 |
); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
#[test] |
| 249 |
fn the_part_lists_hold_every_arm_of_the_match_beside_them() { |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
for part in [ |
| 254 |
RowPart::Primary, |
| 255 |
RowPart::Secondary, |
| 256 |
RowPart::Meta, |
| 257 |
RowPart::Actions, |
| 258 |
RowPart::Tokens, |
| 259 |
RowPart::Proportion, |
| 260 |
] { |
| 261 |
assert!( |
| 262 |
ROW_PART_CLASSES.contains(&part_class(part)), |
| 263 |
"{part:?} is missing from ROW_PART_CLASSES" |
| 264 |
); |
| 265 |
} |
| 266 |
for part in [ |
| 267 |
CellPart::Value, |
| 268 |
CellPart::Tokens, |
| 269 |
CellPart::Actions, |
| 270 |
CellPart::Link, |
| 271 |
] { |
| 272 |
assert!( |
| 273 |
CELL_PART_CLASSES.contains(&cell_part_class(part)), |
| 274 |
"{part:?} is missing from CELL_PART_CLASSES" |
| 275 |
); |
| 276 |
} |
| 277 |
|
| 278 |
assert!(ROW_PART_CLASSES.contains(&"row-part")); |
| 279 |
assert!(CELL_PART_CLASSES.contains(&"cell-part")); |
| 280 |
} |
| 281 |
|
| 282 |
#[test] |
| 283 |
fn a_prefix_moves_the_component_classes_and_leaves_the_states_qualifying_them() { |
| 284 |
let plain = vocabulary(&Emit::default()); |
| 285 |
let prefixed = vocabulary(&Emit { |
| 286 |
class_prefix: "mo-", |
| 287 |
..Emit::default() |
| 288 |
}); |
| 289 |
assert_eq!( |
| 290 |
plain.len(), |
| 291 |
prefixed.len(), |
| 292 |
"a prefix changed how many classes exist" |
| 293 |
); |
| 294 |
|
| 295 |
|
| 296 |
let states = ["chosen", "latched"]; |
| 297 |
for name in &plain { |
| 298 |
let expected = if states.contains(&name.as_str()) { |
| 299 |
name.clone() |
| 300 |
} else { |
| 301 |
format!("mo-{name}") |
| 302 |
}; |
| 303 |
assert!( |
| 304 |
prefixed.contains(&expected), |
| 305 |
".{name} did not move to .{expected} under the prefix" |
| 306 |
); |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
#[test] |
| 311 |
fn a_declaration_value_holding_a_dot_is_not_read_as_a_class() { |
| 312 |
let found = classes_in_css(".real { transition: .2s ease; margin: 0.5rem; }"); |
| 313 |
assert_eq!(found, ["real".to_string()].into_iter().collect()); |
| 314 |
} |
| 315 |
|
| 316 |
#[test] |
| 317 |
fn an_at_rule_does_not_hide_the_selectors_inside_it() { |
| 318 |
let found = classes_in_css( |
| 319 |
"@layer makeover { @media (min-width: 40rem) { .wide { color: red; } } }", |
| 320 |
); |
| 321 |
assert_eq!(found, ["wide".to_string()].into_iter().collect()); |
| 322 |
} |
| 323 |
|
| 324 |
#[test] |
| 325 |
fn a_string_is_opaque_and_a_comment_contributes_nothing() { |
| 326 |
let found = classes_in_css("/* .notaclass */ .caret::after { content: \"} .alsonot\"; }"); |
| 327 |
assert_eq!(found, ["caret".to_string()].into_iter().collect()); |
| 328 |
} |
| 329 |
|
| 330 |
#[test] |
| 331 |
fn a_compound_selector_yields_every_class_it_names() { |
| 332 |
let found = classes_in_css( |
| 333 |
".tab.chosen[aria-sort=\"ascending\"] > .label:not(.muted) { color: red; }", |
| 334 |
); |
| 335 |
let expected: BTreeSet<String> = ["tab", "chosen", "label", "muted"] |
| 336 |
.into_iter() |
| 337 |
.map(String::from) |
| 338 |
.collect(); |
| 339 |
assert_eq!(found, expected); |
| 340 |
} |
| 341 |
} |
| 342 |
|