|
@@ -1,30 +1,30 @@
|
|
|
|
+use convert_case::{Case, Casing};
|
|
use dioxus_rsx::{BodyNode, CallBody, Element, ElementAttr, ElementAttrNamed, IfmtInput};
|
|
use dioxus_rsx::{BodyNode, CallBody, Element, ElementAttr, ElementAttrNamed, IfmtInput};
|
|
pub use html_parser::{Dom, Node};
|
|
pub use html_parser::{Dom, Node};
|
|
use proc_macro2::{Ident, Span};
|
|
use proc_macro2::{Ident, Span};
|
|
use syn::LitStr;
|
|
use syn::LitStr;
|
|
|
|
|
|
-pub fn convert_from_html(dom: Dom) -> CallBody {
|
|
|
|
|
|
+/// Convert an HTML DOM tree into an RSX CallBody
|
|
|
|
+pub fn rsx_from_html(dom: &Dom) -> CallBody {
|
|
CallBody {
|
|
CallBody {
|
|
roots: dom
|
|
roots: dom
|
|
.children
|
|
.children
|
|
- .into_iter()
|
|
|
|
|
|
+ .iter()
|
|
.filter_map(create_body_node_from_node)
|
|
.filter_map(create_body_node_from_node)
|
|
.collect(),
|
|
.collect(),
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-fn create_body_node_from_node(node: Node) -> Option<BodyNode> {
|
|
|
|
|
|
+fn create_body_node_from_node(node: &Node) -> Option<BodyNode> {
|
|
match node {
|
|
match node {
|
|
Node::Text(text) => Some(BodyNode::Text(ifmt_from_text(text))),
|
|
Node::Text(text) => Some(BodyNode::Text(ifmt_from_text(text))),
|
|
Node::Element(el) => {
|
|
Node::Element(el) => {
|
|
- use convert_case::{Case, Casing};
|
|
|
|
-
|
|
|
|
let el_name = el.name.to_case(Case::Snake);
|
|
let el_name = el.name.to_case(Case::Snake);
|
|
let el_name = Ident::new(el_name.as_str(), Span::call_site());
|
|
let el_name = Ident::new(el_name.as_str(), Span::call_site());
|
|
|
|
|
|
let mut attributes: Vec<_> = el
|
|
let mut attributes: Vec<_> = el
|
|
.attributes
|
|
.attributes
|
|
- .into_iter()
|
|
|
|
|
|
+ .iter()
|
|
.map(|(name, value)| {
|
|
.map(|(name, value)| {
|
|
let ident = if matches!(name.as_str(), "for" | "async" | "type" | "as") {
|
|
let ident = if matches!(name.as_str(), "for" | "async" | "type" | "as") {
|
|
Ident::new_raw(name.as_str(), Span::call_site())
|
|
Ident::new_raw(name.as_str(), Span::call_site())
|
|
@@ -34,11 +34,11 @@ fn create_body_node_from_node(node: Node) -> Option<BodyNode> {
|
|
};
|
|
};
|
|
|
|
|
|
ElementAttrNamed {
|
|
ElementAttrNamed {
|
|
|
|
+ el_name: el_name.clone(),
|
|
attr: ElementAttr::AttrText {
|
|
attr: ElementAttr::AttrText {
|
|
|
|
+ value: ifmt_from_text(value.as_deref().unwrap_or("false")),
|
|
name: ident,
|
|
name: ident,
|
|
- value: ifmt_from_text(value.unwrap_or_else(|| "false".to_string())),
|
|
|
|
},
|
|
},
|
|
- el_name: el_name.clone(),
|
|
|
|
}
|
|
}
|
|
})
|
|
})
|
|
.collect();
|
|
.collect();
|
|
@@ -46,27 +46,27 @@ fn create_body_node_from_node(node: Node) -> Option<BodyNode> {
|
|
let class = el.classes.join(" ");
|
|
let class = el.classes.join(" ");
|
|
if !class.is_empty() {
|
|
if !class.is_empty() {
|
|
attributes.push(ElementAttrNamed {
|
|
attributes.push(ElementAttrNamed {
|
|
|
|
+ el_name: el_name.clone(),
|
|
attr: ElementAttr::AttrText {
|
|
attr: ElementAttr::AttrText {
|
|
name: Ident::new("class", Span::call_site()),
|
|
name: Ident::new("class", Span::call_site()),
|
|
- value: ifmt_from_text(class),
|
|
|
|
|
|
+ value: ifmt_from_text(&class),
|
|
},
|
|
},
|
|
- el_name: el_name.clone(),
|
|
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
- if let Some(id) = el.id {
|
|
|
|
|
|
+ if let Some(id) = &el.id {
|
|
attributes.push(ElementAttrNamed {
|
|
attributes.push(ElementAttrNamed {
|
|
|
|
+ el_name: el_name.clone(),
|
|
attr: ElementAttr::AttrText {
|
|
attr: ElementAttr::AttrText {
|
|
name: Ident::new("id", Span::call_site()),
|
|
name: Ident::new("id", Span::call_site()),
|
|
value: ifmt_from_text(id),
|
|
value: ifmt_from_text(id),
|
|
},
|
|
},
|
|
- el_name: el_name.clone(),
|
|
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
let children = el
|
|
let children = el
|
|
.children
|
|
.children
|
|
- .into_iter()
|
|
|
|
|
|
+ .iter()
|
|
.filter_map(create_body_node_from_node)
|
|
.filter_map(create_body_node_from_node)
|
|
.collect();
|
|
.collect();
|
|
|
|
|
|
@@ -78,13 +78,15 @@ fn create_body_node_from_node(node: Node) -> Option<BodyNode> {
|
|
key: None,
|
|
key: None,
|
|
}))
|
|
}))
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ // We ignore comments
|
|
Node::Comment(_) => None,
|
|
Node::Comment(_) => None,
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-fn ifmt_from_text(text: String) -> IfmtInput {
|
|
|
|
|
|
+fn ifmt_from_text(text: &str) -> IfmtInput {
|
|
IfmtInput {
|
|
IfmtInput {
|
|
- source: Some(LitStr::new(text.as_str(), Span::call_site())),
|
|
|
|
|
|
+ source: Some(LitStr::new(text, Span::call_site())),
|
|
segments: vec![],
|
|
segments: vec![],
|
|
}
|
|
}
|
|
}
|
|
}
|