forked from yewstack/yew
-
Notifications
You must be signed in to change notification settings - Fork 0
/
html_component.rs
209 lines (189 loc) · 6.46 KB
/
html_component.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
use proc_macro2::Span;
use quote::{quote, quote_spanned, ToTokens};
use syn::parse::discouraged::Speculative;
use syn::parse::{Parse, ParseStream};
use syn::spanned::Spanned;
use syn::{Token, Type};
use super::{HtmlChildrenTree, TagTokens};
use crate::props::ComponentProps;
pub struct HtmlComponent {
ty: Type,
props: ComponentProps,
children: HtmlChildrenTree,
close: Option<HtmlComponentClose>,
}
impl Parse for HtmlComponent {
fn parse(input: ParseStream) -> syn::Result<Self> {
// check if the next tokens are </
let trying_to_close = || {
let lt = input.peek(Token![<]);
let div = input.peek2(Token![/]);
lt && div
};
if trying_to_close() {
return match input.parse::<HtmlComponentClose>() {
Ok(close) => Err(syn::Error::new_spanned(
close.to_spanned(),
"this closing tag has no corresponding opening tag",
)),
Err(err) => Err(err),
};
}
let open = input.parse::<HtmlComponentOpen>()?;
// Return early if it's a self-closing tag
if open.is_self_closing() {
return Ok(HtmlComponent {
ty: open.ty,
props: open.props,
children: HtmlChildrenTree::new(),
close: None,
});
}
let mut children = HtmlChildrenTree::new();
let close = loop {
if input.is_empty() {
return Err(syn::Error::new_spanned(
open.to_spanned(),
"this opening tag has no corresponding closing tag",
));
}
if trying_to_close() {
let cursor = input.cursor();
let _ = cursor
.punct()
.and_then(|(_, cursor)| cursor.punct())
.and_then(|(_, cursor)| cursor.ident())
.ok_or_else(|| {
syn::Error::new(
Span::call_site(),
"expected a valid closing tag (e.g.: </Component>)",
)
})?;
let fork = input.fork();
let lt = fork.parse::<Token![<]>()?;
let div = Some(fork.parse::<Token![/]>()?);
let ty = fork.parse::<Type>()?;
if ty != open.ty {
fn format_token_stream(ts: impl ToTokens) -> String {
let string = ts.to_token_stream().to_string();
// remove unnecessary spaces
string.replace(' ', "")
}
let open_ty = open.ty;
return Err(syn::Error::new_spanned(
quote!(#open_ty #ty),
format!(
"mismatched closing tags: expected `{}`, found `{}`",
format_token_stream(open_ty),
format_token_stream(ty)
),
));
} else {
let gt = fork.parse::<Token![>]>()?;
let close = HtmlComponentClose {
tag: TagTokens { lt, div, gt },
ty,
};
input.advance_to(&fork);
break close;
}
}
children.parse_child(input)?;
};
if !children.is_empty() {
if let Some(children_prop) = open.props.children() {
return Err(syn::Error::new_spanned(
&children_prop.label,
"cannot specify the `children` prop when the component already has children",
));
}
}
Ok(HtmlComponent {
ty: open.ty,
props: open.props,
children,
close: Some(close),
})
}
}
impl ToTokens for HtmlComponent {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
let Self {
ty,
props,
children,
close,
} = self;
let ty_span = ty.span().resolved_at(Span::call_site());
let props_ty = quote_spanned!(ty_span=> <#ty as ::yew::html::BaseComponent>::Properties);
let children_renderer = if children.is_empty() {
None
} else {
Some(quote! { ::yew::html::ChildrenRenderer::new(#children) })
};
let build_props = props.build_properties_tokens(&props_ty, children_renderer);
let key = props.special().wrap_key_attr();
let use_close_tag = close
.as_ref()
.map(|close| {
let close_ty = &close.ty;
quote_spanned! {close_ty.span()=>
let _ = |_:#close_ty| {};
}
})
.unwrap_or_default();
tokens.extend(quote_spanned! {ty_span=>
{
#use_close_tag
let __yew_props = #build_props;
::yew::virtual_dom::VChild::<#ty>::new(__yew_props, #key)
}
});
}
}
struct HtmlComponentOpen {
tag: TagTokens,
ty: Type,
props: ComponentProps,
}
impl HtmlComponentOpen {
fn is_self_closing(&self) -> bool {
self.tag.div.is_some()
}
fn to_spanned(&self) -> impl ToTokens {
self.tag.to_spanned()
}
}
impl Parse for HtmlComponentOpen {
fn parse(input: ParseStream) -> syn::Result<Self> {
TagTokens::parse_start_content(input, |input, tag| {
let ty = input.parse()?;
let props: ComponentProps = input.parse()?;
if let Some(ref node_ref) = props.special().node_ref {
return Err(syn::Error::new_spanned(
&node_ref.label,
"cannot use `ref` with components. If you want to specify a property, use \
`r#ref` here instead.",
));
}
Ok(Self { tag, ty, props })
})
}
}
struct HtmlComponentClose {
tag: TagTokens,
ty: Type,
}
impl HtmlComponentClose {
fn to_spanned(&self) -> impl ToTokens {
self.tag.to_spanned()
}
}
impl Parse for HtmlComponentClose {
fn parse(input: ParseStream) -> syn::Result<Self> {
TagTokens::parse_end_content(input, |input, tag| {
let ty = input.parse()?;
Ok(Self { tag, ty })
})
}
}