| 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::facet::FACET_CLASSES; |
| 38 |
use crate::figure::FIGURE_CLASSES; |
| 39 |
use crate::form::{FIELD_CLASSES, FIELD_STATE_CLASSES}; |
| 40 |
use crate::list::{ |
| 41 |
CELL_DROP_CLASSES, CELL_PART_CLASSES, CELL_WIDTH_CLASSES, FLOW_CLASSES, ROW_PART_CLASSES, |
| 42 |
}; |
| 43 |
use crate::meter::METER_CLASSES; |
| 44 |
use crate::placeholder::PLACEHOLDER_CLASSES; |
| 45 |
use crate::{Emit, option_class}; |
| 46 |
use makeover_layout::Selector; |
| 47 |
use std::collections::{BTreeMap, BTreeSet}; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
#[must_use] |
| 57 |
pub fn vocabulary(opts: &Emit) -> BTreeSet<String> { |
| 58 |
classes_in_css(&crate::stylesheet(opts)) |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
#[must_use] |
| 85 |
pub fn names(opts: &Emit) -> BTreeSet<String> { |
| 86 |
let mut all = vocabulary(opts); |
| 87 |
all.extend( |
| 88 |
ROW_PART_CLASSES |
| 89 |
.iter() |
| 90 |
.chain(CELL_PART_CLASSES) |
| 91 |
.chain(CELL_WIDTH_CLASSES) |
| 92 |
.chain(CELL_DROP_CLASSES) |
| 93 |
.chain(FLOW_CLASSES) |
| 94 |
.chain(crate::RUN_CLASSES) |
| 95 |
.chain(FACET_CLASSES) |
| 96 |
.chain(FIELD_CLASSES) |
| 97 |
.chain(FIGURE_CLASSES) |
| 98 |
.chain(METER_CLASSES) |
| 99 |
.chain(PLACEHOLDER_CLASSES) |
| 100 |
.map(|name| crate::class(name, opts)), |
| 101 |
); |
| 102 |
all.extend( |
| 103 |
[Selector::Tabs, Selector::Segmented, Selector::Toggle] |
| 104 |
.into_iter() |
| 105 |
.map(|s| crate::class(option_class(s), opts)), |
| 106 |
); |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
all.extend(FIELD_STATE_CLASSES.iter().map(|name| (*name).to_owned())); |
| 111 |
all |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
#[must_use] |
| 140 |
pub fn declarations_by_class(css: &str) -> BTreeMap<String, BTreeSet<String>> { |
| 141 |
by_class(css, |value| !is_handoff(value)) |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
#[must_use] |
| 151 |
pub fn deferrals_by_class(css: &str) -> BTreeMap<String, BTreeSet<String>> { |
| 152 |
by_class(css, is_handoff) |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
fn by_class(css: &str, keep: impl Fn(&str) -> bool) -> BTreeMap<String, BTreeSet<String>> { |
| 158 |
let mut out: BTreeMap<String, BTreeSet<String>> = BTreeMap::new(); |
| 159 |
for (selector, body) in rules(css) { |
| 160 |
let classes = classes_in_selector(&selector); |
| 161 |
if classes.is_empty() { |
| 162 |
continue; |
| 163 |
} |
| 164 |
let properties = properties_in_body(&body, &keep); |
| 165 |
if properties.is_empty() { |
| 166 |
continue; |
| 167 |
} |
| 168 |
for class in classes { |
| 169 |
out.entry(class).or_default().extend(properties.clone()); |
| 170 |
} |
| 171 |
} |
| 172 |
out |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
#[must_use] |
| 204 |
pub fn declarations_by_element(css: &str) -> BTreeMap<String, BTreeMap<String, Specificity>> { |
| 205 |
let mut out: BTreeMap<String, BTreeMap<String, Specificity>> = BTreeMap::new(); |
| 206 |
for (selector, body) in rules(css) { |
| 207 |
let properties = properties_in_body(&body, |value| !is_handoff(value)); |
| 208 |
if properties.is_empty() { |
| 209 |
continue; |
| 210 |
} |
| 211 |
for arm in selector.split(',') { |
| 212 |
let Some(element) = bare_element(arm) else { |
| 213 |
continue; |
| 214 |
}; |
| 215 |
let rank = specificity(arm); |
| 216 |
let entry = out.entry(element).or_default(); |
| 217 |
for property in &properties { |
| 218 |
let strongest = entry.entry(property.clone()).or_default(); |
| 219 |
*strongest = (*strongest).max(rank); |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
out |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
#[must_use] |
| 233 |
pub fn mentions_by_class(css: &str) -> BTreeMap<String, BTreeMap<String, Specificity>> { |
| 234 |
let mut out: BTreeMap<String, BTreeMap<String, Specificity>> = BTreeMap::new(); |
| 235 |
for (selector, body) in rules(css) { |
| 236 |
let properties = properties_in_body(&body, |_| true); |
| 237 |
if properties.is_empty() { |
| 238 |
continue; |
| 239 |
} |
| 240 |
for arm in selector.split(',') { |
| 241 |
let classes = classes_in_selector(arm); |
| 242 |
if classes.is_empty() { |
| 243 |
continue; |
| 244 |
} |
| 245 |
let rank = specificity(arm); |
| 246 |
for class in classes { |
| 247 |
let entry = out.entry(class).or_default(); |
| 248 |
for property in &properties { |
| 249 |
let strongest = entry.entry(property.clone()).or_default(); |
| 250 |
*strongest = (*strongest).max(rank); |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
out |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
pub type Specificity = (usize, usize, usize); |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
#[must_use] |
| 274 |
pub fn specificity(selector: &str) -> Specificity { |
| 275 |
let chars: Vec<char> = selector.chars().collect(); |
| 276 |
let (mut ids, mut classes, mut elements) = (0, 0, 0); |
| 277 |
let mut i = 0; |
| 278 |
while i < chars.len() { |
| 279 |
match chars[i] { |
| 280 |
'#' => { |
| 281 |
ids += 1; |
| 282 |
i = skip_name(&chars, i + 1); |
| 283 |
} |
| 284 |
'.' => { |
| 285 |
classes += 1; |
| 286 |
i = skip_name(&chars, i + 1); |
| 287 |
} |
| 288 |
':' => { |
| 289 |
|
| 290 |
if chars.get(i + 1) == Some(&':') { |
| 291 |
elements += 1; |
| 292 |
i = skip_name(&chars, i + 2); |
| 293 |
} else { |
| 294 |
classes += 1; |
| 295 |
i = skip_name(&chars, i + 1); |
| 296 |
} |
| 297 |
if chars.get(i) == Some(&'(') { |
| 298 |
i = skip_group(&chars, i); |
| 299 |
} |
| 300 |
} |
| 301 |
'[' => { |
| 302 |
classes += 1; |
| 303 |
i = skip_group(&chars, i); |
| 304 |
} |
| 305 |
c if c.is_ascii_alphabetic() => { |
| 306 |
elements += 1; |
| 307 |
i = skip_name(&chars, i); |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
_ => i += 1, |
| 312 |
} |
| 313 |
} |
| 314 |
(ids, classes, elements) |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
fn skip_name(chars: &[char], from: usize) -> usize { |
| 319 |
let mut i = from; |
| 320 |
while i < chars.len() && (chars[i].is_alphanumeric() || chars[i] == '-' || chars[i] == '_') { |
| 321 |
i += 1; |
| 322 |
} |
| 323 |
i |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
fn skip_group(chars: &[char], from: usize) -> usize { |
| 329 |
let mut depth = 0usize; |
| 330 |
let mut i = from; |
| 331 |
while i < chars.len() { |
| 332 |
match chars[i] { |
| 333 |
'[' | '(' => depth += 1, |
| 334 |
']' | ')' => { |
| 335 |
depth -= 1; |
| 336 |
if depth == 0 { |
| 337 |
return i + 1; |
| 338 |
} |
| 339 |
} |
| 340 |
_ => {} |
| 341 |
} |
| 342 |
i += 1; |
| 343 |
} |
| 344 |
i |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
fn is_handoff(value: &str) -> bool { |
| 353 |
value.trim() == "revert-layer" |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
fn properties_in_body(body: &str, keep: impl Fn(&str) -> bool) -> BTreeSet<String> { |
| 358 |
body.split(';') |
| 359 |
.filter_map(|decl| decl.split_once(':')) |
| 360 |
.filter(|(_, value)| keep(value)) |
| 361 |
.map(|(name, _)| name.trim().to_string()) |
| 362 |
.filter(|name| !name.is_empty() && !name.contains(['{', '}'])) |
| 363 |
.collect() |
| 364 |
} |
| 365 |
|
| 366 |
#[must_use] |
| 367 |
pub fn classes_in_css(css: &str) -> BTreeSet<String> { |
| 368 |
rules(css) |
| 369 |
.into_iter() |
| 370 |
.flat_map(|(selector, _)| classes_in_selector(&selector)) |
| 371 |
.collect() |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
fn rules(css: &str) -> Vec<(String, String)> { |
| 388 |
let mut out = Vec::new(); |
| 389 |
|
| 390 |
|
| 391 |
let mut blocks: Vec<bool> = Vec::new(); |
| 392 |
|
| 393 |
|
| 394 |
let mut prelude = String::new(); |
| 395 |
|
| 396 |
let mut open: Vec<(String, String)> = Vec::new(); |
| 397 |
|
| 398 |
let mut chars = css.chars().peekable(); |
| 399 |
while let Some(c) = chars.next() { |
| 400 |
match c { |
| 401 |
'/' if chars.peek() == Some(&'*') => { |
| 402 |
chars.next(); |
| 403 |
let mut star = false; |
| 404 |
for c in chars.by_ref() { |
| 405 |
if star && c == '/' { |
| 406 |
break; |
| 407 |
} |
| 408 |
star = c == '*'; |
| 409 |
} |
| 410 |
prelude.clear(); |
| 411 |
} |
| 412 |
'"' | '\'' => { |
| 413 |
let quote = c; |
| 414 |
let mut escaped = false; |
| 415 |
|
| 416 |
|
| 417 |
if blocks.last().copied().unwrap_or(false) |
| 418 |
&& let Some((_, body)) = open.last_mut() |
| 419 |
{ |
| 420 |
body.push(quote); |
| 421 |
} |
| 422 |
for c in chars.by_ref() { |
| 423 |
if escaped { |
| 424 |
escaped = false; |
| 425 |
} else if c == '\\' { |
| 426 |
escaped = true; |
| 427 |
} else if c == quote { |
| 428 |
break; |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
if blocks.last().copied().unwrap_or(false) |
| 435 |
&& let Some((_, body)) = open.last_mut() |
| 436 |
{ |
| 437 |
body.push(quote); |
| 438 |
} |
| 439 |
} |
| 440 |
'{' => { |
| 441 |
let declarations = !prelude.trim_start().starts_with('@'); |
| 442 |
if declarations { |
| 443 |
open.push((prelude.clone(), String::new())); |
| 444 |
} |
| 445 |
blocks.push(declarations); |
| 446 |
prelude.clear(); |
| 447 |
} |
| 448 |
'}' => { |
| 449 |
if blocks.pop().unwrap_or(false) |
| 450 |
&& let Some(rule) = open.pop() |
| 451 |
{ |
| 452 |
out.push(rule); |
| 453 |
} |
| 454 |
prelude.clear(); |
| 455 |
} |
| 456 |
_ => { |
| 457 |
if blocks.last().copied().unwrap_or(false) |
| 458 |
&& let Some((_, body)) = open.last_mut() |
| 459 |
{ |
| 460 |
body.push(c); |
| 461 |
} else if c == ';' { |
| 462 |
prelude.clear(); |
| 463 |
} else { |
| 464 |
prelude.push(c); |
| 465 |
} |
| 466 |
} |
| 467 |
} |
| 468 |
} |
| 469 |
out |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
pub const ELEMENT_CLASSES: &[(&str, &[&str])] = &[ |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
( |
| 496 |
"a", |
| 497 |
&[ |
| 498 |
"link", |
| 499 |
"button", |
| 500 |
"tab", |
| 501 |
"chip", |
| 502 |
"badge", |
| 503 |
"card", |
| 504 |
"row-activate", |
| 505 |
"figure-act", |
| 506 |
"chrome-place", |
| 507 |
], |
| 508 |
), |
| 509 |
( |
| 510 |
"button", |
| 511 |
&[ |
| 512 |
"button", |
| 513 |
"chip", |
| 514 |
"segment", |
| 515 |
"toggle", |
| 516 |
"tab", |
| 517 |
"link", |
| 518 |
"badge", |
| 519 |
"card", |
| 520 |
"facet-take", |
| 521 |
"facet-prune", |
| 522 |
"chip-remove", |
| 523 |
"row-activate", |
| 524 |
], |
| 525 |
), |
| 526 |
|
| 527 |
|
| 528 |
("details", &["ask"]), |
| 529 |
("summary", &["button", "ask-open", "ask-body"]), |
| 530 |
|
| 531 |
("input", &["field", "toggle", "row-select"]), |
| 532 |
("select", &["field"]), |
| 533 |
("textarea", &["field"]), |
| 534 |
( |
| 535 |
"label", |
| 536 |
&[ |
| 537 |
"form-label", |
| 538 |
"form-checkbox-label", |
| 539 |
"form-radio-label", |
| 540 |
"toggle", |
| 541 |
], |
| 542 |
), |
| 543 |
("form", &["form"]), |
| 544 |
("progress", &["progress"]), |
| 545 |
|
| 546 |
("p", &["text", "facet-name", "placeholder-text"]), |
| 547 |
("ul", &["list", "facet-values"]), |
| 548 |
("ol", &["list"]), |
| 549 |
("li", &["facet-value"]), |
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
("table", &["table"]), |
| 554 |
("thead", &["table-head"]), |
| 555 |
("tr", &["table-row"]), |
| 556 |
("td", &["cell", "cell-value", "cell-content"]), |
| 557 |
("th", &["table-heading"]), |
| 558 |
|
| 559 |
|
| 560 |
("figure", &["picture", "figure"]), |
| 561 |
("img", &["picture-img"]), |
| 562 |
("figcaption", &["picture-caption", "figure-caption"]), |
| 563 |
("nav", &["chrome-nav"]), |
| 564 |
]; |
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
#[must_use] |
| 573 |
pub fn classes_for_element(element: &str, opts: &Emit) -> BTreeSet<String> { |
| 574 |
ELEMENT_CLASSES |
| 575 |
.iter() |
| 576 |
.find(|(name, _)| *name == element) |
| 577 |
.map(|(_, classes)| classes.iter().map(|c| crate::class(c, opts)).collect()) |
| 578 |
.unwrap_or_default() |
| 579 |
} |
| 580 |
|
| 581 |
|
| 582 |
fn bare_element(arm: &str) -> Option<String> { |
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
let mut flat = String::with_capacity(arm.len()); |
| 587 |
let mut depth = 0usize; |
| 588 |
for c in arm.chars() { |
| 589 |
match c { |
| 590 |
'[' | '(' => depth += 1, |
| 591 |
']' | ')' => depth = depth.saturating_sub(1), |
| 592 |
_ if depth == 0 => flat.push(c), |
| 593 |
_ => {} |
| 594 |
} |
| 595 |
} |
| 596 |
let flat = flat.trim(); |
| 597 |
|
| 598 |
if flat.is_empty() || flat.contains(['.', '#', '>', '+', '~', '*']) { |
| 599 |
return None; |
| 600 |
} |
| 601 |
if flat.chars().any(char::is_whitespace) { |
| 602 |
return None; |
| 603 |
} |
| 604 |
let name: String = flat |
| 605 |
.chars() |
| 606 |
.take_while(|c| c.is_alphanumeric() || *c == '-') |
| 607 |
.collect(); |
| 608 |
|
| 609 |
|
| 610 |
if !name.starts_with(|c: char| c.is_ascii_alphabetic()) { |
| 611 |
return None; |
| 612 |
} |
| 613 |
Some(name.to_ascii_lowercase()) |
| 614 |
} |
| 615 |
|
| 616 |
|
| 617 |
fn classes_in_selector(selector: &str) -> Vec<String> { |
| 618 |
let chars: Vec<char> = selector.chars().collect(); |
| 619 |
let mut names = Vec::new(); |
| 620 |
let mut i = 0; |
| 621 |
while i < chars.len() { |
| 622 |
|
| 623 |
|
| 624 |
if chars[i] == '.' |
| 625 |
&& chars |
| 626 |
.get(i + 1) |
| 627 |
.is_some_and(|c| c.is_alphabetic() || *c == '_') |
| 628 |
{ |
| 629 |
let start = i + 1; |
| 630 |
let mut end = start; |
| 631 |
while end < chars.len() |
| 632 |
&& (chars[end].is_alphanumeric() || chars[end] == '-' || chars[end] == '_') |
| 633 |
{ |
| 634 |
end += 1; |
| 635 |
} |
| 636 |
names.push(chars[start..end].iter().collect()); |
| 637 |
i = end; |
| 638 |
} else { |
| 639 |
i += 1; |
| 640 |
} |
| 641 |
} |
| 642 |
names |
| 643 |
} |
| 644 |
|
| 645 |
#[cfg(test)] |
| 646 |
mod tests { |
| 647 |
use super::*; |
| 648 |
use crate::list::{cell_part_class, part_class}; |
| 649 |
use makeover_layout::{CellPart, RowPart}; |
| 650 |
|
| 651 |
#[test] |
| 652 |
fn the_scrape_finds_the_components_the_sheet_is_built_from() { |
| 653 |
let v = vocabulary(&Emit::default()); |
| 654 |
assert!( |
| 655 |
v.len() > 20, |
| 656 |
"scraped {} classes, which reads as a parser failure rather than a small sheet", |
| 657 |
v.len() |
| 658 |
); |
| 659 |
for name in ["card", "tab", "table-heading", "cell-value", "chosen"] { |
| 660 |
assert!( |
| 661 |
v.contains(name), |
| 662 |
"the sheet defines .{name} and the scan missed it" |
| 663 |
); |
| 664 |
} |
| 665 |
} |
| 666 |
|
| 667 |
#[test] |
| 668 |
fn every_name_a_caller_can_ask_for_is_one_this_crate_admits_to() { |
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
let opts = Emit::default(); |
| 674 |
let all = names(&opts); |
| 675 |
|
| 676 |
for selector in [Selector::Tabs, Selector::Segmented, Selector::Toggle] { |
| 677 |
let name = option_class(selector); |
| 678 |
assert!( |
| 679 |
all.contains(name), |
| 680 |
"option_class({selector:?}) is .{name}, which nothing admits to" |
| 681 |
); |
| 682 |
} |
| 683 |
for part in [ |
| 684 |
RowPart::Primary, |
| 685 |
RowPart::Secondary, |
| 686 |
RowPart::Meta, |
| 687 |
RowPart::Actions, |
| 688 |
RowPart::Tokens, |
| 689 |
RowPart::Proportion, |
| 690 |
] { |
| 691 |
let name = part_class(part); |
| 692 |
assert!( |
| 693 |
all.contains(name), |
| 694 |
"part_class({part:?}) is .{name}, which nothing admits to" |
| 695 |
); |
| 696 |
} |
| 697 |
for part in [ |
| 698 |
CellPart::Value, |
| 699 |
CellPart::Tokens, |
| 700 |
CellPart::Actions, |
| 701 |
CellPart::Link, |
| 702 |
] { |
| 703 |
let name = cell_part_class(part); |
| 704 |
assert!( |
| 705 |
all.contains(name), |
| 706 |
"cell_part_class({part:?}) is .{name}, which nothing admits to" |
| 707 |
); |
| 708 |
} |
| 709 |
} |
| 710 |
|
| 711 |
#[test] |
| 712 |
fn the_part_lists_hold_every_arm_of_the_match_beside_them() { |
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
for part in [ |
| 717 |
RowPart::Primary, |
| 718 |
RowPart::Secondary, |
| 719 |
RowPart::Meta, |
| 720 |
RowPart::Actions, |
| 721 |
RowPart::Tokens, |
| 722 |
RowPart::Proportion, |
| 723 |
] { |
| 724 |
assert!( |
| 725 |
ROW_PART_CLASSES.contains(&part_class(part)), |
| 726 |
"{part:?} is missing from ROW_PART_CLASSES" |
| 727 |
); |
| 728 |
} |
| 729 |
for part in [ |
| 730 |
CellPart::Value, |
| 731 |
CellPart::Tokens, |
| 732 |
CellPart::Actions, |
| 733 |
CellPart::Link, |
| 734 |
] { |
| 735 |
assert!( |
| 736 |
CELL_PART_CLASSES.contains(&cell_part_class(part)), |
| 737 |
"{part:?} is missing from CELL_PART_CLASSES" |
| 738 |
); |
| 739 |
} |
| 740 |
|
| 741 |
assert!(ROW_PART_CLASSES.contains(&"row-part")); |
| 742 |
assert!(CELL_PART_CLASSES.contains(&"cell-part")); |
| 743 |
} |
| 744 |
|
| 745 |
#[test] |
| 746 |
fn a_prefix_moves_the_component_classes_and_leaves_the_states_qualifying_them() { |
| 747 |
let plain = vocabulary(&Emit::default()); |
| 748 |
let prefixed = vocabulary(&Emit { |
| 749 |
class_prefix: "mo-", |
| 750 |
..Emit::default() |
| 751 |
}); |
| 752 |
assert_eq!( |
| 753 |
plain.len(), |
| 754 |
prefixed.len(), |
| 755 |
"a prefix changed how many classes exist" |
| 756 |
); |
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
let states = ["chosen", "latched", "current"]; |
| 762 |
for name in &plain { |
| 763 |
let expected = if states.contains(&name.as_str()) { |
| 764 |
name.clone() |
| 765 |
} else { |
| 766 |
format!("mo-{name}") |
| 767 |
}; |
| 768 |
assert!( |
| 769 |
prefixed.contains(&expected), |
| 770 |
".{name} did not move to .{expected} under the prefix" |
| 771 |
); |
| 772 |
} |
| 773 |
} |
| 774 |
|
| 775 |
#[test] |
| 776 |
fn a_handoff_is_not_an_override() { |
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
let css = ".button { background: revert-layer; color: red; }"; |
| 783 |
let taken = declarations_by_class(css); |
| 784 |
let given = deferrals_by_class(css); |
| 785 |
assert_eq!( |
| 786 |
taken.get("button"), |
| 787 |
Some(&["color".to_string()].into_iter().collect()) |
| 788 |
); |
| 789 |
assert_eq!( |
| 790 |
given.get("button"), |
| 791 |
Some(&["background".to_string()].into_iter().collect()) |
| 792 |
); |
| 793 |
} |
| 794 |
|
| 795 |
#[test] |
| 796 |
fn an_important_handoff_is_an_override() { |
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
let css = ".button { background: revert-layer !important; }"; |
| 802 |
assert_eq!( |
| 803 |
declarations_by_class(css).get("button"), |
| 804 |
Some(&["background".to_string()].into_iter().collect()) |
| 805 |
); |
| 806 |
assert!(!deferrals_by_class(css).contains_key("button")); |
| 807 |
} |
| 808 |
|
| 809 |
#[test] |
| 810 |
fn a_class_that_only_hands_properties_back_is_not_in_the_taking_set() { |
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
let by_class = declarations_by_class(".field { background: revert-layer; }"); |
| 815 |
assert!(!by_class.contains_key("field"), "got {by_class:?}"); |
| 816 |
} |
| 817 |
|
| 818 |
#[test] |
| 819 |
fn an_element_rule_is_read_where_a_class_reader_sees_nothing() { |
| 820 |
let css = "button { color: red; background: blue; }"; |
| 821 |
assert!(declarations_by_class(css).is_empty()); |
| 822 |
let by_element = declarations_by_element(css); |
| 823 |
let button = by_element.get("button").expect("button is named"); |
| 824 |
assert_eq!( |
| 825 |
button.keys().cloned().collect::<Vec<_>>(), |
| 826 |
["background", "color"] |
| 827 |
); |
| 828 |
|
| 829 |
assert_eq!(button["color"], (0, 0, 1)); |
| 830 |
} |
| 831 |
|
| 832 |
#[test] |
| 833 |
fn the_strongest_arm_is_the_one_reported() { |
| 834 |
|
| 835 |
|
| 836 |
let css = "input { color: red; }\ninput[type=\"text\"]:focus { color: blue; }\n"; |
| 837 |
assert_eq!(declarations_by_element(css)["input"]["color"], (0, 2, 1)); |
| 838 |
} |
| 839 |
|
| 840 |
#[test] |
| 841 |
fn a_selector_is_ranked_the_way_the_cascade_ranks_it() { |
| 842 |
for (selector, expected) in [ |
| 843 |
("button", (0, 0, 1)), |
| 844 |
("*", (0, 0, 0)), |
| 845 |
(".field", (0, 1, 0)), |
| 846 |
("input.field", (0, 1, 1)), |
| 847 |
("input[type=\"text\"]", (0, 1, 1)), |
| 848 |
("button:hover", (0, 1, 1)), |
| 849 |
("button::before", (0, 0, 2)), |
| 850 |
("#main .card > button:focus-visible", (1, 2, 1)), |
| 851 |
(".chip.latched[aria-pressed=\"true\"]", (0, 3, 0)), |
| 852 |
("button:not(.link)", (0, 1, 1)), |
| 853 |
] { |
| 854 |
assert_eq!(specificity(selector), expected, "{selector}"); |
| 855 |
} |
| 856 |
} |
| 857 |
|
| 858 |
#[test] |
| 859 |
fn what_a_class_is_spoken_for_by_counts_a_handoff_as_speech() { |
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
let css = ".field { background: revert-layer; }\ninput.field:focus { color: red; }\n"; |
| 864 |
let mentions = mentions_by_class(css); |
| 865 |
assert_eq!(mentions["field"]["background"], (0, 1, 0)); |
| 866 |
assert_eq!(mentions["field"]["color"], (0, 2, 1)); |
| 867 |
} |
| 868 |
|
| 869 |
#[test] |
| 870 |
fn only_a_bare_compound_counts_as_an_element_rule() { |
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
for selector in [ |
| 875 |
".page button", |
| 876 |
"button.link", |
| 877 |
".card > button", |
| 878 |
"button + button", |
| 879 |
"* button", |
| 880 |
] { |
| 881 |
let css = format!("{selector} {{ color: red; }}"); |
| 882 |
assert!( |
| 883 |
declarations_by_element(&css).is_empty(), |
| 884 |
"{selector} was read as a bare element rule" |
| 885 |
); |
| 886 |
} |
| 887 |
} |
| 888 |
|
| 889 |
#[test] |
| 890 |
fn a_state_or_an_attribute_does_not_stop_an_arm_being_bare() { |
| 891 |
|
| 892 |
|
| 893 |
for selector in [ |
| 894 |
"button:hover", |
| 895 |
"button:focus-visible", |
| 896 |
"button:disabled", |
| 897 |
"button[aria-disabled=\"true\"]", |
| 898 |
"button:not(.link)", |
| 899 |
"button[data-tone=\"danger\"]:hover", |
| 900 |
] { |
| 901 |
let css = format!("{selector} {{ color: red; }}"); |
| 902 |
assert!( |
| 903 |
declarations_by_element(&css).contains_key("button"), |
| 904 |
"{selector} was not read as a bare element rule" |
| 905 |
); |
| 906 |
} |
| 907 |
} |
| 908 |
|
| 909 |
#[test] |
| 910 |
fn a_pseudo_element_on_nothing_names_no_element() { |
| 911 |
for selector in [":root", "::selection", "::backdrop", ":root:not(.x)"] { |
| 912 |
let css = format!("{selector} {{ color: red; }}"); |
| 913 |
assert!( |
| 914 |
declarations_by_element(&css).is_empty(), |
| 915 |
"{selector} named an element" |
| 916 |
); |
| 917 |
} |
| 918 |
} |
| 919 |
|
| 920 |
#[test] |
| 921 |
fn every_arm_of_a_list_is_read_on_its_own() { |
| 922 |
let css = "input, select, .field, .page textarea { color: red; }"; |
| 923 |
let by_element = declarations_by_element(css); |
| 924 |
assert!(by_element.contains_key("input")); |
| 925 |
assert!(by_element.contains_key("select")); |
| 926 |
assert!(!by_element.contains_key("textarea"), "that arm is scoped"); |
| 927 |
assert_eq!(by_element.len(), 2); |
| 928 |
} |
| 929 |
|
| 930 |
#[test] |
| 931 |
fn an_element_handing_a_property_back_is_not_taking_it() { |
| 932 |
let css = "button { background: revert-layer; }"; |
| 933 |
assert!(declarations_by_element(css).is_empty()); |
| 934 |
} |
| 935 |
|
| 936 |
#[test] |
| 937 |
fn the_pairing_map_carries_the_elements_this_crate_renders_onto() { |
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
let mut checked = 0; |
| 944 |
for (tag, class) in emitted_pairs() { |
| 945 |
if !ELEMENT_CLASSES.iter().any(|(name, _)| *name == tag) { |
| 946 |
continue; |
| 947 |
} |
| 948 |
checked += 1; |
| 949 |
assert!( |
| 950 |
classes_for_element(&tag, &Emit::default()).contains(&class), |
| 951 |
"this crate emits <{tag} class=\"{class}\"> and ELEMENT_CLASSES \ |
| 952 |
does not pair them" |
| 953 |
); |
| 954 |
} |
| 955 |
assert!( |
| 956 |
checked > 5, |
| 957 |
"scraped {checked} pairings off the emitters, which reads as the scan \ |
| 958 |
having stopped matching rather than the renderer having shrunk" |
| 959 |
); |
| 960 |
} |
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
fn emitted_pairs() -> Vec<(String, String)> { |
| 970 |
const OPEN: &str = "class=\\\""; |
| 971 |
let mut out = Vec::new(); |
| 972 |
for file in std::fs::read_dir("src").expect("read src") { |
| 973 |
let path = file.expect("dir entry").path(); |
| 974 |
if path.extension().is_none_or(|e| e != "rs") { |
| 975 |
continue; |
| 976 |
} |
| 977 |
let src = std::fs::read_to_string(&path).expect("read source"); |
| 978 |
for (at, _) in src.match_indices(OPEN) { |
| 979 |
|
| 980 |
let Some(open) = src[..at].rfind('<') else { |
| 981 |
continue; |
| 982 |
}; |
| 983 |
let tag: String = src[open + 1..] |
| 984 |
.chars() |
| 985 |
.take_while(|c| c.is_ascii_alphanumeric() || *c == '-') |
| 986 |
.collect(); |
| 987 |
if tag.is_empty() { |
| 988 |
continue; |
| 989 |
} |
| 990 |
|
| 991 |
let tail = &src[at..(at + 300).min(src.len())]; |
| 992 |
let Some(call) = tail.find("push_class(out, \"") else { |
| 993 |
continue; |
| 994 |
}; |
| 995 |
let name: String = tail[call + "push_class(out, \"".len()..] |
| 996 |
.chars() |
| 997 |
.take_while(|c| *c != '"') |
| 998 |
.collect(); |
| 999 |
if !name.is_empty() { |
| 1000 |
out.push((tag, name)); |
| 1001 |
} |
| 1002 |
} |
| 1003 |
} |
| 1004 |
out |
| 1005 |
} |
| 1006 |
|
| 1007 |
#[test] |
| 1008 |
fn the_properties_a_class_carries_are_read_per_class() { |
| 1009 |
let css = ".badge { padding: 1px; font-weight: 600; }\n .badge[data-color] { border: 1px solid red; }\n @media (min-width: 40rem) { .badge { padding: 2px; } }\n"; |
| 1010 |
let by_class = declarations_by_class(css); |
| 1011 |
let badge = by_class.get("badge").expect("badge is named"); |
| 1012 |
|
| 1013 |
|
| 1014 |
assert!(badge.contains("padding")); |
| 1015 |
assert!(badge.contains("font-weight")); |
| 1016 |
assert!(badge.contains("border")); |
| 1017 |
assert_eq!(badge.len(), 3); |
| 1018 |
} |
| 1019 |
|
| 1020 |
#[test] |
| 1021 |
fn a_value_holding_a_colon_or_a_semicolon_is_not_read_as_a_property() { |
| 1022 |
let css = ".x { background: url(\"a;b:c\"); color: red; }"; |
| 1023 |
let by_class = declarations_by_class(css); |
| 1024 |
let x = by_class.get("x").expect("x is named"); |
| 1025 |
assert_eq!( |
| 1026 |
*x, |
| 1027 |
["background".to_string(), "color".to_string()] |
| 1028 |
.into_iter() |
| 1029 |
.collect::<BTreeSet<_>>() |
| 1030 |
); |
| 1031 |
} |
| 1032 |
|
| 1033 |
#[test] |
| 1034 |
fn the_generated_sheet_sets_fill_on_a_badge_and_not_its_shape() { |
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
let by_class = declarations_by_class(&crate::stylesheet(&Emit::default())); |
| 1040 |
let badge = by_class.get("badge").expect("the sheet defines .badge"); |
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
assert!(badge.contains("color"), "got {badge:?}"); |
| 1045 |
assert!( |
| 1046 |
!badge.contains("padding"), |
| 1047 |
"shape is the app's, got {badge:?}" |
| 1048 |
); |
| 1049 |
} |
| 1050 |
|
| 1051 |
#[test] |
| 1052 |
fn a_declaration_value_holding_a_dot_is_not_read_as_a_class() { |
| 1053 |
let found = classes_in_css(".real { transition: .2s ease; margin: 0.5rem; }"); |
| 1054 |
assert_eq!(found, ["real".to_string()].into_iter().collect()); |
| 1055 |
} |
| 1056 |
|
| 1057 |
#[test] |
| 1058 |
fn an_at_rule_does_not_hide_the_selectors_inside_it() { |
| 1059 |
let found = classes_in_css( |
| 1060 |
"@layer makeover { @media (min-width: 40rem) { .wide { color: red; } } }", |
| 1061 |
); |
| 1062 |
assert_eq!(found, ["wide".to_string()].into_iter().collect()); |
| 1063 |
} |
| 1064 |
|
| 1065 |
#[test] |
| 1066 |
fn a_string_is_opaque_and_a_comment_contributes_nothing() { |
| 1067 |
let found = classes_in_css("/* .notaclass */ .caret::after { content: \"} .alsonot\"; }"); |
| 1068 |
assert_eq!(found, ["caret".to_string()].into_iter().collect()); |
| 1069 |
} |
| 1070 |
|
| 1071 |
#[test] |
| 1072 |
fn a_compound_selector_yields_every_class_it_names() { |
| 1073 |
let found = classes_in_css( |
| 1074 |
".tab.chosen[aria-sort=\"ascending\"] > .label:not(.muted) { color: red; }", |
| 1075 |
); |
| 1076 |
let expected: BTreeSet<String> = ["tab", "chosen", "label", "muted"] |
| 1077 |
.into_iter() |
| 1078 |
.map(String::from) |
| 1079 |
.collect(); |
| 1080 |
assert_eq!(found, expected); |
| 1081 |
} |
| 1082 |
} |
| 1083 |
|