forked from yewstack/yew
/
tag_attributes.rs
210 lines (196 loc) · 6.08 KB
/
tag_attributes.rs
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
use crate::html_tree::HtmlProp as TagAttribute;
use crate::PeekValue;
use lazy_static::lazy_static;
use std::collections::HashSet;
use std::iter::FromIterator;
use syn::parse::{Parse, ParseStream, Result as ParseResult};
use syn::{Expr, ExprTuple};
pub struct TagAttributes {
pub attributes: Vec<TagAttribute>,
pub listeners: Vec<TagAttribute>,
pub classes: Option<ClassesForm>,
pub booleans: Vec<TagAttribute>,
pub value: Option<Expr>,
pub kind: Option<Expr>,
pub checked: Option<Expr>,
pub node_ref: Option<Expr>,
pub key: Option<Expr>,
pub href: Option<Expr>,
}
pub enum ClassesForm {
Tuple(Vec<Expr>),
Single(Box<Expr>),
}
lazy_static! {
static ref BOOLEAN_SET: HashSet<&'static str> = {
HashSet::from_iter(
vec![
"async",
"autofocus",
"controls",
"default",
"defer",
"disabled",
"hidden",
"ismap",
"loop",
"multiple",
"muted",
"novalidate",
"open",
"readonly",
"required",
"selected",
]
.into_iter(),
)
};
}
lazy_static! {
static ref LISTENER_SET: HashSet<&'static str> = {
HashSet::from_iter(
vec![
"onclick",
"ondoubleclick",
"onkeypress",
"onkeydown",
"onkeyup",
"onmousedown",
"onmousemove",
"onmouseout",
"onmouseenter",
"onmouseleave",
"onmousewheel",
"onmouseover",
"onmouseup",
"ontouchcancel",
"ontouchend",
"ontouchenter",
"ontouchmove",
"ontouchstart",
"ongotpointercapture",
"onlostpointercapture",
"onpointercancel",
"onpointerdown",
"onpointerenter",
"onpointerleave",
"onpointermove",
"onpointerout",
"onpointerover",
"onpointerup",
"onscroll",
"onblur",
"onfocus",
"onsubmit",
"oninput",
"onchange",
"ondrag",
"ondragstart",
"ondragend",
"ondragenter",
"ondragleave",
"ondragover",
"ondragexit",
"ondrop",
"oncontextmenu",
]
.into_iter(),
)
};
}
impl TagAttributes {
fn drain_listeners(attrs: &mut Vec<TagAttribute>) -> Vec<TagAttribute> {
let mut i = 0;
let mut drained = Vec::new();
while i < attrs.len() {
let name_str = attrs[i].label.to_string();
if LISTENER_SET.contains(&name_str.as_str()) {
drained.push(attrs.remove(i));
} else {
i += 1;
}
}
drained
}
fn drain_boolean(attrs: &mut Vec<TagAttribute>) -> Vec<TagAttribute> {
let mut i = 0;
let mut drained = Vec::new();
while i < attrs.len() {
let name_str = attrs[i].label.to_string();
if BOOLEAN_SET.contains(&name_str.as_str()) {
drained.push(attrs.remove(i));
} else {
i += 1;
}
}
drained
}
fn remove_attr(attrs: &mut Vec<TagAttribute>, name: &str) -> Option<Expr> {
let mut i = 0;
while i < attrs.len() {
if attrs[i].label.to_string() == name {
return Some(attrs.remove(i).value);
} else {
i += 1;
}
}
None
}
fn map_classes(class_expr: Expr) -> ClassesForm {
match class_expr {
Expr::Tuple(ExprTuple { elems, .. }) => ClassesForm::Tuple(elems.into_iter().collect()),
expr => ClassesForm::Single(Box::new(expr)),
}
}
}
impl Parse for TagAttributes {
fn parse(input: ParseStream) -> ParseResult<Self> {
let mut attributes: Vec<TagAttribute> = Vec::new();
while TagAttribute::peek(input.cursor()).is_some() {
attributes.push(input.parse::<TagAttribute>()?);
}
let mut listeners = Vec::new();
for listener in TagAttributes::drain_listeners(&mut attributes) {
listeners.push(listener);
}
// Multiple listener attributes are allowed, but no others
attributes.sort_by(|a, b| {
a.label
.to_string()
.partial_cmp(&b.label.to_string())
.unwrap()
});
let mut i = 0;
while i + 1 < attributes.len() {
if attributes[i].label.to_string() == attributes[i + 1].label.to_string() {
let label = &attributes[i + 1].label;
return Err(syn::Error::new_spanned(
label,
format!("only one `{}` attribute allowed", label),
));
}
i += 1;
}
let booleans = TagAttributes::drain_boolean(&mut attributes);
let classes =
TagAttributes::remove_attr(&mut attributes, "class").map(TagAttributes::map_classes);
let value = TagAttributes::remove_attr(&mut attributes, "value");
let kind = TagAttributes::remove_attr(&mut attributes, "type");
let checked = TagAttributes::remove_attr(&mut attributes, "checked");
let node_ref = TagAttributes::remove_attr(&mut attributes, "ref");
let key = TagAttributes::remove_attr(&mut attributes, "key");
let href = TagAttributes::remove_attr(&mut attributes, "href");
Ok(TagAttributes {
attributes,
classes,
listeners,
checked,
booleans,
value,
kind,
node_ref,
href,
key,
})
}
}