-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathhtml_element.rs
703 lines (640 loc) · 25.1 KB
/
html_element.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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
use proc_macro2::{Delimiter, Group, Span, TokenStream};
use proc_macro_error2::emit_warning;
use quote::{quote, quote_spanned, ToTokens};
use syn::buffer::Cursor;
use syn::parse::{Parse, ParseStream};
use syn::spanned::Spanned;
use syn::{Expr, Ident, Lit, LitStr, Token};
use super::{HtmlChildrenTree, HtmlDashedName, TagTokens};
use crate::props::{ElementProps, Prop, PropDirective};
use crate::stringify::{Stringify, Value};
use crate::{is_ide_completion, non_capitalized_ascii, Peek, PeekValue};
pub struct HtmlElement {
pub name: TagName,
pub props: ElementProps,
pub children: HtmlChildrenTree,
}
impl PeekValue<()> for HtmlElement {
fn peek(cursor: Cursor) -> Option<()> {
HtmlElementOpen::peek(cursor)
.or_else(|| HtmlElementClose::peek(cursor))
.map(|_| ())
}
}
impl Parse for HtmlElement {
fn parse(input: ParseStream) -> syn::Result<Self> {
if HtmlElementClose::peek(input.cursor()).is_some() {
return match input.parse::<HtmlElementClose>() {
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::<HtmlElementOpen>()?;
// Return early if it's a self-closing tag
if open.is_self_closing() {
return Ok(HtmlElement {
name: open.name,
props: open.props,
children: HtmlChildrenTree::new(),
});
}
if let TagName::Lit(name) = &open.name {
// Void elements should not have children.
// See https://html.spec.whatwg.org/multipage/syntax.html#void-elements
//
// For dynamic tags this is done at runtime!
match name.to_ascii_lowercase_string().as_str() {
"area" | "base" | "br" | "col" | "embed" | "hr" | "img" | "input" | "link"
| "meta" | "param" | "source" | "track" | "wbr" => {
return Err(syn::Error::new_spanned(
open.to_spanned(),
format!(
"the tag `<{name}>` is a void element and cannot have children (hint: \
rewrite this as `<{name} />`)",
),
));
}
_ => {}
}
}
let open_key = open.name.get_key();
let mut children = HtmlChildrenTree::new();
loop {
if input.is_empty() {
if is_ide_completion() {
break;
}
return Err(syn::Error::new_spanned(
open.to_spanned(),
"this opening tag has no corresponding closing tag",
));
}
if let Some(close_key) = HtmlElementClose::peek(input.cursor()) {
if open_key == close_key {
break;
}
}
children.parse_child(input)?;
}
if !input.is_empty() || !is_ide_completion() {
input.parse::<HtmlElementClose>()?;
}
Ok(Self {
name: open.name,
props: open.props,
children,
})
}
}
impl ToTokens for HtmlElement {
#[allow(clippy::cognitive_complexity)]
fn to_tokens(&self, tokens: &mut TokenStream) {
let Self {
name,
props,
children,
} = self;
let ElementProps {
classes,
attributes,
booleans,
value,
checked,
listeners,
special,
} = &props;
// attributes with special treatment
let node_ref = special.wrap_node_ref_attr();
let key = special.wrap_key_attr();
let value = value
.as_ref()
.map(|prop| wrap_attr_value(prop.value.optimize_literals()))
.unwrap_or(quote! { ::std::option::Option::None });
let checked = checked
.as_ref()
.map(|attr| {
let value = &attr.value;
quote! { ::std::option::Option::Some( #value ) }
})
.unwrap_or(quote! { ::std::option::Option::None });
// other attributes
let attributes = {
let normal_attrs = attributes.iter().map(
|Prop {
label,
value,
directive,
..
}| {
(
label.to_lit_str(),
value.optimize_literals_tagged(),
*directive,
)
},
);
let boolean_attrs = booleans.iter().filter_map(
|Prop {
label,
value,
directive,
..
}| {
let key = label.to_lit_str();
Some((
key.clone(),
match value {
Expr::Lit(e) => match &e.lit {
Lit::Bool(b) => Value::Static(if b.value {
quote! { #key }
} else {
return None;
}),
_ => Value::Dynamic(quote_spanned! {value.span()=> {
::yew::utils::__ensure_type::<::std::primitive::bool>(#value);
#key
}}),
},
expr => Value::Dynamic(
quote_spanned! {expr.span().resolved_at(Span::call_site())=>
if #expr {
::std::option::Option::Some(
::yew::virtual_dom::AttrValue::Static(#key)
)
} else {
::std::option::Option::None
}
},
),
},
*directive,
))
},
);
let class_attr =
classes
.as_ref()
.and_then(|classes| match classes.value.try_into_lit() {
Some(lit) => {
if lit.value().is_empty() {
None
} else {
Some((
LitStr::new("class", lit.span()),
Value::Static(quote! { #lit }),
None,
))
}
}
None => {
let expr = &classes.value;
Some((
LitStr::new("class", classes.label.span()),
Value::Dynamic(quote! {
::std::convert::Into::<::yew::html::Classes>::into(#expr)
}),
None,
))
}
});
/// Try to turn attribute list into a `::yew::virtual_dom::Attributes::Static`
fn try_into_static(
src: &[(LitStr, Value, Option<PropDirective>)],
) -> Option<TokenStream> {
if src
.iter()
.any(|(_, _, d)| matches!(d, Some(PropDirective::ApplyAsProperty(_))))
{
// don't try to make a static attribute list if there are any properties to
// assign
return None;
}
let mut kv = Vec::with_capacity(src.len());
for (k, v, directive) in src.iter() {
let v = match v {
Value::Static(v) => quote! { #v },
Value::Dynamic(_) => return None,
};
let v = match directive {
Some(PropDirective::ApplyAsProperty(token)) => {
quote_spanned!(token.span()=> ::yew::virtual_dom::AttributeOrProperty::Property(
::std::convert::Into::into(#v)
))
}
None => quote!(::yew::virtual_dom::AttributeOrProperty::Static(
#v
)),
};
kv.push(quote! { ( #k, #v) });
}
Some(quote! { ::yew::virtual_dom::Attributes::Static(&[#(#kv),*]) })
}
let attrs = normal_attrs
.chain(boolean_attrs)
.chain(class_attr)
.collect::<Vec<(LitStr, Value, Option<PropDirective>)>>();
try_into_static(&attrs).unwrap_or_else(|| {
let keys = attrs.iter().map(|(k, ..)| quote! { #k });
let values = attrs.iter().map(|(_, v, directive)| {
let value = match directive {
Some(PropDirective::ApplyAsProperty(token)) => {
quote_spanned!(token.span()=> ::std::option::Option::Some(
::yew::virtual_dom::AttributeOrProperty::Property(
::std::convert::Into::into(#v)
))
)
}
None => {
let value = wrap_attr_value(v);
quote! {
::std::option::Option::map(#value, ::yew::virtual_dom::AttributeOrProperty::Attribute)
}
},
};
quote! { #value }
});
quote! {
::yew::virtual_dom::Attributes::Dynamic{
keys: &[#(#keys),*],
values: ::std::boxed::Box::new([#(#values),*]),
}
}
})
};
let listeners = if listeners.is_empty() {
quote! { ::yew::virtual_dom::listeners::Listeners::None }
} else {
let listeners_it = listeners.iter().map(|Prop { label, value, .. }| {
let name = &label.name;
quote! {
::yew::html::#name::Wrapper::__macro_new(#value)
}
});
quote! {
::yew::virtual_dom::listeners::Listeners::Pending(
::std::boxed::Box::new([#(#listeners_it),*])
)
}
};
// TODO: if none of the children have possibly None expressions or literals as keys, we can
// compute `VList.fully_keyed` at compile time.
let children = children.to_vnode_tokens();
tokens.extend(match &name {
TagName::Lit(dashedname) => {
let name_span = dashedname.span();
let name = dashedname.to_ascii_lowercase_string();
if name != dashedname.to_string() {
emit_warning!(
dashedname.span(),
format!(
"The tag '{dashedname}' is not matching its normalized form '{name}'. If you want \
to keep this form, change this to a dynamic tag `@{{\"{dashedname}\"}}`."
)
)
}
let node = match &*name {
"input" => {
quote! {
::std::convert::Into::<::yew::virtual_dom::VNode>::into(
::yew::virtual_dom::VTag::__new_input(
#value,
#checked,
#node_ref,
#key,
#attributes,
#listeners,
),
)
}
}
"textarea" => {
quote! {
::std::convert::Into::<::yew::virtual_dom::VNode>::into(
::yew::virtual_dom::VTag::__new_textarea(
#value,
#node_ref,
#key,
#attributes,
#listeners,
),
)
}
}
_ => {
quote! {
::std::convert::Into::<::yew::virtual_dom::VNode>::into(
::yew::virtual_dom::VTag::__new_other(
::yew::virtual_dom::AttrValue::Static(#name),
#node_ref,
#key,
#attributes,
#listeners,
#children,
),
)
}
}
};
// the return value can be inlined without the braces when this is stable:
// https://github.com/rust-lang/rust/issues/15701
quote_spanned!{
name_span =>
{
#[allow(clippy::redundant_clone, unused_braces)]
let node = #node;
node
}
}
}
TagName::Expr(name) => {
let vtag = Ident::new("__yew_vtag", name.span());
let expr = name.expr.as_ref().map(Group::stream);
let vtag_name = Ident::new("__yew_vtag_name", expr.span());
let void_children = Ident::new("__yew_void_children", Span::mixed_site());
// handle special attribute value
let handle_value_attr = props.value.as_ref().map(|prop| {
let v = prop.value.optimize_literals();
quote_spanned! {v.span()=> {
__yew_vtag.__macro_push_attr("value", #v);
}}
});
#[cfg(nightly_yew)]
let invalid_void_tag_msg_start = {
let span = vtag.span().unwrap();
let source_file = span.source_file().path();
let source_file = source_file.display();
let start = span.start();
format!("[{}:{}:{}] ", source_file, start.line(), start.column())
};
#[cfg(not(nightly_yew))]
let invalid_void_tag_msg_start = "";
// this way we get a nice error message (with the correct span) when the expression
// doesn't return a valid value
quote_spanned! {expr.span()=> {
let mut #vtag_name = ::std::convert::Into::<
::yew::virtual_dom::AttrValue
>::into(#expr);
::std::debug_assert!(
#vtag_name.is_ascii(),
"a dynamic tag returned a tag name containing non ASCII characters: `{}`",
#vtag_name,
);
#[allow(clippy::redundant_clone, unused_braces, clippy::let_and_return)]
let mut #vtag = match () {
_ if "input".eq_ignore_ascii_case(::std::convert::AsRef::<::std::primitive::str>::as_ref(&#vtag_name)) => {
::yew::virtual_dom::VTag::__new_input(
#value,
#checked,
#node_ref,
#key,
#attributes,
#listeners,
)
}
_ if "textarea".eq_ignore_ascii_case(::std::convert::AsRef::<::std::primitive::str>::as_ref(&#vtag_name)) => {
::yew::virtual_dom::VTag::__new_textarea(
#value,
#node_ref,
#key,
#attributes,
#listeners,
)
}
_ => {
let mut __yew_vtag = ::yew::virtual_dom::VTag::__new_other(
#vtag_name,
#node_ref,
#key,
#attributes,
#listeners,
#children,
);
#handle_value_attr
__yew_vtag
}
};
// These are the runtime-checks exclusive to dynamic tags.
// For literal tags this is already done at compile-time.
//
// check void element
if ::yew::virtual_dom::VTag::children(&#vtag).is_some() &&
!::std::matches!(
::yew::virtual_dom::VTag::children(&#vtag),
::std::option::Option::Some(::yew::virtual_dom::VNode::VList(ref #void_children)) if ::std::vec::Vec::is_empty(#void_children)
) {
::std::debug_assert!(
!::std::matches!(#vtag.tag().to_ascii_lowercase().as_str(),
"area" | "base" | "br" | "col" | "embed" | "hr" | "img" | "input"
| "link" | "meta" | "param" | "source" | "track" | "wbr"
),
concat!(#invalid_void_tag_msg_start, "a dynamic tag tried to create a `<{0}>` tag with children. `<{0}>` is a void element which can't have any children."),
#vtag.tag(),
);
}
::std::convert::Into::<::yew::virtual_dom::VNode>::into(#vtag)
}}
}
});
}
}
fn wrap_attr_value<T: ToTokens>(value: T) -> TokenStream {
quote_spanned! {value.span()=>
::yew::html::IntoPropValue::<
::std::option::Option<
::yew::virtual_dom::AttrValue
>
>
::into_prop_value(#value)
}
}
pub struct DynamicName {
at: Token![@],
expr: Option<Group>,
}
impl Peek<'_, ()> for DynamicName {
fn peek(cursor: Cursor) -> Option<((), Cursor)> {
let (punct, cursor) = cursor.punct()?;
if punct.as_char() != '@' {
return None;
}
// move cursor past block if there is one
let cursor = cursor
.group(Delimiter::Brace)
.map(|(_, _, cursor)| cursor)
.unwrap_or(cursor);
Some(((), cursor))
}
}
impl Parse for DynamicName {
fn parse(input: ParseStream) -> syn::Result<Self> {
let at = input.parse()?;
// the expression block is optional, closing tags don't have it.
let expr = input.parse().ok();
Ok(Self { at, expr })
}
}
impl ToTokens for DynamicName {
fn to_tokens(&self, tokens: &mut TokenStream) {
let Self { at, expr } = self;
tokens.extend(quote! {#at #expr});
}
}
#[derive(PartialEq)]
enum TagKey {
Lit(HtmlDashedName),
Expr,
}
pub enum TagName {
Lit(HtmlDashedName),
Expr(DynamicName),
}
impl TagName {
fn get_key(&self) -> TagKey {
match self {
TagName::Lit(name) => TagKey::Lit(name.clone()),
TagName::Expr(_) => TagKey::Expr,
}
}
}
impl Peek<'_, TagKey> for TagName {
fn peek(cursor: Cursor) -> Option<(TagKey, Cursor)> {
if let Some((_, cursor)) = DynamicName::peek(cursor) {
Some((TagKey::Expr, cursor))
} else {
HtmlDashedName::peek(cursor).map(|(name, cursor)| (TagKey::Lit(name), cursor))
}
}
}
impl Parse for TagName {
fn parse(input: ParseStream) -> syn::Result<Self> {
if DynamicName::peek(input.cursor()).is_some() {
DynamicName::parse(input).map(Self::Expr)
} else {
HtmlDashedName::parse(input).map(Self::Lit)
}
}
}
impl ToTokens for TagName {
fn to_tokens(&self, tokens: &mut TokenStream) {
match self {
TagName::Lit(name) => name.to_tokens(tokens),
TagName::Expr(name) => name.to_tokens(tokens),
}
}
}
struct HtmlElementOpen {
tag: TagTokens,
name: TagName,
props: ElementProps,
}
impl HtmlElementOpen {
fn is_self_closing(&self) -> bool {
self.tag.div.is_some()
}
fn to_spanned(&self) -> impl ToTokens {
self.tag.to_spanned()
}
}
impl PeekValue<TagKey> for HtmlElementOpen {
fn peek(cursor: Cursor) -> Option<TagKey> {
let (punct, cursor) = cursor.punct()?;
if punct.as_char() != '<' {
return None;
}
let (tag_key, cursor) = TagName::peek(cursor)?;
if let TagKey::Lit(name) = &tag_key {
// Avoid parsing `<key=[...]>` as an element. It needs to be parsed as an `HtmlList`.
if name.to_string() == "key" {
let (punct, _) = cursor.punct()?;
// ... unless it isn't followed by a '='. `<key></key>` is a valid element!
if punct.as_char() == '=' {
return None;
}
} else if !non_capitalized_ascii(&name.to_string()) {
return None;
}
}
Some(tag_key)
}
}
impl Parse for HtmlElementOpen {
fn parse(input: ParseStream) -> syn::Result<Self> {
TagTokens::parse_start_content(input, |input, tag| {
let name = input.parse::<TagName>()?;
let mut props = input.parse::<ElementProps>()?;
match &name {
TagName::Lit(name) => {
// Don't treat value as special for non input / textarea fields
// For dynamic tags this is done at runtime!
match name.to_ascii_lowercase_string().as_str() {
"input" | "textarea" => {}
_ => {
if let Some(attr) = props.value.take() {
props.attributes.push(attr);
}
if let Some(attr) = props.checked.take() {
props.attributes.push(attr);
}
}
}
}
TagName::Expr(name) => {
if name.expr.is_none() {
return Err(syn::Error::new_spanned(
name,
"this dynamic tag is missing an expression block defining its value",
));
}
}
}
Ok(Self { tag, name, props })
})
}
}
struct HtmlElementClose {
tag: TagTokens,
_name: TagName,
}
impl HtmlElementClose {
fn to_spanned(&self) -> impl ToTokens {
self.tag.to_spanned()
}
}
impl PeekValue<TagKey> for HtmlElementClose {
fn peek(cursor: Cursor) -> Option<TagKey> {
let (punct, cursor) = cursor.punct()?;
if punct.as_char() != '<' {
return None;
}
let (punct, cursor) = cursor.punct()?;
if punct.as_char() != '/' {
return None;
}
let (tag_key, cursor) = TagName::peek(cursor)?;
if matches!(&tag_key, TagKey::Lit(name) if !non_capitalized_ascii(&name.to_string())) {
return None;
}
let (punct, _) = cursor.punct()?;
(punct.as_char() == '>').then_some(tag_key)
}
}
impl Parse for HtmlElementClose {
fn parse(input: ParseStream) -> syn::Result<Self> {
TagTokens::parse_end_content(input, |input, tag| {
let name = input.parse()?;
if let TagName::Expr(name) = &name {
if let Some(expr) = &name.expr {
return Err(syn::Error::new_spanned(
expr,
"dynamic closing tags must not have a body (hint: replace it with just \
`</@>`)",
));
}
}
Ok(Self { tag, _name: name })
})
}
}