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