| 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, push_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 |
let mut id = String::new(); |
| 125 |
if let Some(prefix) = self.id_prefix { |
| 126 |
escape_into(prefix, &mut id); |
| 127 |
id.push('-'); |
| 128 |
} |
| 129 |
escape_into(name, &mut id); |
| 130 |
id |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
pub fn escape_into(text: &str, out: &mut String) { |
| 154 |
let mut start = 0; |
| 155 |
for (index, byte) in text.bytes().enumerate() { |
| 156 |
let encoded = match byte { |
| 157 |
b'&' => "&", |
| 158 |
b'<' => "<", |
| 159 |
b'>' => ">", |
| 160 |
b'"' => """, |
| 161 |
b'\'' => "'", |
| 162 |
_ => continue, |
| 163 |
}; |
| 164 |
out.push_str(&text[start..index]); |
| 165 |
out.push_str(encoded); |
| 166 |
start = index + 1; |
| 167 |
} |
| 168 |
out.push_str(&text[start..]); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
#[must_use] |
| 179 |
pub fn escape(text: &str) -> String { |
| 180 |
let mut out = String::with_capacity(text.len()); |
| 181 |
escape_into(text, &mut out); |
| 182 |
out |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
const fn input_type(kind: FieldKind) -> &'static str { |
| 189 |
match kind { |
| 190 |
FieldKind::Secret => "password", |
| 191 |
FieldKind::Number => "number", |
| 192 |
FieldKind::Checkbox => "checkbox", |
| 193 |
FieldKind::File => "file", |
| 194 |
FieldKind::Hidden => "hidden", |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
FieldKind::Email => "email", |
| 200 |
FieldKind::Url => "url", |
| 201 |
FieldKind::Tel => "tel", |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
FieldKind::Date => "date", |
| 207 |
FieldKind::DateTime => "datetime-local", |
| 208 |
FieldKind::Radio => "radio", |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
FieldKind::Text | FieldKind::Select | FieldKind::Textarea => "text", |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
_ => "text", |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
fn push_control_attributes(out: &mut String, field: &Field<'_>, id: &str, name: &str) { |
| 233 |
let _ = write!(out, " id=\"{id}\" name=\""); |
| 234 |
escape_into(name, out); |
| 235 |
out.push('"'); |
| 236 |
if field.required { |
| 237 |
out.push_str(" required"); |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
if let Some(limit) = field.max_length { |
| 244 |
let _ = write!(out, " maxlength=\"{limit}\""); |
| 245 |
} |
| 246 |
if let Some(min) = field.min { |
| 247 |
out.push_str(" min=\""); |
| 248 |
escape_into(min, out); |
| 249 |
out.push('"'); |
| 250 |
} |
| 251 |
if let Some(max) = field.max { |
| 252 |
out.push_str(" max=\""); |
| 253 |
escape_into(max, out); |
| 254 |
out.push('"'); |
| 255 |
} |
| 256 |
if field.invalid() { |
| 257 |
out.push_str(" aria-invalid=\"true\""); |
| 258 |
} |
| 259 |
|
| 260 |
push_described_by(out, field, id); |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
fn push_described_by(out: &mut String, field: &Field<'_>, id: &str) { |
| 273 |
if field.hint.is_none() && field.error.is_none() { |
| 274 |
return; |
| 275 |
} |
| 276 |
out.push_str(" aria-describedby=\""); |
| 277 |
if field.hint.is_some() { |
| 278 |
let _ = write!(out, "{id}-hint"); |
| 279 |
} |
| 280 |
if field.error.is_some() { |
| 281 |
if field.hint.is_some() { |
| 282 |
out.push(' '); |
| 283 |
} |
| 284 |
let _ = write!(out, "{id}-error"); |
| 285 |
} |
| 286 |
out.push('"'); |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
const fn is_group_control(kind: FieldKind) -> bool { |
| 298 |
matches!(kind, FieldKind::Radio) |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
fn push_radio(out: &mut String, field: &Field<'_>, filling: &Filling<'_>, opts: &Emit) { |
| 315 |
let id = filling.id_for(field.name); |
| 316 |
let value = filling.value.as_text(); |
| 317 |
let name = escape(field.name); |
| 318 |
|
| 319 |
out.push_str("<div class=\""); |
| 320 |
push_class(out, "form-radio-group", opts); |
| 321 |
let _ = write!(out, "\" role=\"radiogroup\" aria-labelledby=\"{id}-label\""); |
| 322 |
if field.invalid() { |
| 323 |
out.push_str(" aria-invalid=\"true\""); |
| 324 |
} |
| 325 |
push_described_by(out, field, &id); |
| 326 |
out.push('>'); |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
for (index, opt) in field.options.iter().enumerate() { |
| 332 |
out.push_str("<label class=\""); |
| 333 |
push_class(out, "form-radio-label", opts); |
| 334 |
let _ = write!( |
| 335 |
out, |
| 336 |
"\"><input type=\"radio\" id=\"{id}-{index}\" name=\"{name}\" value=\"" |
| 337 |
); |
| 338 |
escape_into(opt.value, out); |
| 339 |
out.push('"'); |
| 340 |
if opt.value == value { |
| 341 |
out.push_str(" checked"); |
| 342 |
} |
| 343 |
if field.required { |
| 344 |
out.push_str(" required"); |
| 345 |
} |
| 346 |
out.push_str("><span>"); |
| 347 |
escape_into(opt.label, out); |
| 348 |
out.push_str("</span></label>"); |
| 349 |
} |
| 350 |
|
| 351 |
out.push_str("</div>"); |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
fn push_options(out: &mut String, options: &[Choice<'_>], value: &str) { |
| 362 |
if !value.is_empty() && !options.iter().any(|opt| opt.value == value) { |
| 363 |
|
| 364 |
|
| 365 |
let escaped = escape(value); |
| 366 |
let _ = write!( |
| 367 |
out, |
| 368 |
"<option value=\"{escaped}\" selected data-unmatched=\"true\">{escaped}</option>" |
| 369 |
); |
| 370 |
} |
| 371 |
for opt in options { |
| 372 |
out.push_str("<option value=\""); |
| 373 |
escape_into(opt.value, out); |
| 374 |
out.push('"'); |
| 375 |
if opt.value == value { |
| 376 |
out.push_str(" selected"); |
| 377 |
} |
| 378 |
out.push('>'); |
| 379 |
escape_into(opt.label, out); |
| 380 |
out.push_str("</option>"); |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
fn push_control(out: &mut String, field: &Field<'_>, filling: &Filling<'_>, opts: &Emit) { |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
if matches!(field.kind, FieldKind::Radio) { |
| 390 |
push_radio(out, field, filling, opts); |
| 391 |
return; |
| 392 |
} |
| 393 |
|
| 394 |
let id = filling.id_for(field.name); |
| 395 |
let placeholder = |out: &mut String| { |
| 396 |
if let Some(text) = field.placeholder { |
| 397 |
out.push_str(" placeholder=\""); |
| 398 |
escape_into(text, out); |
| 399 |
out.push('"'); |
| 400 |
} |
| 401 |
}; |
| 402 |
|
| 403 |
match field.kind { |
| 404 |
FieldKind::Textarea => { |
| 405 |
out.push_str("<textarea class=\""); |
| 406 |
push_class(out, "field", opts); |
| 407 |
out.push('"'); |
| 408 |
push_control_attributes(out, field, &id, field.name); |
| 409 |
placeholder(out); |
| 410 |
out.push('>'); |
| 411 |
escape_into(filling.value.as_text(), out); |
| 412 |
out.push_str("</textarea>"); |
| 413 |
} |
| 414 |
FieldKind::Select => { |
| 415 |
out.push_str("<select class=\""); |
| 416 |
push_class(out, "field", opts); |
| 417 |
out.push('"'); |
| 418 |
push_control_attributes(out, field, &id, field.name); |
| 419 |
out.push('>'); |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
push_options(out, field.options, filling.value.as_text()); |
| 424 |
out.push_str("</select>"); |
| 425 |
} |
| 426 |
FieldKind::Checkbox => { |
| 427 |
out.push_str("<label class=\""); |
| 428 |
push_class(out, "form-checkbox-label", opts); |
| 429 |
out.push_str("\"><input type=\"checkbox\""); |
| 430 |
push_control_attributes(out, field, &id, field.name); |
| 431 |
if matches!(filling.value, Value::On(true)) { |
| 432 |
out.push_str(" checked"); |
| 433 |
} |
| 434 |
out.push_str("><span>"); |
| 435 |
escape_into(field.label, out); |
| 436 |
out.push_str("</span></label>"); |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
FieldKind::Secret => { |
| 445 |
out.push_str("<input type=\"password\" class=\""); |
| 446 |
push_class(out, "field", opts); |
| 447 |
out.push('"'); |
| 448 |
push_control_attributes(out, field, &id, field.name); |
| 449 |
placeholder(out); |
| 450 |
out.push('>'); |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
FieldKind::File => { |
| 457 |
out.push_str("<input type=\"file\" class=\""); |
| 458 |
push_class(out, "field", opts); |
| 459 |
out.push('"'); |
| 460 |
push_control_attributes(out, field, &id, field.name); |
| 461 |
out.push('>'); |
| 462 |
} |
| 463 |
kind => { |
| 464 |
let _ = write!(out, "<input type=\"{}\" class=\"", input_type(kind)); |
| 465 |
push_class(out, "field", opts); |
| 466 |
out.push('"'); |
| 467 |
push_control_attributes(out, field, &id, field.name); |
| 468 |
placeholder(out); |
| 469 |
out.push_str(" value=\""); |
| 470 |
escape_into(filling.value.as_text(), out); |
| 471 |
out.push_str("\">"); |
| 472 |
} |
| 473 |
} |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
#[must_use] |
| 504 |
pub fn field_html(field: &Field<'_>, filling: &Filling<'_>, opts: &Emit) -> String { |
| 505 |
let mut html = String::new(); |
| 506 |
field_html_into(field, filling, opts, &mut html); |
| 507 |
html |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
pub fn field_html_into(field: &Field<'_>, filling: &Filling<'_>, opts: &Emit, out: &mut String) { |
| 516 |
let id = filling.id_for(field.name); |
| 517 |
|
| 518 |
if !field.kind.visible() { |
| 519 |
|
| 520 |
|
| 521 |
out.push_str("<input type=\"hidden\" name=\""); |
| 522 |
escape_into(field.name, out); |
| 523 |
out.push_str("\" value=\""); |
| 524 |
escape_into(filling.value.as_text(), out); |
| 525 |
out.push_str("\">"); |
| 526 |
return; |
| 527 |
} |
| 528 |
|
| 529 |
out.push_str("<div class=\""); |
| 530 |
push_class(out, "form-group", opts); |
| 531 |
if field.invalid() { |
| 532 |
out.push_str(" has-error"); |
| 533 |
} |
| 534 |
if field.extended { |
| 535 |
|
| 536 |
|
| 537 |
out.push_str("\" data-extended=\"true"); |
| 538 |
} |
| 539 |
out.push_str("\">"); |
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
if !field.kind.labels_itself() { |
| 545 |
out.push_str("<label class=\""); |
| 546 |
push_class(out, "form-label", opts); |
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
if is_group_control(field.kind) { |
| 551 |
let _ = write!(out, "\" id=\"{id}-label\">"); |
| 552 |
} else { |
| 553 |
let _ = write!(out, "\" for=\"{id}\">"); |
| 554 |
} |
| 555 |
escape_into(field.label, out); |
| 556 |
out.push_str("</label>"); |
| 557 |
} |
| 558 |
|
| 559 |
push_control(out, field, filling, opts); |
| 560 |
|
| 561 |
if let Some(hint) = field.hint { |
| 562 |
out.push_str("<div class=\""); |
| 563 |
push_class(out, "form-hint", opts); |
| 564 |
let _ = write!(out, "\" id=\"{id}-hint\">"); |
| 565 |
escape_into(hint, out); |
| 566 |
out.push_str("</div>"); |
| 567 |
} |
| 568 |
if let Some(Markup(markup)) = filling.trailing { |
| 569 |
out.push_str(markup); |
| 570 |
} |
| 571 |
if let Some(error) = field.error { |
| 572 |
out.push_str("<div class=\""); |
| 573 |
push_class(out, "form-error", opts); |
| 574 |
let _ = write!(out, " visible\" id=\"{id}-error\" role=\"alert\">"); |
| 575 |
escape_into(error, out); |
| 576 |
out.push_str("</div>"); |
| 577 |
} |
| 578 |
|
| 579 |
out.push_str("</div>"); |
| 580 |
} |
| 581 |
|
| 582 |
#[cfg(test)] |
| 583 |
mod tests { |
| 584 |
use super::*; |
| 585 |
|
| 586 |
fn field(kind: FieldKind) -> Field<'static> { |
| 587 |
Field::new(kind, "title", "Title") |
| 588 |
} |
| 589 |
|
| 590 |
#[test] |
| 591 |
fn a_value_cannot_break_out_of_the_attribute_it_sits_in() { |
| 592 |
|
| 593 |
let filling = Filling::of(Value::Text("x\" onfocus=alert(1) autofocus=\"")); |
| 594 |
let html = field_html(&field(FieldKind::Text), &filling, &Emit::default()); |
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
assert!(!html.contains("\" onfocus"), "{html}"); |
| 599 |
assert!( |
| 600 |
html.contains("value=\"x" onfocus=alert(1) autofocus="\""), |
| 601 |
"{html}" |
| 602 |
); |
| 603 |
} |
| 604 |
|
| 605 |
#[test] |
| 606 |
fn a_label_cannot_open_a_tag() { |
| 607 |
let mut f = field(FieldKind::Text); |
| 608 |
f.label = "<script>alert(1)</script>"; |
| 609 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 610 |
assert!(!html.contains("<script>"), "{html}"); |
| 611 |
assert!(html.contains("<script>"), "{html}"); |
| 612 |
} |
| 613 |
|
| 614 |
#[test] |
| 615 |
fn every_escaped_sink_is_covered_by_the_one_escaper() { |
| 616 |
assert_eq!(escape("&<>\"'"), "&<>"'"); |
| 617 |
|
| 618 |
|
| 619 |
assert!(escape("\"").contains(""")); |
| 620 |
} |
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
#[test] |
| 627 |
fn the_streaming_escaper_appends_what_the_returning_one_returns() { |
| 628 |
for text in [ |
| 629 |
"", |
| 630 |
"plain", |
| 631 |
"&<>\"'", |
| 632 |
"&&&", |
| 633 |
"a & b", |
| 634 |
"trailing&", |
| 635 |
"&leading", |
| 636 |
"é世 & <b>naïve</b> \u{1f600}", |
| 637 |
] { |
| 638 |
let mut out = String::from("kept: "); |
| 639 |
escape_into(text, &mut out); |
| 640 |
assert_eq!(out, format!("kept: {}", escape(text)), "{text:?}"); |
| 641 |
} |
| 642 |
} |
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
#[test] |
| 647 |
fn a_streamed_field_is_the_field_the_other_form_returns() { |
| 648 |
let kinds = [ |
| 649 |
FieldKind::Text, |
| 650 |
FieldKind::Secret, |
| 651 |
FieldKind::Number, |
| 652 |
FieldKind::Checkbox, |
| 653 |
FieldKind::Radio, |
| 654 |
FieldKind::Select, |
| 655 |
FieldKind::Textarea, |
| 656 |
FieldKind::File, |
| 657 |
FieldKind::Hidden, |
| 658 |
]; |
| 659 |
let choices = [Choice::plain("one"), Choice::plain("two")]; |
| 660 |
let opts = Emit { |
| 661 |
class_prefix: "mk-", |
| 662 |
..Emit::default() |
| 663 |
}; |
| 664 |
for kind in kinds { |
| 665 |
let described = Field { |
| 666 |
hint: Some("a hint"), |
| 667 |
error: Some("wrong <here>"), |
| 668 |
placeholder: Some("x\" y"), |
| 669 |
options: &choices, |
| 670 |
required: true, |
| 671 |
max_length: Some(40), |
| 672 |
min: Some("1"), |
| 673 |
max: Some("9"), |
| 674 |
extended: true, |
| 675 |
..Field::new(kind, "the & name", "The <label>") |
| 676 |
}; |
| 677 |
let filling = Filling { |
| 678 |
value: Value::Text("one"), |
| 679 |
trailing: Some(Markup("<i>t</i>")), |
| 680 |
id_prefix: Some("modal"), |
| 681 |
}; |
| 682 |
let mut streamed = String::new(); |
| 683 |
field_html_into(&described, &filling, &opts, &mut streamed); |
| 684 |
assert_eq!( |
| 685 |
streamed, |
| 686 |
field_html(&described, &filling, &opts), |
| 687 |
"{kind:?}" |
| 688 |
); |
| 689 |
|
| 690 |
|
| 691 |
let plain = Field::new(kind, "name", "Label"); |
| 692 |
let mut streamed = String::new(); |
| 693 |
field_html_into(&plain, &Filling::default(), &opts, &mut streamed); |
| 694 |
assert_eq!( |
| 695 |
streamed, |
| 696 |
field_html(&plain, &Filling::default(), &opts), |
| 697 |
"{kind:?}" |
| 698 |
); |
| 699 |
} |
| 700 |
} |
| 701 |
|
| 702 |
#[test] |
| 703 |
fn markup_is_the_only_way_past_the_escaping() { |
| 704 |
let filling = Filling { |
| 705 |
trailing: Some(Markup("<div class=\"recurrence-config\"></div>")), |
| 706 |
..Filling::default() |
| 707 |
}; |
| 708 |
let html = field_html(&field(FieldKind::Text), &filling, &Emit::default()); |
| 709 |
assert!( |
| 710 |
html.contains("<div class=\"recurrence-config\"></div>"), |
| 711 |
"{html}" |
| 712 |
); |
| 713 |
} |
| 714 |
|
| 715 |
#[test] |
| 716 |
fn an_invalid_field_carries_the_attribute_its_own_stylesheet_keys_on() { |
| 717 |
let mut f = field(FieldKind::Text); |
| 718 |
f.error = Some("Required"); |
| 719 |
let opts = Emit::default(); |
| 720 |
let html = field_html(&f, &Filling::default(), &opts); |
| 721 |
assert!(html.contains("aria-invalid=\"true\""), "{html}"); |
| 722 |
|
| 723 |
assert!(crate::stylesheet(&opts).contains("[aria-invalid=\"true\"]")); |
| 724 |
|
| 725 |
|
| 726 |
assert!(html.contains("has-error"), "{html}"); |
| 727 |
} |
| 728 |
|
| 729 |
#[test] |
| 730 |
fn a_valid_field_claims_nothing_about_being_invalid() { |
| 731 |
let html = field_html( |
| 732 |
&field(FieldKind::Text), |
| 733 |
&Filling::default(), |
| 734 |
&Emit::default(), |
| 735 |
); |
| 736 |
assert!(!html.contains("aria-invalid"), "{html}"); |
| 737 |
assert!(!html.contains("has-error"), "{html}"); |
| 738 |
} |
| 739 |
|
| 740 |
#[test] |
| 741 |
fn the_hint_survives_an_error_arriving() { |
| 742 |
let mut f = field(FieldKind::Text); |
| 743 |
f.hint = Some("Keep it short"); |
| 744 |
f.error = Some("Required"); |
| 745 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 746 |
assert!( |
| 747 |
html.contains("aria-describedby=\"title-hint title-error\""), |
| 748 |
"{html}" |
| 749 |
); |
| 750 |
} |
| 751 |
|
| 752 |
#[test] |
| 753 |
fn a_secret_never_carries_its_value_into_the_markup() { |
| 754 |
let filling = Filling::of(Value::Text("hunter2")); |
| 755 |
let html = field_html(&field(FieldKind::Secret), &filling, &Emit::default()); |
| 756 |
assert!(!html.contains("hunter2"), "{html}"); |
| 757 |
assert!(html.contains("type=\"password\""), "{html}"); |
| 758 |
} |
| 759 |
|
| 760 |
#[test] |
| 761 |
fn a_hidden_field_is_the_input_and_nothing_else() { |
| 762 |
let filling = Filling::of(Value::Text("42")); |
| 763 |
let html = field_html(&field(FieldKind::Hidden), &filling, &Emit::default()); |
| 764 |
assert_eq!(html, "<input type=\"hidden\" name=\"title\" value=\"42\">"); |
| 765 |
} |
| 766 |
|
| 767 |
#[test] |
| 768 |
fn a_checkbox_labels_itself_and_takes_no_separate_label() { |
| 769 |
let html = field_html( |
| 770 |
&field(FieldKind::Checkbox), |
| 771 |
&Filling::of(Value::On(true)), |
| 772 |
&Emit::default(), |
| 773 |
); |
| 774 |
assert!(!html.contains("form-label"), "{html}"); |
| 775 |
assert!(html.contains("checked"), "{html}"); |
| 776 |
assert!(html.contains("<span>Title</span>"), "{html}"); |
| 777 |
} |
| 778 |
|
| 779 |
#[test] |
| 780 |
fn a_select_keeps_a_value_no_option_carries() { |
| 781 |
let options = [Choice::plain("1"), Choice::plain("3"), Choice::plain("7")]; |
| 782 |
let f = Field::select("title", "Title", &options); |
| 783 |
let html = field_html(&f, &Filling::of(Value::Text("10")), &Emit::default()); |
| 784 |
assert!(html.contains("data-unmatched=\"true\""), "{html}"); |
| 785 |
|
| 786 |
|
| 787 |
assert!(html.contains("<option value=\"10\" selected"), "{html}"); |
| 788 |
} |
| 789 |
|
| 790 |
#[test] |
| 791 |
fn a_select_with_no_options_emits_an_empty_select() { |
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
let f = Field::select("title", "Title", &[]); |
| 796 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 797 |
assert!(html.contains("<select"), "{html}"); |
| 798 |
assert!(!html.contains("<option"), "{html}"); |
| 799 |
} |
| 800 |
|
| 801 |
#[test] |
| 802 |
fn a_radio_group_is_named_by_its_label_instead_of_pointing_at_it() { |
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
let options = [Choice::plain("copy"), Choice::plain("reference")]; |
| 807 |
let f = Field::radio("storage", "Storage style", &options); |
| 808 |
let html = field_html(&f, &Filling::of(Value::Text("copy")), &Emit::default()); |
| 809 |
|
| 810 |
assert!(html.contains("id=\"storage-label\""), "{html}"); |
| 811 |
assert!(!html.contains("for=\"storage\""), "{html}"); |
| 812 |
assert!(html.contains("role=\"radiogroup\""), "{html}"); |
| 813 |
assert!(html.contains("aria-labelledby=\"storage-label\""), "{html}"); |
| 814 |
} |
| 815 |
|
| 816 |
#[test] |
| 817 |
fn every_option_shares_the_name_and_only_the_current_one_is_checked() { |
| 818 |
|
| 819 |
|
| 820 |
let options = [ |
| 821 |
Choice::plain("copy"), |
| 822 |
Choice::plain("reference"), |
| 823 |
Choice::plain("link"), |
| 824 |
]; |
| 825 |
let f = Field::radio("storage", "Storage style", &options); |
| 826 |
let html = field_html(&f, &Filling::of(Value::Text("reference")), &Emit::default()); |
| 827 |
|
| 828 |
assert_eq!(html.matches("name=\"storage\"").count(), 3, "{html}"); |
| 829 |
assert_eq!(html.matches(" checked").count(), 1, "{html}"); |
| 830 |
assert!( |
| 831 |
html.contains("value=\"reference\" checked"), |
| 832 |
"the checked one is the one held: {html}" |
| 833 |
); |
| 834 |
for index in 0..3 { |
| 835 |
assert!(html.contains(&format!("id=\"storage-{index}\"")), "{html}"); |
| 836 |
} |
| 837 |
} |
| 838 |
|
| 839 |
#[test] |
| 840 |
fn a_radio_group_carries_the_error_rather_than_any_one_option() { |
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
let options = [Choice::plain("copy"), Choice::plain("reference")]; |
| 845 |
let f = Field { |
| 846 |
error: Some("Pick one."), |
| 847 |
hint: Some("Cannot be changed later."), |
| 848 |
..Field::radio("storage", "Storage style", &options) |
| 849 |
}; |
| 850 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 851 |
|
| 852 |
assert_eq!(html.matches("aria-invalid=\"true\"").count(), 1, "{html}"); |
| 853 |
assert!( |
| 854 |
html.contains("aria-describedby=\"storage-hint storage-error\""), |
| 855 |
"{html}" |
| 856 |
); |
| 857 |
|
| 858 |
|
| 859 |
let group = html.find("role=\"radiogroup\"").expect("group"); |
| 860 |
let first = html.find("type=\"radio\"").expect("an option"); |
| 861 |
assert!(group < first, "{html}"); |
| 862 |
} |
| 863 |
|
| 864 |
#[test] |
| 865 |
fn a_compulsory_radio_group_marks_every_option() { |
| 866 |
|
| 867 |
|
| 868 |
let options = [Choice::plain("copy"), Choice::plain("reference")]; |
| 869 |
let f = Field { |
| 870 |
required: true, |
| 871 |
..Field::radio("storage", "Storage style", &options) |
| 872 |
}; |
| 873 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 874 |
assert_eq!(html.matches(" required").count(), 2, "{html}"); |
| 875 |
} |
| 876 |
|
| 877 |
#[test] |
| 878 |
fn a_radio_option_cannot_break_out_of_its_attribute() { |
| 879 |
|
| 880 |
|
| 881 |
let hostile = [Choice { |
| 882 |
value: "x\" onclick=alert(1) data-x=\"", |
| 883 |
label: "<script>alert(1)</script>", |
| 884 |
}]; |
| 885 |
let f = Field::radio("storage", "Storage style", &hostile); |
| 886 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
assert!(html.contains("value=\"x" onclick=alert(1)"), "{html}"); |
| 891 |
assert!(!html.contains("<script>"), "{html}"); |
| 892 |
assert!(html.contains("id=\"storage-0\""), "{html}"); |
| 893 |
} |
| 894 |
|
| 895 |
#[test] |
| 896 |
fn a_radio_group_with_no_options_emits_an_empty_group() { |
| 897 |
|
| 898 |
let f = Field::radio("storage", "Storage style", &[]); |
| 899 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 900 |
assert!(html.contains("role=\"radiogroup\""), "{html}"); |
| 901 |
assert!(!html.contains("type=\"radio\""), "{html}"); |
| 902 |
} |
| 903 |
|
| 904 |
#[test] |
| 905 |
fn a_placeholder_comes_off_the_description_and_is_escaped() { |
| 906 |
|
| 907 |
|
| 908 |
let f = Field { |
| 909 |
placeholder: Some("x\" onfocus=alert(1) autofocus=\""), |
| 910 |
..field(FieldKind::Text) |
| 911 |
}; |
| 912 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 913 |
assert!(html.contains("placeholder=\""), "{html}"); |
| 914 |
assert!(!html.contains("\" onfocus"), "{html}"); |
| 915 |
} |
| 916 |
|
| 917 |
#[test] |
| 918 |
fn a_select_marks_the_option_that_matches() { |
| 919 |
let options = [Choice::plain("1"), Choice::plain("3")]; |
| 920 |
let f = Field::select("title", "Title", &options); |
| 921 |
let html = field_html(&f, &Filling::of(Value::Text("3")), &Emit::default()); |
| 922 |
assert!( |
| 923 |
html.contains("<option value=\"3\" selected>3</option>"), |
| 924 |
"{html}" |
| 925 |
); |
| 926 |
assert!(html.contains("<option value=\"1\">1</option>"), "{html}"); |
| 927 |
assert!(!html.contains("data-unmatched"), "{html}"); |
| 928 |
} |
| 929 |
|
| 930 |
#[test] |
| 931 |
fn a_textarea_carries_its_value_as_text_and_not_as_an_attribute() { |
| 932 |
let filling = Filling::of(Value::Text("two\nlines")); |
| 933 |
let html = field_html(&field(FieldKind::Textarea), &filling, &Emit::default()); |
| 934 |
assert!(html.contains(">two\nlines</textarea>"), "{html}"); |
| 935 |
} |
| 936 |
|
| 937 |
#[test] |
| 938 |
fn the_class_prefix_reaches_the_markup_as_well_as_the_stylesheet() { |
| 939 |
let opts = Emit { |
| 940 |
class_prefix: "mk-", |
| 941 |
..Emit::default() |
| 942 |
}; |
| 943 |
let html = field_html(&field(FieldKind::Text), &Filling::default(), &opts); |
| 944 |
assert!(html.contains("class=\"mk-form-group\""), "{html}"); |
| 945 |
assert!(html.contains("class=\"mk-field\""), "{html}"); |
| 946 |
} |
| 947 |
|
| 948 |
#[test] |
| 949 |
fn an_extended_field_says_so_and_leaves_the_disclosure_to_the_form() { |
| 950 |
let mut f = field(FieldKind::Text); |
| 951 |
f.extended = true; |
| 952 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 953 |
assert!(html.contains("data-extended=\"true\""), "{html}"); |
| 954 |
} |
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
#[test] |
| 960 |
fn the_id_prefix_scopes_the_id_and_never_the_name() { |
| 961 |
let mut f = field(FieldKind::Text); |
| 962 |
f.hint = Some("Keep it short"); |
| 963 |
f.error = Some("Required"); |
| 964 |
let filling = Filling { |
| 965 |
id_prefix: Some("form-modal-task-edit"), |
| 966 |
..Filling::default() |
| 967 |
}; |
| 968 |
let html = field_html(&f, &filling, &Emit::default()); |
| 969 |
|
| 970 |
assert!( |
| 971 |
html.contains(r#"id="form-modal-task-edit-title""#), |
| 972 |
"{html}" |
| 973 |
); |
| 974 |
assert!(html.contains(r#"name="title""#), "{html}"); |
| 975 |
assert!( |
| 976 |
!html.contains(r#"name="form-modal-task-edit-title""#), |
| 977 |
"{html}" |
| 978 |
); |
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
assert!( |
| 983 |
html.contains(r#"for="form-modal-task-edit-title""#), |
| 984 |
"{html}" |
| 985 |
); |
| 986 |
assert!( |
| 987 |
html.contains( |
| 988 |
r#"aria-describedby="form-modal-task-edit-title-hint form-modal-task-edit-title-error""# |
| 989 |
), |
| 990 |
"{html}" |
| 991 |
); |
| 992 |
assert!( |
| 993 |
html.contains(r#"id="form-modal-task-edit-title-hint""#), |
| 994 |
"{html}" |
| 995 |
); |
| 996 |
} |
| 997 |
|
| 998 |
#[test] |
| 999 |
fn a_hidden_field_submits_its_bare_name_under_a_prefix() { |
| 1000 |
let filling = Filling { |
| 1001 |
value: Value::Text("42"), |
| 1002 |
id_prefix: Some("scoped"), |
| 1003 |
..Filling::default() |
| 1004 |
}; |
| 1005 |
let html = field_html(&field(FieldKind::Hidden), &filling, &Emit::default()); |
| 1006 |
assert_eq!(html, r#"<input type="hidden" name="title" value="42">"#); |
| 1007 |
} |
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
#[test] |
| 1013 |
fn a_constraint_becomes_the_browsers_own_attribute() { |
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
let html = field_html( |
| 1018 |
&Field { |
| 1019 |
max_length: Some(100), |
| 1020 |
min: Some("1"), |
| 1021 |
max: Some("240"), |
| 1022 |
required: true, |
| 1023 |
..Field::new(FieldKind::Number, "minutes", "Minutes") |
| 1024 |
}, |
| 1025 |
&Filling::default(), |
| 1026 |
&Emit::default(), |
| 1027 |
); |
| 1028 |
assert!(html.contains(r#"maxlength="100""#)); |
| 1029 |
assert!(html.contains(r#"min="1""#)); |
| 1030 |
assert!(html.contains(r#"max="240""#)); |
| 1031 |
assert!(html.contains(" required")); |
| 1032 |
} |
| 1033 |
|
| 1034 |
#[test] |
| 1035 |
fn a_bound_is_emitted_as_written_and_escaped_like_anything_else() { |
| 1036 |
|
| 1037 |
|
| 1038 |
let html = field_html( |
| 1039 |
&Field { |
| 1040 |
min: Some("2026-08-09T14:30"), |
| 1041 |
..Field::new(FieldKind::Text, "starts", "Starts") |
| 1042 |
}, |
| 1043 |
&Filling::default(), |
| 1044 |
&Emit::default(), |
| 1045 |
); |
| 1046 |
assert!(html.contains(r#"min="2026-08-09T14:30""#)); |
| 1047 |
} |
| 1048 |
|
| 1049 |
#[test] |
| 1050 |
fn a_file_field_is_a_file_input() { |
| 1051 |
|
| 1052 |
|
| 1053 |
let html = field_html( |
| 1054 |
&Field::new(FieldKind::File, "attachment", "Attachment"), |
| 1055 |
&Filling::default(), |
| 1056 |
&Emit::default(), |
| 1057 |
); |
| 1058 |
assert!(html.contains(r#"type="file""#)); |
| 1059 |
assert!(!html.contains("accept=")); |
| 1060 |
|
| 1061 |
|
| 1062 |
assert!(!html.contains("value=")); |
| 1063 |
} |
| 1064 |
|
| 1065 |
#[test] |
| 1066 |
fn the_typed_text_kinds_keep_their_input_type() { |
| 1067 |
for (kind, expected) in [ |
| 1068 |
(FieldKind::Email, "email"), |
| 1069 |
(FieldKind::Url, "url"), |
| 1070 |
(FieldKind::Tel, "tel"), |
| 1071 |
(FieldKind::Date, "date"), |
| 1072 |
(FieldKind::DateTime, "datetime-local"), |
| 1073 |
] { |
| 1074 |
let html = field_html(&field(kind), &Filling::default(), &Emit::default()); |
| 1075 |
assert!( |
| 1076 |
html.contains(&format!(r#"type="{expected}""#)), |
| 1077 |
"{kind:?} emitted {html}" |
| 1078 |
); |
| 1079 |
} |
| 1080 |
} |
| 1081 |
|
| 1082 |
#[test] |
| 1083 |
fn a_temporal_field_is_a_native_control_and_not_a_hinted_text_box() { |
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
for kind in [FieldKind::Date, FieldKind::DateTime] { |
| 1088 |
let html = field_html(&field(kind), &Filling::default(), &Emit::default()); |
| 1089 |
assert!(!html.contains(r#"type="text""#), "{kind:?} emitted {html}"); |
| 1090 |
} |
| 1091 |
} |
| 1092 |
|
| 1093 |
#[test] |
| 1094 |
fn no_prefix_leaves_the_id_as_the_name() { |
| 1095 |
let html = field_html( |
| 1096 |
&field(FieldKind::Text), |
| 1097 |
&Filling::default(), |
| 1098 |
&Emit::default(), |
| 1099 |
); |
| 1100 |
assert!(html.contains(r#"id="title" name="title""#), "{html}"); |
| 1101 |
} |
| 1102 |
} |
| 1103 |
|