acton-htmx 1.0.0-beta.7

Opinionated Rust web framework for HTMX applications
Documentation
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
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
633
634
635
636
637
638
639
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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
//! Template helper functions for HTMX applications
//!
//! Provides utility functions that can be used within Askama templates
//! for common HTMX patterns.
//!
//! # HTMX Attribute Helpers
//!
//! These helpers generate proper HTMX attributes for common operations:
//!
//! ```rust
//! use acton_htmx::template::helpers::*;
//!
//! // Generate HTMX POST form attributes
//! let attrs = hx_post("/api/items", "#item-list", "innerHTML");
//! // Result: hx-post="/api/items" hx-target="#item-list" hx-swap="innerHTML"
//! ```

use crate::auth::session::FlashMessage;
use crate::template::FrameworkTemplates;
use std::sync::OnceLock;

/// Get or initialize the framework templates (lazy singleton)
fn templates() -> &'static FrameworkTemplates {
    static TEMPLATES: OnceLock<FrameworkTemplates> = OnceLock::new();
    TEMPLATES.get_or_init(|| FrameworkTemplates::new().expect("Failed to initialize templates"))
}

/// Generate CSRF token input field
///
/// **DEPRECATED**: Use `csrf_token_with(token)` instead, passing the token from your template context.
///
/// This function returns a placeholder value and should not be used in production.
/// Extract the CSRF token in your handler using the `CsrfToken` extractor and pass it
/// to your template context, then use `csrf_token_with(token)` in your template.
///
/// # Example
///
/// ```rust,ignore
/// use acton_htmx::middleware::CsrfToken;
/// use acton_htmx::template::helpers::csrf_token_with;
///
/// async fn handler(CsrfToken(token): CsrfToken) -> impl IntoResponse {
///     // Pass token to template context
///     MyTemplate { csrf_token: token }
/// }
/// ```
///
/// In your template:
/// ```html
/// {{ csrf_token_with(csrf_token) }}
/// ```
#[deprecated(since = "1.1.0", note = "Use csrf_token_with(token) instead, passing token from CsrfToken extractor")]
#[must_use]
pub fn csrf_token() -> String {
    r#"<input type="hidden" name="_csrf_token" value="placeholder">"#.to_string()
}

/// Generate CSRF token input field with a specific token value
///
/// Used when the token is provided from the session.
///
/// # Panics
///
/// Panics if the CSRF template cannot be rendered. Ensure templates are
/// initialized via `acton-htmx templates init` before using this function.
#[must_use]
pub fn csrf_token_with(token: &str) -> String {
    templates()
        .render("forms/csrf-input.html", minijinja::context! { token => token })
        .expect("Failed to render CSRF token template - run `acton-htmx templates init`")
}

/// Render flash messages as HTML
///
/// Renders a collection of flash messages with appropriate styling and ARIA attributes.
/// Each message is rendered with its level-specific CSS class and can include an optional title.
///
/// The generated HTML includes:
/// - Container div with class `flash-messages`
/// - Individual message divs with level-specific classes (`flash-success`, `flash-info`, etc.)
/// - ARIA role and live region attributes for accessibility
/// - Optional title in a `<strong>` tag
/// - Message text in a `<span>` tag
///
/// # Examples
///
/// ```rust
/// use acton_htmx::auth::session::FlashMessage;
/// use acton_htmx::template::helpers::flash_messages;
///
/// let messages = vec![
///     FlashMessage::success("Profile updated successfully"),
///     FlashMessage::error("Invalid email address"),
/// ];
///
/// let html = flash_messages(&messages);
/// assert!(html.contains("flash-success"));
/// assert!(html.contains("Profile updated"));
/// ```
///
/// In your Askama templates:
/// ```html
/// <!-- Extract flash messages in handler -->
/// {{ flash_messages(messages) }}
/// ```
///
/// # Usage with `FlashExtractor`
///
/// ```rust,ignore
/// use acton_htmx::extractors::FlashExtractor;
/// use acton_htmx::template::helpers::flash_messages;
///
/// async fn handler(FlashExtractor(messages): FlashExtractor) -> impl IntoResponse {
///     MyTemplate { flash_html: flash_messages(&messages) }
/// }
/// ```
///
/// # Panics
///
/// Panics if the flash messages template cannot be rendered. Ensure templates are
/// initialized via `acton-htmx templates init` before using this function.
#[must_use]
pub fn flash_messages(messages: &[FlashMessage]) -> String {
    if messages.is_empty() {
        return String::new();
    }

    // Convert to serializable format for template
    let msgs: Vec<_> = messages
        .iter()
        .map(|m| {
            minijinja::context! {
                css_class => m.css_class(),
                title => m.title.as_deref(),
                message => &m.message,
            }
        })
        .collect();

    templates()
        .render(
            "flash/container.html",
            minijinja::context! {
                container_class => "flash-messages",
                messages => msgs,
            },
        )
        .expect("Failed to render flash messages template - run `acton-htmx templates init`")
}

// Note: The route() helper has been removed as named routes are not currently implemented.
// Use hardcoded paths in your templates instead:
//   href="/posts/{{ post.id }}"
// If named routes are needed in the future, they can be implemented in Phase 3.

/// Generate asset URL with cache busting
///
/// **Note**: Currently returns the path as-is without cache busting.
/// Cache busting implementation is deferred to Phase 3.
///
/// # Current Behavior
///
/// Simply returns the provided path unchanged:
/// ```rust
/// use acton_htmx::template::helpers::asset;
///
/// assert_eq!(asset("/css/styles.css"), "/css/styles.css");
/// ```
///
/// # Recommended Production Approach
///
/// Until cache busting is implemented, use one of these strategies:
///
/// 1. **CDN with query string versioning**: Append a version parameter to assets
///    ```html
///    <link rel="stylesheet" href="{{ asset("/css/styles.css") }}?v=1.2.3">
///    ```
///
/// 2. **Filename-based versioning**: Include version in filename during build
///    ```html
///    <link rel="stylesheet" href="/css/styles.v1.2.3.css">
///    ```
///
/// 3. **HTTP Cache-Control headers**: Configure your static file server with proper caching headers
///    ```
///    Cache-Control: public, max-age=31536000, immutable
///    ```
///
/// # Future Implementation (Phase 3)
///
/// When implemented, this helper will:
/// - Read a manifest file (e.g., `mix-manifest.json`) generated during build
/// - Map logical paths to versioned paths (e.g., `/css/app.css` → `/css/app.abc123.css`)
/// - Support both filename hashing and query string approaches
///
/// Usage in templates: `{{ asset("/css/styles.css") }}`
#[must_use]
pub fn asset(path: &str) -> String {
    path.to_string()
}

// =============================================================================
// HTMX Attribute Helpers
// =============================================================================

/// Generate hx-post attribute with optional target and swap
///
/// # Examples
///
/// ```rust
/// use acton_htmx::template::helpers::hx_post;
///
/// let attrs = hx_post("/api/items", "#list", "innerHTML");
/// assert!(attrs.contains(r#"hx-post="/api/items""#));
/// ```
#[must_use]
pub fn hx_post(url: &str, target: &str, swap: &str) -> String {
    format!(r#"hx-post="{url}" hx-target="{target}" hx-swap="{swap}""#)
}

/// Generate hx-get attribute with optional target and swap
#[must_use]
pub fn hx_get(url: &str, target: &str, swap: &str) -> String {
    format!(r#"hx-get="{url}" hx-target="{target}" hx-swap="{swap}""#)
}

/// Generate hx-put attribute with optional target and swap
#[must_use]
pub fn hx_put(url: &str, target: &str, swap: &str) -> String {
    format!(r#"hx-put="{url}" hx-target="{target}" hx-swap="{swap}""#)
}

/// Generate hx-delete attribute with optional target and swap
#[must_use]
pub fn hx_delete(url: &str, target: &str, swap: &str) -> String {
    format!(r#"hx-delete="{url}" hx-target="{target}" hx-swap="{swap}""#)
}

/// Generate hx-patch attribute with optional target and swap
#[must_use]
pub fn hx_patch(url: &str, target: &str, swap: &str) -> String {
    format!(r#"hx-patch="{url}" hx-target="{target}" hx-swap="{swap}""#)
}

/// Generate hx-trigger attribute
///
/// # Examples
///
/// ```rust
/// use acton_htmx::template::helpers::hx_trigger;
///
/// let attr = hx_trigger("click");
/// assert_eq!(attr, r#"hx-trigger="click""#);
///
/// let attr = hx_trigger("keyup changed delay:500ms");
/// assert!(attr.contains("keyup"));
/// ```
#[must_use]
pub fn hx_trigger(trigger: &str) -> String {
    format!(r#"hx-trigger="{trigger}""#)
}

/// Generate hx-swap attribute
#[must_use]
pub fn hx_swap(strategy: &str) -> String {
    format!(r#"hx-swap="{strategy}""#)
}

/// Generate hx-target attribute
#[must_use]
pub fn hx_target(selector: &str) -> String {
    format!(r#"hx-target="{selector}""#)
}

/// Generate hx-indicator attribute for loading state
#[must_use]
pub fn hx_indicator(selector: &str) -> String {
    format!(r#"hx-indicator="{selector}""#)
}

/// Generate hx-confirm attribute for confirmation dialogs
#[must_use]
pub fn hx_confirm(message: &str) -> String {
    format!(r#"hx-confirm="{message}""#)
}

/// Generate hx-vals attribute for additional values
///
/// # Examples
///
/// ```rust
/// use acton_htmx::template::helpers::hx_vals;
///
/// let attr = hx_vals(r#"{"key": "value"}"#);
/// assert!(attr.contains("hx-vals"));
/// ```
#[must_use]
pub fn hx_vals(json: &str) -> String {
    format!(r"hx-vals='{json}'")
}

/// Generate hx-headers attribute for additional headers
#[must_use]
pub fn hx_headers(json: &str) -> String {
    format!(r"hx-headers='{json}'")
}

/// Generate hx-push-url attribute
#[must_use]
pub fn hx_push_url(url: &str) -> String {
    format!(r#"hx-push-url="{url}""#)
}

/// Generate hx-select attribute for partial selection
#[must_use]
pub fn hx_select(selector: &str) -> String {
    format!(r#"hx-select="{selector}""#)
}

/// Generate hx-select-oob attribute for out-of-band selection
#[must_use]
pub fn hx_select_oob(selector: &str) -> String {
    format!(r#"hx-select-oob="{selector}""#)
}

/// Generate hx-boost="true" for progressively enhanced links
#[must_use]
pub const fn hx_boost() -> &'static str {
    r#"hx-boost="true""#
}

/// Generate hx-disabled-elt attribute for disabling elements during request
#[must_use]
pub fn hx_disabled_elt(selector: &str) -> String {
    format!(r#"hx-disabled-elt="{selector}""#)
}

// =============================================================================
// HTML Safe Output
// =============================================================================

/// HTML-safe string wrapper
///
/// Marks a string as safe for direct HTML output (already escaped).
/// Use this when you have pre-escaped HTML that shouldn't be double-escaped.
#[derive(Debug, Clone)]
pub struct SafeString(pub String);

impl SafeString {
    /// Create a new `SafeString`
    #[must_use]
    pub fn new(s: impl Into<String>) -> Self {
        Self(s.into())
    }
}

impl std::fmt::Display for SafeString {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl From<String> for SafeString {
    fn from(s: String) -> Self {
        Self(s)
    }
}

impl From<&str> for SafeString {
    fn from(s: &str) -> Self {
        Self(s.to_string())
    }
}

// =============================================================================
// HTML Escaping Utilities
// =============================================================================

/// Escape a string for safe use in HTML content
///
/// Escapes special HTML characters to prevent XSS attacks.
/// This is used internally by helpers that generate HTML.
///
/// # Examples
///
/// ```rust
/// use acton_htmx::template::helpers::escape_html;
///
/// assert_eq!(escape_html("<script>alert('xss')</script>"),
///            "&lt;script&gt;alert('xss')&lt;/script&gt;");
/// assert_eq!(escape_html("Hello & goodbye"), "Hello &amp; goodbye");
/// ```
#[must_use]
pub fn escape_html(s: &str) -> String {
    s.replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
}

// =============================================================================
// Validation Error Helpers
// =============================================================================

/// Render validation errors for a specific field
///
/// Returns HTML markup for displaying validation errors next to form fields.
///
/// # Examples
///
/// ```rust
/// use acton_htmx::template::helpers::validation_errors_for;
/// use validator::ValidationErrors;
///
/// let errors = ValidationErrors::new();
/// let html = validation_errors_for(&errors, "email");
/// ```
///
/// # Panics
///
/// Panics if the field errors template cannot be rendered. Ensure templates are
/// initialized via `acton-htmx templates init` before using this function.
#[must_use]
pub fn validation_errors_for(errors: &validator::ValidationErrors, field: &str) -> String {
    errors.field_errors().get(field).map_or_else(String::new, |field_errors| {
        let error_messages: Vec<String> = field_errors
            .iter()
            .map(|error| {
                error.message.as_ref().map_or_else(
                    || format!("{field}: {}", error.code),
                    ToString::to_string,
                )
            })
            .collect();

        templates()
            .render(
                "validation/field-errors.html",
                minijinja::context! {
                    container_class => "field-errors",
                    error_class => "error",
                    errors => error_messages,
                },
            )
            .expect("Failed to render field errors template - run `acton-htmx templates init`")
    })
}

/// Check if a field has validation errors
///
/// Useful for conditionally applying error classes in templates.
///
/// # Examples
///
/// ```rust
/// use acton_htmx::template::helpers::has_error;
/// use validator::ValidationErrors;
///
/// let errors = ValidationErrors::new();
/// let has_err = has_error(&errors, "email");
/// assert!(!has_err);
/// ```
#[must_use]
pub fn has_error(errors: &validator::ValidationErrors, field: &str) -> bool {
    errors.field_errors().contains_key(field)
}

/// Get error class string if field has errors
///
/// Returns " error" or " is-invalid" if the field has errors, empty string otherwise.
/// Useful for conditionally applying CSS classes.
///
/// # Examples
///
/// ```rust
/// use acton_htmx::template::helpers::error_class;
/// use validator::ValidationErrors;
///
/// let mut errors = ValidationErrors::new();
/// errors.add("email", validator::ValidationError::new("email"));
///
/// let class = error_class(&errors, "email");
/// assert_eq!(class, " error");
/// ```
#[must_use]
pub fn error_class(errors: &validator::ValidationErrors, field: &str) -> &'static str {
    if has_error(errors, field) {
        " error"
    } else {
        ""
    }
}

/// Render all validation errors as an unordered list
///
/// Useful for displaying all errors at the top of a form.
///
/// # Examples
///
/// ```rust
/// use acton_htmx::template::helpers::validation_errors_list;
/// use validator::ValidationErrors;
///
/// let errors = ValidationErrors::new();
/// let html = validation_errors_list(&errors);
/// ```
///
/// # Panics
///
/// Panics if the validation summary template cannot be rendered. Ensure templates are
/// initialized via `acton-htmx templates init` before using this function.
#[must_use]
pub fn validation_errors_list(errors: &validator::ValidationErrors) -> String {
    if errors.is_empty() {
        return String::new();
    }

    let error_messages: Vec<String> = errors
        .field_errors()
        .iter()
        .flat_map(|(field, field_errors)| {
            field_errors.iter().map(move |error| {
                error.message.as_ref().map_or_else(
                    || format!("{field}: {}", error.code),
                    ToString::to_string,
                )
            })
        })
        .collect();

    templates()
        .render(
            "validation/validation-summary.html",
            minijinja::context! {
                container_class => "validation-errors",
                errors => error_messages,
            },
        )
        .expect("Failed to render validation summary template - run `acton-htmx templates init`")
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    #[allow(deprecated)]
    fn test_csrf_token() {
        let token = csrf_token();
        assert!(token.contains("_csrf_token"));
        assert!(token.contains("hidden"));
    }

    #[test]
    fn test_csrf_token_with_value() {
        let token = csrf_token_with("abc123");
        assert!(token.contains(r#"value="abc123""#));
    }

    #[test]
    fn test_asset() {
        let path = asset("/css/styles.css");
        assert_eq!(path, "/css/styles.css");
    }

    #[test]
    fn test_hx_post() {
        let attrs = hx_post("/api/items", "#list", "innerHTML");
        assert!(attrs.contains("hx-post=\"/api/items\""));
        assert!(attrs.contains("hx-target=\"#list\""));
        assert!(attrs.contains("hx-swap=\"innerHTML\""));
    }

    #[test]
    fn test_hx_get() {
        let attrs = hx_get("/search", "#results", "outerHTML");
        assert!(attrs.contains(r#"hx-get="/search""#));
    }

    #[test]
    fn test_hx_trigger() {
        let attr = hx_trigger("click");
        assert_eq!(attr, r#"hx-trigger="click""#);
    }

    #[test]
    fn test_hx_confirm() {
        let attr = hx_confirm("Are you sure?");
        assert!(attr.contains("Are you sure?"));
    }

    #[test]
    fn test_hx_boost() {
        assert_eq!(hx_boost(), r#"hx-boost="true""#);
    }

    #[test]
    fn test_safe_string() {
        let safe = SafeString::new("<p>Hello</p>");
        assert_eq!(format!("{safe}"), "<p>Hello</p>");
    }

    #[test]
    fn test_safe_string_from() {
        let safe: SafeString = "test".into();
        assert_eq!(safe.0, "test");
    }

    #[test]
    fn test_validation_errors_for() {
        let mut errors = validator::ValidationErrors::new();
        errors.add(
            "email",
            validator::ValidationError::new("email")
                .with_message(std::borrow::Cow::Borrowed("Invalid email")),
        );

        let html = validation_errors_for(&errors, "email");
        assert!(html.contains("Invalid email"));
        assert!(html.contains("field-errors"));
    }

    #[test]
    fn test_validation_errors_for_no_errors() {
        let errors = validator::ValidationErrors::new();
        let html = validation_errors_for(&errors, "email");
        assert!(html.is_empty());
    }

    #[test]
    fn test_has_error() {
        let mut errors = validator::ValidationErrors::new();
        errors.add("email", validator::ValidationError::new("email"));

        assert!(has_error(&errors, "email"));
        assert!(!has_error(&errors, "password"));
    }

    #[test]
    fn test_error_class() {
        let mut errors = validator::ValidationErrors::new();
        errors.add("email", validator::ValidationError::new("email"));

        assert_eq!(error_class(&errors, "email"), " error");
        assert_eq!(error_class(&errors, "password"), "");
    }

    #[test]
    fn test_validation_errors_list() {
        let mut errors = validator::ValidationErrors::new();
        errors.add(
            "email",
            validator::ValidationError::new("email")
                .with_message(std::borrow::Cow::Borrowed("Invalid email")),
        );
        errors.add(
            "password",
            validator::ValidationError::new("length")
                .with_message(std::borrow::Cow::Borrowed("Too short")),
        );

        let html = validation_errors_list(&errors);
        assert!(html.contains("Invalid email"));
        assert!(html.contains("Too short"));
        assert!(html.contains("<ul>"));
    }

    #[test]
    fn test_validation_errors_list_empty() {
        let errors = validator::ValidationErrors::new();
        let html = validation_errors_list(&errors);
        assert!(html.is_empty());
    }

    #[test]
    fn test_flash_messages_empty() {
        let messages: Vec<FlashMessage> = vec![];
        let html = flash_messages(&messages);
        assert!(html.is_empty());
    }

    #[test]
    fn test_flash_messages_single() {
        use crate::auth::session::FlashMessage;

        let messages = vec![FlashMessage::success("Operation successful")];
        let html = flash_messages(&messages);

        assert!(html.contains("flash-messages"));
        assert!(html.contains("flash-success"));
        assert!(html.contains("Operation successful"));
        assert!(html.contains("role=\"alert\""));
        assert!(html.contains("role=\"status\""));
    }

    #[test]
    fn test_flash_messages_multiple_levels() {
        use crate::auth::session::FlashMessage;

        let messages = vec![
            FlashMessage::success("Success message"),
            FlashMessage::error("Error message"),
            FlashMessage::warning("Warning message"),
            FlashMessage::info("Info message"),
        ];
        let html = flash_messages(&messages);

        assert!(html.contains("flash-success"));
        assert!(html.contains("flash-error"));
        assert!(html.contains("flash-warning"));
        assert!(html.contains("flash-info"));
        assert!(html.contains("Success message"));
        assert!(html.contains("Error message"));
        assert!(html.contains("Warning message"));
        assert!(html.contains("Info message"));
    }

    #[test]
    fn test_flash_messages_with_title() {
        use crate::auth::session::FlashMessage;

        let messages = vec![
            FlashMessage::success("Message text").with_title("Success!"),
        ];
        let html = flash_messages(&messages);

        assert!(html.contains("<strong>Success!</strong>"));
        assert!(html.contains("Message text"));
    }

    #[test]
    fn test_flash_messages_xss_protection() {
        use crate::auth::session::FlashMessage;

        let messages = vec![
            FlashMessage::error("<script>alert('xss')</script>"),
        ];
        let html = flash_messages(&messages);

        // Should be escaped
        assert!(html.contains("&lt;script&gt;"));
        assert!(!html.contains("<script>"));
    }

    #[test]
    fn test_escape_html() {
        assert_eq!(escape_html("Hello, world!"), "Hello, world!");
        assert_eq!(escape_html("<script>"), "&lt;script&gt;");
        assert_eq!(escape_html("A & B"), "A &amp; B");
        assert_eq!(escape_html("<div>content</div>"), "&lt;div&gt;content&lt;/div&gt;");
        assert_eq!(
            escape_html("<script>alert('xss')</script>"),
            "&lt;script&gt;alert('xss')&lt;/script&gt;"
        );
    }

    #[test]
    fn test_escape_html_preserves_safe_chars() {
        assert_eq!(escape_html("Hello 123 !@#$%^*()_+-=[]{}|;:',./? "),
                   "Hello 123 !@#$%^*()_+-=[]{}|;:',./? ");
    }
}