From 6f1fbd3175ea184cff734286bb5b0d72973c98b3 Mon Sep 17 00:00:00 2001 From: Matt Hoyt Date: Fri, 3 Jan 2020 16:44:48 -0600 Subject: [PATCH 1/5] Updated so boolean attributes are either there or not. Issue #394 --- crates/macro/src/html_tree/html_tag/mod.rs | 19 +++--- .../src/html_tree/html_tag/tag_attributes.rs | 66 ++++++++++++++++++- 2 files changed, 73 insertions(+), 12 deletions(-) diff --git a/crates/macro/src/html_tree/html_tag/mod.rs b/crates/macro/src/html_tree/html_tag/mod.rs index 727e947ea33..3e8509e3642 100644 --- a/crates/macro/src/html_tree/html_tag/mod.rs +++ b/crates/macro/src/html_tree/html_tag/mod.rs @@ -91,10 +91,10 @@ impl ToTokens for HtmlTag { let TagAttributes { classes, attributes, + booleans, kind, value, checked, - disabled, selected, node_ref, href, @@ -106,6 +106,14 @@ impl ToTokens for HtmlTag { let label_str = label.to_string(); quote_spanned! {value.span() => (#label_str.to_owned(), (#value).to_string()) } }); + let set_booleans = booleans.iter().map(|TagAttribute {label, value}| { + let label_str = label.to_string(); + quote_spanned! {value.span() => + if #value { + #vtag.add_attribute(&#label_str, &#label_str); + } + } + }); let set_kind = kind.iter().map(|kind| { quote_spanned! {kind.span()=> #vtag.set_kind(&(#kind)); } }); @@ -121,13 +129,6 @@ impl ToTokens for HtmlTag { let set_checked = checked.iter().map(|checked| { quote_spanned! {checked.span()=> #vtag.set_checked(#checked); } }); - let add_disabled = disabled.iter().map(|disabled| { - quote_spanned! {disabled.span()=> - if #disabled { - #vtag.add_attribute("disabled", &"true"); - } - } - }); let add_selected = selected.iter().map(|selected| { quote_spanned! {selected.span()=> if #selected { @@ -167,7 +168,7 @@ impl ToTokens for HtmlTag { #(#set_value)* #(#add_href)* #(#set_checked)* - #(#add_disabled)* + #(#set_booleans)* #(#add_selected)* #(#set_classes)* #(#set_node_ref)* diff --git a/crates/macro/src/html_tree/html_tag/tag_attributes.rs b/crates/macro/src/html_tree/html_tag/tag_attributes.rs index 543b29c693b..b53289b5cc9 100644 --- a/crates/macro/src/html_tree/html_tag/tag_attributes.rs +++ b/crates/macro/src/html_tree/html_tag/tag_attributes.rs @@ -10,10 +10,10 @@ pub struct TagAttributes { pub attributes: Vec, pub listeners: Vec, pub classes: Option, + pub booleans: Vec, pub value: Option, pub kind: Option, pub checked: Option, - pub disabled: Option, pub selected: Option, pub node_ref: Option, pub href: Option, @@ -24,6 +24,52 @@ pub enum ClassesForm { Single(Expr), } +lazy_static!{ + static ref BOOLEAN_SET: HashSet<&'static str> = { + HashSet::from_iter( + vec![ + "async", + "autocomplete", + "autofocus", + "autoplay", + "border", + "challenge", + "checked", + "compact", + "contenteditable", + "controls", + "default", + "defer", + "disabled", + "formNoValidate", + "frameborder", + "hidden", + "indeterminate", + "ismap", + "loop", + "multiple", + "muted", + "nohref", + "noresize", + "noshade", + "novalidate", + "nowrap", + "open", + "readonly", + "required", + "reversed", + "scoped", + "scrolling", + "seamless", + "selected", + "sortable", + "spellcheck", + "translate", + ].into_iter() + ) + }; +} + lazy_static! { static ref LISTENER_SET: HashSet<&'static str> = { HashSet::from_iter( @@ -92,6 +138,20 @@ impl TagAttributes { drained } + fn drain_boolean(attrs: &mut Vec) -> Vec { + 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, name: &str) -> Option { let mut i = 0; while i < attrs.len() { @@ -142,13 +202,13 @@ impl Parse for TagAttributes { } 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 disabled = TagAttributes::remove_attr(&mut attributes, "disabled"); let selected = TagAttributes::remove_attr(&mut attributes, "selected"); let node_ref = TagAttributes::remove_attr(&mut attributes, "ref"); let href = TagAttributes::remove_attr(&mut attributes, "href"); @@ -157,10 +217,10 @@ impl Parse for TagAttributes { attributes, classes, listeners, + booleans, value, kind, checked, - disabled, selected, node_ref, href, From d7707103124bd14c39d03ef1b22ae712e6e4c4eb Mon Sep 17 00:00:00 2001 From: Matt Hoyt Date: Sat, 4 Jan 2020 14:46:27 -0600 Subject: [PATCH 2/5] Ran cargo fmt. --- crates/macro/src/html_tree/html_tag/mod.rs | 2 +- crates/macro/src/html_tree/html_tag/tag_attributes.rs | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/crates/macro/src/html_tree/html_tag/mod.rs b/crates/macro/src/html_tree/html_tag/mod.rs index 3e8509e3642..fe4b1645260 100644 --- a/crates/macro/src/html_tree/html_tag/mod.rs +++ b/crates/macro/src/html_tree/html_tag/mod.rs @@ -106,7 +106,7 @@ impl ToTokens for HtmlTag { let label_str = label.to_string(); quote_spanned! {value.span() => (#label_str.to_owned(), (#value).to_string()) } }); - let set_booleans = booleans.iter().map(|TagAttribute {label, value}| { + let set_booleans = booleans.iter().map(|TagAttribute { label, value }| { let label_str = label.to_string(); quote_spanned! {value.span() => if #value { diff --git a/crates/macro/src/html_tree/html_tag/tag_attributes.rs b/crates/macro/src/html_tree/html_tag/tag_attributes.rs index b53289b5cc9..374fcf57f13 100644 --- a/crates/macro/src/html_tree/html_tag/tag_attributes.rs +++ b/crates/macro/src/html_tree/html_tag/tag_attributes.rs @@ -24,7 +24,7 @@ pub enum ClassesForm { Single(Expr), } -lazy_static!{ +lazy_static! { static ref BOOLEAN_SET: HashSet<&'static str> = { HashSet::from_iter( vec![ @@ -65,7 +65,8 @@ lazy_static!{ "sortable", "spellcheck", "translate", - ].into_iter() + ] + .into_iter(), ) }; } From f78a12804cb6e08dbb34227b00ca549cfc9037b4 Mon Sep 17 00:00:00 2001 From: Matt Hoyt Date: Sun, 5 Jan 2020 21:07:14 -0600 Subject: [PATCH 3/5] Updated to remove checked and selected special handling. --- crates/macro/src/html_tree/html_tag/mod.rs | 14 -------------- .../macro/src/html_tree/html_tag/tag_attributes.rs | 6 ------ 2 files changed, 20 deletions(-) diff --git a/crates/macro/src/html_tree/html_tag/mod.rs b/crates/macro/src/html_tree/html_tag/mod.rs index fe4b1645260..82d1ee62a13 100644 --- a/crates/macro/src/html_tree/html_tag/mod.rs +++ b/crates/macro/src/html_tree/html_tag/mod.rs @@ -94,8 +94,6 @@ impl ToTokens for HtmlTag { booleans, kind, value, - checked, - selected, node_ref, href, listeners, @@ -126,16 +124,6 @@ impl ToTokens for HtmlTag { #vtag.add_attribute("href", &__yew_href); } }); - let set_checked = checked.iter().map(|checked| { - quote_spanned! {checked.span()=> #vtag.set_checked(#checked); } - }); - let add_selected = selected.iter().map(|selected| { - quote_spanned! {selected.span()=> - if #selected { - #vtag.add_attribute("selected", &"selected"); - } - } - }); let set_classes = classes.iter().map(|classes_form| match classes_form { ClassesForm::Tuple(classes) => quote! { #vtag.add_classes(vec![#(&(#classes)),*]); @@ -167,9 +155,7 @@ impl ToTokens for HtmlTag { #(#set_kind)* #(#set_value)* #(#add_href)* - #(#set_checked)* #(#set_booleans)* - #(#add_selected)* #(#set_classes)* #(#set_node_ref)* #vtag.add_attributes(vec![#(#attr_pairs),*]); diff --git a/crates/macro/src/html_tree/html_tag/tag_attributes.rs b/crates/macro/src/html_tree/html_tag/tag_attributes.rs index 374fcf57f13..6627ee9a2cd 100644 --- a/crates/macro/src/html_tree/html_tag/tag_attributes.rs +++ b/crates/macro/src/html_tree/html_tag/tag_attributes.rs @@ -13,8 +13,6 @@ pub struct TagAttributes { pub booleans: Vec, pub value: Option, pub kind: Option, - pub checked: Option, - pub selected: Option, pub node_ref: Option, pub href: Option, } @@ -209,8 +207,6 @@ impl Parse for TagAttributes { 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 selected = TagAttributes::remove_attr(&mut attributes, "selected"); let node_ref = TagAttributes::remove_attr(&mut attributes, "ref"); let href = TagAttributes::remove_attr(&mut attributes, "href"); @@ -221,8 +217,6 @@ impl Parse for TagAttributes { booleans, value, kind, - checked, - selected, node_ref, href, }) From e4b11a4654b9a2ba4b489682791df133c887bb63 Mon Sep 17 00:00:00 2001 From: Matt Hoyt Date: Sun, 5 Jan 2020 21:27:30 -0600 Subject: [PATCH 4/5] Update the attribute list to match what mdn says. Removed deceprated attributes. --- .../src/html_tree/html_tag/tag_attributes.rs | 20 ------------------- 1 file changed, 20 deletions(-) diff --git a/crates/macro/src/html_tree/html_tag/tag_attributes.rs b/crates/macro/src/html_tree/html_tag/tag_attributes.rs index 6627ee9a2cd..119d616cf70 100644 --- a/crates/macro/src/html_tree/html_tag/tag_attributes.rs +++ b/crates/macro/src/html_tree/html_tag/tag_attributes.rs @@ -27,42 +27,22 @@ lazy_static! { HashSet::from_iter( vec![ "async", - "autocomplete", "autofocus", - "autoplay", - "border", - "challenge", "checked", - "compact", - "contenteditable", "controls", "default", "defer", "disabled", - "formNoValidate", - "frameborder", "hidden", - "indeterminate", "ismap", "loop", "multiple", "muted", - "nohref", - "noresize", - "noshade", "novalidate", - "nowrap", "open", "readonly", "required", - "reversed", - "scoped", - "scrolling", - "seamless", "selected", - "sortable", - "spellcheck", - "translate", ] .into_iter(), ) From d6e4963deb958076d923340678fa043c844c0ccc Mon Sep 17 00:00:00 2001 From: Matt Hoyt Date: Sun, 5 Jan 2020 21:45:51 -0600 Subject: [PATCH 5/5] Added back the checked special handling. --- crates/macro/src/html_tree/html_tag/mod.rs | 5 +++++ crates/macro/src/html_tree/html_tag/tag_attributes.rs | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/crates/macro/src/html_tree/html_tag/mod.rs b/crates/macro/src/html_tree/html_tag/mod.rs index 82d1ee62a13..a92ebe216cd 100644 --- a/crates/macro/src/html_tree/html_tag/mod.rs +++ b/crates/macro/src/html_tree/html_tag/mod.rs @@ -94,6 +94,7 @@ impl ToTokens for HtmlTag { booleans, kind, value, + checked, node_ref, href, listeners, @@ -124,6 +125,9 @@ impl ToTokens for HtmlTag { #vtag.add_attribute("href", &__yew_href); } }); + let set_checked = checked.iter().map(|checked| { + quote_spanned! {checked.span()=> #vtag.set_checked(#checked); } + }); let set_classes = classes.iter().map(|classes_form| match classes_form { ClassesForm::Tuple(classes) => quote! { #vtag.add_classes(vec![#(&(#classes)),*]); @@ -155,6 +159,7 @@ impl ToTokens for HtmlTag { #(#set_kind)* #(#set_value)* #(#add_href)* + #(#set_checked)* #(#set_booleans)* #(#set_classes)* #(#set_node_ref)* diff --git a/crates/macro/src/html_tree/html_tag/tag_attributes.rs b/crates/macro/src/html_tree/html_tag/tag_attributes.rs index 119d616cf70..61ae5581e17 100644 --- a/crates/macro/src/html_tree/html_tag/tag_attributes.rs +++ b/crates/macro/src/html_tree/html_tag/tag_attributes.rs @@ -13,6 +13,7 @@ pub struct TagAttributes { pub booleans: Vec, pub value: Option, pub kind: Option, + pub checked: Option, pub node_ref: Option, pub href: Option, } @@ -28,7 +29,6 @@ lazy_static! { vec![ "async", "autofocus", - "checked", "controls", "default", "defer", @@ -187,6 +187,7 @@ impl Parse for TagAttributes { 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 href = TagAttributes::remove_attr(&mut attributes, "href"); @@ -194,6 +195,7 @@ impl Parse for TagAttributes { attributes, classes, listeners, + checked, booleans, value, kind,