| 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::form::escape_into; |
| 38 |
use crate::reset::Reset; |
| 39 |
use crate::{Emit, class, push_class}; |
| 40 |
use makeover_layout::{Depth, Facet, FacetValue, Selecting, Standing}; |
| 41 |
use std::fmt::Write as _; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
pub const FACET_CLASSES: &[&str] = &[ |
| 50 |
"facet", |
| 51 |
"facet-name", |
| 52 |
"facet-values", |
| 53 |
"facet-value", |
| 54 |
"facet-take", |
| 55 |
"facet-count", |
| 56 |
"facet-prune", |
| 57 |
]; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
#[must_use] |
| 66 |
pub const fn selecting_name(mode: Selecting) -> &'static str { |
| 67 |
match mode { |
| 68 |
Selecting::OneOf => "one-of", |
| 69 |
Selecting::AnyOf => "any-of", |
| 70 |
Selecting::Range => "range", |
| 71 |
Selecting::Text => "text", |
| 72 |
Selecting::Subtree => "subtree", |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
_ => "unknown", |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
#[must_use] |
| 83 |
pub const fn standing_name(standing: Standing) -> &'static str { |
| 84 |
match standing { |
| 85 |
Standing::Open => "open", |
| 86 |
Standing::Taken => "taken", |
| 87 |
Standing::Inherited => "inherited", |
| 88 |
Standing::Pruned => "pruned", |
| 89 |
|
| 90 |
|
| 91 |
_ => "open", |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
#[must_use] |
| 123 |
pub fn facet_html(facet: &Facet<'_>, opts: &Emit) -> String { |
| 124 |
let mut html = String::new(); |
| 125 |
facet_html_into(facet, opts, &mut html); |
| 126 |
html |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
pub fn facet_html_into(facet: &Facet<'_>, opts: &Emit, out: &mut String) { |
| 133 |
out.push_str("<div class=\""); |
| 134 |
push_class(out, "facet", opts); |
| 135 |
out.push_str("\" role=\"group\" data-selecting=\""); |
| 136 |
out.push_str(selecting_name(facet.mode)); |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
let _ = write!(out, "\" style=\"--facet-reach: {}\">", facet.reach()); |
| 141 |
|
| 142 |
out.push_str("<p class=\""); |
| 143 |
push_class(out, "facet-name", opts); |
| 144 |
out.push_str("\">"); |
| 145 |
escape_into(facet.name, out); |
| 146 |
out.push_str("</p>"); |
| 147 |
|
| 148 |
out.push_str("<ul class=\""); |
| 149 |
push_class(out, "facet-values", opts); |
| 150 |
out.push_str("\">"); |
| 151 |
if facet.mode.offers_values() { |
| 152 |
for value in facet.values { |
| 153 |
value_html_into(facet, value, opts, out); |
| 154 |
} |
| 155 |
} |
| 156 |
out.push_str("</ul></div>"); |
| 157 |
} |
| 158 |
|
| 159 |
fn value_html_into(facet: &Facet<'_>, value: &FacetValue<'_>, opts: &Emit, out: &mut String) { |
| 160 |
out.push_str("<li class=\""); |
| 161 |
push_class(out, "facet-value", opts); |
| 162 |
out.push_str("\" data-standing=\""); |
| 163 |
out.push_str(standing_name(value.standing)); |
| 164 |
let _ = write!(out, "\" style=\"--facet-depth: {}\">", value.depth); |
| 165 |
|
| 166 |
out.push_str("<button type=\"button\" class=\""); |
| 167 |
push_class(out, "facet-take", opts); |
| 168 |
out.push_str("\" data-facet-value=\""); |
| 169 |
escape_into(value.value, out); |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
out.push_str("\" aria-pressed=\""); |
| 175 |
out.push_str(if value.standing.in_force() { |
| 176 |
"true\"" |
| 177 |
} else { |
| 178 |
"false\"" |
| 179 |
}); |
| 180 |
|
| 181 |
|
| 182 |
if value.branching { |
| 183 |
out.push_str(" aria-expanded=\""); |
| 184 |
out.push_str(if value.standing.in_force() { |
| 185 |
"true\"" |
| 186 |
} else { |
| 187 |
"false\"" |
| 188 |
}); |
| 189 |
} |
| 190 |
out.push('>'); |
| 191 |
escape_into(value.label, out); |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
if let Some(count) = value.count { |
| 196 |
out.push_str("<span class=\""); |
| 197 |
push_class(out, "facet-count", opts); |
| 198 |
let _ = write!(out, "\">{count}</span>"); |
| 199 |
} |
| 200 |
out.push_str("</button>"); |
| 201 |
|
| 202 |
if facet.mode.prunes() { |
| 203 |
out.push_str("<button type=\"button\" class=\""); |
| 204 |
push_class(out, "facet-prune", opts); |
| 205 |
out.push_str("\" data-facet-value=\""); |
| 206 |
escape_into(value.value, out); |
| 207 |
out.push_str("\" aria-pressed=\""); |
| 208 |
out.push_str(if value.standing == Standing::Pruned { |
| 209 |
"true\"" |
| 210 |
} else { |
| 211 |
"false\"" |
| 212 |
}); |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
out.push_str(" aria-label=\"Exclude "); |
| 218 |
escape_into(value.label, out); |
| 219 |
|
| 220 |
|
| 221 |
out.push_str("\">\u{2715}</button>"); |
| 222 |
} |
| 223 |
|
| 224 |
out.push_str("</li>"); |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
pub(crate) fn facet_rules(opts: &Emit) -> String { |
| 236 |
let mut css = String::new(); |
| 237 |
let panel = class("facet", opts); |
| 238 |
let name = class("facet-name", opts); |
| 239 |
let values = class("facet-values", opts); |
| 240 |
let value = class("facet-value", opts); |
| 241 |
let take = class("facet-take", opts); |
| 242 |
let count = class("facet-count", opts); |
| 243 |
let prune = class("facet-prune", opts); |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
let _ = writeln!(css, ".{name} {{\n color: var(--content-muted);\n}}"); |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
css.push_str(&Reset::BULLETS.rule(&format!(".{values}"))); |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
let _ = writeln!( |
| 258 |
css, |
| 259 |
".{value} {{\n padding-inline-start: calc(var(--facet-depth, 0) * var(--space-tight, 0.5rem));\n}}" |
| 260 |
); |
| 261 |
|
| 262 |
let _ = writeln!( |
| 263 |
css, |
| 264 |
".{panel} {{\n min-inline-size: calc(var(--facet-reach, 0) * var(--space-tight, 0.5rem));\n}}" |
| 265 |
); |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
css.push_str(&Reset::FLAT_BUTTON.rule(&format!(".{take}"))); |
| 277 |
css.push_str(&crate::interactive_rules(&take, Depth::Flat, opts)); |
| 278 |
css.push_str(&crate::depth_rule( |
| 279 |
&format!("{take}[aria-pressed=\"true\"]"), |
| 280 |
Depth::Well, |
| 281 |
)); |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
let _ = writeln!( |
| 287 |
css, |
| 288 |
".{value}[data-standing=\"pruned\"] .{take} {{\n color: var(--{});\n}}", |
| 289 |
Standing::Pruned.intent() |
| 290 |
); |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
let _ = writeln!(css, ".{count} {{\n color: var(--content-muted);\n}}"); |
| 296 |
|
| 297 |
|
| 298 |
css.push_str(&Reset::FLAT_BUTTON.rule(&format!(".{prune}"))); |
| 299 |
css.push_str(&crate::interactive_rules(&prune, Depth::Flat, opts)); |
| 300 |
css.push_str(&crate::depth_rule( |
| 301 |
&format!("{prune}[aria-pressed=\"true\"]"), |
| 302 |
Depth::Well, |
| 303 |
)); |
| 304 |
|
| 305 |
css |
| 306 |
} |
| 307 |
|
| 308 |
#[cfg(test)] |
| 309 |
mod tests { |
| 310 |
use super::*; |
| 311 |
|
| 312 |
fn tag_values() -> [FacetValue<'static>; 3] { |
| 313 |
[ |
| 314 |
FacetValue::new("music", "Music") |
| 315 |
.standing(Standing::Taken) |
| 316 |
.counted(128) |
| 317 |
.at(0, true), |
| 318 |
FacetValue::new("music/synths", "Synths") |
| 319 |
.standing(Standing::Inherited) |
| 320 |
.at(1, false), |
| 321 |
FacetValue::new("music/drums", "Drums") |
| 322 |
.standing(Standing::Pruned) |
| 323 |
.at(1, false), |
| 324 |
] |
| 325 |
} |
| 326 |
|
| 327 |
#[test] |
| 328 |
fn a_streamed_facet_is_the_facet_the_other_form_returns() { |
| 329 |
let opts = Emit { |
| 330 |
class_prefix: "mk-", |
| 331 |
..Emit::default() |
| 332 |
}; |
| 333 |
let values = tag_values(); |
| 334 |
for facet in [ |
| 335 |
Facet::new("Tag", Selecting::Subtree, &values), |
| 336 |
Facet::new("Type", Selecting::AnyOf, &values), |
| 337 |
Facet::new("Search", Selecting::Text, &[]), |
| 338 |
] { |
| 339 |
let mut streamed = String::new(); |
| 340 |
facet_html_into(&facet, &opts, &mut streamed); |
| 341 |
assert_eq!(streamed, facet_html(&facet, &opts)); |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
#[test] |
| 346 |
fn only_a_subtree_draws_an_exclude_affordance() { |
| 347 |
|
| 348 |
|
| 349 |
let values = tag_values(); |
| 350 |
let subtree = facet_html( |
| 351 |
&Facet::new("Tag", Selecting::Subtree, &values), |
| 352 |
&Emit::default(), |
| 353 |
); |
| 354 |
assert!(subtree.contains("facet-prune")); |
| 355 |
assert!(subtree.contains(r#"aria-label="Exclude Drums""#)); |
| 356 |
|
| 357 |
let flat = facet_html( |
| 358 |
&Facet::new("Type", Selecting::AnyOf, &values), |
| 359 |
&Emit::default(), |
| 360 |
); |
| 361 |
assert!(!flat.contains("facet-prune")); |
| 362 |
} |
| 363 |
|
| 364 |
#[test] |
| 365 |
fn an_inherited_value_reads_as_pressed_without_having_been_pressed() { |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
let values = tag_values(); |
| 370 |
let html = facet_html( |
| 371 |
&Facet::new("Tag", Selecting::Subtree, &values), |
| 372 |
&Emit::default(), |
| 373 |
); |
| 374 |
let synths = html |
| 375 |
.split("<li") |
| 376 |
.find(|chunk| chunk.contains("music/synths")) |
| 377 |
.expect("the inherited value"); |
| 378 |
assert!(synths.contains(r#"data-standing="inherited""#)); |
| 379 |
assert!(synths.contains(r#"aria-pressed="true""#)); |
| 380 |
|
| 381 |
let drums = html |
| 382 |
.split("<li") |
| 383 |
.find(|chunk| chunk.contains("music/drums")) |
| 384 |
.expect("the pruned value"); |
| 385 |
|
| 386 |
|
| 387 |
assert!(drums.contains(r#"data-standing="pruned""#)); |
| 388 |
assert!(drums.contains(r#"aria-pressed="false""#)); |
| 389 |
assert!(drums.contains(r#"aria-label="Exclude Drums""#)); |
| 390 |
} |
| 391 |
|
| 392 |
#[test] |
| 393 |
fn a_mode_that_lists_nothing_still_renders_its_panel() { |
| 394 |
|
| 395 |
|
| 396 |
let html = facet_html( |
| 397 |
&Facet::new("Search", Selecting::Text, &[]), |
| 398 |
&Emit::default(), |
| 399 |
); |
| 400 |
assert!(html.contains("facet-name")); |
| 401 |
assert!(html.contains(r#"data-selecting="text""#)); |
| 402 |
assert!(!html.contains("facet-take")); |
| 403 |
} |
| 404 |
|
| 405 |
#[test] |
| 406 |
fn an_unmeasured_count_emits_no_number_at_all() { |
| 407 |
|
| 408 |
|
| 409 |
let values = [FacetValue::of("Ambient")]; |
| 410 |
let html = facet_html( |
| 411 |
&Facet::new("Tag", Selecting::AnyOf, &values), |
| 412 |
&Emit::default(), |
| 413 |
); |
| 414 |
assert!(!html.contains("facet-count")); |
| 415 |
|
| 416 |
let counted = [FacetValue::of("Ambient").counted(0)]; |
| 417 |
let html = facet_html( |
| 418 |
&Facet::new("Tag", Selecting::AnyOf, &counted), |
| 419 |
&Emit::default(), |
| 420 |
); |
| 421 |
assert!(html.contains(">0</span>")); |
| 422 |
} |
| 423 |
|
| 424 |
#[test] |
| 425 |
fn the_gutter_is_reserved_from_the_deepest_value_before_anything_is_drawn() { |
| 426 |
|
| 427 |
|
| 428 |
let values = tag_values(); |
| 429 |
let html = facet_html( |
| 430 |
&Facet::new("Tag", Selecting::Subtree, &values), |
| 431 |
&Emit::default(), |
| 432 |
); |
| 433 |
assert!(html.contains("--facet-reach: 1")); |
| 434 |
assert!(html.contains("--facet-depth: 0")); |
| 435 |
assert!(html.contains("--facet-depth: 1")); |
| 436 |
} |
| 437 |
|
| 438 |
#[test] |
| 439 |
fn labels_and_identifiers_are_escaped_like_every_other_string() { |
| 440 |
|
| 441 |
|
| 442 |
let values = [FacetValue::new("a&b", "A & B")]; |
| 443 |
let html = facet_html( |
| 444 |
&Facet::new("T<ag>", Selecting::AnyOf, &values), |
| 445 |
&Emit::default(), |
| 446 |
); |
| 447 |
assert!(html.contains("A & B")); |
| 448 |
assert!(html.contains(r#"data-facet-value="a&b""#)); |
| 449 |
assert!(html.contains("T<ag>")); |
| 450 |
assert!(!html.contains("<ag>")); |
| 451 |
} |
| 452 |
|
| 453 |
#[test] |
| 454 |
fn a_value_reads_flat_before_it_is_touched() { |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
let css = facet_rules(&Emit::default()); |
| 461 |
for name in ["facet-take", "facet-prune"] { |
| 462 |
assert!( |
| 463 |
css.contains(&format!( |
| 464 |
".{name} {{\n background: none;\n border: none;\n box-shadow: none;\n}}" |
| 465 |
)), |
| 466 |
"{name} is not withdrawn: {css}" |
| 467 |
); |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
#[test] |
| 472 |
fn every_class_this_module_emits_is_one_the_stylesheet_rules() { |
| 473 |
|
| 474 |
|
| 475 |
let names = crate::vocabulary::names(&Emit::default()); |
| 476 |
for name in FACET_CLASSES { |
| 477 |
assert!( |
| 478 |
names.contains(&crate::class(name, &Emit::default())), |
| 479 |
"{name} is not in the vocabulary" |
| 480 |
); |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
#[test] |
| 485 |
fn the_prefix_reaches_every_class_in_the_markup() { |
| 486 |
|
| 487 |
|
| 488 |
let opts = Emit { |
| 489 |
class_prefix: "mo-", |
| 490 |
..Emit::default() |
| 491 |
}; |
| 492 |
let values = tag_values(); |
| 493 |
let html = facet_html(&Facet::new("Tag", Selecting::Subtree, &values), &opts); |
| 494 |
for name in FACET_CLASSES { |
| 495 |
assert!(html.contains(&format!("mo-{name}")), "{name} is unprefixed"); |
| 496 |
} |
| 497 |
} |
| 498 |
} |
| 499 |
|