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 destroyed 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§

source§

impl Vis

source

pub fn load_options<'html>( html: impl Into<Cow<'html, str>>, options: ParseOptions ) -> Result<Elements<'html>, BoxDynError>

load the html with options, get an elements collection

source

pub fn load_options_catch<'html>( html: impl Into<Cow<'html, str>>, options: ParseOptions, handle: Box<dyn Fn(BoxDynError)> ) -> Elements<'html>

load the html with options, and catch the errors

source

pub fn load<'html>( html: impl Into<Cow<'html, str>> ) -> Result<Elements<'html>, BoxDynError>

load the html into elements

source

pub fn load_catch<'html>( html: impl Into<Cow<'html, str>>, handle: Box<dyn Fn(BoxDynError)> ) -> Elements<'html>

load the html, and catch the errors

source

pub fn dom<'b>(ele: &BoxDynElement<'_>) -> Elements<'b>

return an elements collection from an BoxDynElement

Auto Trait Implementations§

§

impl Freeze for Vis

§

impl RefUnwindSafe for Vis

§

impl Send for Vis

§

impl Sync for Vis

§

impl Unpin for Vis

§

impl UnwindSafe for Vis

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.