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
impl Vis
sourcepub fn load_options<'html>(
html: impl Into<Cow<'html, str>>,
options: ParseOptions
) -> Result<Elements<'html>, BoxDynError>
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
sourcepub fn load_options_catch<'html>(
html: impl Into<Cow<'html, str>>,
options: ParseOptions,
handle: Box<dyn Fn(BoxDynError)>
) -> Elements<'html>
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
sourcepub fn load<'html>(
html: impl Into<Cow<'html, str>>
) -> Result<Elements<'html>, BoxDynError>
pub fn load<'html>( html: impl Into<Cow<'html, str>> ) -> Result<Elements<'html>, BoxDynError>
load the html into elements
sourcepub fn load_catch<'html>(
html: impl Into<Cow<'html, str>>,
handle: Box<dyn Fn(BoxDynError)>
) -> Elements<'html>
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
sourcepub fn dom<'b>(ele: &BoxDynElement<'_>) -> Elements<'b>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more