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