1
0

rendering.rs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. println!("html: {}", html);
  23. value.set(Some(html.to_string()));
  24. }
  25. });
  26. });
  27. value()
  28. }
  29. 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>"#;
  30. fn check_html_renders() -> Element {
  31. let inner_html = use_inner_html("main_div");
  32. let desktop_context: DesktopContext = consume_context();
  33. if let Some(raw_html) = inner_html {
  34. println!("{}", raw_html);
  35. let fragment = &raw_html;
  36. let expected = EXPECTED_HTML;
  37. assert_eq!(raw_html, EXPECTED_HTML);
  38. if fragment == expected {
  39. println!("html matches");
  40. desktop_context.close();
  41. }
  42. }
  43. let dyn_value = 0;
  44. let dyn_element = rsx! {
  45. div {
  46. dangerous_inner_html: "<p>hello world</p>",
  47. }
  48. };
  49. rsx! {
  50. div {
  51. id: "main_div",
  52. div {
  53. width: "100px",
  54. height: "100px",
  55. color: "rgb({dyn_value}, {dyn_value}, {dyn_value})",
  56. id: 5,
  57. input {
  58. "type": "checkbox",
  59. },
  60. h1 {
  61. "text"
  62. }
  63. {dyn_element}
  64. }
  65. }
  66. }
  67. }