sauron_core/html/attributes/
attribute_macros.rs

1use crate::html::attributes::Value;
2use crate::vdom::attr;
3use crate::vdom::AttributeValue;
4
5/// declare a function with the name corresponds to attribute name for easy usage in html elements
6/// Example:
7/// ```rust,ignore
8/// declare_attributes!{value;}
9/// ```
10/// This will create a function `fn value(){}` which sets the attribute `value` to the element.
11#[macro_export]
12macro_rules! declare_attributes {
13    ( $(
14         $(#[$attr:meta])*
15         $name:ident;
16       )*
17     ) => {
18        $(
19            doc_comment!{
20                concat!("Creates html [",stringify!($name),"](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/",stringify!($name),") attribute"),
21
22                $(#[$attr])*
23                #[inline]
24                #[allow(non_snake_case)]
25                pub fn $name<V, MSG>(v: V) -> $crate::vdom::Attribute<MSG>
26                    where V: Into<Value>,
27                    {
28                        attr(stringify!($name), AttributeValue::from(v.into()))
29                }
30            }
31         )*
32
33    };
34    ( $(
35         $(#[$attr:meta])*
36         $name:ident => $attribute:tt;
37       )*
38     ) => {
39        $(
40            doc_comment!{
41                concat!("Creates html [",$attribute,"](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/",$attribute,") attribute"),
42                $(#[$attr])*
43                #[inline]
44                #[allow(non_snake_case)]
45                pub fn $name<V, MSG>(v: V) -> $crate::vdom::Attribute<MSG>
46                    where V: Into<Value>,
47                    {
48                        attr($attribute, AttributeValue::from(v.into()))
49                }
50             }
51         )*
52    }
53}
54
55/// declare html attributes, at the same time this also
56/// fills up the HTML_ATTRS const with all the common html attributes
57macro_rules! declare_html_attributes{
58    ( $(
59         $(#[$attr:meta])*
60         $name:ident;
61       )*
62     ) => {
63        declare_attributes!{ $($name;)*}
64
65        #[cfg(feature = "with-lookup")]
66        /// These are most commonly used html attributes such as class, id, etc
67        pub const HTML_ATTRS:&[&'static str] = &[$(stringify!($name),)*];
68    }
69}
70
71/// declare html attributes that are non commonly used to avoid conflict with commonly used tags
72/// and names
73macro_rules! declare_html_attributes_non_common{
74    ( $(
75         $(#[$attr:meta])*
76         $name:ident;
77       )*
78     ) => {
79        declare_attributes!{ $($name;)*}
80
81        #[cfg(feature = "with-lookup")]
82        /// These are html attributes with names that are non proper rust identifier therefore
83        /// handled differently. ie: (for, in)
84        pub const HTML_ATTRS_NON_COMMON: &[&'static str] = &[$(stringify!($name),)*];
85    }
86}
87
88/// declare html attributes, at the same time this also
89/// fills up the HTML_ATTRS_SPECIAL const with the html attribute that are not
90/// regular identifiers
91macro_rules! declare_html_attributes_special{
92    ( $(
93         $(#[$attr:meta])*
94         $name:ident : $attribute:tt;
95       )*
96     ) => {
97        declare_attributes!{ $($name => $attribute;)*}
98
99        #[cfg(feature = "with-lookup")]
100        /// These are html attributes with names that are non proper rust identifier therefore
101        /// handled differently. ie: (for, in)
102        pub const HTML_ATTRS_SPECIAL: &[(&'static str,&'static str)] = &[$((stringify!($name),$attribute),)*];
103    }
104}
105
106/// common used html attributes
107pub mod commons {
108    use crate::html::attributes::Value;
109    use crate::vdom::attr;
110    use crate::vdom::AttributeValue;
111    // List from html attributes
112    // https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes
113    declare_html_attributes! {
114        accept;
115        accesskey;
116        action;
117        align;
118        allow;
119        alt;
120        autocapitalize;
121        autocomplete;
122        autofocus;
123        autoplay;
124        background;
125        bgcolor;
126        border;
127        buffered;
128        challenge;
129        charset;
130        class;
131        codebase;
132        color;
133        cols;
134        colspan;
135        content;
136        contenteditable;
137        contextmenu;
138        controls;
139        coords;
140        crossorigin;
141        csp;
142        datetime;
143        decoding;
144        default;
145        defer;
146        dir;
147        dirname;
148        download;
149        draggable;
150        dropzone;
151        enctype;
152        enterkeyhint;
153        formaction;
154        formnovalidate;
155        headers;
156        height;
157        hidden;
158        high;
159        href;
160        hreflang;
161        http;
162        icon;
163        id;
164        importance;
165        integrity;
166        intrinsicsize;
167        inputmode;
168        ismap;
169        itemprop;
170        keytype;
171        kind;
172        lang;
173        language;
174        loading;
175        list;
176        low;
177        manifest;
178        max;
179        maxlength;
180        minlength;
181        media;
182        method;
183        min;
184        multiple;
185        muted;
186        name;
187        novalidate;
188        optimum;
189        ping;
190        placeholder;
191        poster;
192        preload;
193        radiogroup;
194        readonly;
195        referrerpolicy;
196        rel;
197        required;
198        reversed;
199        rows;
200        rowspan;
201        sandbox;
202        scope;
203        scoped;
204        selected;
205        shape;
206        size;
207        sizes;
208        slot;
209        spellcheck;
210        src;
211        srcdoc;
212        srclang;
213        srcset;
214        start;
215        step;
216        tabindex;
217        target;
218        title;
219        translate;
220        usemap;
221        value;
222        width;
223        wrap;
224    }
225}
226
227declare_html_attributes_non_common! {
228    cite; //conflicts with html::tag::cite
229    summary; //conflicts with html::tag::summary
230    pattern; //conflicts with svg::pattern
231    data; //data is a commonly used in local variables
232}
233
234// attributes with dash
235declare_html_attributes_special! {
236    accept_charset : "accept-charset";
237    r#async : "async";
238    r#for : "for";
239    font_family : "font-family";
240    font_size : "font-size";
241    flex_direction : "flex-direction";
242    r#loop : "loop";
243    r#type : "type";
244}