| 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 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
use crate::{Emit, class}; |
| 44 |
use makeover_layout::{Choice, Field, FieldKind}; |
| 45 |
use std::fmt::Write as _; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 55 |
pub struct Markup<'a>(pub &'a str); |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 63 |
pub enum Value<'a> { |
| 64 |
|
| 65 |
#[default] |
| 66 |
Absent, |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
Text(&'a str), |
| 76 |
|
| 77 |
On(bool), |
| 78 |
} |
| 79 |
|
| 80 |
impl<'a> Value<'a> { |
| 81 |
|
| 82 |
const fn as_text(&self) -> &'a str { |
| 83 |
match self { |
| 84 |
Self::Text(text) => text, |
| 85 |
Self::Absent | Self::On(_) => "", |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
#[derive(Debug, Clone, Copy, Default)] |
| 92 |
pub struct Filling<'a> { |
| 93 |
|
| 94 |
pub value: Value<'a>, |
| 95 |
|
| 96 |
pub trailing: Option<Markup<'a>>, |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
pub id_prefix: Option<&'a str>, |
| 109 |
} |
| 110 |
|
| 111 |
impl<'a> Filling<'a> { |
| 112 |
|
| 113 |
#[must_use] |
| 114 |
pub const fn of(value: Value<'a>) -> Self { |
| 115 |
Self { |
| 116 |
value, |
| 117 |
trailing: None, |
| 118 |
id_prefix: None, |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
fn id_for(&self, name: &str) -> String { |
| 124 |
match self.id_prefix { |
| 125 |
Some(prefix) => format!("{}-{}", escape(prefix), escape(name)), |
| 126 |
None => escape(name), |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
#[must_use] |
| 137 |
pub fn escape(text: &str) -> String { |
| 138 |
let mut out = String::with_capacity(text.len()); |
| 139 |
for ch in text.chars() { |
| 140 |
match ch { |
| 141 |
'&' => out.push_str("&"), |
| 142 |
'<' => out.push_str("<"), |
| 143 |
'>' => out.push_str(">"), |
| 144 |
'"' => out.push_str("""), |
| 145 |
'\'' => out.push_str("'"), |
| 146 |
other => out.push(other), |
| 147 |
} |
| 148 |
} |
| 149 |
out |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
const fn input_type(kind: FieldKind) -> &'static str { |
| 156 |
match kind { |
| 157 |
FieldKind::Secret => "password", |
| 158 |
FieldKind::Number => "number", |
| 159 |
FieldKind::Checkbox => "checkbox", |
| 160 |
FieldKind::File => "file", |
| 161 |
FieldKind::Hidden => "hidden", |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
FieldKind::Email => "email", |
| 167 |
FieldKind::Url => "url", |
| 168 |
FieldKind::Tel => "tel", |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
FieldKind::Date => "date", |
| 174 |
FieldKind::DateTime => "datetime-local", |
| 175 |
FieldKind::Radio => "radio", |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
FieldKind::Text | FieldKind::Select | FieldKind::Textarea => "text", |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
_ => "text", |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
fn control_attributes(field: &Field<'_>, id: &str, name: &str) -> String { |
| 200 |
let mut attrs = format!(" id=\"{id}\" name=\"{}\"", escape(name)); |
| 201 |
if field.required { |
| 202 |
attrs.push_str(" required"); |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
if let Some(limit) = field.max_length { |
| 209 |
let _ = write!(attrs, " maxlength=\"{limit}\""); |
| 210 |
} |
| 211 |
if let Some(min) = field.min { |
| 212 |
let _ = write!(attrs, " min=\"{}\"", escape(min)); |
| 213 |
} |
| 214 |
if let Some(max) = field.max { |
| 215 |
let _ = write!(attrs, " max=\"{}\"", escape(max)); |
| 216 |
} |
| 217 |
if field.invalid() { |
| 218 |
attrs.push_str(" aria-invalid=\"true\""); |
| 219 |
} |
| 220 |
|
| 221 |
attrs.push_str(&described_by(field, id)); |
| 222 |
attrs |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
fn described_by(field: &Field<'_>, id: &str) -> String { |
| 235 |
let mut described = Vec::new(); |
| 236 |
if field.hint.is_some() { |
| 237 |
described.push(format!("{id}-hint")); |
| 238 |
} |
| 239 |
if field.error.is_some() { |
| 240 |
described.push(format!("{id}-error")); |
| 241 |
} |
| 242 |
if described.is_empty() { |
| 243 |
return String::new(); |
| 244 |
} |
| 245 |
format!(" aria-describedby=\"{}\"", described.join(" ")) |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
const fn is_group_control(kind: FieldKind) -> bool { |
| 257 |
matches!(kind, FieldKind::Radio) |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
fn radio_html(field: &Field<'_>, filling: &Filling<'_>, opts: &Emit) -> String { |
| 274 |
let id = filling.id_for(field.name); |
| 275 |
let value = filling.value.as_text(); |
| 276 |
let name = escape(field.name); |
| 277 |
|
| 278 |
let mut html = format!( |
| 279 |
"<div class=\"{}\" role=\"radiogroup\"", |
| 280 |
class("form-radio-group", opts) |
| 281 |
); |
| 282 |
let _ = write!(html, " aria-labelledby=\"{id}-label\""); |
| 283 |
if field.invalid() { |
| 284 |
html.push_str(" aria-invalid=\"true\""); |
| 285 |
} |
| 286 |
html.push_str(&described_by(field, &id)); |
| 287 |
html.push('>'); |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
for (index, opt) in field.options.iter().enumerate() { |
| 293 |
let checked = if opt.value == value { " checked" } else { "" }; |
| 294 |
let required = if field.required { " required" } else { "" }; |
| 295 |
let _ = write!( |
| 296 |
html, |
| 297 |
"<label class=\"{}\"><input type=\"radio\" id=\"{id}-{index}\" name=\"{name}\" \ |
| 298 |
value=\"{}\"{checked}{required}><span>{}</span></label>", |
| 299 |
class("form-radio-label", opts), |
| 300 |
escape(opt.value), |
| 301 |
escape(opt.label) |
| 302 |
); |
| 303 |
} |
| 304 |
|
| 305 |
html.push_str("</div>"); |
| 306 |
html |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
fn options_html(options: &[Choice<'_>], value: &str) -> String { |
| 317 |
let mut html = String::new(); |
| 318 |
if !value.is_empty() && !options.iter().any(|opt| opt.value == value) { |
| 319 |
let escaped = escape(value); |
| 320 |
let _ = write!( |
| 321 |
html, |
| 322 |
"<option value=\"{escaped}\" selected data-unmatched=\"true\">{escaped}</option>" |
| 323 |
); |
| 324 |
} |
| 325 |
for opt in options { |
| 326 |
let selected = if opt.value == value { " selected" } else { "" }; |
| 327 |
let _ = write!( |
| 328 |
html, |
| 329 |
"<option value=\"{}\"{selected}>{}</option>", |
| 330 |
escape(opt.value), |
| 331 |
escape(opt.label) |
| 332 |
); |
| 333 |
} |
| 334 |
html |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
fn control_html(field: &Field<'_>, filling: &Filling<'_>, opts: &Emit) -> String { |
| 339 |
let id = filling.id_for(field.name); |
| 340 |
let attrs = control_attributes(field, &id, field.name); |
| 341 |
let field_class = class("field", opts); |
| 342 |
let placeholder = field.placeholder.map_or_else(String::new, |text| { |
| 343 |
format!(" placeholder=\"{}\"", escape(text)) |
| 344 |
}); |
| 345 |
|
| 346 |
match field.kind { |
| 347 |
FieldKind::Radio => radio_html(field, filling, opts), |
| 348 |
FieldKind::Textarea => format!( |
| 349 |
"<textarea class=\"{field_class}\"{attrs}{placeholder}>{}</textarea>", |
| 350 |
escape(filling.value.as_text()) |
| 351 |
), |
| 352 |
FieldKind::Select => { |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
let options = options_html(field.options, filling.value.as_text()); |
| 357 |
format!("<select class=\"{field_class}\"{attrs}>{options}</select>") |
| 358 |
} |
| 359 |
FieldKind::Checkbox => { |
| 360 |
let checked = if matches!(filling.value, Value::On(true)) { |
| 361 |
" checked" |
| 362 |
} else { |
| 363 |
"" |
| 364 |
}; |
| 365 |
format!( |
| 366 |
"<label class=\"{}\"><input type=\"checkbox\"{attrs}{checked}><span>{}</span></label>", |
| 367 |
class("form-checkbox-label", opts), |
| 368 |
escape(field.label) |
| 369 |
) |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
FieldKind::Secret => { |
| 378 |
format!("<input type=\"password\" class=\"{field_class}\"{attrs}{placeholder}>") |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
FieldKind::File => { |
| 385 |
format!("<input type=\"file\" class=\"{field_class}\"{attrs}>") |
| 386 |
} |
| 387 |
kind => format!( |
| 388 |
"<input type=\"{}\" class=\"{field_class}\"{attrs}{placeholder} value=\"{}\">", |
| 389 |
input_type(kind), |
| 390 |
escape(filling.value.as_text()) |
| 391 |
), |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
#[must_use] |
| 423 |
pub fn field_html(field: &Field<'_>, filling: &Filling<'_>, opts: &Emit) -> String { |
| 424 |
let id = filling.id_for(field.name); |
| 425 |
|
| 426 |
if !field.kind.visible() { |
| 427 |
|
| 428 |
|
| 429 |
return format!( |
| 430 |
"<input type=\"hidden\" name=\"{}\" value=\"{}\">", |
| 431 |
escape(field.name), |
| 432 |
escape(filling.value.as_text()) |
| 433 |
); |
| 434 |
} |
| 435 |
|
| 436 |
let mut html = format!("<div class=\"{}", class("form-group", opts)); |
| 437 |
if field.invalid() { |
| 438 |
html.push_str(" has-error"); |
| 439 |
} |
| 440 |
if field.extended { |
| 441 |
|
| 442 |
|
| 443 |
html.push_str("\" data-extended=\"true"); |
| 444 |
} |
| 445 |
html.push_str("\">"); |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
if !field.kind.labels_itself() { |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
let association = if is_group_control(field.kind) { |
| 455 |
format!(" id=\"{id}-label\"") |
| 456 |
} else { |
| 457 |
format!(" for=\"{id}\"") |
| 458 |
}; |
| 459 |
let _ = write!( |
| 460 |
html, |
| 461 |
"<label class=\"{}\"{association}>{}</label>", |
| 462 |
class("form-label", opts), |
| 463 |
escape(field.label) |
| 464 |
); |
| 465 |
} |
| 466 |
|
| 467 |
html.push_str(&control_html(field, filling, opts)); |
| 468 |
|
| 469 |
if let Some(hint) = field.hint { |
| 470 |
let _ = write!( |
| 471 |
html, |
| 472 |
"<div class=\"{}\" id=\"{id}-hint\">{}</div>", |
| 473 |
class("form-hint", opts), |
| 474 |
escape(hint) |
| 475 |
); |
| 476 |
} |
| 477 |
if let Some(Markup(markup)) = filling.trailing { |
| 478 |
html.push_str(markup); |
| 479 |
} |
| 480 |
if let Some(error) = field.error { |
| 481 |
let _ = write!( |
| 482 |
html, |
| 483 |
"<div class=\"{} visible\" id=\"{id}-error\" role=\"alert\">{}</div>", |
| 484 |
class("form-error", opts), |
| 485 |
escape(error) |
| 486 |
); |
| 487 |
} |
| 488 |
|
| 489 |
html.push_str("</div>"); |
| 490 |
html |
| 491 |
} |
| 492 |
|
| 493 |
#[cfg(test)] |
| 494 |
mod tests { |
| 495 |
use super::*; |
| 496 |
|
| 497 |
fn field(kind: FieldKind) -> Field<'static> { |
| 498 |
Field::new(kind, "title", "Title") |
| 499 |
} |
| 500 |
|
| 501 |
#[test] |
| 502 |
fn a_value_cannot_break_out_of_the_attribute_it_sits_in() { |
| 503 |
|
| 504 |
let filling = Filling::of(Value::Text("x\" onfocus=alert(1) autofocus=\"")); |
| 505 |
let html = field_html(&field(FieldKind::Text), &filling, &Emit::default()); |
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
assert!(!html.contains("\" onfocus"), "{html}"); |
| 510 |
assert!( |
| 511 |
html.contains("value=\"x" onfocus=alert(1) autofocus="\""), |
| 512 |
"{html}" |
| 513 |
); |
| 514 |
} |
| 515 |
|
| 516 |
#[test] |
| 517 |
fn a_label_cannot_open_a_tag() { |
| 518 |
let mut f = field(FieldKind::Text); |
| 519 |
f.label = "<script>alert(1)</script>"; |
| 520 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 521 |
assert!(!html.contains("<script>"), "{html}"); |
| 522 |
assert!(html.contains("<script>"), "{html}"); |
| 523 |
} |
| 524 |
|
| 525 |
#[test] |
| 526 |
fn every_escaped_sink_is_covered_by_the_one_escaper() { |
| 527 |
assert_eq!(escape("&<>\"'"), "&<>"'"); |
| 528 |
|
| 529 |
|
| 530 |
assert!(escape("\"").contains(""")); |
| 531 |
} |
| 532 |
|
| 533 |
#[test] |
| 534 |
fn markup_is_the_only_way_past_the_escaping() { |
| 535 |
let filling = Filling { |
| 536 |
trailing: Some(Markup("<div class=\"recurrence-config\"></div>")), |
| 537 |
..Filling::default() |
| 538 |
}; |
| 539 |
let html = field_html(&field(FieldKind::Text), &filling, &Emit::default()); |
| 540 |
assert!( |
| 541 |
html.contains("<div class=\"recurrence-config\"></div>"), |
| 542 |
"{html}" |
| 543 |
); |
| 544 |
} |
| 545 |
|
| 546 |
#[test] |
| 547 |
fn an_invalid_field_carries_the_attribute_its_own_stylesheet_keys_on() { |
| 548 |
let mut f = field(FieldKind::Text); |
| 549 |
f.error = Some("Required"); |
| 550 |
let opts = Emit::default(); |
| 551 |
let html = field_html(&f, &Filling::default(), &opts); |
| 552 |
assert!(html.contains("aria-invalid=\"true\""), "{html}"); |
| 553 |
|
| 554 |
assert!(crate::stylesheet(&opts).contains("[aria-invalid=\"true\"]")); |
| 555 |
|
| 556 |
|
| 557 |
assert!(html.contains("has-error"), "{html}"); |
| 558 |
} |
| 559 |
|
| 560 |
#[test] |
| 561 |
fn a_valid_field_claims_nothing_about_being_invalid() { |
| 562 |
let html = field_html( |
| 563 |
&field(FieldKind::Text), |
| 564 |
&Filling::default(), |
| 565 |
&Emit::default(), |
| 566 |
); |
| 567 |
assert!(!html.contains("aria-invalid"), "{html}"); |
| 568 |
assert!(!html.contains("has-error"), "{html}"); |
| 569 |
} |
| 570 |
|
| 571 |
#[test] |
| 572 |
fn the_hint_survives_an_error_arriving() { |
| 573 |
let mut f = field(FieldKind::Text); |
| 574 |
f.hint = Some("Keep it short"); |
| 575 |
f.error = Some("Required"); |
| 576 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 577 |
assert!( |
| 578 |
html.contains("aria-describedby=\"title-hint title-error\""), |
| 579 |
"{html}" |
| 580 |
); |
| 581 |
} |
| 582 |
|
| 583 |
#[test] |
| 584 |
fn a_secret_never_carries_its_value_into_the_markup() { |
| 585 |
let filling = Filling::of(Value::Text("hunter2")); |
| 586 |
let html = field_html(&field(FieldKind::Secret), &filling, &Emit::default()); |
| 587 |
assert!(!html.contains("hunter2"), "{html}"); |
| 588 |
assert!(html.contains("type=\"password\""), "{html}"); |
| 589 |
} |
| 590 |
|
| 591 |
#[test] |
| 592 |
fn a_hidden_field_is_the_input_and_nothing_else() { |
| 593 |
let filling = Filling::of(Value::Text("42")); |
| 594 |
let html = field_html(&field(FieldKind::Hidden), &filling, &Emit::default()); |
| 595 |
assert_eq!(html, "<input type=\"hidden\" name=\"title\" value=\"42\">"); |
| 596 |
} |
| 597 |
|
| 598 |
#[test] |
| 599 |
fn a_checkbox_labels_itself_and_takes_no_separate_label() { |
| 600 |
let html = field_html( |
| 601 |
&field(FieldKind::Checkbox), |
| 602 |
&Filling::of(Value::On(true)), |
| 603 |
&Emit::default(), |
| 604 |
); |
| 605 |
assert!(!html.contains("form-label"), "{html}"); |
| 606 |
assert!(html.contains("checked"), "{html}"); |
| 607 |
assert!(html.contains("<span>Title</span>"), "{html}"); |
| 608 |
} |
| 609 |
|
| 610 |
#[test] |
| 611 |
fn a_select_keeps_a_value_no_option_carries() { |
| 612 |
let options = [Choice::plain("1"), Choice::plain("3"), Choice::plain("7")]; |
| 613 |
let f = Field::select("title", "Title", &options); |
| 614 |
let html = field_html(&f, &Filling::of(Value::Text("10")), &Emit::default()); |
| 615 |
assert!(html.contains("data-unmatched=\"true\""), "{html}"); |
| 616 |
|
| 617 |
|
| 618 |
assert!(html.contains("<option value=\"10\" selected"), "{html}"); |
| 619 |
} |
| 620 |
|
| 621 |
#[test] |
| 622 |
fn a_select_with_no_options_emits_an_empty_select() { |
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
let f = Field::select("title", "Title", &[]); |
| 627 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 628 |
assert!(html.contains("<select"), "{html}"); |
| 629 |
assert!(!html.contains("<option"), "{html}"); |
| 630 |
} |
| 631 |
|
| 632 |
#[test] |
| 633 |
fn a_radio_group_is_named_by_its_label_instead_of_pointing_at_it() { |
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
let options = [Choice::plain("copy"), Choice::plain("reference")]; |
| 638 |
let f = Field::radio("storage", "Storage style", &options); |
| 639 |
let html = field_html(&f, &Filling::of(Value::Text("copy")), &Emit::default()); |
| 640 |
|
| 641 |
assert!(html.contains("id=\"storage-label\""), "{html}"); |
| 642 |
assert!(!html.contains("for=\"storage\""), "{html}"); |
| 643 |
assert!(html.contains("role=\"radiogroup\""), "{html}"); |
| 644 |
assert!(html.contains("aria-labelledby=\"storage-label\""), "{html}"); |
| 645 |
} |
| 646 |
|
| 647 |
#[test] |
| 648 |
fn every_option_shares_the_name_and_only_the_current_one_is_checked() { |
| 649 |
|
| 650 |
|
| 651 |
let options = [ |
| 652 |
Choice::plain("copy"), |
| 653 |
Choice::plain("reference"), |
| 654 |
Choice::plain("link"), |
| 655 |
]; |
| 656 |
let f = Field::radio("storage", "Storage style", &options); |
| 657 |
let html = field_html(&f, &Filling::of(Value::Text("reference")), &Emit::default()); |
| 658 |
|
| 659 |
assert_eq!(html.matches("name=\"storage\"").count(), 3, "{html}"); |
| 660 |
assert_eq!(html.matches(" checked").count(), 1, "{html}"); |
| 661 |
assert!( |
| 662 |
html.contains("value=\"reference\" checked"), |
| 663 |
"the checked one is the one held: {html}" |
| 664 |
); |
| 665 |
for index in 0..3 { |
| 666 |
assert!(html.contains(&format!("id=\"storage-{index}\"")), "{html}"); |
| 667 |
} |
| 668 |
} |
| 669 |
|
| 670 |
#[test] |
| 671 |
fn a_radio_group_carries_the_error_rather_than_any_one_option() { |
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
let options = [Choice::plain("copy"), Choice::plain("reference")]; |
| 676 |
let f = Field { |
| 677 |
error: Some("Pick one."), |
| 678 |
hint: Some("Cannot be changed later."), |
| 679 |
..Field::radio("storage", "Storage style", &options) |
| 680 |
}; |
| 681 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 682 |
|
| 683 |
assert_eq!(html.matches("aria-invalid=\"true\"").count(), 1, "{html}"); |
| 684 |
assert!( |
| 685 |
html.contains("aria-describedby=\"storage-hint storage-error\""), |
| 686 |
"{html}" |
| 687 |
); |
| 688 |
|
| 689 |
|
| 690 |
let group = html.find("role=\"radiogroup\"").expect("group"); |
| 691 |
let first = html.find("type=\"radio\"").expect("an option"); |
| 692 |
assert!(group < first, "{html}"); |
| 693 |
} |
| 694 |
|
| 695 |
#[test] |
| 696 |
fn a_compulsory_radio_group_marks_every_option() { |
| 697 |
|
| 698 |
|
| 699 |
let options = [Choice::plain("copy"), Choice::plain("reference")]; |
| 700 |
let f = Field { |
| 701 |
required: true, |
| 702 |
..Field::radio("storage", "Storage style", &options) |
| 703 |
}; |
| 704 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 705 |
assert_eq!(html.matches(" required").count(), 2, "{html}"); |
| 706 |
} |
| 707 |
|
| 708 |
#[test] |
| 709 |
fn a_radio_option_cannot_break_out_of_its_attribute() { |
| 710 |
|
| 711 |
|
| 712 |
let hostile = [Choice { |
| 713 |
value: "x\" onclick=alert(1) data-x=\"", |
| 714 |
label: "<script>alert(1)</script>", |
| 715 |
}]; |
| 716 |
let f = Field::radio("storage", "Storage style", &hostile); |
| 717 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
assert!(html.contains("value=\"x" onclick=alert(1)"), "{html}"); |
| 722 |
assert!(!html.contains("<script>"), "{html}"); |
| 723 |
assert!(html.contains("id=\"storage-0\""), "{html}"); |
| 724 |
} |
| 725 |
|
| 726 |
#[test] |
| 727 |
fn a_radio_group_with_no_options_emits_an_empty_group() { |
| 728 |
|
| 729 |
let f = Field::radio("storage", "Storage style", &[]); |
| 730 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 731 |
assert!(html.contains("role=\"radiogroup\""), "{html}"); |
| 732 |
assert!(!html.contains("type=\"radio\""), "{html}"); |
| 733 |
} |
| 734 |
|
| 735 |
#[test] |
| 736 |
fn a_placeholder_comes_off_the_description_and_is_escaped() { |
| 737 |
|
| 738 |
|
| 739 |
let f = Field { |
| 740 |
placeholder: Some("x\" onfocus=alert(1) autofocus=\""), |
| 741 |
..field(FieldKind::Text) |
| 742 |
}; |
| 743 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 744 |
assert!(html.contains("placeholder=\""), "{html}"); |
| 745 |
assert!(!html.contains("\" onfocus"), "{html}"); |
| 746 |
} |
| 747 |
|
| 748 |
#[test] |
| 749 |
fn a_select_marks_the_option_that_matches() { |
| 750 |
let options = [Choice::plain("1"), Choice::plain("3")]; |
| 751 |
let f = Field::select("title", "Title", &options); |
| 752 |
let html = field_html(&f, &Filling::of(Value::Text("3")), &Emit::default()); |
| 753 |
assert!( |
| 754 |
html.contains("<option value=\"3\" selected>3</option>"), |
| 755 |
"{html}" |
| 756 |
); |
| 757 |
assert!(html.contains("<option value=\"1\">1</option>"), "{html}"); |
| 758 |
assert!(!html.contains("data-unmatched"), "{html}"); |
| 759 |
} |
| 760 |
|
| 761 |
#[test] |
| 762 |
fn a_textarea_carries_its_value_as_text_and_not_as_an_attribute() { |
| 763 |
let filling = Filling::of(Value::Text("two\nlines")); |
| 764 |
let html = field_html(&field(FieldKind::Textarea), &filling, &Emit::default()); |
| 765 |
assert!(html.contains(">two\nlines</textarea>"), "{html}"); |
| 766 |
} |
| 767 |
|
| 768 |
#[test] |
| 769 |
fn the_class_prefix_reaches_the_markup_as_well_as_the_stylesheet() { |
| 770 |
let opts = Emit { |
| 771 |
class_prefix: "mk-", |
| 772 |
..Emit::default() |
| 773 |
}; |
| 774 |
let html = field_html(&field(FieldKind::Text), &Filling::default(), &opts); |
| 775 |
assert!(html.contains("class=\"mk-form-group\""), "{html}"); |
| 776 |
assert!(html.contains("class=\"mk-field\""), "{html}"); |
| 777 |
} |
| 778 |
|
| 779 |
#[test] |
| 780 |
fn an_extended_field_says_so_and_leaves_the_disclosure_to_the_form() { |
| 781 |
let mut f = field(FieldKind::Text); |
| 782 |
f.extended = true; |
| 783 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 784 |
assert!(html.contains("data-extended=\"true\""), "{html}"); |
| 785 |
} |
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
#[test] |
| 791 |
fn the_id_prefix_scopes_the_id_and_never_the_name() { |
| 792 |
let mut f = field(FieldKind::Text); |
| 793 |
f.hint = Some("Keep it short"); |
| 794 |
f.error = Some("Required"); |
| 795 |
let filling = Filling { |
| 796 |
id_prefix: Some("form-modal-task-edit"), |
| 797 |
..Filling::default() |
| 798 |
}; |
| 799 |
let html = field_html(&f, &filling, &Emit::default()); |
| 800 |
|
| 801 |
assert!( |
| 802 |
html.contains(r#"id="form-modal-task-edit-title""#), |
| 803 |
"{html}" |
| 804 |
); |
| 805 |
assert!(html.contains(r#"name="title""#), "{html}"); |
| 806 |
assert!( |
| 807 |
!html.contains(r#"name="form-modal-task-edit-title""#), |
| 808 |
"{html}" |
| 809 |
); |
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
assert!( |
| 814 |
html.contains(r#"for="form-modal-task-edit-title""#), |
| 815 |
"{html}" |
| 816 |
); |
| 817 |
assert!( |
| 818 |
html.contains( |
| 819 |
r#"aria-describedby="form-modal-task-edit-title-hint form-modal-task-edit-title-error""# |
| 820 |
), |
| 821 |
"{html}" |
| 822 |
); |
| 823 |
assert!( |
| 824 |
html.contains(r#"id="form-modal-task-edit-title-hint""#), |
| 825 |
"{html}" |
| 826 |
); |
| 827 |
} |
| 828 |
|
| 829 |
#[test] |
| 830 |
fn a_hidden_field_submits_its_bare_name_under_a_prefix() { |
| 831 |
let filling = Filling { |
| 832 |
value: Value::Text("42"), |
| 833 |
id_prefix: Some("scoped"), |
| 834 |
..Filling::default() |
| 835 |
}; |
| 836 |
let html = field_html(&field(FieldKind::Hidden), &filling, &Emit::default()); |
| 837 |
assert_eq!(html, r#"<input type="hidden" name="title" value="42">"#); |
| 838 |
} |
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
#[test] |
| 844 |
fn a_constraint_becomes_the_browsers_own_attribute() { |
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
let html = field_html( |
| 849 |
&Field { |
| 850 |
max_length: Some(100), |
| 851 |
min: Some("1"), |
| 852 |
max: Some("240"), |
| 853 |
required: true, |
| 854 |
..Field::new(FieldKind::Number, "minutes", "Minutes") |
| 855 |
}, |
| 856 |
&Filling::default(), |
| 857 |
&Emit::default(), |
| 858 |
); |
| 859 |
assert!(html.contains(r#"maxlength="100""#)); |
| 860 |
assert!(html.contains(r#"min="1""#)); |
| 861 |
assert!(html.contains(r#"max="240""#)); |
| 862 |
assert!(html.contains(" required")); |
| 863 |
} |
| 864 |
|
| 865 |
#[test] |
| 866 |
fn a_bound_is_emitted_as_written_and_escaped_like_anything_else() { |
| 867 |
|
| 868 |
|
| 869 |
let html = field_html( |
| 870 |
&Field { |
| 871 |
min: Some("2026-08-09T14:30"), |
| 872 |
..Field::new(FieldKind::Text, "starts", "Starts") |
| 873 |
}, |
| 874 |
&Filling::default(), |
| 875 |
&Emit::default(), |
| 876 |
); |
| 877 |
assert!(html.contains(r#"min="2026-08-09T14:30""#)); |
| 878 |
} |
| 879 |
|
| 880 |
#[test] |
| 881 |
fn a_file_field_is_a_file_input() { |
| 882 |
|
| 883 |
|
| 884 |
let html = field_html( |
| 885 |
&Field::new(FieldKind::File, "attachment", "Attachment"), |
| 886 |
&Filling::default(), |
| 887 |
&Emit::default(), |
| 888 |
); |
| 889 |
assert!(html.contains(r#"type="file""#)); |
| 890 |
assert!(!html.contains("accept=")); |
| 891 |
|
| 892 |
|
| 893 |
assert!(!html.contains("value=")); |
| 894 |
} |
| 895 |
|
| 896 |
#[test] |
| 897 |
fn the_typed_text_kinds_keep_their_input_type() { |
| 898 |
for (kind, expected) in [ |
| 899 |
(FieldKind::Email, "email"), |
| 900 |
(FieldKind::Url, "url"), |
| 901 |
(FieldKind::Tel, "tel"), |
| 902 |
(FieldKind::Date, "date"), |
| 903 |
(FieldKind::DateTime, "datetime-local"), |
| 904 |
] { |
| 905 |
let html = field_html(&field(kind), &Filling::default(), &Emit::default()); |
| 906 |
assert!( |
| 907 |
html.contains(&format!(r#"type="{expected}""#)), |
| 908 |
"{kind:?} emitted {html}" |
| 909 |
); |
| 910 |
} |
| 911 |
} |
| 912 |
|
| 913 |
#[test] |
| 914 |
fn a_temporal_field_is_a_native_control_and_not_a_hinted_text_box() { |
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
for kind in [FieldKind::Date, FieldKind::DateTime] { |
| 919 |
let html = field_html(&field(kind), &Filling::default(), &Emit::default()); |
| 920 |
assert!(!html.contains(r#"type="text""#), "{kind:?} emitted {html}"); |
| 921 |
} |
| 922 |
} |
| 923 |
|
| 924 |
#[test] |
| 925 |
fn no_prefix_leaves_the_id_as_the_name() { |
| 926 |
let html = field_html( |
| 927 |
&field(FieldKind::Text), |
| 928 |
&Filling::default(), |
| 929 |
&Emit::default(), |
| 930 |
); |
| 931 |
assert!(html.contains(r#"id="title" name="title""#), "{html}"); |
| 932 |
} |
| 933 |
} |
| 934 |
|