Skip to main content

euv_core/vdom/style/
impl.rs

1use crate::*;
2
3/// Implementation of style CSS serialization.
4impl Style {
5    /// Adds a style property.
6    ///
7    /// Property names are automatically converted from snake_case to kebab-case
8    /// (e.g., `flex_direction` becomes `flex-direction`).
9    ///
10    /// # Arguments
11    ///
12    /// - `N` - The property name (snake_case will be converted to kebab-case).
13    /// - `V` - The property value.
14    ///
15    /// # Returns
16    ///
17    /// - `Self` - This style with the property added.
18    pub fn property<N, V>(mut self, name: N, value: V) -> Self
19    where
20        N: AsRef<str>,
21        V: AsRef<str>,
22    {
23        self.get_mut_properties().push(StyleProperty::new(
24            name.as_ref().replace('_', "-"),
25            value.as_ref().to_string(),
26        ));
27        self
28    }
29
30    /// Converts the style to a CSS string.
31    ///
32    /// # Returns
33    ///
34    /// - `String` - The CSS string representation.
35    pub fn to_css_string(&self) -> String {
36        self.get_properties()
37            .iter()
38            .map(|p| format!("{}: {};", p.get_name(), p.get_value()))
39            .collect::<Vec<String>>()
40            .join(" ")
41    }
42}
43
44/// Provides a default empty style.
45impl Default for Style {
46    /// Returns a default `Style` with no properties.
47    ///
48    /// # Returns
49    ///
50    /// - `Self` - An empty style.
51    fn default() -> Self {
52        Self::new(Vec::new())
53    }
54}