| 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, push_class}; |
| 44 |
use makeover_layout::{Choice, Depth, Field, FieldKind, Intent as _, Selector, Tone}; |
| 45 |
use std::fmt::Write as _; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
pub const FIELD_CLASSES: &[&str] = &[ |
| 64 |
"field", |
| 65 |
"form-checkbox-label", |
| 66 |
"form-editor-modes", |
| 67 |
"form-editor-preview", |
| 68 |
"form-error", |
| 69 |
"form-group", |
| 70 |
"form-hint", |
| 71 |
"form-interval", |
| 72 |
"form-label", |
| 73 |
"form-note", |
| 74 |
"form-option-reason", |
| 75 |
"form-radio-group", |
| 76 |
"form-radio-label", |
| 77 |
"form-unit", |
| 78 |
]; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
pub const FIELD_STATE_CLASSES: &[&str] = &["has-error", "visible"]; |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 108 |
pub struct Markup<'a>(pub &'a str); |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 116 |
pub enum Value<'a> { |
| 117 |
|
| 118 |
#[default] |
| 119 |
Absent, |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
Text(&'a str), |
| 129 |
|
| 130 |
On(bool), |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
Between { |
| 144 |
|
| 145 |
lower: &'a str, |
| 146 |
|
| 147 |
upper: &'a str, |
| 148 |
}, |
| 149 |
} |
| 150 |
|
| 151 |
impl<'a> Value<'a> { |
| 152 |
|
| 153 |
const fn as_text(&self) -> &'a str { |
| 154 |
match self { |
| 155 |
Self::Text(text) | Self::Between { lower: text, .. } => text, |
| 156 |
Self::Absent | Self::On(_) => "", |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
impl<'a> Value<'a> { |
| 162 |
|
| 163 |
const fn upper_text(&self) -> &'a str { |
| 164 |
match self { |
| 165 |
Self::Between { upper, .. } => upper, |
| 166 |
Self::Absent | Self::Text(_) | Self::On(_) => "", |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
#[derive(Debug, Clone, Copy, Default)] |
| 173 |
pub struct Filling<'a> { |
| 174 |
|
| 175 |
pub value: Value<'a>, |
| 176 |
|
| 177 |
pub trailing: Option<Markup<'a>>, |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
pub control_attrs: Option<Markup<'a>>, |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
pub id_prefix: Option<&'a str>, |
| 210 |
} |
| 211 |
|
| 212 |
impl<'a> Filling<'a> { |
| 213 |
|
| 214 |
#[must_use] |
| 215 |
pub const fn of(value: Value<'a>) -> Self { |
| 216 |
Self { |
| 217 |
value, |
| 218 |
trailing: None, |
| 219 |
control_attrs: None, |
| 220 |
id_prefix: None, |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
fn id_for(&self, name: &str) -> String { |
| 226 |
let mut id = String::new(); |
| 227 |
if let Some(prefix) = self.id_prefix { |
| 228 |
escape_into(prefix, &mut id); |
| 229 |
id.push('-'); |
| 230 |
} |
| 231 |
escape_into(name, &mut id); |
| 232 |
id |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
pub fn escape_into(text: &str, out: &mut String) { |
| 256 |
let mut start = 0; |
| 257 |
for (index, byte) in text.bytes().enumerate() { |
| 258 |
let encoded = match byte { |
| 259 |
b'&' => "&", |
| 260 |
b'<' => "<", |
| 261 |
b'>' => ">", |
| 262 |
b'"' => """, |
| 263 |
b'\'' => "'", |
| 264 |
_ => continue, |
| 265 |
}; |
| 266 |
out.push_str(&text[start..index]); |
| 267 |
out.push_str(encoded); |
| 268 |
start = index + 1; |
| 269 |
} |
| 270 |
out.push_str(&text[start..]); |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
#[must_use] |
| 281 |
pub fn escape(text: &str) -> String { |
| 282 |
let mut out = String::with_capacity(text.len()); |
| 283 |
escape_into(text, &mut out); |
| 284 |
out |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
const fn input_type(kind: FieldKind) -> &'static str { |
| 291 |
match kind { |
| 292 |
FieldKind::Secret => "password", |
| 293 |
FieldKind::Number => "number", |
| 294 |
FieldKind::Checkbox => "checkbox", |
| 295 |
FieldKind::File => "file", |
| 296 |
FieldKind::Hidden => "hidden", |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
FieldKind::Email => "email", |
| 302 |
FieldKind::Url => "url", |
| 303 |
FieldKind::Tel => "tel", |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
FieldKind::Date => "date", |
| 309 |
FieldKind::DateTime => "datetime-local", |
| 310 |
FieldKind::Radio => "radio", |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
FieldKind::Range => "range", |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
FieldKind::Text | FieldKind::Select | FieldKind::Textarea | FieldKind::Rich => "text", |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
_ => "text", |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
fn push_accept(out: &mut String, field: &Field<'_>) { |
| 355 |
if field.accept.is_empty() { |
| 356 |
return; |
| 357 |
} |
| 358 |
out.push_str(" accept=\""); |
| 359 |
for (index, one) in field.accept.iter().enumerate() { |
| 360 |
if index > 0 { |
| 361 |
out.push(','); |
| 362 |
} |
| 363 |
escape_into(one.as_str(), out); |
| 364 |
} |
| 365 |
out.push('"'); |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
fn push_bounds(out: &mut String, field: &Field<'_>) { |
| 374 |
if let Some(min) = field.min { |
| 375 |
out.push_str(" min=\""); |
| 376 |
escape_into(min, out); |
| 377 |
out.push('"'); |
| 378 |
} |
| 379 |
if let Some(max) = field.max { |
| 380 |
out.push_str(" max=\""); |
| 381 |
escape_into(max, out); |
| 382 |
out.push('"'); |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
let step = if field.kind == FieldKind::Range { |
| 393 |
field.curve.step() |
| 394 |
} else { |
| 395 |
field.step |
| 396 |
}; |
| 397 |
if let Some(step) = step { |
| 398 |
out.push_str(" step=\""); |
| 399 |
escape_into(step, out); |
| 400 |
out.push('"'); |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
fn push_control_attributes( |
| 405 |
out: &mut String, |
| 406 |
field: &Field<'_>, |
| 407 |
filling: &Filling<'_>, |
| 408 |
id: &str, |
| 409 |
name: &str, |
| 410 |
) { |
| 411 |
let _ = write!(out, " id=\"{id}\" name=\""); |
| 412 |
escape_into(name, out); |
| 413 |
out.push('"'); |
| 414 |
if field.required { |
| 415 |
out.push_str(" required"); |
| 416 |
} |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
if let Some(limit) = field.max_length { |
| 422 |
let _ = write!(out, " maxlength=\"{limit}\""); |
| 423 |
} |
| 424 |
push_bounds(out, field); |
| 425 |
if field.invalid() { |
| 426 |
out.push_str(" aria-invalid=\"true\""); |
| 427 |
} |
| 428 |
|
| 429 |
push_described_by(out, field, id); |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
if let Some(Markup(attrs)) = filling.control_attrs { |
| 436 |
out.push(' '); |
| 437 |
out.push_str(attrs); |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
fn push_described_by(out: &mut String, field: &Field<'_>, id: &str) { |
| 451 |
let unit = unit_of(field).is_some(); |
| 452 |
if field.hint.is_none() && field.error.is_none() && field.note.is_none() && !unit { |
| 453 |
return; |
| 454 |
} |
| 455 |
let mut written = false; |
| 456 |
out.push_str(" aria-describedby=\""); |
| 457 |
if field.hint.is_some() { |
| 458 |
let _ = write!(out, "{id}-hint"); |
| 459 |
written = true; |
| 460 |
} |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
if unit { |
| 465 |
if written { |
| 466 |
out.push(' '); |
| 467 |
} |
| 468 |
let _ = write!(out, "{id}-unit"); |
| 469 |
written = true; |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
if field.note.is_some() { |
| 475 |
if written { |
| 476 |
out.push(' '); |
| 477 |
} |
| 478 |
let _ = write!(out, "{id}-note"); |
| 479 |
written = true; |
| 480 |
} |
| 481 |
if field.error.is_some() { |
| 482 |
if written { |
| 483 |
out.push(' '); |
| 484 |
} |
| 485 |
let _ = write!(out, "{id}-error"); |
| 486 |
} |
| 487 |
out.push('"'); |
| 488 |
} |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
fn unit_of<'a>(field: &Field<'a>) -> Option<&'a str> { |
| 497 |
field.unit.filter(|_| field.kind.measurable()) |
| 498 |
} |
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
const fn is_group_control(kind: FieldKind) -> bool { |
| 509 |
matches!(kind, FieldKind::Radio | FieldKind::Interval) |
| 510 |
} |
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
fn push_interval(out: &mut String, field: &Field<'_>, filling: &Filling<'_>, opts: &Emit) { |
| 541 |
let id = filling.id_for(field.name); |
| 542 |
|
| 543 |
out.push_str("<div class=\""); |
| 544 |
push_class(out, "form-interval", opts); |
| 545 |
let _ = write!(out, "\" role=\"group\" aria-labelledby=\"{id}-label\""); |
| 546 |
if field.invalid() { |
| 547 |
out.push_str(" aria-invalid=\"true\""); |
| 548 |
} |
| 549 |
push_described_by(out, field, &id); |
| 550 |
out.push('>'); |
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
let ends: [(&str, &str, &str); 2] = [ |
| 557 |
("lower", field.name, filling.value.as_text()), |
| 558 |
( |
| 559 |
"upper", |
| 560 |
field.upper_name.unwrap_or(""), |
| 561 |
filling.value.upper_text(), |
| 562 |
), |
| 563 |
]; |
| 564 |
for (end, name, value) in ends { |
| 565 |
if name.is_empty() { |
| 566 |
continue; |
| 567 |
} |
| 568 |
out.push_str("<input type=\"number\" class=\""); |
| 569 |
push_class(out, "field", opts); |
| 570 |
let _ = write!(out, "\" id=\"{id}-{end}\" name=\""); |
| 571 |
escape_into(name, out); |
| 572 |
let _ = write!(out, "\" aria-label=\"{end}\""); |
| 573 |
if field.required { |
| 574 |
out.push_str(" required"); |
| 575 |
} |
| 576 |
push_bounds(out, field); |
| 577 |
if let Some(text) = field.placeholder { |
| 578 |
out.push_str(" placeholder=\""); |
| 579 |
escape_into(text, out); |
| 580 |
out.push('"'); |
| 581 |
} |
| 582 |
out.push_str(" value=\""); |
| 583 |
escape_into(value, out); |
| 584 |
out.push_str("\">"); |
| 585 |
} |
| 586 |
|
| 587 |
out.push_str("</div>"); |
| 588 |
} |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
fn push_radio(out: &mut String, field: &Field<'_>, filling: &Filling<'_>, opts: &Emit) { |
| 604 |
let id = filling.id_for(field.name); |
| 605 |
let value = filling.value.as_text(); |
| 606 |
let name = escape(field.name); |
| 607 |
|
| 608 |
out.push_str("<div class=\""); |
| 609 |
push_class(out, "form-radio-group", opts); |
| 610 |
let _ = write!(out, "\" role=\"radiogroup\" aria-labelledby=\"{id}-label\""); |
| 611 |
if field.invalid() { |
| 612 |
out.push_str(" aria-invalid=\"true\""); |
| 613 |
} |
| 614 |
push_described_by(out, field, &id); |
| 615 |
out.push('>'); |
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
for (index, opt) in field.options.iter().enumerate() { |
| 621 |
out.push_str("<label class=\""); |
| 622 |
push_class(out, "form-radio-label", opts); |
| 623 |
let _ = write!( |
| 624 |
out, |
| 625 |
"\"><input type=\"radio\" id=\"{id}-{index}\" name=\"{name}\" value=\"" |
| 626 |
); |
| 627 |
escape_into(opt.value, out); |
| 628 |
out.push('"'); |
| 629 |
if opt.value == value { |
| 630 |
out.push_str(" checked"); |
| 631 |
} |
| 632 |
if field.required { |
| 633 |
out.push_str(" required"); |
| 634 |
} |
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
if let Some(reason) = opt.unavailable { |
| 641 |
out.push_str(" disabled"); |
| 642 |
out.push_str("><span>"); |
| 643 |
escape_into(opt.label, out); |
| 644 |
out.push_str("</span><span class=\""); |
| 645 |
push_class(out, "form-option-reason", opts); |
| 646 |
out.push_str("\">"); |
| 647 |
escape_into(reason, out); |
| 648 |
out.push_str("</span></label>"); |
| 649 |
continue; |
| 650 |
} |
| 651 |
out.push_str("><span>"); |
| 652 |
escape_into(opt.label, out); |
| 653 |
out.push_str("</span></label>"); |
| 654 |
} |
| 655 |
|
| 656 |
out.push_str("</div>"); |
| 657 |
} |
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
fn push_options(out: &mut String, field: &Field<'_>, options: &[Choice<'_>], value: &str) { |
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
if value.is_empty() |
| 684 |
&& let Some(text) = field.placeholder |
| 685 |
{ |
| 686 |
out.push_str("<option value=\"\" disabled selected>"); |
| 687 |
escape_into(text, out); |
| 688 |
out.push_str("</option>"); |
| 689 |
} |
| 690 |
if !value.is_empty() && !options.iter().any(|opt| opt.value == value) { |
| 691 |
|
| 692 |
|
| 693 |
let escaped = escape(value); |
| 694 |
let _ = write!( |
| 695 |
out, |
| 696 |
"<option value=\"{escaped}\" selected data-unmatched=\"true\">{escaped}</option>" |
| 697 |
); |
| 698 |
} |
| 699 |
for opt in options { |
| 700 |
out.push_str("<option value=\""); |
| 701 |
escape_into(opt.value, out); |
| 702 |
out.push('"'); |
| 703 |
if opt.value == value { |
| 704 |
out.push_str(" selected"); |
| 705 |
} |
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
if let Some(reason) = opt.unavailable { |
| 714 |
out.push_str(" disabled"); |
| 715 |
out.push('>'); |
| 716 |
escape_into(opt.label, out); |
| 717 |
out.push_str(": "); |
| 718 |
escape_into(reason, out); |
| 719 |
out.push_str("</option>"); |
| 720 |
continue; |
| 721 |
} |
| 722 |
out.push('>'); |
| 723 |
escape_into(opt.label, out); |
| 724 |
out.push_str("</option>"); |
| 725 |
} |
| 726 |
} |
| 727 |
|
| 728 |
|
| 729 |
fn push_control(out: &mut String, field: &Field<'_>, filling: &Filling<'_>, opts: &Emit) { |
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
if matches!(field.kind, FieldKind::Radio) { |
| 734 |
push_radio(out, field, filling, opts); |
| 735 |
return; |
| 736 |
} |
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
if matches!(field.kind, FieldKind::Interval) { |
| 741 |
push_interval(out, field, filling, opts); |
| 742 |
return; |
| 743 |
} |
| 744 |
|
| 745 |
let id = filling.id_for(field.name); |
| 746 |
let placeholder = |out: &mut String| { |
| 747 |
if let Some(text) = field.placeholder { |
| 748 |
out.push_str(" placeholder=\""); |
| 749 |
escape_into(text, out); |
| 750 |
out.push('"'); |
| 751 |
} |
| 752 |
}; |
| 753 |
|
| 754 |
match field.kind { |
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
kind if kind.multiline() => { |
| 767 |
let rich = matches!(kind, FieldKind::Rich); |
| 768 |
if rich { |
| 769 |
push_editor_open(out, opts); |
| 770 |
} |
| 771 |
out.push_str("<textarea class=\""); |
| 772 |
push_class(out, "field", opts); |
| 773 |
out.push('"'); |
| 774 |
if rich { |
| 775 |
out.push_str(" data-format=\"markdown\""); |
| 776 |
} |
| 777 |
push_control_attributes(out, field, filling, &id, field.name); |
| 778 |
placeholder(out); |
| 779 |
out.push('>'); |
| 780 |
escape_into(filling.value.as_text(), out); |
| 781 |
out.push_str("</textarea>"); |
| 782 |
if rich { |
| 783 |
push_editor_close(out, opts); |
| 784 |
} |
| 785 |
} |
| 786 |
FieldKind::Select => { |
| 787 |
out.push_str("<select class=\""); |
| 788 |
push_class(out, "field", opts); |
| 789 |
out.push('"'); |
| 790 |
push_control_attributes(out, field, filling, &id, field.name); |
| 791 |
out.push('>'); |
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
push_options(out, field, field.options, filling.value.as_text()); |
| 796 |
out.push_str("</select>"); |
| 797 |
} |
| 798 |
FieldKind::Checkbox => { |
| 799 |
out.push_str("<label class=\""); |
| 800 |
push_class(out, "form-checkbox-label", opts); |
| 801 |
out.push_str("\"><input type=\"checkbox\""); |
| 802 |
push_control_attributes(out, field, filling, &id, field.name); |
| 803 |
if matches!(filling.value, Value::On(true)) { |
| 804 |
out.push_str(" checked"); |
| 805 |
} |
| 806 |
out.push_str("><span>"); |
| 807 |
escape_into(field.label, out); |
| 808 |
out.push_str("</span></label>"); |
| 809 |
} |
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
FieldKind::Secret => { |
| 817 |
out.push_str("<input type=\"password\" class=\""); |
| 818 |
push_class(out, "field", opts); |
| 819 |
out.push('"'); |
| 820 |
push_control_attributes(out, field, filling, &id, field.name); |
| 821 |
placeholder(out); |
| 822 |
out.push('>'); |
| 823 |
} |
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
FieldKind::File => { |
| 829 |
out.push_str("<input type=\"file\" class=\""); |
| 830 |
push_class(out, "field", opts); |
| 831 |
out.push('"'); |
| 832 |
push_control_attributes(out, field, filling, &id, field.name); |
| 833 |
push_accept(out, field); |
| 834 |
if field.multiple { |
| 835 |
out.push_str(" multiple"); |
| 836 |
} |
| 837 |
out.push('>'); |
| 838 |
} |
| 839 |
kind => { |
| 840 |
let _ = write!(out, "<input type=\"{}\" class=\"", input_type(kind)); |
| 841 |
push_class(out, "field", opts); |
| 842 |
out.push('"'); |
| 843 |
push_control_attributes(out, field, filling, &id, field.name); |
| 844 |
placeholder(out); |
| 845 |
out.push_str(" value=\""); |
| 846 |
escape_into(filling.value.as_text(), out); |
| 847 |
out.push_str("\">"); |
| 848 |
} |
| 849 |
} |
| 850 |
} |
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
fn push_editor_open(out: &mut String, opts: &Emit) { |
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
out.push_str("<div data-format=\"markdown\"><div class=\""); |
| 893 |
push_class(out, "form-editor-modes", opts); |
| 894 |
out.push_str("\">"); |
| 895 |
push_mode(out, "write", "Write", true, opts); |
| 896 |
push_mode(out, "preview", "Preview", false, opts); |
| 897 |
out.push_str("</div>"); |
| 898 |
} |
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
fn push_mode(out: &mut String, mode: &str, label: &str, chosen: bool, opts: &Emit) { |
| 909 |
out.push_str("<button type=\"button\" class=\""); |
| 910 |
push_class(out, crate::option_class(Selector::Segmented), opts); |
| 911 |
if chosen { |
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
out.push_str(" chosen"); |
| 916 |
} |
| 917 |
let _ = write!( |
| 918 |
out, |
| 919 |
"\" data-editor-mode=\"{mode}\" aria-pressed=\"{chosen}\">{label}</button>" |
| 920 |
); |
| 921 |
} |
| 922 |
|
| 923 |
|
| 924 |
fn push_editor_close(out: &mut String, opts: &Emit) { |
| 925 |
out.push_str("<div class=\""); |
| 926 |
push_class(out, "form-editor-preview", opts); |
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
out.push_str("\" data-editor-preview></div></div>"); |
| 931 |
} |
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
pub(crate) fn editor_rules(opts: &Emit) -> String { |
| 946 |
let mut css = String::new(); |
| 947 |
let modes = class("form-editor-modes", opts); |
| 948 |
let preview = class("form-editor-preview", opts); |
| 949 |
let field = class("field", opts); |
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
let _ = writeln!( |
| 954 |
css, |
| 955 |
"[data-format=\"markdown\"] > .{modes} {{\n display: none;\n}}" |
| 956 |
); |
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
let _ = writeln!( |
| 962 |
css, |
| 963 |
"[data-format=\"markdown\"][data-ready] > .{modes} {{\n display: block;\n}}" |
| 964 |
); |
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
let _ = writeln!( |
| 970 |
css, |
| 971 |
"[data-format=\"markdown\"] > .{preview} {{\n display: none;\n}}" |
| 972 |
); |
| 973 |
let _ = writeln!( |
| 974 |
css, |
| 975 |
"[data-format=\"markdown\"][data-ready][data-mode=\"preview\"] > .{preview} \ |
| 976 |
{{\n display: block;\n}}" |
| 977 |
); |
| 978 |
|
| 979 |
|
| 980 |
let _ = writeln!( |
| 981 |
css, |
| 982 |
"[data-format=\"markdown\"][data-ready][data-mode=\"preview\"] > .{field} \ |
| 983 |
{{\n display: none;\n}}" |
| 984 |
); |
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
let _ = write!( |
| 991 |
css, |
| 992 |
"[data-format=\"markdown\"] > .{preview} {{\n{}}}\n", |
| 993 |
crate::depth_declarations(Depth::Well) |
| 994 |
); |
| 995 |
|
| 996 |
css |
| 997 |
} |
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
pub(crate) fn note_rules(opts: &Emit) -> String { |
| 1027 |
let note = class("form-note", opts); |
| 1028 |
let mut css = String::new(); |
| 1029 |
let _ = writeln!(css, ".{note} {{\n color: var(--content);\n}}"); |
| 1030 |
for tone in [Tone::Info, Tone::Success, Tone::Warning, Tone::Danger] { |
| 1031 |
let _ = writeln!( |
| 1032 |
css, |
| 1033 |
".{note}[data-tone=\"{0}\"] {{\n color: var(--{0});\n}}", |
| 1034 |
tone.token() |
| 1035 |
); |
| 1036 |
} |
| 1037 |
css |
| 1038 |
} |
| 1039 |
|
| 1040 |
pub(crate) fn unit_rules(opts: &Emit) -> String { |
| 1041 |
let unit = class("form-unit", opts); |
| 1042 |
let mut css = String::new(); |
| 1043 |
let _ = writeln!(css, ".{unit} {{\n color: var(--content-muted);\n}}"); |
| 1044 |
css |
| 1045 |
} |
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
pub(crate) fn suggestion_rules(opts: &Emit) -> String { |
| 1076 |
let list = class("form-suggestions", opts); |
| 1077 |
let entry = class("form-suggestion", opts); |
| 1078 |
let detail = class("form-suggestion-detail", opts); |
| 1079 |
let mut css = String::new(); |
| 1080 |
|
| 1081 |
let _ = writeln!(css, ".{list}:empty {{\n display: none;\n}}"); |
| 1082 |
|
| 1083 |
|
| 1084 |
css.push_str(&crate::depth_rule(&list, Depth::Overlay)); |
| 1085 |
|
| 1086 |
css.push_str(&crate::interactive_rules(&entry, Depth::Flat, opts)); |
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
let _ = writeln!( |
| 1098 |
css, |
| 1099 |
".{entry}[aria-selected=\"true\"] {{\n background: var(--hover-surface);\n}}" |
| 1100 |
); |
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
let _ = writeln!(css, ".{detail} {{\n color: var(--content-muted);\n}}"); |
| 1107 |
|
| 1108 |
css |
| 1109 |
} |
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
#[must_use] |
| 1139 |
pub fn field_html(field: &Field<'_>, filling: &Filling<'_>, opts: &Emit) -> String { |
| 1140 |
let mut html = String::new(); |
| 1141 |
field_html_into(field, filling, opts, &mut html); |
| 1142 |
html |
| 1143 |
} |
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
|
| 1149 |
|
| 1150 |
pub fn field_html_into(field: &Field<'_>, filling: &Filling<'_>, opts: &Emit, out: &mut String) { |
| 1151 |
let id = filling.id_for(field.name); |
| 1152 |
|
| 1153 |
if !field.kind.visible() { |
| 1154 |
|
| 1155 |
|
| 1156 |
out.push_str("<input type=\"hidden\" name=\""); |
| 1157 |
escape_into(field.name, out); |
| 1158 |
out.push_str("\" value=\""); |
| 1159 |
escape_into(filling.value.as_text(), out); |
| 1160 |
out.push_str("\">"); |
| 1161 |
return; |
| 1162 |
} |
| 1163 |
|
| 1164 |
out.push_str("<div class=\""); |
| 1165 |
push_class(out, "form-group", opts); |
| 1166 |
if field.invalid() { |
| 1167 |
out.push_str(" has-error"); |
| 1168 |
} |
| 1169 |
if field.extended { |
| 1170 |
|
| 1171 |
|
| 1172 |
out.push_str("\" data-extended=\"true"); |
| 1173 |
} |
| 1174 |
out.push_str("\">"); |
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
if !field.kind.labels_itself() { |
| 1180 |
out.push_str("<label class=\""); |
| 1181 |
push_class(out, "form-label", opts); |
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
if is_group_control(field.kind) { |
| 1186 |
let _ = write!(out, "\" id=\"{id}-label\">"); |
| 1187 |
} else { |
| 1188 |
let _ = write!(out, "\" for=\"{id}\">"); |
| 1189 |
} |
| 1190 |
escape_into(field.label, out); |
| 1191 |
out.push_str("</label>"); |
| 1192 |
} |
| 1193 |
|
| 1194 |
push_control(out, field, filling, opts); |
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
if let Some(unit) = unit_of(field) { |
| 1202 |
out.push_str("<span class=\""); |
| 1203 |
push_class(out, "form-unit", opts); |
| 1204 |
let _ = write!(out, "\" id=\"{id}-unit\">"); |
| 1205 |
escape_into(unit, out); |
| 1206 |
out.push_str("</span>"); |
| 1207 |
} |
| 1208 |
|
| 1209 |
if let Some(hint) = field.hint { |
| 1210 |
out.push_str("<div class=\""); |
| 1211 |
push_class(out, "form-hint", opts); |
| 1212 |
let _ = write!(out, "\" id=\"{id}-hint\">"); |
| 1213 |
escape_into(hint, out); |
| 1214 |
out.push_str("</div>"); |
| 1215 |
} |
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
if let Some((tone, note)) = field.note { |
| 1222 |
out.push_str("<div class=\""); |
| 1223 |
push_class(out, "form-note", opts); |
| 1224 |
let assertive = matches!(tone, Tone::Warning | Tone::Danger); |
| 1225 |
let _ = write!( |
| 1226 |
out, |
| 1227 |
"\" id=\"{id}-note\" role=\"{}\"", |
| 1228 |
if assertive { "alert" } else { "status" } |
| 1229 |
); |
| 1230 |
|
| 1231 |
|
| 1232 |
if tone != Tone::Neutral { |
| 1233 |
let _ = write!(out, " data-tone=\"{}\"", tone.token()); |
| 1234 |
} |
| 1235 |
out.push('>'); |
| 1236 |
escape_into(note, out); |
| 1237 |
out.push_str("</div>"); |
| 1238 |
} |
| 1239 |
if let Some(Markup(markup)) = filling.trailing { |
| 1240 |
out.push_str(markup); |
| 1241 |
} |
| 1242 |
if let Some(error) = field.error { |
| 1243 |
out.push_str("<div class=\""); |
| 1244 |
push_class(out, "form-error", opts); |
| 1245 |
let _ = write!(out, " visible\" id=\"{id}-error\" role=\"alert\">"); |
| 1246 |
escape_into(error, out); |
| 1247 |
out.push_str("</div>"); |
| 1248 |
} |
| 1249 |
|
| 1250 |
out.push_str("</div>"); |
| 1251 |
} |
| 1252 |
|
| 1253 |
#[cfg(test)] |
| 1254 |
mod tests { |
| 1255 |
use super::*; |
| 1256 |
use makeover_layout::{Accepted, Curve, Family}; |
| 1257 |
|
| 1258 |
fn field(kind: FieldKind) -> Field<'static> { |
| 1259 |
Field::new(kind, "title", "Title") |
| 1260 |
} |
| 1261 |
|
| 1262 |
#[test] |
| 1263 |
fn a_value_cannot_break_out_of_the_attribute_it_sits_in() { |
| 1264 |
|
| 1265 |
let filling = Filling::of(Value::Text("x\" onfocus=alert(1) autofocus=\"")); |
| 1266 |
let html = field_html(&field(FieldKind::Text), &filling, &Emit::default()); |
| 1267 |
|
| 1268 |
|
| 1269 |
|
| 1270 |
assert!(!html.contains("\" onfocus"), "{html}"); |
| 1271 |
assert!( |
| 1272 |
html.contains("value=\"x" onfocus=alert(1) autofocus="\""), |
| 1273 |
"{html}" |
| 1274 |
); |
| 1275 |
} |
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
#[test] |
| 1280 |
fn a_host_can_write_its_own_attributes_onto_the_control() { |
| 1281 |
let mut filling = Filling::of(Value::Text("ru")); |
| 1282 |
filling.control_attrs = Some(Markup( |
| 1283 |
r#"role="combobox" aria-expanded="false" aria-controls="title-suggestions""#, |
| 1284 |
)); |
| 1285 |
let html = field_html(&field(FieldKind::Text), &filling, &Emit::default()); |
| 1286 |
assert!(html.contains(r#"role="combobox""#), "{html}"); |
| 1287 |
assert!( |
| 1288 |
html.contains(r#"aria-controls="title-suggestions""#), |
| 1289 |
"{html}" |
| 1290 |
); |
| 1291 |
|
| 1292 |
|
| 1293 |
let id = html.find(r#"id="title""#).expect("id"); |
| 1294 |
let role = html.find(r#"role="combobox""#).expect("role"); |
| 1295 |
assert!(id < role, "{html}"); |
| 1296 |
} |
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
#[test] |
| 1301 |
fn a_radio_group_drops_control_attributes() { |
| 1302 |
let mut f = field(FieldKind::Radio); |
| 1303 |
let options = [Choice::new("a", "A")]; |
| 1304 |
f.options = &options; |
| 1305 |
let filling = Filling { |
| 1306 |
control_attrs: Some(Markup(r#"data-host="1""#)), |
| 1307 |
..Filling::default() |
| 1308 |
}; |
| 1309 |
let html = field_html(&f, &filling, &Emit::default()); |
| 1310 |
assert!(!html.contains("data-host"), "{html}"); |
| 1311 |
} |
| 1312 |
|
| 1313 |
#[test] |
| 1314 |
fn a_label_cannot_open_a_tag() { |
| 1315 |
let mut f = field(FieldKind::Text); |
| 1316 |
f.label = "<script>alert(1)</script>"; |
| 1317 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 1318 |
assert!(!html.contains("<script>"), "{html}"); |
| 1319 |
assert!(html.contains("<script>"), "{html}"); |
| 1320 |
} |
| 1321 |
|
| 1322 |
#[test] |
| 1323 |
fn every_escaped_sink_is_covered_by_the_one_escaper() { |
| 1324 |
assert_eq!(escape("&<>\"'"), "&<>"'"); |
| 1325 |
|
| 1326 |
|
| 1327 |
assert!(escape("\"").contains(""")); |
| 1328 |
} |
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
|
| 1333 |
|
| 1334 |
#[test] |
| 1335 |
fn the_streaming_escaper_appends_what_the_returning_one_returns() { |
| 1336 |
for text in [ |
| 1337 |
"", |
| 1338 |
"plain", |
| 1339 |
"&<>\"'", |
| 1340 |
"&&&", |
| 1341 |
"a & b", |
| 1342 |
"trailing&", |
| 1343 |
"&leading", |
| 1344 |
"é世 & <b>naïve</b> \u{1f600}", |
| 1345 |
] { |
| 1346 |
let mut out = String::from("kept: "); |
| 1347 |
escape_into(text, &mut out); |
| 1348 |
assert_eq!(out, format!("kept: {}", escape(text)), "{text:?}"); |
| 1349 |
} |
| 1350 |
} |
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
#[test] |
| 1355 |
fn a_streamed_field_is_the_field_the_other_form_returns() { |
| 1356 |
let kinds = [ |
| 1357 |
FieldKind::Text, |
| 1358 |
FieldKind::Secret, |
| 1359 |
FieldKind::Number, |
| 1360 |
FieldKind::Checkbox, |
| 1361 |
FieldKind::Radio, |
| 1362 |
FieldKind::Select, |
| 1363 |
FieldKind::Textarea, |
| 1364 |
FieldKind::File, |
| 1365 |
FieldKind::Hidden, |
| 1366 |
]; |
| 1367 |
let choices = [Choice::plain("one"), Choice::plain("two")]; |
| 1368 |
let opts = Emit { |
| 1369 |
class_prefix: "mk-", |
| 1370 |
..Emit::default() |
| 1371 |
}; |
| 1372 |
for kind in kinds { |
| 1373 |
let described = Field { |
| 1374 |
hint: Some("a hint"), |
| 1375 |
error: Some("wrong <here>"), |
| 1376 |
placeholder: Some("x\" y"), |
| 1377 |
options: &choices, |
| 1378 |
required: true, |
| 1379 |
max_length: Some(40), |
| 1380 |
min: Some("1"), |
| 1381 |
max: Some("9"), |
| 1382 |
extended: true, |
| 1383 |
..Field::new(kind, "the & name", "The <label>") |
| 1384 |
}; |
| 1385 |
let filling = Filling { |
| 1386 |
value: Value::Text("one"), |
| 1387 |
trailing: Some(Markup("<i>t</i>")), |
| 1388 |
control_attrs: Some(Markup(r#"data-host="1""#)), |
| 1389 |
id_prefix: Some("modal"), |
| 1390 |
}; |
| 1391 |
let mut streamed = String::new(); |
| 1392 |
field_html_into(&described, &filling, &opts, &mut streamed); |
| 1393 |
assert_eq!( |
| 1394 |
streamed, |
| 1395 |
field_html(&described, &filling, &opts), |
| 1396 |
"{kind:?}" |
| 1397 |
); |
| 1398 |
|
| 1399 |
|
| 1400 |
let plain = Field::new(kind, "name", "Label"); |
| 1401 |
let mut streamed = String::new(); |
| 1402 |
field_html_into(&plain, &Filling::default(), &opts, &mut streamed); |
| 1403 |
assert_eq!( |
| 1404 |
streamed, |
| 1405 |
field_html(&plain, &Filling::default(), &opts), |
| 1406 |
"{kind:?}" |
| 1407 |
); |
| 1408 |
} |
| 1409 |
} |
| 1410 |
|
| 1411 |
#[test] |
| 1412 |
fn markup_is_the_only_way_past_the_escaping() { |
| 1413 |
let filling = Filling { |
| 1414 |
trailing: Some(Markup("<div class=\"recurrence-config\"></div>")), |
| 1415 |
..Filling::default() |
| 1416 |
}; |
| 1417 |
let html = field_html(&field(FieldKind::Text), &filling, &Emit::default()); |
| 1418 |
assert!( |
| 1419 |
html.contains("<div class=\"recurrence-config\"></div>"), |
| 1420 |
"{html}" |
| 1421 |
); |
| 1422 |
} |
| 1423 |
|
| 1424 |
#[test] |
| 1425 |
fn an_invalid_field_carries_the_attribute_its_own_stylesheet_keys_on() { |
| 1426 |
let mut f = field(FieldKind::Text); |
| 1427 |
f.error = Some("Required"); |
| 1428 |
let opts = Emit::default(); |
| 1429 |
let html = field_html(&f, &Filling::default(), &opts); |
| 1430 |
assert!(html.contains("aria-invalid=\"true\""), "{html}"); |
| 1431 |
|
| 1432 |
assert!(crate::stylesheet(&opts).contains("[aria-invalid=\"true\"]")); |
| 1433 |
|
| 1434 |
|
| 1435 |
assert!(html.contains("has-error"), "{html}"); |
| 1436 |
} |
| 1437 |
|
| 1438 |
#[test] |
| 1439 |
fn a_valid_field_claims_nothing_about_being_invalid() { |
| 1440 |
let html = field_html( |
| 1441 |
&field(FieldKind::Text), |
| 1442 |
&Filling::default(), |
| 1443 |
&Emit::default(), |
| 1444 |
); |
| 1445 |
assert!(!html.contains("aria-invalid"), "{html}"); |
| 1446 |
assert!(!html.contains("has-error"), "{html}"); |
| 1447 |
} |
| 1448 |
|
| 1449 |
#[test] |
| 1450 |
fn a_note_sits_between_the_hint_and_the_error_and_carries_its_tone() { |
| 1451 |
let mut f = field(FieldKind::Text); |
| 1452 |
f.hint = Some("Keep it short"); |
| 1453 |
f.note = Some((Tone::Warning, "Re-encoding drops embedded BWF")); |
| 1454 |
f.error = Some("Required"); |
| 1455 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 1456 |
|
| 1457 |
|
| 1458 |
assert!( |
| 1459 |
html.contains(r#"aria-describedby="title-hint title-note title-error""#), |
| 1460 |
"{html}" |
| 1461 |
); |
| 1462 |
assert!( |
| 1463 |
html.contains(r#"id="title-note" role="alert" data-tone="warning""#), |
| 1464 |
"{html}" |
| 1465 |
); |
| 1466 |
|
| 1467 |
let hint = html.find("title-hint").unwrap(); |
| 1468 |
let note = html.rfind("title-note").unwrap(); |
| 1469 |
let err = html.rfind("title-error").unwrap(); |
| 1470 |
assert!(hint < note && note < err, "{html}"); |
| 1471 |
} |
| 1472 |
|
| 1473 |
#[test] |
| 1474 |
fn a_quiet_note_is_polite_and_wears_no_tone_attribute() { |
| 1475 |
|
| 1476 |
|
| 1477 |
let mut f = field(FieldKind::Text); |
| 1478 |
f.note = Some((Tone::Info, "This is what that setting implies")); |
| 1479 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 1480 |
assert!(html.contains(r#"role="status" data-tone="info""#), "{html}"); |
| 1481 |
|
| 1482 |
f.note = Some((Tone::Neutral, "An ordinary fact")); |
| 1483 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 1484 |
assert!(html.contains(r#"id="title-note" role="status">"#), "{html}"); |
| 1485 |
assert!(!html.contains("data-tone"), "{html}"); |
| 1486 |
} |
| 1487 |
|
| 1488 |
#[test] |
| 1489 |
fn a_note_does_not_mark_the_group_invalid() { |
| 1490 |
|
| 1491 |
|
| 1492 |
let mut f = field(FieldKind::Text); |
| 1493 |
f.note = Some((Tone::Danger, "This cannot be undone")); |
| 1494 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 1495 |
assert!(!html.contains("has-error"), "{html}"); |
| 1496 |
assert!(!html.contains(r#"aria-invalid="true""#), "{html}"); |
| 1497 |
} |
| 1498 |
|
| 1499 |
#[test] |
| 1500 |
fn the_hint_survives_an_error_arriving() { |
| 1501 |
let mut f = field(FieldKind::Text); |
| 1502 |
f.hint = Some("Keep it short"); |
| 1503 |
f.error = Some("Required"); |
| 1504 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 1505 |
assert!( |
| 1506 |
html.contains("aria-describedby=\"title-hint title-error\""), |
| 1507 |
"{html}" |
| 1508 |
); |
| 1509 |
} |
| 1510 |
|
| 1511 |
#[test] |
| 1512 |
fn a_secret_never_carries_its_value_into_the_markup() { |
| 1513 |
let filling = Filling::of(Value::Text("hunter2")); |
| 1514 |
let html = field_html(&field(FieldKind::Secret), &filling, &Emit::default()); |
| 1515 |
assert!(!html.contains("hunter2"), "{html}"); |
| 1516 |
assert!(html.contains("type=\"password\""), "{html}"); |
| 1517 |
} |
| 1518 |
|
| 1519 |
#[test] |
| 1520 |
fn a_hidden_field_is_the_input_and_nothing_else() { |
| 1521 |
let filling = Filling::of(Value::Text("42")); |
| 1522 |
let html = field_html(&field(FieldKind::Hidden), &filling, &Emit::default()); |
| 1523 |
assert_eq!(html, "<input type=\"hidden\" name=\"title\" value=\"42\">"); |
| 1524 |
} |
| 1525 |
|
| 1526 |
#[test] |
| 1527 |
fn a_checkbox_labels_itself_and_takes_no_separate_label() { |
| 1528 |
let html = field_html( |
| 1529 |
&field(FieldKind::Checkbox), |
| 1530 |
&Filling::of(Value::On(true)), |
| 1531 |
&Emit::default(), |
| 1532 |
); |
| 1533 |
assert!(!html.contains("form-label"), "{html}"); |
| 1534 |
assert!(html.contains("checked"), "{html}"); |
| 1535 |
assert!(html.contains("<span>Title</span>"), "{html}"); |
| 1536 |
} |
| 1537 |
|
| 1538 |
#[test] |
| 1539 |
fn a_select_keeps_a_value_no_option_carries() { |
| 1540 |
let options = [Choice::plain("1"), Choice::plain("3"), Choice::plain("7")]; |
| 1541 |
let f = Field::select("title", "Title", &options); |
| 1542 |
let html = field_html(&f, &Filling::of(Value::Text("10")), &Emit::default()); |
| 1543 |
assert!(html.contains("data-unmatched=\"true\""), "{html}"); |
| 1544 |
|
| 1545 |
|
| 1546 |
assert!(html.contains("<option value=\"10\" selected"), "{html}"); |
| 1547 |
} |
| 1548 |
|
| 1549 |
#[test] |
| 1550 |
fn a_select_with_no_options_emits_an_empty_select() { |
| 1551 |
|
| 1552 |
|
| 1553 |
|
| 1554 |
let f = Field::select("title", "Title", &[]); |
| 1555 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 1556 |
assert!(html.contains("<select"), "{html}"); |
| 1557 |
assert!(!html.contains("<option"), "{html}"); |
| 1558 |
} |
| 1559 |
|
| 1560 |
#[test] |
| 1561 |
fn an_unanswered_select_shows_its_ghost_text_and_cannot_be_chosen_back() { |
| 1562 |
let options = [Choice::new("sp404", "SP-404")]; |
| 1563 |
let f = Field { |
| 1564 |
placeholder: Some("Select device..."), |
| 1565 |
..Field::select("device", "Conform for device", &options) |
| 1566 |
}; |
| 1567 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 1568 |
|
| 1569 |
assert!( |
| 1570 |
html.contains("<option value=\"\" disabled selected>Select device...</option>"), |
| 1571 |
"{html}" |
| 1572 |
); |
| 1573 |
|
| 1574 |
|
| 1575 |
assert!( |
| 1576 |
html.find("Select device...") < html.find("SP-404"), |
| 1577 |
"{html}" |
| 1578 |
); |
| 1579 |
} |
| 1580 |
|
| 1581 |
#[test] |
| 1582 |
fn an_answered_select_drops_the_ghost_text() { |
| 1583 |
|
| 1584 |
|
| 1585 |
|
| 1586 |
let options = [Choice::new("sp404", "SP-404")]; |
| 1587 |
let f = Field { |
| 1588 |
placeholder: Some("Select device..."), |
| 1589 |
..Field::select("device", "Conform for device", &options) |
| 1590 |
}; |
| 1591 |
let html = field_html(&f, &Filling::of(Value::Text("sp404")), &Emit::default()); |
| 1592 |
assert!(!html.contains("Select device..."), "{html}"); |
| 1593 |
} |
| 1594 |
|
| 1595 |
#[test] |
| 1596 |
fn a_wrong_answer_is_kept_and_is_not_the_ghost_text() { |
| 1597 |
|
| 1598 |
|
| 1599 |
|
| 1600 |
let options = [Choice::plain("1"), Choice::plain("7")]; |
| 1601 |
let f = Field { |
| 1602 |
placeholder: Some("Pick one"), |
| 1603 |
..Field::select("retention", "Keep backups for", &options) |
| 1604 |
}; |
| 1605 |
let html = field_html(&f, &Filling::of(Value::Text("10")), &Emit::default()); |
| 1606 |
assert!(html.contains("data-unmatched=\"true\""), "{html}"); |
| 1607 |
assert!(!html.contains("Pick one"), "{html}"); |
| 1608 |
} |
| 1609 |
|
| 1610 |
#[test] |
| 1611 |
fn a_range_is_a_range_input_and_carries_its_extent() { |
| 1612 |
let f = Field { |
| 1613 |
curve: Curve::Linear { step: Some("0.01") }, |
| 1614 |
..Field::range("review", "Review above", "0", "1") |
| 1615 |
}; |
| 1616 |
let html = field_html(&f, &Filling::of(Value::Text("0.72")), &Emit::default()); |
| 1617 |
assert!(html.contains("type=\"range\""), "{html}"); |
| 1618 |
assert!(html.contains("min=\"0\""), "{html}"); |
| 1619 |
assert!(html.contains("max=\"1\""), "{html}"); |
| 1620 |
|
| 1621 |
|
| 1622 |
assert!(html.contains("step=\"0.01\""), "{html}"); |
| 1623 |
} |
| 1624 |
|
| 1625 |
#[test] |
| 1626 |
fn a_range_reads_its_granularity_off_the_curve_and_not_off_field_step() { |
| 1627 |
|
| 1628 |
|
| 1629 |
|
| 1630 |
let f = Field { |
| 1631 |
step: Some("99"), |
| 1632 |
..Field::range("review", "Review above", "0", "1") |
| 1633 |
}; |
| 1634 |
let html = field_html(&f, &Filling::of(Value::Text("0.5")), &Emit::default()); |
| 1635 |
assert!(!html.contains("step="), "{html}"); |
| 1636 |
} |
| 1637 |
|
| 1638 |
#[test] |
| 1639 |
fn a_unit_is_adjacent_text_and_the_control_points_at_it() { |
| 1640 |
|
| 1641 |
|
| 1642 |
let f = Field { |
| 1643 |
unit: Some("dBFS"), |
| 1644 |
..Field::range("threshold", "Threshold", "-96", "-20") |
| 1645 |
}; |
| 1646 |
let html = field_html(&f, &Filling::of(Value::Text("-40")), &Emit::default()); |
| 1647 |
assert!(html.contains(r#"id="threshold-unit""#), "{html}"); |
| 1648 |
assert!(html.contains(">dBFS</span>"), "{html}"); |
| 1649 |
assert!( |
| 1650 |
html.contains(r#"aria-describedby="threshold-unit""#), |
| 1651 |
"{html}" |
| 1652 |
); |
| 1653 |
|
| 1654 |
assert!(html.contains(">Threshold</label>"), "{html}"); |
| 1655 |
} |
| 1656 |
|
| 1657 |
#[test] |
| 1658 |
fn a_unit_takes_its_place_between_the_hint_and_the_error() { |
| 1659 |
let f = Field { |
| 1660 |
unit: Some("ms"), |
| 1661 |
hint: Some("How long the fade runs."), |
| 1662 |
error: Some("Too long."), |
| 1663 |
..Field::new(FieldKind::Number, "fade", "Fade") |
| 1664 |
}; |
| 1665 |
let html = field_html(&f, &Filling::of(Value::Text("50")), &Emit::default()); |
| 1666 |
assert!( |
| 1667 |
html.contains(r#"aria-describedby="fade-hint fade-unit fade-error""#), |
| 1668 |
"{html}" |
| 1669 |
); |
| 1670 |
} |
| 1671 |
|
| 1672 |
#[test] |
| 1673 |
fn a_unit_on_a_kind_that_is_not_a_quantity_is_ignored() { |
| 1674 |
|
| 1675 |
|
| 1676 |
|
| 1677 |
let f = Field { |
| 1678 |
unit: Some("s"), |
| 1679 |
..Field::new(FieldKind::Text, "name", "Name") |
| 1680 |
}; |
| 1681 |
let html = field_html(&f, &Filling::of(Value::Text("kick")), &Emit::default()); |
| 1682 |
assert!(!html.contains("name-unit"), "{html}"); |
| 1683 |
assert!(!html.contains("aria-describedby"), "{html}"); |
| 1684 |
} |
| 1685 |
|
| 1686 |
#[test] |
| 1687 |
fn a_unit_cannot_break_out_of_the_span_it_sits_in() { |
| 1688 |
let f = Field { |
| 1689 |
unit: Some("</span><script>"), |
| 1690 |
..Field::new(FieldKind::Number, "n", "N") |
| 1691 |
}; |
| 1692 |
let html = field_html(&f, &Filling::of(Value::Text("1")), &Emit::default()); |
| 1693 |
assert!(!html.contains("<script>"), "{html}"); |
| 1694 |
assert!(html.contains("<script>"), "{html}"); |
| 1695 |
} |
| 1696 |
|
| 1697 |
#[test] |
| 1698 |
fn a_constant_ratio_curve_still_emits_a_linear_track() { |
| 1699 |
|
| 1700 |
|
| 1701 |
|
| 1702 |
|
| 1703 |
let f = Field { |
| 1704 |
curve: Curve::Logarithmic { |
| 1705 |
step: Some("0.001"), |
| 1706 |
}, |
| 1707 |
..Field::range("attack", "Attack", "0.001", "5") |
| 1708 |
}; |
| 1709 |
let html = field_html(&f, &Filling::of(Value::Text("0.005")), &Emit::default()); |
| 1710 |
assert!(html.contains("type=\"range\""), "{html}"); |
| 1711 |
assert!(html.contains("min=\"0.001\""), "{html}"); |
| 1712 |
assert!(html.contains("max=\"5\""), "{html}"); |
| 1713 |
assert!(html.contains("step=\"0.001\""), "{html}"); |
| 1714 |
} |
| 1715 |
|
| 1716 |
#[test] |
| 1717 |
fn a_number_with_bounds_is_still_typed_into() { |
| 1718 |
|
| 1719 |
|
| 1720 |
|
| 1721 |
let f = Field { |
| 1722 |
min: Some("1"), |
| 1723 |
..Field::new(FieldKind::Number, "minutes", "Minutes") |
| 1724 |
}; |
| 1725 |
let html = field_html(&f, &Filling::of(Value::Text("30")), &Emit::default()); |
| 1726 |
assert!(html.contains("type=\"number\""), "{html}"); |
| 1727 |
assert!(!html.contains("type=\"range\""), "{html}"); |
| 1728 |
|
| 1729 |
assert!(!html.contains("step="), "{html}"); |
| 1730 |
} |
| 1731 |
|
| 1732 |
#[test] |
| 1733 |
fn an_unavailable_option_is_disabled_and_says_why() { |
| 1734 |
let options = [ |
| 1735 |
Choice::new("chromatic", "Chromatic"), |
| 1736 |
Choice::new("multi", "Multi-sample").unless("Drop a second sample."), |
| 1737 |
]; |
| 1738 |
let f = Field::radio("mode", "Mode", &options); |
| 1739 |
let html = field_html(&f, &Filling::of(Value::Text("chromatic")), &Emit::default()); |
| 1740 |
|
| 1741 |
assert!(html.contains(" disabled"), "{html}"); |
| 1742 |
assert!(html.contains("Drop a second sample."), "{html}"); |
| 1743 |
|
| 1744 |
|
| 1745 |
assert!(html.contains("value=\"multi\""), "{html}"); |
| 1746 |
|
| 1747 |
assert!(html.contains("form-option-reason"), "{html}"); |
| 1748 |
} |
| 1749 |
|
| 1750 |
#[test] |
| 1751 |
fn an_unavailable_select_option_carries_its_reason_in_its_text() { |
| 1752 |
|
| 1753 |
|
| 1754 |
let options = [Choice::new("multi", "Multi-sample").unless("Drop a second sample.")]; |
| 1755 |
let f = Field::select("mode", "Mode", &options); |
| 1756 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 1757 |
assert!( |
| 1758 |
html.contains(">Multi-sample: Drop a second sample.</option>"), |
| 1759 |
"{html}" |
| 1760 |
); |
| 1761 |
assert!(html.contains("disabled"), "{html}"); |
| 1762 |
} |
| 1763 |
|
| 1764 |
#[test] |
| 1765 |
fn a_radio_group_is_named_by_its_label_instead_of_pointing_at_it() { |
| 1766 |
|
| 1767 |
|
| 1768 |
|
| 1769 |
let options = [Choice::plain("copy"), Choice::plain("reference")]; |
| 1770 |
let f = Field::radio("storage", "Storage style", &options); |
| 1771 |
let html = field_html(&f, &Filling::of(Value::Text("copy")), &Emit::default()); |
| 1772 |
|
| 1773 |
assert!(html.contains("id=\"storage-label\""), "{html}"); |
| 1774 |
assert!(!html.contains("for=\"storage\""), "{html}"); |
| 1775 |
assert!(html.contains("role=\"radiogroup\""), "{html}"); |
| 1776 |
assert!(html.contains("aria-labelledby=\"storage-label\""), "{html}"); |
| 1777 |
} |
| 1778 |
|
| 1779 |
#[test] |
| 1780 |
fn an_interval_is_one_labelled_group_holding_both_ends() { |
| 1781 |
|
| 1782 |
|
| 1783 |
|
| 1784 |
let f = Field::interval("min_price", "max_price", "Price"); |
| 1785 |
let html = field_html( |
| 1786 |
&f, |
| 1787 |
&Filling::of(Value::Between { |
| 1788 |
lower: "5", |
| 1789 |
upper: "40", |
| 1790 |
}), |
| 1791 |
&Emit::default(), |
| 1792 |
); |
| 1793 |
|
| 1794 |
assert!(html.contains("role=\"group\""), "{html}"); |
| 1795 |
assert!( |
| 1796 |
html.contains("aria-labelledby=\"min_price-label\""), |
| 1797 |
"{html}" |
| 1798 |
); |
| 1799 |
assert!(html.contains("id=\"min_price-label\""), "{html}"); |
| 1800 |
assert!(!html.contains("for=\"min_price\""), "{html}"); |
| 1801 |
assert!(html.contains("name=\"min_price\""), "{html}"); |
| 1802 |
assert!(html.contains("name=\"max_price\""), "{html}"); |
| 1803 |
assert!(html.contains("value=\"5\""), "{html}"); |
| 1804 |
assert!(html.contains("value=\"40\""), "{html}"); |
| 1805 |
assert_eq!(html.matches("type=\"number\"").count(), 2, "{html}"); |
| 1806 |
} |
| 1807 |
|
| 1808 |
#[test] |
| 1809 |
fn both_ends_of_an_interval_take_the_whole_extent() { |
| 1810 |
|
| 1811 |
|
| 1812 |
let f = Field { |
| 1813 |
min: Some("0"), |
| 1814 |
max: Some("300"), |
| 1815 |
step: Some("1"), |
| 1816 |
..Field::interval("bpm_min", "bpm_max", "BPM") |
| 1817 |
}; |
| 1818 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 1819 |
|
| 1820 |
assert_eq!(html.matches("min=\"0\"").count(), 2, "{html}"); |
| 1821 |
assert_eq!(html.matches("max=\"300\"").count(), 2, "{html}"); |
| 1822 |
assert_eq!(html.matches("step=\"1\"").count(), 2, "{html}"); |
| 1823 |
|
| 1824 |
|
| 1825 |
assert_eq!(html.matches("value=\"\"").count(), 2, "{html}"); |
| 1826 |
} |
| 1827 |
|
| 1828 |
#[test] |
| 1829 |
fn an_interval_carries_the_fault_on_the_group_and_not_on_one_end() { |
| 1830 |
|
| 1831 |
|
| 1832 |
let f = Field { |
| 1833 |
error: Some("The high end is below the low one."), |
| 1834 |
hint: Some("Leave an end empty for no bound."), |
| 1835 |
..Field::interval("bpm_min", "bpm_max", "BPM") |
| 1836 |
}; |
| 1837 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 1838 |
|
| 1839 |
assert_eq!(html.matches("aria-invalid=\"true\"").count(), 1, "{html}"); |
| 1840 |
let group = html.find("role=\"group\"").expect("group"); |
| 1841 |
let invalid = html.find("aria-invalid").expect("invalid"); |
| 1842 |
let first_input = html.find("<input").expect("input"); |
| 1843 |
assert!(invalid > group && invalid < first_input, "{html}"); |
| 1844 |
assert!( |
| 1845 |
html.contains("aria-describedby=\"bpm_min-hint bpm_min-error\""), |
| 1846 |
"{html}" |
| 1847 |
); |
| 1848 |
} |
| 1849 |
|
| 1850 |
#[test] |
| 1851 |
fn an_interval_with_one_end_named_draws_one_box() { |
| 1852 |
|
| 1853 |
|
| 1854 |
|
| 1855 |
let f = Field::new(FieldKind::Interval, "bpm_min", "BPM"); |
| 1856 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 1857 |
|
| 1858 |
assert_eq!(html.matches("<input").count(), 1, "{html}"); |
| 1859 |
assert!(html.contains("name=\"bpm_min\""), "{html}"); |
| 1860 |
} |
| 1861 |
|
| 1862 |
#[test] |
| 1863 |
fn every_option_shares_the_name_and_only_the_current_one_is_checked() { |
| 1864 |
|
| 1865 |
|
| 1866 |
let options = [ |
| 1867 |
Choice::plain("copy"), |
| 1868 |
Choice::plain("reference"), |
| 1869 |
Choice::plain("link"), |
| 1870 |
]; |
| 1871 |
let f = Field::radio("storage", "Storage style", &options); |
| 1872 |
let html = field_html(&f, &Filling::of(Value::Text("reference")), &Emit::default()); |
| 1873 |
|
| 1874 |
assert_eq!(html.matches("name=\"storage\"").count(), 3, "{html}"); |
| 1875 |
assert_eq!(html.matches(" checked").count(), 1, "{html}"); |
| 1876 |
assert!( |
| 1877 |
html.contains("value=\"reference\" checked"), |
| 1878 |
"the checked one is the one held: {html}" |
| 1879 |
); |
| 1880 |
for index in 0..3 { |
| 1881 |
assert!(html.contains(&format!("id=\"storage-{index}\"")), "{html}"); |
| 1882 |
} |
| 1883 |
} |
| 1884 |
|
| 1885 |
#[test] |
| 1886 |
fn a_radio_group_carries_the_error_rather_than_any_one_option() { |
| 1887 |
|
| 1888 |
|
| 1889 |
|
| 1890 |
let options = [Choice::plain("copy"), Choice::plain("reference")]; |
| 1891 |
let f = Field { |
| 1892 |
error: Some("Pick one."), |
| 1893 |
hint: Some("Cannot be changed later."), |
| 1894 |
..Field::radio("storage", "Storage style", &options) |
| 1895 |
}; |
| 1896 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 1897 |
|
| 1898 |
assert_eq!(html.matches("aria-invalid=\"true\"").count(), 1, "{html}"); |
| 1899 |
assert!( |
| 1900 |
html.contains("aria-describedby=\"storage-hint storage-error\""), |
| 1901 |
"{html}" |
| 1902 |
); |
| 1903 |
|
| 1904 |
|
| 1905 |
let group = html.find("role=\"radiogroup\"").expect("group"); |
| 1906 |
let first = html.find("type=\"radio\"").expect("an option"); |
| 1907 |
assert!(group < first, "{html}"); |
| 1908 |
} |
| 1909 |
|
| 1910 |
#[test] |
| 1911 |
fn a_compulsory_radio_group_marks_every_option() { |
| 1912 |
|
| 1913 |
|
| 1914 |
let options = [Choice::plain("copy"), Choice::plain("reference")]; |
| 1915 |
let f = Field { |
| 1916 |
required: true, |
| 1917 |
..Field::radio("storage", "Storage style", &options) |
| 1918 |
}; |
| 1919 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 1920 |
assert_eq!(html.matches(" required").count(), 2, "{html}"); |
| 1921 |
} |
| 1922 |
|
| 1923 |
#[test] |
| 1924 |
fn a_radio_option_cannot_break_out_of_its_attribute() { |
| 1925 |
|
| 1926 |
|
| 1927 |
let hostile = [Choice::new( |
| 1928 |
"x\" onclick=alert(1) data-x=\"", |
| 1929 |
"<script>alert(1)</script>", |
| 1930 |
)]; |
| 1931 |
let f = Field::radio("storage", "Storage style", &hostile); |
| 1932 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 1933 |
|
| 1934 |
|
| 1935 |
|
| 1936 |
assert!(html.contains("value=\"x" onclick=alert(1)"), "{html}"); |
| 1937 |
assert!(!html.contains("<script>"), "{html}"); |
| 1938 |
assert!(html.contains("id=\"storage-0\""), "{html}"); |
| 1939 |
} |
| 1940 |
|
| 1941 |
#[test] |
| 1942 |
fn a_radio_group_with_no_options_emits_an_empty_group() { |
| 1943 |
|
| 1944 |
let f = Field::radio("storage", "Storage style", &[]); |
| 1945 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 1946 |
assert!(html.contains("role=\"radiogroup\""), "{html}"); |
| 1947 |
assert!(!html.contains("type=\"radio\""), "{html}"); |
| 1948 |
} |
| 1949 |
|
| 1950 |
#[test] |
| 1951 |
fn a_placeholder_comes_off_the_description_and_is_escaped() { |
| 1952 |
|
| 1953 |
|
| 1954 |
let f = Field { |
| 1955 |
placeholder: Some("x\" onfocus=alert(1) autofocus=\""), |
| 1956 |
..field(FieldKind::Text) |
| 1957 |
}; |
| 1958 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 1959 |
assert!(html.contains("placeholder=\""), "{html}"); |
| 1960 |
assert!(!html.contains("\" onfocus"), "{html}"); |
| 1961 |
} |
| 1962 |
|
| 1963 |
#[test] |
| 1964 |
fn a_select_marks_the_option_that_matches() { |
| 1965 |
let options = [Choice::plain("1"), Choice::plain("3")]; |
| 1966 |
let f = Field::select("title", "Title", &options); |
| 1967 |
let html = field_html(&f, &Filling::of(Value::Text("3")), &Emit::default()); |
| 1968 |
assert!( |
| 1969 |
html.contains("<option value=\"3\" selected>3</option>"), |
| 1970 |
"{html}" |
| 1971 |
); |
| 1972 |
assert!(html.contains("<option value=\"1\">1</option>"), "{html}"); |
| 1973 |
assert!(!html.contains("data-unmatched"), "{html}"); |
| 1974 |
} |
| 1975 |
|
| 1976 |
#[test] |
| 1977 |
fn a_textarea_carries_its_value_as_text_and_not_as_an_attribute() { |
| 1978 |
let filling = Filling::of(Value::Text("two\nlines")); |
| 1979 |
let html = field_html(&field(FieldKind::Textarea), &filling, &Emit::default()); |
| 1980 |
assert!(html.contains(">two\nlines</textarea>"), "{html}"); |
| 1981 |
} |
| 1982 |
|
| 1983 |
#[test] |
| 1984 |
fn a_markdown_field_is_a_textarea_that_says_what_its_value_is() { |
| 1985 |
|
| 1986 |
|
| 1987 |
|
| 1988 |
|
| 1989 |
let filling = Filling::of(Value::Text("# Heading")); |
| 1990 |
let html = field_html(&field(FieldKind::Rich), &filling, &Emit::default()); |
| 1991 |
assert!(html.contains("<textarea"), "{html}"); |
| 1992 |
assert!(html.contains(r#"data-format="markdown""#), "{html}"); |
| 1993 |
assert!(html.contains("># Heading</textarea>"), "{html}"); |
| 1994 |
|
| 1995 |
|
| 1996 |
|
| 1997 |
let plain = field_html(&field(FieldKind::Textarea), &filling, &Emit::default()); |
| 1998 |
assert!(!plain.contains("data-format"), "{plain}"); |
| 1999 |
|
| 2000 |
|
| 2001 |
|
| 2002 |
|
| 2003 |
assert!(!html.contains("<input"), "{html}"); |
| 2004 |
} |
| 2005 |
|
| 2006 |
#[test] |
| 2007 |
fn a_markdown_field_gets_the_preview_the_member_permits() { |
| 2008 |
|
| 2009 |
|
| 2010 |
|
| 2011 |
let filling = Filling::of(Value::Text("# Heading")); |
| 2012 |
let html = field_html(&field(FieldKind::Rich), &filling, &Emit::default()); |
| 2013 |
assert!(html.contains("data-editor-mode=\"write\""), "{html}"); |
| 2014 |
assert!(html.contains("data-editor-mode=\"preview\""), "{html}"); |
| 2015 |
assert!(html.contains("data-editor-preview"), "{html}"); |
| 2016 |
|
| 2017 |
|
| 2018 |
assert!( |
| 2019 |
html.contains( |
| 2020 |
"class=\"segment chosen\" data-editor-mode=\"write\" aria-pressed=\"true\"" |
| 2021 |
), |
| 2022 |
"{html}" |
| 2023 |
); |
| 2024 |
assert!( |
| 2025 |
html.contains("data-editor-mode=\"preview\" aria-pressed=\"false\""), |
| 2026 |
"{html}" |
| 2027 |
); |
| 2028 |
|
| 2029 |
|
| 2030 |
assert!(html.contains("># Heading</textarea>"), "{html}"); |
| 2031 |
} |
| 2032 |
|
| 2033 |
#[test] |
| 2034 |
fn a_plain_textarea_gets_no_editor_chrome() { |
| 2035 |
let filling = Filling::of(Value::Text("plain")); |
| 2036 |
let html = field_html(&field(FieldKind::Textarea), &filling, &Emit::default()); |
| 2037 |
assert!(!html.contains("data-editor-mode"), "{html}"); |
| 2038 |
assert!(!html.contains("data-editor-preview"), "{html}"); |
| 2039 |
assert!(!html.contains("segment"), "{html}"); |
| 2040 |
} |
| 2041 |
|
| 2042 |
#[test] |
| 2043 |
fn nothing_the_editor_emits_renders_the_value_as_markup() { |
| 2044 |
|
| 2045 |
|
| 2046 |
|
| 2047 |
let filling = Filling::of(Value::Text("<img src=x onerror=alert(1)>")); |
| 2048 |
let html = field_html(&field(FieldKind::Rich), &filling, &Emit::default()); |
| 2049 |
assert!(html.contains("data-editor-preview></div>"), "{html}"); |
| 2050 |
assert!(!html.contains("<img"), "{html}"); |
| 2051 |
assert!( |
| 2052 |
html.contains("<img src=x onerror=alert(1)>"), |
| 2053 |
"{html}" |
| 2054 |
); |
| 2055 |
} |
| 2056 |
|
| 2057 |
#[test] |
| 2058 |
fn the_editor_rules_gate_on_the_attribute_and_on_a_binding() { |
| 2059 |
let css = editor_rules(&Emit::default()); |
| 2060 |
|
| 2061 |
|
| 2062 |
|
| 2063 |
for line in css.lines().filter(|line| line.contains('{')) { |
| 2064 |
assert!(line.contains("[data-format=\"markdown\"]"), "{line}"); |
| 2065 |
} |
| 2066 |
|
| 2067 |
|
| 2068 |
assert!( |
| 2069 |
css.contains( |
| 2070 |
"[data-format=\"markdown\"] > .form-editor-modes {\n display: none;\n}" |
| 2071 |
) |
| 2072 |
); |
| 2073 |
assert!(css.contains( |
| 2074 |
"[data-format=\"markdown\"][data-ready] > .form-editor-modes {\n display: block;\n}" |
| 2075 |
)); |
| 2076 |
assert!(css.contains( |
| 2077 |
"[data-ready][data-mode=\"preview\"] > .form-editor-preview {\n display: block;\n}" |
| 2078 |
)); |
| 2079 |
assert!( |
| 2080 |
css.contains("[data-ready][data-mode=\"preview\"] > .field {\n display: none;\n}") |
| 2081 |
); |
| 2082 |
|
| 2083 |
assert!(!css.contains("px"), "{css}"); |
| 2084 |
assert!(!css.contains("rem"), "{css}"); |
| 2085 |
} |
| 2086 |
|
| 2087 |
|
| 2088 |
|
| 2089 |
|
| 2090 |
#[test] |
| 2091 |
fn the_editor_chrome_is_prefixed_and_its_gate_is_not() { |
| 2092 |
let opts = Emit { |
| 2093 |
class_prefix: "mk-", |
| 2094 |
..Emit::default() |
| 2095 |
}; |
| 2096 |
let html = field_html(&field(FieldKind::Rich), &Filling::default(), &opts); |
| 2097 |
assert!(html.contains("class=\"mk-form-editor-modes\""), "{html}"); |
| 2098 |
assert!(html.contains("class=\"mk-form-editor-preview\""), "{html}"); |
| 2099 |
assert!(html.contains("class=\"mk-segment chosen\""), "{html}"); |
| 2100 |
assert!(html.contains("data-format=\"markdown\""), "{html}"); |
| 2101 |
|
| 2102 |
let css = editor_rules(&opts); |
| 2103 |
assert!(css.contains(".mk-form-editor-modes"), "{css}"); |
| 2104 |
assert!(css.contains("[data-format=\"markdown\"]"), "{css}"); |
| 2105 |
} |
| 2106 |
|
| 2107 |
|
| 2108 |
|
| 2109 |
|
| 2110 |
#[test] |
| 2111 |
fn the_editor_classes_are_in_the_vocabulary() { |
| 2112 |
let opts = Emit::default(); |
| 2113 |
let names = crate::vocabulary::names(&opts); |
| 2114 |
for name in ["form-editor-modes", "form-editor-preview", "segment"] { |
| 2115 |
assert!(names.contains(name), "{name} is not in the vocabulary"); |
| 2116 |
} |
| 2117 |
} |
| 2118 |
|
| 2119 |
#[test] |
| 2120 |
fn the_class_prefix_reaches_the_markup_as_well_as_the_stylesheet() { |
| 2121 |
let opts = Emit { |
| 2122 |
class_prefix: "mk-", |
| 2123 |
..Emit::default() |
| 2124 |
}; |
| 2125 |
let html = field_html(&field(FieldKind::Text), &Filling::default(), &opts); |
| 2126 |
assert!(html.contains("class=\"mk-form-group\""), "{html}"); |
| 2127 |
assert!(html.contains("class=\"mk-field\""), "{html}"); |
| 2128 |
} |
| 2129 |
|
| 2130 |
#[test] |
| 2131 |
fn an_extended_field_says_so_and_leaves_the_disclosure_to_the_form() { |
| 2132 |
let mut f = field(FieldKind::Text); |
| 2133 |
f.extended = true; |
| 2134 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 2135 |
assert!(html.contains("data-extended=\"true\""), "{html}"); |
| 2136 |
} |
| 2137 |
|
| 2138 |
|
| 2139 |
|
| 2140 |
|
| 2141 |
#[test] |
| 2142 |
fn the_id_prefix_scopes_the_id_and_never_the_name() { |
| 2143 |
let mut f = field(FieldKind::Text); |
| 2144 |
f.hint = Some("Keep it short"); |
| 2145 |
f.error = Some("Required"); |
| 2146 |
let filling = Filling { |
| 2147 |
id_prefix: Some("form-modal-task-edit"), |
| 2148 |
..Filling::default() |
| 2149 |
}; |
| 2150 |
let html = field_html(&f, &filling, &Emit::default()); |
| 2151 |
|
| 2152 |
assert!( |
| 2153 |
html.contains(r#"id="form-modal-task-edit-title""#), |
| 2154 |
"{html}" |
| 2155 |
); |
| 2156 |
assert!(html.contains(r#"name="title""#), "{html}"); |
| 2157 |
assert!( |
| 2158 |
!html.contains(r#"name="form-modal-task-edit-title""#), |
| 2159 |
"{html}" |
| 2160 |
); |
| 2161 |
|
| 2162 |
|
| 2163 |
|
| 2164 |
assert!( |
| 2165 |
html.contains(r#"for="form-modal-task-edit-title""#), |
| 2166 |
"{html}" |
| 2167 |
); |
| 2168 |
assert!( |
| 2169 |
html.contains( |
| 2170 |
r#"aria-describedby="form-modal-task-edit-title-hint form-modal-task-edit-title-error""# |
| 2171 |
), |
| 2172 |
"{html}" |
| 2173 |
); |
| 2174 |
assert!( |
| 2175 |
html.contains(r#"id="form-modal-task-edit-title-hint""#), |
| 2176 |
"{html}" |
| 2177 |
); |
| 2178 |
} |
| 2179 |
|
| 2180 |
#[test] |
| 2181 |
fn a_hidden_field_submits_its_bare_name_under_a_prefix() { |
| 2182 |
let filling = Filling { |
| 2183 |
value: Value::Text("42"), |
| 2184 |
id_prefix: Some("scoped"), |
| 2185 |
..Filling::default() |
| 2186 |
}; |
| 2187 |
let html = field_html(&field(FieldKind::Hidden), &filling, &Emit::default()); |
| 2188 |
assert_eq!(html, r#"<input type="hidden" name="title" value="42">"#); |
| 2189 |
} |
| 2190 |
|
| 2191 |
|
| 2192 |
|
| 2193 |
|
| 2194 |
#[test] |
| 2195 |
fn a_constraint_becomes_the_browsers_own_attribute() { |
| 2196 |
|
| 2197 |
|
| 2198 |
|
| 2199 |
let html = field_html( |
| 2200 |
&Field { |
| 2201 |
max_length: Some(100), |
| 2202 |
min: Some("1"), |
| 2203 |
max: Some("240"), |
| 2204 |
required: true, |
| 2205 |
..Field::new(FieldKind::Number, "minutes", "Minutes") |
| 2206 |
}, |
| 2207 |
&Filling::default(), |
| 2208 |
&Emit::default(), |
| 2209 |
); |
| 2210 |
assert!(html.contains(r#"maxlength="100""#)); |
| 2211 |
assert!(html.contains(r#"min="1""#)); |
| 2212 |
assert!(html.contains(r#"max="240""#)); |
| 2213 |
assert!(html.contains(" required")); |
| 2214 |
} |
| 2215 |
|
| 2216 |
#[test] |
| 2217 |
fn a_bound_is_emitted_as_written_and_escaped_like_anything_else() { |
| 2218 |
|
| 2219 |
|
| 2220 |
let html = field_html( |
| 2221 |
&Field { |
| 2222 |
min: Some("2026-08-09T14:30"), |
| 2223 |
..Field::new(FieldKind::Text, "starts", "Starts") |
| 2224 |
}, |
| 2225 |
&Filling::default(), |
| 2226 |
&Emit::default(), |
| 2227 |
); |
| 2228 |
assert!(html.contains(r#"min="2026-08-09T14:30""#)); |
| 2229 |
} |
| 2230 |
|
| 2231 |
#[test] |
| 2232 |
fn a_file_field_is_a_file_input() { |
| 2233 |
|
| 2234 |
|
| 2235 |
|
| 2236 |
let html = field_html( |
| 2237 |
&Field::new(FieldKind::File, "attachment", "Attachment"), |
| 2238 |
&Filling::default(), |
| 2239 |
&Emit::default(), |
| 2240 |
); |
| 2241 |
assert!(html.contains(r#"type="file""#)); |
| 2242 |
assert!(!html.contains("accept=")); |
| 2243 |
assert!(!html.contains("multiple")); |
| 2244 |
|
| 2245 |
|
| 2246 |
assert!(!html.contains("value=")); |
| 2247 |
} |
| 2248 |
|
| 2249 |
#[test] |
| 2250 |
fn an_accept_list_is_comma_joined_in_the_attributes_own_format() { |
| 2251 |
|
| 2252 |
|
| 2253 |
|
| 2254 |
const MIXED: &[Accepted<'_>] = &[ |
| 2255 |
Accepted::Family(Family::Image), |
| 2256 |
Accepted::Type("text/csv"), |
| 2257 |
Accepted::Suffix(".tar.gz"), |
| 2258 |
]; |
| 2259 |
let html = field_html( |
| 2260 |
&Field { |
| 2261 |
multiple: true, |
| 2262 |
..Field::upload("drop", "Drop files", MIXED) |
| 2263 |
}, |
| 2264 |
&Filling::default(), |
| 2265 |
&Emit::default(), |
| 2266 |
); |
| 2267 |
assert!( |
| 2268 |
html.contains(r#"accept="image/*,text/csv,.tar.gz""#), |
| 2269 |
"{html}" |
| 2270 |
); |
| 2271 |
assert!(html.contains(" multiple"), "{html}"); |
| 2272 |
} |
| 2273 |
|
| 2274 |
#[test] |
| 2275 |
fn an_accept_entry_cannot_end_the_attribute_it_sits_in() { |
| 2276 |
|
| 2277 |
|
| 2278 |
|
| 2279 |
const HOSTILE: &[Accepted<'_>] = &[Accepted::Type(r#"image/x" onload="x"#)]; |
| 2280 |
let html = field_html( |
| 2281 |
&Field::upload("cover", "Cover", HOSTILE), |
| 2282 |
&Filling::default(), |
| 2283 |
&Emit::default(), |
| 2284 |
); |
| 2285 |
assert!(!html.contains(r#"onload="x"#), "{html}"); |
| 2286 |
} |
| 2287 |
|
| 2288 |
#[test] |
| 2289 |
fn the_typed_text_kinds_keep_their_input_type() { |
| 2290 |
for (kind, expected) in [ |
| 2291 |
(FieldKind::Email, "email"), |
| 2292 |
(FieldKind::Url, "url"), |
| 2293 |
(FieldKind::Tel, "tel"), |
| 2294 |
(FieldKind::Date, "date"), |
| 2295 |
(FieldKind::DateTime, "datetime-local"), |
| 2296 |
] { |
| 2297 |
let html = field_html(&field(kind), &Filling::default(), &Emit::default()); |
| 2298 |
assert!( |
| 2299 |
html.contains(&format!(r#"type="{expected}""#)), |
| 2300 |
"{kind:?} emitted {html}" |
| 2301 |
); |
| 2302 |
} |
| 2303 |
} |
| 2304 |
|
| 2305 |
#[test] |
| 2306 |
fn a_temporal_field_is_a_native_control_and_not_a_hinted_text_box() { |
| 2307 |
|
| 2308 |
|
| 2309 |
|
| 2310 |
for kind in [FieldKind::Date, FieldKind::DateTime] { |
| 2311 |
let html = field_html(&field(kind), &Filling::default(), &Emit::default()); |
| 2312 |
assert!(!html.contains(r#"type="text""#), "{kind:?} emitted {html}"); |
| 2313 |
} |
| 2314 |
} |
| 2315 |
|
| 2316 |
#[test] |
| 2317 |
fn no_prefix_leaves_the_id_as_the_name() { |
| 2318 |
let html = field_html( |
| 2319 |
&field(FieldKind::Text), |
| 2320 |
&Filling::default(), |
| 2321 |
&Emit::default(), |
| 2322 |
); |
| 2323 |
assert!(html.contains(r#"id="title" name="title""#), "{html}"); |
| 2324 |
} |
| 2325 |
} |
| 2326 |
|