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
extern crate proc_macro;
use syn::{
parse::{ParseStream, Parser as _},
Result,
};
mod node;
mod parser;
pub mod punctuation {
use syn::custom_punctuation;
custom_punctuation!(Dash, -);
}
pub use node::{Node, NodeName, NodeType};
pub use parser::{Parser, ParserConfig};
pub fn parse(tokens: proc_macro::TokenStream, config: Option<ParserConfig>) -> Result<Vec<Node>> {
let parser = move |input: ParseStream| {
let config = config.unwrap_or_else(ParserConfig::default);
Parser::new(config).parse(input)
};
parser.parse(tokens)
}
pub fn parse2(tokens: proc_macro2::TokenStream, config: Option<ParserConfig>) -> Result<Vec<Node>> {
let parser = move |input: ParseStream| {
let config = config.unwrap_or_else(ParserConfig::default);
Parser::new(config).parse(input)
};
parser.parse2(tokens)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_single_empty_element() {
let tokens = quote::quote! {
<foo></foo>
};
let nodes = parse2(tokens, None).unwrap();
assert_eq!(nodes[0].name_as_string().unwrap(), "foo");
}
#[test]
fn test_single_element_with_attributes() {
let tokens = quote::quote! {
<foo bar="moo" baz="42"></foo>
};
let nodes = parse2(tokens, None).unwrap();
let attribute = &nodes[0].attributes[0];
assert_eq!(attribute.name_as_string().unwrap(), "bar");
assert_eq!(attribute.value_as_string().unwrap(), "moo");
}
#[test]
fn test_single_element_with_text() {
let tokens = quote::quote! {
<foo>"bar"</foo>
};
let nodes = parse2(tokens, None).unwrap();
assert_eq!(nodes[0].children[0].value_as_string().unwrap(), "bar");
}
#[test]
fn test_reserved_keyword_attributes() {
let tokens = quote::quote! {
<input type="foo" />
};
let nodes = parse2(tokens, None).unwrap();
assert_eq!(nodes[0].name_as_string().unwrap(), "input");
assert_eq!(nodes[0].attributes[0].name_as_string().unwrap(), "type");
}
#[test]
fn test_block_node() {
let tokens = quote::quote! {
<div>{hello}</div>
};
let nodes = parse2(tokens, None).unwrap();
assert_eq!(nodes[0].children.len(), 1);
}
#[test]
fn test_flat_tree() {
let config = ParserConfig { flatten: true };
let tokens = quote::quote! {
<div>
<div>
<div>{hello}</div>
<div>"world"</div>
</div>
</div>
<div />
};
let nodes = parse2(tokens, Some(config)).unwrap();
assert_eq!(nodes.len(), 7);
}
#[test]
fn test_path_as_tag_name() {
let tokens = quote::quote! {
<some::path />
};
let nodes = parse2(tokens, None).unwrap();
assert_eq!(nodes[0].name_as_string().unwrap(), "some::path");
}
#[test]
fn test_dashed_attribute_name() {
let tokens = quote::quote! {
<div data-foo="bar" />
};
let nodes = parse2(tokens, None).unwrap();
assert_eq!(nodes[0].attributes[0].name_as_string().unwrap(), "data-foo");
}
#[test]
fn test_coloned_attribute_name() {
let tokens = quote::quote! {
<div on:click={foo} />
};
let nodes = parse2(tokens, None).unwrap();
assert_eq!(nodes[0].attributes[0].name_as_string().unwrap(), "on:click");
}
}