0
  • goal convert this HTMl string into macro or into node element
use html_parser::Dom;

let html = r#"<span class="text_editor"><h1 title="title is here">hello world</h1><p>hi paragraph</p></span>"#;
let nodes = Dom::parse(html).unwrap_throw();

// add the html
let doc = window().unwrap_throw().document().unwrap_throw();
let my_dom_element = &doc.get_element_by_id("1").unwrap_throw();
my_dom_element.append_child(&nodes.children[0]).unwrap_throw();


31 |         my_dom_element.append_child(&nodes.children[0]).unwrap_throw();
   |                                     ^^^^^^^^^^^^^^^^^ expected struct `web_sys::Node`, found enum `html_parser::Node`
Jon
  • 3,573
  • 2
  • 17
  • 24
Ali Husham
  • 816
  • 10
  • 31
  • 2
    Do you want to populate the document with the content of your string "html" ? If so, I think that you should try using : this method set_inner_html of the root element of the document : https://rustwasm.github.io/wasm-bindgen/api/web_sys/struct.HtmlElement.html#method.set_inner_html . I don't think that html_parser can interact with web_sys. (because you are doing a WASM library isn't it ?) – Adrien BARRAL Apr 01 '22 at 14:25
  • Wow man it worked like magic. – Ali Husham Apr 02 '22 at 06:38

1 Answers1

0
use web_sys::{window};

let doc = window().unwrap_throw().document().unwrap_throw();
let my_dom_element = &doc.get_element_by_id("1").unwrap_throw();
let html = r#"<span class="text_editor"><h1 title="title is here">hello world</h1><p>hi paragraph</p></span>"#;
my_dom_element.set_inner_html(html);
Ali Husham
  • 816
  • 10
  • 31