use pagetop::prelude::*; // Every way an element of `html!` gets its `class` attribute: literal `class="..."`, shorthand // classes (`.a`, `.a[cond]`, `.(expr)`), both together, and the classes of a `Props` splice. // **< Literal class attribute >******************************************************************** #[pagetop::test] async fn literal_class_attribute() { assert_eq!( html! { div class="a b" {} }.into_string(), r#"
"# ); } #[pagetop::test] async fn literal_class_attribute_from_expression_is_escaped() { let value = "a\"b"; assert_eq!( html! { div class=(value) {} }.into_string(), r#""# ); } #[pagetop::test] async fn optional_class_attribute() { let some = Some("a"); let none: Option<&str> = None; assert_eq!( html! { div class=[some] {} }.into_string(), r#""# ); assert_eq!(html! { div class=[none] {} }.into_string(), ""); } // **< Shorthand classes >************************************************************************** #[pagetop::test] async fn shorthand_classes() { assert_eq!( html! { div.a.b {} }.into_string(), r#""# ); } #[pagetop::test] async fn shorthand_classes_without_element_name_are_a_div() { assert_eq!( html! { .a.b {} }.into_string(), r#""# ); } #[pagetop::test] async fn shorthand_toggled_classes() { assert_eq!( html! { div.a.b[true].c[false] {} }.into_string(), r#""# ); } #[pagetop::test] async fn shorthand_toggled_classes_go_after_the_fixed_ones() { // Toggled classes are written after all the fixed ones, whatever their order in the source. assert_eq!( html! { div.a[true].b {} }.into_string(), r#""# ); } #[pagetop::test] async fn shorthand_only_toggled_classes() { assert_eq!( html! { div.a[true].b[true] {} }.into_string(), r#""# ); // A leading space when the first toggled class is off: harmless, browsers ignore it. assert_eq!( html! { div.a[false].b[true] {} }.into_string(), r#""# ); assert_eq!( html! { div.a[false] {} }.into_string(), r#""# ); } #[pagetop::test] async fn shorthand_class_from_expression() { let name = "dynamic"; assert_eq!( html! { div.a.(name) {} }.into_string(), r#""# ); assert_eq!( html! { div.a.(name)[false] {} }.into_string(), r#""# ); } // **< Literal and shorthand classes together >***************************************************** #[pagetop::test] async fn literal_and_shorthand_classes_merge_into_one_attribute() { assert_eq!( html! { div class="a" .b {} }.into_string(), r#""# ); assert_eq!( html! { div class="a b" .c.d {} }.into_string(), r#""# ); } #[pagetop::test] async fn literal_class_goes_first_whatever_its_position() { assert_eq!( html! { div.b class="a" {} }.into_string(), r#""# ); } #[pagetop::test] async fn literal_and_toggled_classes_merge() { assert_eq!( html! { li class="item" .active[true] {} }.into_string(), r#""# ); assert_eq!( html! { li class="item" .active[false] {} }.into_string(), r#""# ); assert_eq!( html! { li class="item" .active[true].disabled[false].first {} }.into_string(), r#""# ); } #[pagetop::test] async fn class_from_expression_and_shorthand_classes_merge() { let base = "card"; assert_eq!( html! { div class=(base) .shadow {} }.into_string(), r#""# ); let escaped = "a\"b"; assert_eq!( html! { div class=(escaped) .c[true] {} }.into_string(), r#""# ); } #[pagetop::test] async fn optional_class_and_shorthand_classes_merge() { let some = Some("a"); let none: Option<&str> = None; assert_eq!( html! { div class=[some] .b {} }.into_string(), r#""# ); // A leading space when the optional value is missing: harmless, browsers ignore it. assert_eq!( html! { div class=[none] .b {} }.into_string(), r#""# ); } #[pagetop::test] async fn merged_class_keeps_the_order_of_the_other_attributes() { // `class` is written first, then `id`, then the rest in source order, as always. assert_eq!( html! { a href="/" class="link" #home .active[true] title="Home" { "x" } }.into_string(), r#"x"# ); } #[pagetop::test] async fn merged_class_works_with_conditional_markup() { let items = ["a", "b", "c"]; let current = 1; let markup = html! { ul { @for (i, item) in items.iter().enumerate() { li class="item" .current[i == current] { (item) } } } }; assert_eq!( markup.into_string(), r#"