pub fn parse<Ext>(template: &str) -> Result<(Dom<Ext>, Vec<NodeId>)>where
Ext: Default + 'static,Expand description
Parse template into a fresh Dom<Ext> with a Fragment root. The
returned ids are the top-level children of the fragment.
Examples found in repository?
examples/round_trip.rs (line 52)
51fn round_trip(src: &str) -> Result<String, rdom_parser::ParseError> {
52 let (dom, ids): (Dom<()>, _) = parse(src)?;
53 // For single top-level nodes (most cases), serialize the node
54 // itself; for multi-top-level (like "<!-- β¦ --><div></div>"),
55 // serialize the fragment root's inner markup.
56 let out = if ids.len() == 1 {
57 dom.outer_markup(ids[0])
58 } else {
59 dom.inner_markup(dom.root())
60 };
61 Ok(out)
62}More examples
examples/parse_html.rs (line 23)
8fn main() {
9 let template = r#"
10 <div class="card" id="hero">
11 <h1>Welcome & hello</h1>
12 <p>This is <strong>rdom-parser</strong>.</p>
13 <!-- TODO: add icon -->
14 <ul>
15 <li>Zero external deps</li>
16 <li>Full UTF-8: δΈζ, π¦, π¨βπ©βπ§</li>
17 <li>Error reporting with line/col + hints</li>
18 </ul>
19 <button disabled>OK</button>
20 </div>
21 "#;
22
23 let (dom, _ids): (Dom<()>, _) = parse(template).unwrap();
24 let root = dom.root();
25
26 println!("ββ Parsed tree βββββββββββββββββββββββββββββββββ");
27 walk(&dom, root, 0);
28
29 println!("\nββ Selector queries ββββββββββββββββββββββββββββ");
30 let lis = dom.query_selector_all_in(root, "li").unwrap();
31 println!("Found {} <li>:", lis.len());
32 for li in lis {
33 println!(" β’ {}", dom.text_content(li));
34 }
35
36 let card = dom.query_selector_in(root, ".card").unwrap().unwrap();
37 println!(
38 "\n.card's id attribute: {:?}",
39 dom.get_attribute(card, "id")
40 );
41
42 let button = dom.query_selector_in(root, "button[disabled]").unwrap();
43 if let Some(b) = button {
44 println!("Disabled button: {}", dom.text_content(b));
45 }
46
47 println!("\nββ Text content (flattened) ββββββββββββββββββββ");
48 println!(
49 "{}",
50 dom.text_content(card)
51 .split_whitespace()
52 .collect::<Vec<_>>()
53 .join(" ")
54 );
55}