Struct visdom::Vis[][src]

pub struct Vis;

Vis: Entry struct of the mesdoc's api.

Examples

use visdom::Vis;
use std::error::Error;
fn main()-> Result<(), Box<dyn Error>>{
  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

impl Vis[src]

pub fn load(html: &str) -> Result<Elements<'_>, Box<dyn Error>>[src]

pub fn load_catch(html: &str, handle: IErrorHandle) -> Elements<'_>[src]

pub fn dom<'b>(node: &BoxDynElement<'_>) -> Elements<'b>[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,