Skip to main content

leptos_css/
view_transition.rs

1use std::fmt;
2
3use crate::checked_value::impl_checked_value;
4use crate::{identifier::is_checked_identifier, serialization::CssWriteTo};
5
6/// Error returned when a view-transition name is not in the checked `<custom-ident>` subset.
7#[derive(Clone, Debug, PartialEq, Eq)]
8pub struct InvalidViewTransitionName {
9    name: String,
10}
11
12impl InvalidViewTransitionName {
13    /// Return the rejected identifier.
14    pub fn name(&self) -> &str {
15        &self.name
16    }
17}
18
19impl fmt::Display for InvalidViewTransitionName {
20    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21        write!(
22            f,
23            "view-transition-name must be a checked custom identifier other than none, auto, default, or a CSS-wide keyword, got {:?}",
24            self.name
25        )
26    }
27}
28
29impl std::error::Error for InvalidViewTransitionName {}
30
31fn is_reserved_custom_identifier(identifier: &str) -> bool {
32    [
33        "default",
34        "inherit",
35        "initial",
36        "revert",
37        "revert-layer",
38        "unset",
39    ]
40    .iter()
41    .any(|reserved| identifier.eq_ignore_ascii_case(reserved))
42}
43
44/// A checked `view-transition-name` value.
45///
46/// Names use a strict ASCII `<custom-ident>` subset and exclude the property-specific `none` and
47/// `auto` keywords. The private representation prevents constructing a reserved identifier.
48///
49/// [CSS View Transitions ยง 2.1]: https://www.w3.org/TR/css-view-transitions-1/#view-transition-name-property
50#[derive(Clone, Debug, PartialEq, Eq, Hash)]
51pub struct ViewTransitionName(ViewTransitionNameValue);
52
53#[derive(Clone, Debug, PartialEq, Eq, Hash)]
54enum ViewTransitionNameValue {
55    None,
56    Named(String),
57}
58
59impl ViewTransitionName {
60    /// Construct the `none` keyword.
61    pub const fn none() -> Self {
62        Self(ViewTransitionNameValue::None)
63    }
64
65    /// Construct a checked named view transition.
66    ///
67    /// # Panics
68    ///
69    /// Panics if `name` is not in the checked identifier subset or is reserved by CSS.
70    #[track_caller]
71    pub fn named(name: impl Into<String>) -> Self {
72        Self::try_named(name).unwrap_or_else(|error| panic!("{error}"))
73    }
74
75    /// Attempt to construct a checked named view transition.
76    pub fn try_named(name: impl Into<String>) -> Result<Self, InvalidViewTransitionName> {
77        let name = name.into();
78        let property_reserved =
79            name.eq_ignore_ascii_case("none") || name.eq_ignore_ascii_case("auto");
80        if is_checked_identifier(&name)
81            && !is_reserved_custom_identifier(&name)
82            && !property_reserved
83        {
84            Ok(Self(ViewTransitionNameValue::Named(name)))
85        } else {
86            Err(InvalidViewTransitionName { name })
87        }
88    }
89}
90
91impl Default for ViewTransitionName {
92    fn default() -> Self {
93        Self::none()
94    }
95}
96
97impl CssWriteTo for ViewTransitionName {
98    fn css_fmt<W: fmt::Write>(&self, w: &mut W) -> fmt::Result {
99        match &self.0 {
100            ViewTransitionNameValue::None => w.write_str("none"),
101            ViewTransitionNameValue::Named(name) => w.write_str(name),
102        }
103    }
104}
105
106impl_checked_value!(ViewTransitionName);