Merge branch 'master' into pr/atty303/1349

This commit is contained in:
Evan Almloff committed 2024-01-06 08:52:37 -06:00
commit e5df83ceec
278 files changed
+11299 -3674

No files matched your search

+1
View File
@@ -15,6 +15,7 @@ keywords = ["dom", "ui", "gui", "react"]
[dependencies]
dioxus-autofmt = { workspace = true }
dioxus-rsx = { workspace = true }
dioxus-html = { workspace = true, features = ["html-to-rsx"]}
html_parser.workspace = true
proc-macro2 = "1.0.49"
quote = "1.0.23"
+32 -12
View File
@@ -3,6 +3,7 @@
#![doc(html_favicon_url = "https://avatars.githubusercontent.com/u/79236386")]
use convert_case::{Case, Casing};
use dioxus_html::{map_html_attribute_to_rsx, map_html_element_to_rsx};
use dioxus_rsx::{
AttributeType, BodyNode, CallBody, Component, Element, ElementAttr, ElementAttrNamed,
ElementName, IfmtInput,
@@ -25,28 +26,47 @@ pub fn rsx_node_from_html(node: &Node) -> Option<BodyNode> {
match node {
Node::Text(text) => Some(BodyNode::Text(ifmt_from_text(text))),
Node::Element(el) => {
let el_name = el.name.to_case(Case::Snake);
let el_name = ElementName::Ident(Ident::new(el_name.as_str(), Span::call_site()));
let el_name = if let Some(name) = map_html_element_to_rsx(&el.name) {
ElementName::Ident(Ident::new(name, Span::call_site()))
} else {
// if we don't recognize it and it has a dash, we assume it's a web component
if el.name.contains('-') {
ElementName::Custom(LitStr::new(&el.name, Span::call_site()))
} else {
// otherwise, it might be an element that isn't supported yet
ElementName::Ident(Ident::new(&el.name.to_case(Case::Snake), Span::call_site()))
}
};
let mut attributes: Vec<_> = el
.attributes
.iter()
.map(|(name, value)| {
let ident = if matches!(name.as_str(), "for" | "async" | "type" | "as") {
Ident::new_raw(name.as_str(), Span::call_site())
let value = ifmt_from_text(value.as_deref().unwrap_or("false"));
let attr = if let Some(name) = map_html_attribute_to_rsx(name) {
let ident = if let Some(name) = name.strip_prefix("r#") {
Ident::new_raw(name, Span::call_site())
} else {
Ident::new(name, Span::call_site())
};
ElementAttr {
value: dioxus_rsx::ElementAttrValue::AttrLiteral(value),
name: dioxus_rsx::ElementAttrName::BuiltIn(ident),
}
} else {
let new_name = name.to_case(Case::Snake);
Ident::new(new_name.as_str(), Span::call_site())
// If we don't recognize the attribute, we assume it's a custom attribute
ElementAttr {
value: dioxus_rsx::ElementAttrValue::AttrLiteral(value),
name: dioxus_rsx::ElementAttrName::Custom(LitStr::new(
name,
Span::call_site(),
)),
}
};
AttributeType::Named(ElementAttrNamed {
el_name: el_name.clone(),
attr: ElementAttr {
value: dioxus_rsx::ElementAttrValue::AttrLiteral(ifmt_from_text(
value.as_deref().unwrap_or("false"),
)),
name: dioxus_rsx::ElementAttrName::BuiltIn(ident),
},
attr,
})
})
.collect();
+33
View File
@@ -0,0 +1,33 @@
use html_parser::Dom;
#[test]
fn h_tags_translate() {
let html = r#"
<div>
<h1>hello world!</h1>
<h2>hello world!</h2>
<h3>hello world!</h3>
<h4>hello world!</h4>
<h5>hello world!</h5>
<h6>hello world!</h6>
</div>
"#
.trim();
let dom = Dom::parse(html).unwrap();
let body = rsx_rosetta::rsx_from_html(&dom);
let out = dioxus_autofmt::write_block_out(body).unwrap();
let expected = r#"
div {
h1 { "hello world!" }
h2 { "hello world!" }
h3 { "hello world!" }
h4 { "hello world!" }
h5 { "hello world!" }
h6 { "hello world!" }
}"#;
pretty_assertions::assert_eq!(&out, &expected);
}
+21
View File
@@ -0,0 +1,21 @@
use html_parser::Dom;
#[test]
fn raw_attribute() {
let html = r#"
<div>
<div unrecognizedattribute="asd">hello world!</div>
</div>
"#
.trim();
let dom = Dom::parse(html).unwrap();
let body = rsx_rosetta::rsx_from_html(&dom);
let out = dioxus_autofmt::write_block_out(body).unwrap();
let expected = r#"
div { div { "unrecognizedattribute": "asd", "hello world!" } }"#;
pretty_assertions::assert_eq!(&out, &expected);
}
-4
View File
@@ -9,8 +9,6 @@ fn simple_elements() {
<div id="asd">hello world!</div>
<div for="asd">hello world!</div>
<div async="asd">hello world!</div>
<div LargeThing="asd">hello world!</div>
<ai-is-awesome>hello world!</ai-is-awesome>
</div>
"#
.trim();
@@ -28,8 +26,6 @@ fn simple_elements() {
div { id: "asd", "hello world!" }
div { r#for: "asd", "hello world!" }
div { r#async: "asd", "hello world!" }
div { large_thing: "asd", "hello world!" }
ai_is_awesome { "hello world!" }
}"#;
pretty_assertions::assert_eq!(&out, &expected);
}
@@ -0,0 +1,21 @@
use html_parser::Dom;
#[test]
fn web_components_translate() {
let html = r#"
<div>
<my-component></my-component>
</div>
"#
.trim();
let dom = Dom::parse(html).unwrap();
let body = rsx_rosetta::rsx_from_html(&dom);
let out = dioxus_autofmt::write_block_out(body).unwrap();
let expected = r#"
div { my-component {} }"#;
pretty_assertions::assert_eq!(&out, &expected);
}