rendering.rs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. use dioxus::prelude::*;
  2. use dioxus_core::Element;
  3. use dioxus_desktop::DesktopContext;
  4. #[path = "./utils.rs"]
  5. mod utils;
  6. fn main() {
  7. utils::check_app_exits(check_html_renders);
  8. }
  9. fn use_inner_html(id: &'static str) -> Option<String> {
  10. let mut value = use_signal(|| None as Option<String>);
  11. use_effect(move || {
  12. spawn(async move {
  13. tokio::time::sleep(std::time::Duration::from_millis(500)).await;
  14. let res = eval(&format!(
  15. r#"let element = document.getElementById('{}');
  16. return element.innerHTML"#,
  17. id
  18. ))
  19. .await
  20. .unwrap();
  21. if let Some(html) = res.as_str() {
  22. // serde_json::Value::String(html)
  23. println!("html: {}", html);
  24. value.set(Some(html.to_string()));
  25. }
  26. });
  27. });
  28. value.read().clone()
  29. }
  30. const EXPECTED_HTML: &str = r#"<div style="width: 100px; height: 100px; color: rgb(0, 0, 0);" id="5"><input type="checkbox"><h1>text</h1><div><p>hello world</p></div></div>"#;
  31. fn check_html_renders() -> Element {
  32. let inner_html = use_inner_html("main_div");
  33. let desktop_context: DesktopContext = consume_context();
  34. if let Some(raw_html) = inner_html {
  35. println!("{}", raw_html);
  36. let fragment = &raw_html;
  37. let expected = EXPECTED_HTML;
  38. assert_eq!(raw_html, EXPECTED_HTML);
  39. if fragment == expected {
  40. println!("html matches");
  41. desktop_context.close();
  42. }
  43. }
  44. let dyn_value = 0;
  45. let dyn_element = rsx! {
  46. div {
  47. dangerous_inner_html: "<p>hello world</p>",
  48. }
  49. };
  50. rsx! {
  51. div {
  52. id: "main_div",
  53. div {
  54. width: "100px",
  55. height: "100px",
  56. color: "rgb({dyn_value}, {dyn_value}, {dyn_value})",
  57. id: 5,
  58. input {
  59. "type": "checkbox",
  60. },
  61. h1 {
  62. "text"
  63. }
  64. {dyn_element}
  65. }
  66. }
  67. }
  68. }