Skip to main content

euv_core/vdom/attribute/
impl.rs

1use crate::*;
2
3/// Visual equality comparison for attribute values.
4///
5/// Compares values by their visual output rather than identity. `Signal`
6/// values are compared by their current resolved string, `Event` values
7/// are always considered equal (re-binding is handled by the handler
8/// registry), and `CssClass` values are compared by class name.
9impl PartialEq for AttributeValue {
10    /// Compares two attribute values for visual equality.
11    ///
12    /// # Arguments
13    ///
14    /// - `&Self` - The first attribute value.
15    /// - `&Self` - The second attribute value.
16    ///
17    /// # Returns
18    ///
19    /// - `bool` - `true` if the values are visually equal.
20    fn eq(&self, other: &Self) -> bool {
21        match (self, other) {
22            (AttributeValue::Text(a_val), AttributeValue::Text(b_val)) => a_val == b_val,
23            (AttributeValue::Signal(a_sig), AttributeValue::Signal(b_sig)) => {
24                a_sig.get() == b_sig.get()
25            }
26            (AttributeValue::Signal(a_sig), AttributeValue::Text(b_val)) => a_sig.get() == *b_val,
27            (AttributeValue::Text(a_val), AttributeValue::Signal(b_sig)) => *a_val == b_sig.get(),
28            (AttributeValue::Event(_), AttributeValue::Event(_)) => true,
29            (AttributeValue::Css(a_css), AttributeValue::Css(b_css)) => {
30                a_css.get_name() == b_css.get_name()
31            }
32            (AttributeValue::Dynamic(a_dyn), AttributeValue::Dynamic(b_dyn)) => a_dyn == b_dyn,
33            _ => false,
34        }
35    }
36}
37
38/// Visual equality comparison for attribute entries.
39///
40/// Two attribute entries are equal when their names match and their values
41/// are visually equal as defined by `AttributeValue::eq`.
42impl PartialEq for AttributeEntry {
43    /// Compares two attribute entries for visual equality.
44    ///
45    /// # Arguments
46    ///
47    /// - `&Self` - The first attribute entry.
48    /// - `&Self` - The second attribute entry.
49    ///
50    /// # Returns
51    ///
52    /// - `bool` - `true` if both names and values match.
53    fn eq(&self, other: &Self) -> bool {
54        self.get_name() == other.get_name() && self.get_value() == other.get_value()
55    }
56}
57
58/// Visual equality comparison for CSS classes.
59///
60/// Two CSS classes are considered equal when their class names match,
61/// since the name uniquely identifies the visual style rule.
62impl PartialEq for CssClass {
63    /// Compares two CSS classes by name.
64    ///
65    /// # Arguments
66    ///
67    /// - `&Self` - The first CSS class.
68    /// - `&Self` - The second CSS class.
69    ///
70    /// # Returns
71    ///
72    /// - `bool` - `true` if the class names match.
73    fn eq(&self, other: &Self) -> bool {
74        self.get_name() == other.get_name()
75    }
76}
77
78/// Maps each `Attribute` variant to its corresponding DOM attribute string.
79impl Attribute {
80    /// Returns the string representation of this attribute name for DOM binding.
81    ///
82    /// Static variants return `Cow::Borrowed` (zero allocation), while
83    /// `Data` and `Other` variants return `Cow::Owned` (heap allocation).
84    ///
85    /// # Returns
86    ///
87    /// - `Cow<'static, str>` - The attribute name as a static or owned string.
88    pub fn as_str(&self) -> Cow<'static, str> {
89        match self {
90            Attribute::AccessKey => Cow::Borrowed("accesskey"),
91            Attribute::Action => Cow::Borrowed("action"),
92            Attribute::Alt => Cow::Borrowed("alt"),
93            Attribute::AriaLabel => Cow::Borrowed("aria-label"),
94            Attribute::AutoComplete => Cow::Borrowed("autocomplete"),
95            Attribute::AutoFocus => Cow::Borrowed("autofocus"),
96            Attribute::Checked => Cow::Borrowed("checked"),
97            Attribute::Class => Cow::Borrowed("class"),
98            Attribute::Cols => Cow::Borrowed("cols"),
99            Attribute::ContentEditable => Cow::Borrowed("contenteditable"),
100            Attribute::Data(name) => Cow::Owned(format!("data-{}", name)),
101            Attribute::Dir => Cow::Borrowed("dir"),
102            Attribute::Disabled => Cow::Borrowed("disabled"),
103            Attribute::Draggable => Cow::Borrowed("draggable"),
104            Attribute::EncType => Cow::Borrowed("enctype"),
105            Attribute::For => Cow::Borrowed("for"),
106            Attribute::Form => Cow::Borrowed("form"),
107            Attribute::Height => Cow::Borrowed("height"),
108            Attribute::Hidden => Cow::Borrowed("hidden"),
109            Attribute::Href => Cow::Borrowed("href"),
110            Attribute::Id => Cow::Borrowed("id"),
111            Attribute::Lang => Cow::Borrowed("lang"),
112            Attribute::Max => Cow::Borrowed("max"),
113            Attribute::MaxLength => Cow::Borrowed("maxlength"),
114            Attribute::Method => Cow::Borrowed("method"),
115            Attribute::Min => Cow::Borrowed("min"),
116            Attribute::MinLength => Cow::Borrowed("minlength"),
117            Attribute::Multiple => Cow::Borrowed("multiple"),
118            Attribute::Name => Cow::Borrowed("name"),
119            Attribute::Pattern => Cow::Borrowed("pattern"),
120            Attribute::Placeholder => Cow::Borrowed("placeholder"),
121            Attribute::ReadOnly => Cow::Borrowed("readonly"),
122            Attribute::Required => Cow::Borrowed("required"),
123            Attribute::Rows => Cow::Borrowed("rows"),
124            Attribute::Selected => Cow::Borrowed("selected"),
125            Attribute::Size => Cow::Borrowed("size"),
126            Attribute::SpellCheck => Cow::Borrowed("spellcheck"),
127            Attribute::Src => Cow::Borrowed("src"),
128            Attribute::Step => Cow::Borrowed("step"),
129            Attribute::Style => Cow::Borrowed("style"),
130            Attribute::TabIndex => Cow::Borrowed("tabindex"),
131            Attribute::Target => Cow::Borrowed("target"),
132            Attribute::Title => Cow::Borrowed("title"),
133            Attribute::Type => Cow::Borrowed("type"),
134            Attribute::Value => Cow::Borrowed("value"),
135            Attribute::Width => Cow::Borrowed("width"),
136            Attribute::Other(name) => Cow::Owned(name.clone()),
137        }
138    }
139}