From 300a7dc12b0684255a66268e31178b272b822f5c Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Tue, 30 Jan 2024 11:10:45 -0600 Subject: [PATCH] Create a new function for dioxus-rsx Element --- packages/rsx/src/element.rs | 73 ++++++++++++++++++++++--------------- 1 file changed, 43 insertions(+), 30 deletions(-) diff --git a/packages/rsx/src/element.rs b/packages/rsx/src/element.rs index e7ee84a1f..1dbd9ce43 100644 --- a/packages/rsx/src/element.rs +++ b/packages/rsx/src/element.rs @@ -20,11 +20,52 @@ pub struct Element { pub name: ElementName, pub key: Option, pub attributes: Vec, - pub merged_attributes: Vec, + pub(crate) merged_attributes: Vec, pub children: Vec, pub brace: syn::token::Brace, } +impl Element { + /// Create a new element with the given name, attributes and children + pub fn new( + key: Option, + name: ElementName, + attributes: Vec, + children: Vec, + brace: syn::token::Brace, + ) -> Self { + // Deduplicate any attributes that can be combined + // For example, if there are two `class` attributes, combine them into one + let mut merged_attributes: Vec = Vec::new(); + for attr in &attributes { + let attr_index = merged_attributes + .iter() + .position(|a| a.matches_attr_name(attr)); + + if let Some(old_attr_index) = attr_index { + let old_attr = &mut merged_attributes[old_attr_index]; + + if let Some(combined) = old_attr.try_combine(attr) { + *old_attr = combined; + } + + continue; + } + + merged_attributes.push(attr.clone()); + } + + Self { + name, + key, + attributes, + merged_attributes, + children, + brace, + } + } +} + impl Parse for Element { fn parse(stream: ParseStream) -> Result { let el_name = ElementName::parse(stream)?; @@ -194,27 +235,6 @@ Like so: break; } - // Deduplicate any attributes that can be combined - // For example, if there are two `class` attributes, combine them into one - let mut merged_attributes: Vec = Vec::new(); - for attr in &attributes { - let attr_index = merged_attributes - .iter() - .position(|a| a.matches_attr_name(attr)); - - if let Some(old_attr_index) = attr_index { - let old_attr = &mut merged_attributes[old_attr_index]; - - if let Some(combined) = old_attr.try_combine(attr) { - *old_attr = combined; - } - - continue; - } - - merged_attributes.push(attr.clone()); - } - while !content.is_empty() { if (content.peek(LitStr) && content.peek2(Token![:])) && !content.peek3(Token![:]) { attr_after_element!(content.span()); @@ -232,14 +252,7 @@ Like so: } } - Ok(Self { - key, - name: el_name, - attributes, - merged_attributes, - children, - brace, - }) + Ok(Self::new(key, el_name, attributes, children, brace)) } }