| 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, Selector}; |
| 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::Range => "range", |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
FieldKind::Text | FieldKind::Select | FieldKind::Textarea | FieldKind::Rich => "text", |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
_ => "text", |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
fn push_accept(out: &mut String, field: &Field<'_>) { |
| 253 |
if field.accept.is_empty() { |
| 254 |
return; |
| 255 |
} |
| 256 |
out.push_str(" accept=\""); |
| 257 |
for (index, one) in field.accept.iter().enumerate() { |
| 258 |
if index > 0 { |
| 259 |
out.push(','); |
| 260 |
} |
| 261 |
escape_into(one.as_str(), out); |
| 262 |
} |
| 263 |
out.push('"'); |
| 264 |
} |
| 265 |
|
| 266 |
fn push_control_attributes(out: &mut String, field: &Field<'_>, id: &str, name: &str) { |
| 267 |
let _ = write!(out, " id=\"{id}\" name=\""); |
| 268 |
escape_into(name, out); |
| 269 |
out.push('"'); |
| 270 |
if field.required { |
| 271 |
out.push_str(" required"); |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
if let Some(limit) = field.max_length { |
| 278 |
let _ = write!(out, " maxlength=\"{limit}\""); |
| 279 |
} |
| 280 |
if let Some(min) = field.min { |
| 281 |
out.push_str(" min=\""); |
| 282 |
escape_into(min, out); |
| 283 |
out.push('"'); |
| 284 |
} |
| 285 |
if let Some(max) = field.max { |
| 286 |
out.push_str(" max=\""); |
| 287 |
escape_into(max, out); |
| 288 |
out.push('"'); |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
if let Some(step) = field.step { |
| 295 |
out.push_str(" step=\""); |
| 296 |
escape_into(step, out); |
| 297 |
out.push('"'); |
| 298 |
} |
| 299 |
if field.invalid() { |
| 300 |
out.push_str(" aria-invalid=\"true\""); |
| 301 |
} |
| 302 |
|
| 303 |
push_described_by(out, field, id); |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
fn push_described_by(out: &mut String, field: &Field<'_>, id: &str) { |
| 316 |
if field.hint.is_none() && field.error.is_none() { |
| 317 |
return; |
| 318 |
} |
| 319 |
out.push_str(" aria-describedby=\""); |
| 320 |
if field.hint.is_some() { |
| 321 |
let _ = write!(out, "{id}-hint"); |
| 322 |
} |
| 323 |
if field.error.is_some() { |
| 324 |
if field.hint.is_some() { |
| 325 |
out.push(' '); |
| 326 |
} |
| 327 |
let _ = write!(out, "{id}-error"); |
| 328 |
} |
| 329 |
out.push('"'); |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
const fn is_group_control(kind: FieldKind) -> bool { |
| 341 |
matches!(kind, FieldKind::Radio) |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
fn push_radio(out: &mut String, field: &Field<'_>, filling: &Filling<'_>, opts: &Emit) { |
| 358 |
let id = filling.id_for(field.name); |
| 359 |
let value = filling.value.as_text(); |
| 360 |
let name = escape(field.name); |
| 361 |
|
| 362 |
out.push_str("<div class=\""); |
| 363 |
push_class(out, "form-radio-group", opts); |
| 364 |
let _ = write!(out, "\" role=\"radiogroup\" aria-labelledby=\"{id}-label\""); |
| 365 |
if field.invalid() { |
| 366 |
out.push_str(" aria-invalid=\"true\""); |
| 367 |
} |
| 368 |
push_described_by(out, field, &id); |
| 369 |
out.push('>'); |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
for (index, opt) in field.options.iter().enumerate() { |
| 375 |
out.push_str("<label class=\""); |
| 376 |
push_class(out, "form-radio-label", opts); |
| 377 |
let _ = write!( |
| 378 |
out, |
| 379 |
"\"><input type=\"radio\" id=\"{id}-{index}\" name=\"{name}\" value=\"" |
| 380 |
); |
| 381 |
escape_into(opt.value, out); |
| 382 |
out.push('"'); |
| 383 |
if opt.value == value { |
| 384 |
out.push_str(" checked"); |
| 385 |
} |
| 386 |
if field.required { |
| 387 |
out.push_str(" required"); |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
if let Some(reason) = opt.unavailable { |
| 395 |
out.push_str(" disabled"); |
| 396 |
out.push_str("><span>"); |
| 397 |
escape_into(opt.label, out); |
| 398 |
out.push_str("</span><span class=\""); |
| 399 |
push_class(out, "form-option-reason", opts); |
| 400 |
out.push_str("\">"); |
| 401 |
escape_into(reason, out); |
| 402 |
out.push_str("</span></label>"); |
| 403 |
continue; |
| 404 |
} |
| 405 |
out.push_str("><span>"); |
| 406 |
escape_into(opt.label, out); |
| 407 |
out.push_str("</span></label>"); |
| 408 |
} |
| 409 |
|
| 410 |
out.push_str("</div>"); |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
fn push_options(out: &mut String, field: &Field<'_>, options: &[Choice<'_>], value: &str) { |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
if value.is_empty() |
| 438 |
&& let Some(text) = field.placeholder |
| 439 |
{ |
| 440 |
out.push_str("<option value=\"\" disabled selected>"); |
| 441 |
escape_into(text, out); |
| 442 |
out.push_str("</option>"); |
| 443 |
} |
| 444 |
if !value.is_empty() && !options.iter().any(|opt| opt.value == value) { |
| 445 |
|
| 446 |
|
| 447 |
let escaped = escape(value); |
| 448 |
let _ = write!( |
| 449 |
out, |
| 450 |
"<option value=\"{escaped}\" selected data-unmatched=\"true\">{escaped}</option>" |
| 451 |
); |
| 452 |
} |
| 453 |
for opt in options { |
| 454 |
out.push_str("<option value=\""); |
| 455 |
escape_into(opt.value, out); |
| 456 |
out.push('"'); |
| 457 |
if opt.value == value { |
| 458 |
out.push_str(" selected"); |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
if let Some(reason) = opt.unavailable { |
| 468 |
out.push_str(" disabled"); |
| 469 |
out.push('>'); |
| 470 |
escape_into(opt.label, out); |
| 471 |
out.push_str(": "); |
| 472 |
escape_into(reason, out); |
| 473 |
out.push_str("</option>"); |
| 474 |
continue; |
| 475 |
} |
| 476 |
out.push('>'); |
| 477 |
escape_into(opt.label, out); |
| 478 |
out.push_str("</option>"); |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
|
| 483 |
fn push_control(out: &mut String, field: &Field<'_>, filling: &Filling<'_>, opts: &Emit) { |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
if matches!(field.kind, FieldKind::Radio) { |
| 488 |
push_radio(out, field, filling, opts); |
| 489 |
return; |
| 490 |
} |
| 491 |
|
| 492 |
let id = filling.id_for(field.name); |
| 493 |
let placeholder = |out: &mut String| { |
| 494 |
if let Some(text) = field.placeholder { |
| 495 |
out.push_str(" placeholder=\""); |
| 496 |
escape_into(text, out); |
| 497 |
out.push('"'); |
| 498 |
} |
| 499 |
}; |
| 500 |
|
| 501 |
match field.kind { |
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
kind if kind.multiline() => { |
| 514 |
let rich = matches!(kind, FieldKind::Rich); |
| 515 |
if rich { |
| 516 |
push_editor_open(out, opts); |
| 517 |
} |
| 518 |
out.push_str("<textarea class=\""); |
| 519 |
push_class(out, "field", opts); |
| 520 |
out.push('"'); |
| 521 |
if rich { |
| 522 |
out.push_str(" data-format=\"markdown\""); |
| 523 |
} |
| 524 |
push_control_attributes(out, field, &id, field.name); |
| 525 |
placeholder(out); |
| 526 |
out.push('>'); |
| 527 |
escape_into(filling.value.as_text(), out); |
| 528 |
out.push_str("</textarea>"); |
| 529 |
if rich { |
| 530 |
push_editor_close(out, opts); |
| 531 |
} |
| 532 |
} |
| 533 |
FieldKind::Select => { |
| 534 |
out.push_str("<select class=\""); |
| 535 |
push_class(out, "field", opts); |
| 536 |
out.push('"'); |
| 537 |
push_control_attributes(out, field, &id, field.name); |
| 538 |
out.push('>'); |
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
push_options(out, field, field.options, filling.value.as_text()); |
| 543 |
out.push_str("</select>"); |
| 544 |
} |
| 545 |
FieldKind::Checkbox => { |
| 546 |
out.push_str("<label class=\""); |
| 547 |
push_class(out, "form-checkbox-label", opts); |
| 548 |
out.push_str("\"><input type=\"checkbox\""); |
| 549 |
push_control_attributes(out, field, &id, field.name); |
| 550 |
if matches!(filling.value, Value::On(true)) { |
| 551 |
out.push_str(" checked"); |
| 552 |
} |
| 553 |
out.push_str("><span>"); |
| 554 |
escape_into(field.label, out); |
| 555 |
out.push_str("</span></label>"); |
| 556 |
} |
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
FieldKind::Secret => { |
| 564 |
out.push_str("<input type=\"password\" class=\""); |
| 565 |
push_class(out, "field", opts); |
| 566 |
out.push('"'); |
| 567 |
push_control_attributes(out, field, &id, field.name); |
| 568 |
placeholder(out); |
| 569 |
out.push('>'); |
| 570 |
} |
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
FieldKind::File => { |
| 576 |
out.push_str("<input type=\"file\" class=\""); |
| 577 |
push_class(out, "field", opts); |
| 578 |
out.push('"'); |
| 579 |
push_control_attributes(out, field, &id, field.name); |
| 580 |
push_accept(out, field); |
| 581 |
if field.multiple { |
| 582 |
out.push_str(" multiple"); |
| 583 |
} |
| 584 |
out.push('>'); |
| 585 |
} |
| 586 |
kind => { |
| 587 |
let _ = write!(out, "<input type=\"{}\" class=\"", input_type(kind)); |
| 588 |
push_class(out, "field", opts); |
| 589 |
out.push('"'); |
| 590 |
push_control_attributes(out, field, &id, field.name); |
| 591 |
placeholder(out); |
| 592 |
out.push_str(" value=\""); |
| 593 |
escape_into(filling.value.as_text(), out); |
| 594 |
out.push_str("\">"); |
| 595 |
} |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
fn push_editor_open(out: &mut String, opts: &Emit) { |
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
out.push_str("<div data-format=\"markdown\"><div class=\""); |
| 640 |
push_class(out, "form-editor-modes", opts); |
| 641 |
out.push_str("\">"); |
| 642 |
push_mode(out, "write", "Write", true, opts); |
| 643 |
push_mode(out, "preview", "Preview", false, opts); |
| 644 |
out.push_str("</div>"); |
| 645 |
} |
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
fn push_mode(out: &mut String, mode: &str, label: &str, chosen: bool, opts: &Emit) { |
| 656 |
out.push_str("<button type=\"button\" class=\""); |
| 657 |
push_class(out, crate::option_class(Selector::Segmented), opts); |
| 658 |
if chosen { |
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
out.push_str(" chosen"); |
| 663 |
} |
| 664 |
let _ = write!( |
| 665 |
out, |
| 666 |
"\" data-editor-mode=\"{mode}\" aria-pressed=\"{chosen}\">{label}</button>" |
| 667 |
); |
| 668 |
} |
| 669 |
|
| 670 |
|
| 671 |
fn push_editor_close(out: &mut String, opts: &Emit) { |
| 672 |
out.push_str("<div class=\""); |
| 673 |
push_class(out, "form-editor-preview", opts); |
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
out.push_str("\" data-editor-preview></div></div>"); |
| 678 |
} |
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
pub(crate) fn editor_rules(opts: &Emit) -> String { |
| 693 |
let mut css = String::new(); |
| 694 |
let modes = class("form-editor-modes", opts); |
| 695 |
let preview = class("form-editor-preview", opts); |
| 696 |
let field = class("field", opts); |
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
let _ = writeln!( |
| 701 |
css, |
| 702 |
"[data-format=\"markdown\"] > .{modes} {{\n display: none;\n}}" |
| 703 |
); |
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
let _ = writeln!( |
| 709 |
css, |
| 710 |
"[data-format=\"markdown\"][data-ready] > .{modes} {{\n display: block;\n}}" |
| 711 |
); |
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
let _ = writeln!( |
| 717 |
css, |
| 718 |
"[data-format=\"markdown\"] > .{preview} {{\n display: none;\n}}" |
| 719 |
); |
| 720 |
let _ = writeln!( |
| 721 |
css, |
| 722 |
"[data-format=\"markdown\"][data-ready][data-mode=\"preview\"] > .{preview} \ |
| 723 |
{{\n display: block;\n}}" |
| 724 |
); |
| 725 |
|
| 726 |
|
| 727 |
let _ = writeln!( |
| 728 |
css, |
| 729 |
"[data-format=\"markdown\"][data-ready][data-mode=\"preview\"] > .{field} \ |
| 730 |
{{\n display: none;\n}}" |
| 731 |
); |
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
let _ = write!( |
| 738 |
css, |
| 739 |
"[data-format=\"markdown\"] > .{preview} {{\n{}}}\n", |
| 740 |
crate::depth_declarations(Depth::Well) |
| 741 |
); |
| 742 |
|
| 743 |
css |
| 744 |
} |
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
#[must_use] |
| 774 |
pub fn field_html(field: &Field<'_>, filling: &Filling<'_>, opts: &Emit) -> String { |
| 775 |
let mut html = String::new(); |
| 776 |
field_html_into(field, filling, opts, &mut html); |
| 777 |
html |
| 778 |
} |
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
pub fn field_html_into(field: &Field<'_>, filling: &Filling<'_>, opts: &Emit, out: &mut String) { |
| 786 |
let id = filling.id_for(field.name); |
| 787 |
|
| 788 |
if !field.kind.visible() { |
| 789 |
|
| 790 |
|
| 791 |
out.push_str("<input type=\"hidden\" name=\""); |
| 792 |
escape_into(field.name, out); |
| 793 |
out.push_str("\" value=\""); |
| 794 |
escape_into(filling.value.as_text(), out); |
| 795 |
out.push_str("\">"); |
| 796 |
return; |
| 797 |
} |
| 798 |
|
| 799 |
out.push_str("<div class=\""); |
| 800 |
push_class(out, "form-group", opts); |
| 801 |
if field.invalid() { |
| 802 |
out.push_str(" has-error"); |
| 803 |
} |
| 804 |
if field.extended { |
| 805 |
|
| 806 |
|
| 807 |
out.push_str("\" data-extended=\"true"); |
| 808 |
} |
| 809 |
out.push_str("\">"); |
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
if !field.kind.labels_itself() { |
| 815 |
out.push_str("<label class=\""); |
| 816 |
push_class(out, "form-label", opts); |
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
if is_group_control(field.kind) { |
| 821 |
let _ = write!(out, "\" id=\"{id}-label\">"); |
| 822 |
} else { |
| 823 |
let _ = write!(out, "\" for=\"{id}\">"); |
| 824 |
} |
| 825 |
escape_into(field.label, out); |
| 826 |
out.push_str("</label>"); |
| 827 |
} |
| 828 |
|
| 829 |
push_control(out, field, filling, opts); |
| 830 |
|
| 831 |
if let Some(hint) = field.hint { |
| 832 |
out.push_str("<div class=\""); |
| 833 |
push_class(out, "form-hint", opts); |
| 834 |
let _ = write!(out, "\" id=\"{id}-hint\">"); |
| 835 |
escape_into(hint, out); |
| 836 |
out.push_str("</div>"); |
| 837 |
} |
| 838 |
if let Some(Markup(markup)) = filling.trailing { |
| 839 |
out.push_str(markup); |
| 840 |
} |
| 841 |
if let Some(error) = field.error { |
| 842 |
out.push_str("<div class=\""); |
| 843 |
push_class(out, "form-error", opts); |
| 844 |
let _ = write!(out, " visible\" id=\"{id}-error\" role=\"alert\">"); |
| 845 |
escape_into(error, out); |
| 846 |
out.push_str("</div>"); |
| 847 |
} |
| 848 |
|
| 849 |
out.push_str("</div>"); |
| 850 |
} |
| 851 |
|
| 852 |
#[cfg(test)] |
| 853 |
mod tests { |
| 854 |
use super::*; |
| 855 |
use makeover_layout::{Accepted, Family}; |
| 856 |
|
| 857 |
fn field(kind: FieldKind) -> Field<'static> { |
| 858 |
Field::new(kind, "title", "Title") |
| 859 |
} |
| 860 |
|
| 861 |
#[test] |
| 862 |
fn a_value_cannot_break_out_of_the_attribute_it_sits_in() { |
| 863 |
|
| 864 |
let filling = Filling::of(Value::Text("x\" onfocus=alert(1) autofocus=\"")); |
| 865 |
let html = field_html(&field(FieldKind::Text), &filling, &Emit::default()); |
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
assert!(!html.contains("\" onfocus"), "{html}"); |
| 870 |
assert!( |
| 871 |
html.contains("value=\"x" onfocus=alert(1) autofocus="\""), |
| 872 |
"{html}" |
| 873 |
); |
| 874 |
} |
| 875 |
|
| 876 |
#[test] |
| 877 |
fn a_label_cannot_open_a_tag() { |
| 878 |
let mut f = field(FieldKind::Text); |
| 879 |
f.label = "<script>alert(1)</script>"; |
| 880 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 881 |
assert!(!html.contains("<script>"), "{html}"); |
| 882 |
assert!(html.contains("<script>"), "{html}"); |
| 883 |
} |
| 884 |
|
| 885 |
#[test] |
| 886 |
fn every_escaped_sink_is_covered_by_the_one_escaper() { |
| 887 |
assert_eq!(escape("&<>\"'"), "&<>"'"); |
| 888 |
|
| 889 |
|
| 890 |
assert!(escape("\"").contains(""")); |
| 891 |
} |
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
#[test] |
| 898 |
fn the_streaming_escaper_appends_what_the_returning_one_returns() { |
| 899 |
for text in [ |
| 900 |
"", |
| 901 |
"plain", |
| 902 |
"&<>\"'", |
| 903 |
"&&&", |
| 904 |
"a & b", |
| 905 |
"trailing&", |
| 906 |
"&leading", |
| 907 |
"é世 & <b>naïve</b> \u{1f600}", |
| 908 |
] { |
| 909 |
let mut out = String::from("kept: "); |
| 910 |
escape_into(text, &mut out); |
| 911 |
assert_eq!(out, format!("kept: {}", escape(text)), "{text:?}"); |
| 912 |
} |
| 913 |
} |
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
#[test] |
| 918 |
fn a_streamed_field_is_the_field_the_other_form_returns() { |
| 919 |
let kinds = [ |
| 920 |
FieldKind::Text, |
| 921 |
FieldKind::Secret, |
| 922 |
FieldKind::Number, |
| 923 |
FieldKind::Checkbox, |
| 924 |
FieldKind::Radio, |
| 925 |
FieldKind::Select, |
| 926 |
FieldKind::Textarea, |
| 927 |
FieldKind::File, |
| 928 |
FieldKind::Hidden, |
| 929 |
]; |
| 930 |
let choices = [Choice::plain("one"), Choice::plain("two")]; |
| 931 |
let opts = Emit { |
| 932 |
class_prefix: "mk-", |
| 933 |
..Emit::default() |
| 934 |
}; |
| 935 |
for kind in kinds { |
| 936 |
let described = Field { |
| 937 |
hint: Some("a hint"), |
| 938 |
error: Some("wrong <here>"), |
| 939 |
placeholder: Some("x\" y"), |
| 940 |
options: &choices, |
| 941 |
required: true, |
| 942 |
max_length: Some(40), |
| 943 |
min: Some("1"), |
| 944 |
max: Some("9"), |
| 945 |
extended: true, |
| 946 |
..Field::new(kind, "the & name", "The <label>") |
| 947 |
}; |
| 948 |
let filling = Filling { |
| 949 |
value: Value::Text("one"), |
| 950 |
trailing: Some(Markup("<i>t</i>")), |
| 951 |
id_prefix: Some("modal"), |
| 952 |
}; |
| 953 |
let mut streamed = String::new(); |
| 954 |
field_html_into(&described, &filling, &opts, &mut streamed); |
| 955 |
assert_eq!( |
| 956 |
streamed, |
| 957 |
field_html(&described, &filling, &opts), |
| 958 |
"{kind:?}" |
| 959 |
); |
| 960 |
|
| 961 |
|
| 962 |
let plain = Field::new(kind, "name", "Label"); |
| 963 |
let mut streamed = String::new(); |
| 964 |
field_html_into(&plain, &Filling::default(), &opts, &mut streamed); |
| 965 |
assert_eq!( |
| 966 |
streamed, |
| 967 |
field_html(&plain, &Filling::default(), &opts), |
| 968 |
"{kind:?}" |
| 969 |
); |
| 970 |
} |
| 971 |
} |
| 972 |
|
| 973 |
#[test] |
| 974 |
fn markup_is_the_only_way_past_the_escaping() { |
| 975 |
let filling = Filling { |
| 976 |
trailing: Some(Markup("<div class=\"recurrence-config\"></div>")), |
| 977 |
..Filling::default() |
| 978 |
}; |
| 979 |
let html = field_html(&field(FieldKind::Text), &filling, &Emit::default()); |
| 980 |
assert!( |
| 981 |
html.contains("<div class=\"recurrence-config\"></div>"), |
| 982 |
"{html}" |
| 983 |
); |
| 984 |
} |
| 985 |
|
| 986 |
#[test] |
| 987 |
fn an_invalid_field_carries_the_attribute_its_own_stylesheet_keys_on() { |
| 988 |
let mut f = field(FieldKind::Text); |
| 989 |
f.error = Some("Required"); |
| 990 |
let opts = Emit::default(); |
| 991 |
let html = field_html(&f, &Filling::default(), &opts); |
| 992 |
assert!(html.contains("aria-invalid=\"true\""), "{html}"); |
| 993 |
|
| 994 |
assert!(crate::stylesheet(&opts).contains("[aria-invalid=\"true\"]")); |
| 995 |
|
| 996 |
|
| 997 |
assert!(html.contains("has-error"), "{html}"); |
| 998 |
} |
| 999 |
|
| 1000 |
#[test] |
| 1001 |
fn a_valid_field_claims_nothing_about_being_invalid() { |
| 1002 |
let html = field_html( |
| 1003 |
&field(FieldKind::Text), |
| 1004 |
&Filling::default(), |
| 1005 |
&Emit::default(), |
| 1006 |
); |
| 1007 |
assert!(!html.contains("aria-invalid"), "{html}"); |
| 1008 |
assert!(!html.contains("has-error"), "{html}"); |
| 1009 |
} |
| 1010 |
|
| 1011 |
#[test] |
| 1012 |
fn the_hint_survives_an_error_arriving() { |
| 1013 |
let mut f = field(FieldKind::Text); |
| 1014 |
f.hint = Some("Keep it short"); |
| 1015 |
f.error = Some("Required"); |
| 1016 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 1017 |
assert!( |
| 1018 |
html.contains("aria-describedby=\"title-hint title-error\""), |
| 1019 |
"{html}" |
| 1020 |
); |
| 1021 |
} |
| 1022 |
|
| 1023 |
#[test] |
| 1024 |
fn a_secret_never_carries_its_value_into_the_markup() { |
| 1025 |
let filling = Filling::of(Value::Text("hunter2")); |
| 1026 |
let html = field_html(&field(FieldKind::Secret), &filling, &Emit::default()); |
| 1027 |
assert!(!html.contains("hunter2"), "{html}"); |
| 1028 |
assert!(html.contains("type=\"password\""), "{html}"); |
| 1029 |
} |
| 1030 |
|
| 1031 |
#[test] |
| 1032 |
fn a_hidden_field_is_the_input_and_nothing_else() { |
| 1033 |
let filling = Filling::of(Value::Text("42")); |
| 1034 |
let html = field_html(&field(FieldKind::Hidden), &filling, &Emit::default()); |
| 1035 |
assert_eq!(html, "<input type=\"hidden\" name=\"title\" value=\"42\">"); |
| 1036 |
} |
| 1037 |
|
| 1038 |
#[test] |
| 1039 |
fn a_checkbox_labels_itself_and_takes_no_separate_label() { |
| 1040 |
let html = field_html( |
| 1041 |
&field(FieldKind::Checkbox), |
| 1042 |
&Filling::of(Value::On(true)), |
| 1043 |
&Emit::default(), |
| 1044 |
); |
| 1045 |
assert!(!html.contains("form-label"), "{html}"); |
| 1046 |
assert!(html.contains("checked"), "{html}"); |
| 1047 |
assert!(html.contains("<span>Title</span>"), "{html}"); |
| 1048 |
} |
| 1049 |
|
| 1050 |
#[test] |
| 1051 |
fn a_select_keeps_a_value_no_option_carries() { |
| 1052 |
let options = [Choice::plain("1"), Choice::plain("3"), Choice::plain("7")]; |
| 1053 |
let f = Field::select("title", "Title", &options); |
| 1054 |
let html = field_html(&f, &Filling::of(Value::Text("10")), &Emit::default()); |
| 1055 |
assert!(html.contains("data-unmatched=\"true\""), "{html}"); |
| 1056 |
|
| 1057 |
|
| 1058 |
assert!(html.contains("<option value=\"10\" selected"), "{html}"); |
| 1059 |
} |
| 1060 |
|
| 1061 |
#[test] |
| 1062 |
fn a_select_with_no_options_emits_an_empty_select() { |
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
let f = Field::select("title", "Title", &[]); |
| 1067 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 1068 |
assert!(html.contains("<select"), "{html}"); |
| 1069 |
assert!(!html.contains("<option"), "{html}"); |
| 1070 |
} |
| 1071 |
|
| 1072 |
#[test] |
| 1073 |
fn an_unanswered_select_shows_its_ghost_text_and_cannot_be_chosen_back() { |
| 1074 |
let options = [Choice::new("sp404", "SP-404")]; |
| 1075 |
let f = Field { |
| 1076 |
placeholder: Some("Select device..."), |
| 1077 |
..Field::select("device", "Conform for device", &options) |
| 1078 |
}; |
| 1079 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 1080 |
|
| 1081 |
assert!( |
| 1082 |
html.contains("<option value=\"\" disabled selected>Select device...</option>"), |
| 1083 |
"{html}" |
| 1084 |
); |
| 1085 |
|
| 1086 |
|
| 1087 |
assert!( |
| 1088 |
html.find("Select device...") < html.find("SP-404"), |
| 1089 |
"{html}" |
| 1090 |
); |
| 1091 |
} |
| 1092 |
|
| 1093 |
#[test] |
| 1094 |
fn an_answered_select_drops_the_ghost_text() { |
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
let options = [Choice::new("sp404", "SP-404")]; |
| 1099 |
let f = Field { |
| 1100 |
placeholder: Some("Select device..."), |
| 1101 |
..Field::select("device", "Conform for device", &options) |
| 1102 |
}; |
| 1103 |
let html = field_html(&f, &Filling::of(Value::Text("sp404")), &Emit::default()); |
| 1104 |
assert!(!html.contains("Select device..."), "{html}"); |
| 1105 |
} |
| 1106 |
|
| 1107 |
#[test] |
| 1108 |
fn a_wrong_answer_is_kept_and_is_not_the_ghost_text() { |
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
let options = [Choice::plain("1"), Choice::plain("7")]; |
| 1113 |
let f = Field { |
| 1114 |
placeholder: Some("Pick one"), |
| 1115 |
..Field::select("retention", "Keep backups for", &options) |
| 1116 |
}; |
| 1117 |
let html = field_html(&f, &Filling::of(Value::Text("10")), &Emit::default()); |
| 1118 |
assert!(html.contains("data-unmatched=\"true\""), "{html}"); |
| 1119 |
assert!(!html.contains("Pick one"), "{html}"); |
| 1120 |
} |
| 1121 |
|
| 1122 |
#[test] |
| 1123 |
fn a_range_is_a_range_input_and_carries_its_extent() { |
| 1124 |
let f = Field { |
| 1125 |
step: Some("0.01"), |
| 1126 |
..Field::range("review", "Review above", "0", "1") |
| 1127 |
}; |
| 1128 |
let html = field_html(&f, &Filling::of(Value::Text("0.72")), &Emit::default()); |
| 1129 |
assert!(html.contains("type=\"range\""), "{html}"); |
| 1130 |
assert!(html.contains("min=\"0\""), "{html}"); |
| 1131 |
assert!(html.contains("max=\"1\""), "{html}"); |
| 1132 |
|
| 1133 |
|
| 1134 |
assert!(html.contains("step=\"0.01\""), "{html}"); |
| 1135 |
} |
| 1136 |
|
| 1137 |
#[test] |
| 1138 |
fn a_number_with_bounds_is_still_typed_into() { |
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
let f = Field { |
| 1143 |
min: Some("1"), |
| 1144 |
..Field::new(FieldKind::Number, "minutes", "Minutes") |
| 1145 |
}; |
| 1146 |
let html = field_html(&f, &Filling::of(Value::Text("30")), &Emit::default()); |
| 1147 |
assert!(html.contains("type=\"number\""), "{html}"); |
| 1148 |
assert!(!html.contains("type=\"range\""), "{html}"); |
| 1149 |
|
| 1150 |
assert!(!html.contains("step="), "{html}"); |
| 1151 |
} |
| 1152 |
|
| 1153 |
#[test] |
| 1154 |
fn an_unavailable_option_is_disabled_and_says_why() { |
| 1155 |
let options = [ |
| 1156 |
Choice::new("chromatic", "Chromatic"), |
| 1157 |
Choice::new("multi", "Multi-sample").unless("Drop a second sample."), |
| 1158 |
]; |
| 1159 |
let f = Field::radio("mode", "Mode", &options); |
| 1160 |
let html = field_html(&f, &Filling::of(Value::Text("chromatic")), &Emit::default()); |
| 1161 |
|
| 1162 |
assert!(html.contains(" disabled"), "{html}"); |
| 1163 |
assert!(html.contains("Drop a second sample."), "{html}"); |
| 1164 |
|
| 1165 |
|
| 1166 |
assert!(html.contains("value=\"multi\""), "{html}"); |
| 1167 |
|
| 1168 |
assert!(html.contains("form-option-reason"), "{html}"); |
| 1169 |
} |
| 1170 |
|
| 1171 |
#[test] |
| 1172 |
fn an_unavailable_select_option_carries_its_reason_in_its_text() { |
| 1173 |
|
| 1174 |
|
| 1175 |
let options = [Choice::new("multi", "Multi-sample").unless("Drop a second sample.")]; |
| 1176 |
let f = Field::select("mode", "Mode", &options); |
| 1177 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 1178 |
assert!( |
| 1179 |
html.contains(">Multi-sample: Drop a second sample.</option>"), |
| 1180 |
"{html}" |
| 1181 |
); |
| 1182 |
assert!(html.contains("disabled"), "{html}"); |
| 1183 |
} |
| 1184 |
|
| 1185 |
#[test] |
| 1186 |
fn a_radio_group_is_named_by_its_label_instead_of_pointing_at_it() { |
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
let options = [Choice::plain("copy"), Choice::plain("reference")]; |
| 1191 |
let f = Field::radio("storage", "Storage style", &options); |
| 1192 |
let html = field_html(&f, &Filling::of(Value::Text("copy")), &Emit::default()); |
| 1193 |
|
| 1194 |
assert!(html.contains("id=\"storage-label\""), "{html}"); |
| 1195 |
assert!(!html.contains("for=\"storage\""), "{html}"); |
| 1196 |
assert!(html.contains("role=\"radiogroup\""), "{html}"); |
| 1197 |
assert!(html.contains("aria-labelledby=\"storage-label\""), "{html}"); |
| 1198 |
} |
| 1199 |
|
| 1200 |
#[test] |
| 1201 |
fn every_option_shares_the_name_and_only_the_current_one_is_checked() { |
| 1202 |
|
| 1203 |
|
| 1204 |
let options = [ |
| 1205 |
Choice::plain("copy"), |
| 1206 |
Choice::plain("reference"), |
| 1207 |
Choice::plain("link"), |
| 1208 |
]; |
| 1209 |
let f = Field::radio("storage", "Storage style", &options); |
| 1210 |
let html = field_html(&f, &Filling::of(Value::Text("reference")), &Emit::default()); |
| 1211 |
|
| 1212 |
assert_eq!(html.matches("name=\"storage\"").count(), 3, "{html}"); |
| 1213 |
assert_eq!(html.matches(" checked").count(), 1, "{html}"); |
| 1214 |
assert!( |
| 1215 |
html.contains("value=\"reference\" checked"), |
| 1216 |
"the checked one is the one held: {html}" |
| 1217 |
); |
| 1218 |
for index in 0..3 { |
| 1219 |
assert!(html.contains(&format!("id=\"storage-{index}\"")), "{html}"); |
| 1220 |
} |
| 1221 |
} |
| 1222 |
|
| 1223 |
#[test] |
| 1224 |
fn a_radio_group_carries_the_error_rather_than_any_one_option() { |
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
let options = [Choice::plain("copy"), Choice::plain("reference")]; |
| 1229 |
let f = Field { |
| 1230 |
error: Some("Pick one."), |
| 1231 |
hint: Some("Cannot be changed later."), |
| 1232 |
..Field::radio("storage", "Storage style", &options) |
| 1233 |
}; |
| 1234 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 1235 |
|
| 1236 |
assert_eq!(html.matches("aria-invalid=\"true\"").count(), 1, "{html}"); |
| 1237 |
assert!( |
| 1238 |
html.contains("aria-describedby=\"storage-hint storage-error\""), |
| 1239 |
"{html}" |
| 1240 |
); |
| 1241 |
|
| 1242 |
|
| 1243 |
let group = html.find("role=\"radiogroup\"").expect("group"); |
| 1244 |
let first = html.find("type=\"radio\"").expect("an option"); |
| 1245 |
assert!(group < first, "{html}"); |
| 1246 |
} |
| 1247 |
|
| 1248 |
#[test] |
| 1249 |
fn a_compulsory_radio_group_marks_every_option() { |
| 1250 |
|
| 1251 |
|
| 1252 |
let options = [Choice::plain("copy"), Choice::plain("reference")]; |
| 1253 |
let f = Field { |
| 1254 |
required: true, |
| 1255 |
..Field::radio("storage", "Storage style", &options) |
| 1256 |
}; |
| 1257 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 1258 |
assert_eq!(html.matches(" required").count(), 2, "{html}"); |
| 1259 |
} |
| 1260 |
|
| 1261 |
#[test] |
| 1262 |
fn a_radio_option_cannot_break_out_of_its_attribute() { |
| 1263 |
|
| 1264 |
|
| 1265 |
let hostile = [Choice::new( |
| 1266 |
"x\" onclick=alert(1) data-x=\"", |
| 1267 |
"<script>alert(1)</script>", |
| 1268 |
)]; |
| 1269 |
let f = Field::radio("storage", "Storage style", &hostile); |
| 1270 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 1271 |
|
| 1272 |
|
| 1273 |
|
| 1274 |
assert!(html.contains("value=\"x" onclick=alert(1)"), "{html}"); |
| 1275 |
assert!(!html.contains("<script>"), "{html}"); |
| 1276 |
assert!(html.contains("id=\"storage-0\""), "{html}"); |
| 1277 |
} |
| 1278 |
|
| 1279 |
#[test] |
| 1280 |
fn a_radio_group_with_no_options_emits_an_empty_group() { |
| 1281 |
|
| 1282 |
let f = Field::radio("storage", "Storage style", &[]); |
| 1283 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 1284 |
assert!(html.contains("role=\"radiogroup\""), "{html}"); |
| 1285 |
assert!(!html.contains("type=\"radio\""), "{html}"); |
| 1286 |
} |
| 1287 |
|
| 1288 |
#[test] |
| 1289 |
fn a_placeholder_comes_off_the_description_and_is_escaped() { |
| 1290 |
|
| 1291 |
|
| 1292 |
let f = Field { |
| 1293 |
placeholder: Some("x\" onfocus=alert(1) autofocus=\""), |
| 1294 |
..field(FieldKind::Text) |
| 1295 |
}; |
| 1296 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 1297 |
assert!(html.contains("placeholder=\""), "{html}"); |
| 1298 |
assert!(!html.contains("\" onfocus"), "{html}"); |
| 1299 |
} |
| 1300 |
|
| 1301 |
#[test] |
| 1302 |
fn a_select_marks_the_option_that_matches() { |
| 1303 |
let options = [Choice::plain("1"), Choice::plain("3")]; |
| 1304 |
let f = Field::select("title", "Title", &options); |
| 1305 |
let html = field_html(&f, &Filling::of(Value::Text("3")), &Emit::default()); |
| 1306 |
assert!( |
| 1307 |
html.contains("<option value=\"3\" selected>3</option>"), |
| 1308 |
"{html}" |
| 1309 |
); |
| 1310 |
assert!(html.contains("<option value=\"1\">1</option>"), "{html}"); |
| 1311 |
assert!(!html.contains("data-unmatched"), "{html}"); |
| 1312 |
} |
| 1313 |
|
| 1314 |
#[test] |
| 1315 |
fn a_textarea_carries_its_value_as_text_and_not_as_an_attribute() { |
| 1316 |
let filling = Filling::of(Value::Text("two\nlines")); |
| 1317 |
let html = field_html(&field(FieldKind::Textarea), &filling, &Emit::default()); |
| 1318 |
assert!(html.contains(">two\nlines</textarea>"), "{html}"); |
| 1319 |
} |
| 1320 |
|
| 1321 |
#[test] |
| 1322 |
fn a_markdown_field_is_a_textarea_that_says_what_its_value_is() { |
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
let filling = Filling::of(Value::Text("# Heading")); |
| 1328 |
let html = field_html(&field(FieldKind::Rich), &filling, &Emit::default()); |
| 1329 |
assert!(html.contains("<textarea"), "{html}"); |
| 1330 |
assert!(html.contains(r#"data-format="markdown""#), "{html}"); |
| 1331 |
assert!(html.contains("># Heading</textarea>"), "{html}"); |
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
let plain = field_html(&field(FieldKind::Textarea), &filling, &Emit::default()); |
| 1336 |
assert!(!plain.contains("data-format"), "{plain}"); |
| 1337 |
|
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
assert!(!html.contains("<input"), "{html}"); |
| 1342 |
} |
| 1343 |
|
| 1344 |
#[test] |
| 1345 |
fn a_markdown_field_gets_the_preview_the_member_permits() { |
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
let filling = Filling::of(Value::Text("# Heading")); |
| 1350 |
let html = field_html(&field(FieldKind::Rich), &filling, &Emit::default()); |
| 1351 |
assert!(html.contains("data-editor-mode=\"write\""), "{html}"); |
| 1352 |
assert!(html.contains("data-editor-mode=\"preview\""), "{html}"); |
| 1353 |
assert!(html.contains("data-editor-preview"), "{html}"); |
| 1354 |
|
| 1355 |
|
| 1356 |
assert!( |
| 1357 |
html.contains( |
| 1358 |
"class=\"segment chosen\" data-editor-mode=\"write\" aria-pressed=\"true\"" |
| 1359 |
), |
| 1360 |
"{html}" |
| 1361 |
); |
| 1362 |
assert!( |
| 1363 |
html.contains("data-editor-mode=\"preview\" aria-pressed=\"false\""), |
| 1364 |
"{html}" |
| 1365 |
); |
| 1366 |
|
| 1367 |
|
| 1368 |
assert!(html.contains("># Heading</textarea>"), "{html}"); |
| 1369 |
} |
| 1370 |
|
| 1371 |
#[test] |
| 1372 |
fn a_plain_textarea_gets_no_editor_chrome() { |
| 1373 |
let filling = Filling::of(Value::Text("plain")); |
| 1374 |
let html = field_html(&field(FieldKind::Textarea), &filling, &Emit::default()); |
| 1375 |
assert!(!html.contains("data-editor-mode"), "{html}"); |
| 1376 |
assert!(!html.contains("data-editor-preview"), "{html}"); |
| 1377 |
assert!(!html.contains("segment"), "{html}"); |
| 1378 |
} |
| 1379 |
|
| 1380 |
#[test] |
| 1381 |
fn nothing_the_editor_emits_renders_the_value_as_markup() { |
| 1382 |
|
| 1383 |
|
| 1384 |
|
| 1385 |
let filling = Filling::of(Value::Text("<img src=x onerror=alert(1)>")); |
| 1386 |
let html = field_html(&field(FieldKind::Rich), &filling, &Emit::default()); |
| 1387 |
assert!(html.contains("data-editor-preview></div>"), "{html}"); |
| 1388 |
assert!(!html.contains("<img"), "{html}"); |
| 1389 |
assert!( |
| 1390 |
html.contains("<img src=x onerror=alert(1)>"), |
| 1391 |
"{html}" |
| 1392 |
); |
| 1393 |
} |
| 1394 |
|
| 1395 |
#[test] |
| 1396 |
fn the_editor_rules_gate_on_the_attribute_and_on_a_binding() { |
| 1397 |
let css = editor_rules(&Emit::default()); |
| 1398 |
|
| 1399 |
|
| 1400 |
|
| 1401 |
for line in css.lines().filter(|line| line.contains('{')) { |
| 1402 |
assert!(line.contains("[data-format=\"markdown\"]"), "{line}"); |
| 1403 |
} |
| 1404 |
|
| 1405 |
|
| 1406 |
assert!( |
| 1407 |
css.contains( |
| 1408 |
"[data-format=\"markdown\"] > .form-editor-modes {\n display: none;\n}" |
| 1409 |
) |
| 1410 |
); |
| 1411 |
assert!(css.contains( |
| 1412 |
"[data-format=\"markdown\"][data-ready] > .form-editor-modes {\n display: block;\n}" |
| 1413 |
)); |
| 1414 |
assert!(css.contains( |
| 1415 |
"[data-ready][data-mode=\"preview\"] > .form-editor-preview {\n display: block;\n}" |
| 1416 |
)); |
| 1417 |
assert!( |
| 1418 |
css.contains("[data-ready][data-mode=\"preview\"] > .field {\n display: none;\n}") |
| 1419 |
); |
| 1420 |
|
| 1421 |
assert!(!css.contains("px"), "{css}"); |
| 1422 |
assert!(!css.contains("rem"), "{css}"); |
| 1423 |
} |
| 1424 |
|
| 1425 |
|
| 1426 |
|
| 1427 |
|
| 1428 |
#[test] |
| 1429 |
fn the_editor_chrome_is_prefixed_and_its_gate_is_not() { |
| 1430 |
let opts = Emit { |
| 1431 |
class_prefix: "mk-", |
| 1432 |
..Emit::default() |
| 1433 |
}; |
| 1434 |
let html = field_html(&field(FieldKind::Rich), &Filling::default(), &opts); |
| 1435 |
assert!(html.contains("class=\"mk-form-editor-modes\""), "{html}"); |
| 1436 |
assert!(html.contains("class=\"mk-form-editor-preview\""), "{html}"); |
| 1437 |
assert!(html.contains("class=\"mk-segment chosen\""), "{html}"); |
| 1438 |
assert!(html.contains("data-format=\"markdown\""), "{html}"); |
| 1439 |
|
| 1440 |
let css = editor_rules(&opts); |
| 1441 |
assert!(css.contains(".mk-form-editor-modes"), "{css}"); |
| 1442 |
assert!(css.contains("[data-format=\"markdown\"]"), "{css}"); |
| 1443 |
} |
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
#[test] |
| 1449 |
fn the_editor_classes_are_in_the_vocabulary() { |
| 1450 |
let opts = Emit::default(); |
| 1451 |
let names = crate::vocabulary::names(&opts); |
| 1452 |
for name in ["form-editor-modes", "form-editor-preview", "segment"] { |
| 1453 |
assert!(names.contains(name), "{name} is not in the vocabulary"); |
| 1454 |
} |
| 1455 |
} |
| 1456 |
|
| 1457 |
#[test] |
| 1458 |
fn the_class_prefix_reaches_the_markup_as_well_as_the_stylesheet() { |
| 1459 |
let opts = Emit { |
| 1460 |
class_prefix: "mk-", |
| 1461 |
..Emit::default() |
| 1462 |
}; |
| 1463 |
let html = field_html(&field(FieldKind::Text), &Filling::default(), &opts); |
| 1464 |
assert!(html.contains("class=\"mk-form-group\""), "{html}"); |
| 1465 |
assert!(html.contains("class=\"mk-field\""), "{html}"); |
| 1466 |
} |
| 1467 |
|
| 1468 |
#[test] |
| 1469 |
fn an_extended_field_says_so_and_leaves_the_disclosure_to_the_form() { |
| 1470 |
let mut f = field(FieldKind::Text); |
| 1471 |
f.extended = true; |
| 1472 |
let html = field_html(&f, &Filling::default(), &Emit::default()); |
| 1473 |
assert!(html.contains("data-extended=\"true\""), "{html}"); |
| 1474 |
} |
| 1475 |
|
| 1476 |
|
| 1477 |
|
| 1478 |
|
| 1479 |
#[test] |
| 1480 |
fn the_id_prefix_scopes_the_id_and_never_the_name() { |
| 1481 |
let mut f = field(FieldKind::Text); |
| 1482 |
f.hint = Some("Keep it short"); |
| 1483 |
f.error = Some("Required"); |
| 1484 |
let filling = Filling { |
| 1485 |
id_prefix: Some("form-modal-task-edit"), |
| 1486 |
..Filling::default() |
| 1487 |
}; |
| 1488 |
let html = field_html(&f, &filling, &Emit::default()); |
| 1489 |
|
| 1490 |
assert!( |
| 1491 |
html.contains(r#"id="form-modal-task-edit-title""#), |
| 1492 |
"{html}" |
| 1493 |
); |
| 1494 |
assert!(html.contains(r#"name="title""#), "{html}"); |
| 1495 |
assert!( |
| 1496 |
!html.contains(r#"name="form-modal-task-edit-title""#), |
| 1497 |
"{html}" |
| 1498 |
); |
| 1499 |
|
| 1500 |
|
| 1501 |
|
| 1502 |
assert!( |
| 1503 |
html.contains(r#"for="form-modal-task-edit-title""#), |
| 1504 |
"{html}" |
| 1505 |
); |
| 1506 |
assert!( |
| 1507 |
html.contains( |
| 1508 |
r#"aria-describedby="form-modal-task-edit-title-hint form-modal-task-edit-title-error""# |
| 1509 |
), |
| 1510 |
"{html}" |
| 1511 |
); |
| 1512 |
assert!( |
| 1513 |
html.contains(r#"id="form-modal-task-edit-title-hint""#), |
| 1514 |
"{html}" |
| 1515 |
); |
| 1516 |
} |
| 1517 |
|
| 1518 |
#[test] |
| 1519 |
fn a_hidden_field_submits_its_bare_name_under_a_prefix() { |
| 1520 |
let filling = Filling { |
| 1521 |
value: Value::Text("42"), |
| 1522 |
id_prefix: Some("scoped"), |
| 1523 |
..Filling::default() |
| 1524 |
}; |
| 1525 |
let html = field_html(&field(FieldKind::Hidden), &filling, &Emit::default()); |
| 1526 |
assert_eq!(html, r#"<input type="hidden" name="title" value="42">"#); |
| 1527 |
} |
| 1528 |
|
| 1529 |
|
| 1530 |
|
| 1531 |
|
| 1532 |
#[test] |
| 1533 |
fn a_constraint_becomes_the_browsers_own_attribute() { |
| 1534 |
|
| 1535 |
|
| 1536 |
|
| 1537 |
let html = field_html( |
| 1538 |
&Field { |
| 1539 |
max_length: Some(100), |
| 1540 |
min: Some("1"), |
| 1541 |
max: Some("240"), |
| 1542 |
required: true, |
| 1543 |
..Field::new(FieldKind::Number, "minutes", "Minutes") |
| 1544 |
}, |
| 1545 |
&Filling::default(), |
| 1546 |
&Emit::default(), |
| 1547 |
); |
| 1548 |
assert!(html.contains(r#"maxlength="100""#)); |
| 1549 |
assert!(html.contains(r#"min="1""#)); |
| 1550 |
assert!(html.contains(r#"max="240""#)); |
| 1551 |
assert!(html.contains(" required")); |
| 1552 |
} |
| 1553 |
|
| 1554 |
#[test] |
| 1555 |
fn a_bound_is_emitted_as_written_and_escaped_like_anything_else() { |
| 1556 |
|
| 1557 |
|
| 1558 |
let html = field_html( |
| 1559 |
&Field { |
| 1560 |
min: Some("2026-08-09T14:30"), |
| 1561 |
..Field::new(FieldKind::Text, "starts", "Starts") |
| 1562 |
}, |
| 1563 |
&Filling::default(), |
| 1564 |
&Emit::default(), |
| 1565 |
); |
| 1566 |
assert!(html.contains(r#"min="2026-08-09T14:30""#)); |
| 1567 |
} |
| 1568 |
|
| 1569 |
#[test] |
| 1570 |
fn a_file_field_is_a_file_input() { |
| 1571 |
|
| 1572 |
|
| 1573 |
|
| 1574 |
let html = field_html( |
| 1575 |
&Field::new(FieldKind::File, "attachment", "Attachment"), |
| 1576 |
&Filling::default(), |
| 1577 |
&Emit::default(), |
| 1578 |
); |
| 1579 |
assert!(html.contains(r#"type="file""#)); |
| 1580 |
assert!(!html.contains("accept=")); |
| 1581 |
assert!(!html.contains("multiple")); |
| 1582 |
|
| 1583 |
|
| 1584 |
assert!(!html.contains("value=")); |
| 1585 |
} |
| 1586 |
|
| 1587 |
#[test] |
| 1588 |
fn an_accept_list_is_comma_joined_in_the_attributes_own_format() { |
| 1589 |
|
| 1590 |
|
| 1591 |
|
| 1592 |
const MIXED: &[Accepted<'_>] = &[ |
| 1593 |
Accepted::Family(Family::Image), |
| 1594 |
Accepted::Type("text/csv"), |
| 1595 |
Accepted::Suffix(".tar.gz"), |
| 1596 |
]; |
| 1597 |
let html = field_html( |
| 1598 |
&Field { |
| 1599 |
multiple: true, |
| 1600 |
..Field::upload("drop", "Drop files", MIXED) |
| 1601 |
}, |
| 1602 |
&Filling::default(), |
| 1603 |
&Emit::default(), |
| 1604 |
); |
| 1605 |
assert!( |
| 1606 |
html.contains(r#"accept="image/*,text/csv,.tar.gz""#), |
| 1607 |
"{html}" |
| 1608 |
); |
| 1609 |
assert!(html.contains(" multiple"), "{html}"); |
| 1610 |
} |
| 1611 |
|
| 1612 |
#[test] |
| 1613 |
fn an_accept_entry_cannot_end_the_attribute_it_sits_in() { |
| 1614 |
|
| 1615 |
|
| 1616 |
|
| 1617 |
const HOSTILE: &[Accepted<'_>] = &[Accepted::Type(r#"image/x" onload="x"#)]; |
| 1618 |
let html = field_html( |
| 1619 |
&Field::upload("cover", "Cover", HOSTILE), |
| 1620 |
&Filling::default(), |
| 1621 |
&Emit::default(), |
| 1622 |
); |
| 1623 |
assert!(!html.contains(r#"onload="x"#), "{html}"); |
| 1624 |
} |
| 1625 |
|
| 1626 |
#[test] |
| 1627 |
fn the_typed_text_kinds_keep_their_input_type() { |
| 1628 |
for (kind, expected) in [ |
| 1629 |
(FieldKind::Email, "email"), |
| 1630 |
(FieldKind::Url, "url"), |
| 1631 |
(FieldKind::Tel, "tel"), |
| 1632 |
(FieldKind::Date, "date"), |
| 1633 |
(FieldKind::DateTime, "datetime-local"), |
| 1634 |
] { |
| 1635 |
let html = field_html(&field(kind), &Filling::default(), &Emit::default()); |
| 1636 |
assert!( |
| 1637 |
html.contains(&format!(r#"type="{expected}""#)), |
| 1638 |
"{kind:?} emitted {html}" |
| 1639 |
); |
| 1640 |
} |
| 1641 |
} |
| 1642 |
|
| 1643 |
#[test] |
| 1644 |
fn a_temporal_field_is_a_native_control_and_not_a_hinted_text_box() { |
| 1645 |
|
| 1646 |
|
| 1647 |
|
| 1648 |
for kind in [FieldKind::Date, FieldKind::DateTime] { |
| 1649 |
let html = field_html(&field(kind), &Filling::default(), &Emit::default()); |
| 1650 |
assert!(!html.contains(r#"type="text""#), "{kind:?} emitted {html}"); |
| 1651 |
} |
| 1652 |
} |
| 1653 |
|
| 1654 |
#[test] |
| 1655 |
fn no_prefix_leaves_the_id_as_the_name() { |
| 1656 |
let html = field_html( |
| 1657 |
&field(FieldKind::Text), |
| 1658 |
&Filling::default(), |
| 1659 |
&Emit::default(), |
| 1660 |
); |
| 1661 |
assert!(html.contains(r#"id="title" name="title""#), "{html}"); |
| 1662 |
} |
| 1663 |
} |
| 1664 |
|