sauron_core/vdom/attribute/
special.rs

1//! special attributes which is treated differently
2//!
3//!
4use super::{attr, Attribute, Value};
5use crate::vdom::AttributeName;
6
7/// Special Node attributes that are treated differently
8/// such as key and skip which both greatly affects the diffing algorithm
9
10/// NOTE: this is specific to sauron framework
11/// The key attribute
12pub static KEY: &AttributeName = &"key";
13
14/// NOTE: this is specific to sauron framework
15/// The replace attribute
16pub static REPLACE: &AttributeName = &"replace";
17
18/// NOTE: this is specific to sauron framework
19/// The skip attribute
20pub static SKIP: &AttributeName = &"skip";
21
22/// NOTE: this is specific to sauron framework
23/// The skip criteria attribute
24pub static SKIP_CRITERIA: &AttributeName = &"skip_criteria";
25
26///
27/// NOTE: The following attributes have special behaviour in the dom, the framework
28/// need to call the specific methods to reflect the state of this attribute to the element
29///
30/// the value attribute
31#[cfg(feature = "ensure-attr-set")]
32pub static VALUE: &AttributeName = &"value";
33/// the open attribute
34#[cfg(feature = "ensure-attr-set")]
35pub static OPEN: &AttributeName = &"open";
36/// the checked attribute
37#[cfg(feature = "ensure-attr-set")]
38pub static CHECKED: &AttributeName = &"checked";
39/// the disabled attribute
40#[cfg(feature = "ensure-attr-set")]
41pub static DISABLED: &AttributeName = &"disabled";
42
43/// creates a key attribute using a formatter
44/// # Examples
45/// ```rust
46/// use sauron::{*, html::{*, attributes::*}};
47///
48/// let number = 42;
49/// let button:Node<()> = button([key!("content-{}", 42)], [text("Click")]);
50///
51/// assert_eq!(node!{<button key=format!("content-42")>"Click"</button>}, button);
52/// ```
53#[macro_export]
54macro_rules! key {
55    ( $($arg: tt)* ) => {
56        $crate::html::attributes::key(format!($($arg)*))
57    };
58}
59
60/// key attributes is used to match
61/// old element and new element when diffing
62pub fn key<V, MSG>(v: V) -> Attribute<MSG>
63where
64    V: Into<Value>,
65{
66    attr(KEY, v)
67}
68
69/// if the value is true, then the diffing of this element
70/// and its descendants are skip entirely
71pub fn skip<MSG>(v: bool) -> Attribute<MSG> {
72    attr(SKIP, v)
73}
74
75/// if the value of this attribute of the old element and the new element is the same
76/// the diffing of this element and its descentdants are skip entirely
77pub fn skip_criteria<V, MSG>(v: V) -> Attribute<MSG>
78where
79    V: Into<Value>,
80{
81    attr(SKIP_CRITERIA, v.into())
82}
83
84/// if the value is true, then this node is made to replace the old
85/// node it matches
86pub fn replace<MSG>(v: bool) -> Attribute<MSG> {
87    attr(REPLACE, v)
88}