Struct visdom::Vis

source · []
pub struct Vis;
Expand description

Vis: Entry struct of the mesdoc’s api.

Examples

use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
  let html = r##"
     <!doctype html>
     <html>
       <head>
         <meta charset="utf-8" />
       </head>
       <body>
          <nav id="header">
           <ul>
             <li>Hello,</li>
             <li>Vis</li>
             <li>Dom</li>
           </ul>
         </nav>
       </body>
    </html>
  "##;
  let doc = Vis::load(html)?;
  // All useful css selectors are well supported.
  let header = doc.find("#header");
  let list_items = header.children("ul > li");
  assert_eq!(list_items.length(), 3);
  assert_eq!(list_items.text(), "Hello,VisDom");
  let second_child = list_items.filter(":nth-child(2)");
  assert_eq!(second_child.text(), "Vis");
  // Easy to operate the nodes.
  let mut fourth_child = Vis::load("<li>!</li>")?;
  let mut parent = list_items.parent("");
  assert_eq!(parent.length(), 1);
  fourth_child.append_to(&mut parent);
  let mut cur_list_items = header.children("ul > li");
  assert_eq!(cur_list_items.length(), 4);
  assert_eq!(cur_list_items.text(), "Hello,VisDom!");
  // Powerful api for operate text nodes
  // use append_text and prepend_text to change text
  let mut texts = cur_list_items.texts(0);
  texts.for_each(|_index, text_node|{
     text_node.prepend_text("[");
     text_node.append_text("]");
     true
  });
  assert_eq!(cur_list_items.text(), "[Hello,][Vis][Dom][!]");
  // use set_text to change text
  texts.for_each(|_index, text_node|{
      text_node.set_text("@");
      true
  });
  assert_eq!(cur_list_items.text(), "@@@@");
  // use set_html to mix content
  texts.for_each(|_index, text_node|{
      let orig_text = text_node.text();
      let replace_html = format!("<span>{}</span><b>!</b>", orig_text);
      // Be careful that now the text_node is destoryed by `set_html`
      text_node.set_html(&replace_html);
      true
  });
  assert_eq!(cur_list_items.children("b").length(), 4);
  assert_eq!(cur_list_items.text(), "@!@!@!@!");
  Ok(())
}

Implementations

load the html with options, get an elements collection

load the html with options, and catch the errors

load the html into elements

load the html, and catch the errors

return an elements collection from an BoxDynElement

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.