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