pub struct Elements<'a> { /* private fields */ }
Implementations§
source§impl<'a> Elements<'a>
impl<'a> Elements<'a>
pub fn new() -> Self
pub fn with_nodes(nodes: Vec<BoxDynElement<'a>>) -> Self
pub fn with_capacity(size: usize) -> Self
pub fn get(&self, index: usize) -> Option<&BoxDynElement<'a>>
pub fn get_ref(&self) -> &Vec<BoxDynElement<'a>>
source§impl<'a> Elements<'a>
impl<'a> Elements<'a>
sourcepub fn for_each<F>(&mut self, handle: F) -> &mut Self
pub fn for_each<F>(&mut self, handle: F) -> &mut Self
Iterate over the element in Elements, when the handle return false, stop the iterator.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<ul>
<li>item1</li>
<li>item2</li>
<li>item3</li>
</ul>
"##;
let doc = Vis::load(html)?;
let mut items = doc.find("ul > li");
assert_eq!(items.length(), 3);
let mut items_texts:Vec<String> = vec![];
items.for_each(|index, ele| {
if index < 2{
items_texts.push(ele.text());
return true;
}
false
});
assert_eq!(items_texts.len(), 2);
assert_eq!(items_texts.join(","), "item1,item2");
Ok(())
}
sourcepub fn map<F, T: Sized>(&self, handle: F) -> Vec<T>
pub fn map<F, T: Sized>(&self, handle: F) -> Vec<T>
Get a collection of values by iterate the each element in Elements and call the handle
function.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<ul>
<li>item1</li>
<li>item2</li>
<li>item3</li>
</ul>
"##;
let doc = Vis::load(html)?;
let mut items = doc.find("ul > li");
assert_eq!(items.length(), 3);
let items_texts: Vec<String> = items.map(|index, ele| {
ele.text()
});
assert_eq!(items_texts.len(), 3);
assert_eq!(items_texts.join(","), "item1,item2,item3");
Ok(())
}
sourcepub fn length(&self) -> usize
pub fn length(&self) -> usize
Return the length of the Elements set.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<ul>
<li>item1</li>
<li>item2</li>
<li>item3</li>
</ul>
"##;
let doc = Vis::load(html)?;
let mut items = doc.find("ul > li:contains('item3')");
assert_eq!(items.length(), 1);
Ok(())
}
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Check if the Elements is empty, it’s a short alias for .length() == 0
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<ul>
<li>item1</li>
<li>item2</li>
<li>item3</li>
</ul>
"##;
let doc = Vis::load(html)?;
let mut items = doc.find("ul > li:empty");
assert!(items.is_empty());
assert_eq!(items.length(), 0);
Ok(())
}
sourcepub fn document(&self) -> Option<Box<dyn IDocumentTrait + '_>>
pub fn document(&self) -> Option<Box<dyn IDocumentTrait + '_>>
A quick way to get the document object when the loaded html is a document.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<ul>
<li>item1</li>
<li>item2</li>
<li>item3</li>
</ul>
</body>
</html>
"##;
let root = Vis::load(html)?;
let mut document = root.document();
assert!(document.is_some());
assert_eq!(document.unwrap().title(), Some(String::from("document")));
Ok(())
}
source§impl<'a> Elements<'a>
impl<'a> Elements<'a>
sourcepub fn find(&self, selector: &str) -> Elements<'a>
pub fn find(&self, selector: &str) -> Elements<'a>
Get the descendants of each element in the Elements, filtered by the selector
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<ul>
<li>item1</li>
<li>item2</li>
<li>
<ol>
<li>subitem1</li>
<li>subitem2</li>
</ol>
</li>
</ul>
</body>
</html>
"##;
let doc = Vis::load(html)?;
assert_eq!(doc.find("ul").length(), 1);
assert_eq!(doc.find("ul li").length(), 5);
assert_eq!(doc.find("ul > li").length(), 3);
assert_eq!(doc.find("ul li:first-child").text(), "item1subitem1");
Ok(())
}
sourcepub fn filter(&self, selector: &str) -> Elements<'a>
pub fn filter(&self, selector: &str) -> Elements<'a>
Reduce the Elements to those that match the selector.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<ul>
<li>item1</li>
<li class="item2">item2</li>
<li>item3</li>
</ul>
</body>
</html>
"##;
let doc = Vis::load(html)?;
let items = doc.find("li");
assert_eq!(items.length(), 3);
assert_eq!(items.filter("[class]").length(), 1);
assert_eq!(items.filter("[class]").text(), "item2");
assert_eq!(items.filter("li:contains('item3')").length(), 1);
assert_eq!(items.filter("li:contains('item3')").text(), "item3");
Ok(())
}
sourcepub fn filter_by<F>(&self, handle: F) -> Elements<'a>
pub fn filter_by<F>(&self, handle: F) -> Elements<'a>
Reduce the Elements to those that pass the handle function test.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<ul>
<li>item1</li>
<li class="item2">item2</li>
<li>item3</li>
</ul>
</body>
</html>
"##;
let doc = Vis::load(html)?;
let items = doc.find("li");
assert_eq!(items.length(), 3);
let class_items = items.filter_by(|_, ele| ele.get_attribute("class").is_some());
assert_eq!(class_items.length(), 1);
assert_eq!(class_items.text(), "item2");
Ok(())
}
sourcepub fn filter_in(&self, search: &Elements<'_>) -> Elements<'a>
pub fn filter_in(&self, search: &Elements<'_>) -> Elements<'a>
Reduce the Elements to those that also in the searched Elements.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<ul>
<li></li>
<li class="item2">item2</li>
<li class="item3"></li>
</ul>
</body>
</html>
"##;
let doc = Vis::load(html)?;
let items = doc.find("li");
assert_eq!(items.length(), 3);
let empty_items = items.filter(":empty");
let class_items = items.filter("[class]");
assert_eq!(empty_items.length(), 2);
assert_eq!(class_items.length(), 2);
// has class and also empty
let class_and_empty_items = class_items.filter_in(&empty_items);
assert_eq!(class_and_empty_items.length(), 1);
assert_eq!(class_and_empty_items.attr("class").unwrap().to_string(), "item3");
Ok(())
}
sourcepub fn children(&self, selector: &str) -> Elements<'a>
pub fn children(&self, selector: &str) -> Elements<'a>
Get the children of each element in Elements, when the selector is not empty, will filtered by the selector
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<dl>
<dt>Title</dt>
<dd>item1</dd>
<dd class="item2">item2</dd>
<dd class="item3">
<dl>
<dd>subitem1</dd>
<dd>subitem2</dd>
</dl>
</dd>
</dl>
</body>
</html>
"##;
let doc = Vis::load(html)?;
let top_dl = doc.find("dl").eq(0);
let all_dd = top_dl.find("dd");
assert_eq!(all_dd.length(), 5);
let child_items = top_dl.children("");
assert_eq!(child_items.length(), 4);
let child_dd = top_dl.children("dd");
assert_eq!(child_dd.length(),3);
Ok(())
}
sourcepub fn prev(&self, selector: &str) -> Elements<'a>
pub fn prev(&self, selector: &str) -> Elements<'a>
Get the previous sibling of each element in Elements, when the selector is not empty, will filtered by the selector.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<dl>
<dt>Title</dt>
<dd>item1</dd>
<dd class="item2">item2</dd>
<dd class="item3">item3</dd>
</dl>
</body>
</html>
"##;
let doc = Vis::load(html)?;
let dl = doc.find("dl");
let item3 = dl.children(".item3");
assert_eq!(item3.prev("").text(), "item2");
assert_eq!(item3.prev(":not[class]").is_empty(),true);
Ok(())
}
sourcepub fn prev_all(&self, selector: &str) -> Elements<'a>
pub fn prev_all(&self, selector: &str) -> Elements<'a>
Get all preceding siblings of each element in Elements, when the selector is not empty, will filtered by the selector.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<dl>
<dt>Title</dt>
<dd>item1</dd>
<dd class="item2">item2</dd>
<dd class="item3">item3</dd>
</dl>
</body>
</html>
"##;
let doc = Vis::load(html)?;
let dl = doc.find("dl");
let item3 = dl.children(".item3");
assert_eq!(item3.prev_all("").length(), 3);
assert_eq!(item3.prev_all("dd").length(),2);
assert_eq!(item3.prev_all("dt").length(),1);
assert_eq!(item3.prev_all("dd[class]").length(),1);
Ok(())
}
sourcepub fn prev_until(
&self,
selector: &str,
filter: &str,
contains: bool
) -> Elements<'a>
pub fn prev_until( &self, selector: &str, filter: &str, contains: bool ) -> Elements<'a>
Get all preceding siblings of each element in Elements, until the previous sibling element matched the selector, when contains is true, the matched previous sibling will be included, otherwise it will exclude; when the filter is not empty, will filtered by the selector;
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<dl>
<dt>Title</dt>
<dd>item1</dd>
<dd class="item2">item2</dd>
<dd class="item3">item3</dd>
</dl>
</body>
</html>
"##;
let doc = Vis::load(html)?;
let dl = doc.find("dl");
let item3 = dl.children(".item3");
assert_eq!(item3.prev_until("dt", "", false).length(), 2);
assert_eq!(item3.prev_until("dt", "", false).eq(0).is("dd"), true);
assert_eq!(item3.prev_until("dt", "", true).length(), 3);
assert_eq!(item3.prev_until("dt", "", true).eq(0).is("dt"), true);
assert_eq!(item3.prev_until("dt", "dd", true).length(), 2);
assert_eq!(item3.prev_until("dt", "dd", true).eq(0).is("dd"), true);
Ok(())
}
sourcepub fn next(&self, selector: &str) -> Elements<'a>
pub fn next(&self, selector: &str) -> Elements<'a>
Get the next sibling of each element in Elements, when the selector is not empty, will filtered by the selector.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<dl>
<dt>Title</dt>
<dd>item1</dd>
<dd class="item2">item2</dd>
<dd class="item3">item3</dd>
</dl>
</body>
</html>
"##;
let doc = Vis::load(html)?;
let dl = doc.find("dl");
let dt = dl.children("dt");
assert_eq!(dt.next("").text(), "item1");
assert_eq!(dt.next("[class]").is_empty(), true);
Ok(())
}
sourcepub fn next_all(&self, selector: &str) -> Elements<'a>
pub fn next_all(&self, selector: &str) -> Elements<'a>
Get all following siblings of each element in Elements, when the selector is not empty, will filtered by the selector.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<dl>
<dt>Title</dt>
<dd>item1</dd>
<dd class="item2">item2</dd>
<dd class="item3">item3</dd>
</dl>
</body>
</html>
"##;
let doc = Vis::load(html)?;
let dl = doc.find("dl");
let dt = dl.children("dt");
assert_eq!(dt.next_all("").length(), 3);
assert_eq!(dt.next_all("[class]").length(), 2);
assert_eq!(dt.next_all("[class]").text(), "item2item3");
Ok(())
}
sourcepub fn next_until(
&self,
selector: &str,
filter: &str,
contains: bool
) -> Elements<'a>
pub fn next_until( &self, selector: &str, filter: &str, contains: bool ) -> Elements<'a>
Get all following siblings of each element in Elements, until the sibling element matched the selector, when contains is true, the matched sibling will be included, otherwise it will exclude; when the filter is not empty, will filtered by the selector;
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<dl>
<dt>Title</dt>
<dd>item1</dd>
<dd class="item2">item2</dd>
<dd class="item3">item3</dd>
</dl>
</body>
</html>
"##;
let doc = Vis::load(html)?;
let dl = doc.find("dl");
let dt = dl.children("dt");
assert_eq!(dt.next_until(".item3", "", false).length(), 2);
assert_eq!(dt.next_until(".item3", "", true).length(), 3);
assert_eq!(dt.next_until(".item3", "[class]", false).length(), 1);
assert_eq!(dt.next_until(".item3", "[class]", true).length(), 2);
Ok(())
}
sourcepub fn siblings(&self, selector: &str) -> Elements<'a>
pub fn siblings(&self, selector: &str) -> Elements<'a>
Get the siblings of each element in Elements, when the selector is not empty, will filtered by the selector.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<dl>
<dt>Title</dt>
<dd>item1</dd>
<dd class="item2">item2</dd>
<dd class="item3">item3</dd>
</dl>
</body>
</html>
"##;
let doc = Vis::load(html)?;
let dl = doc.find("dl");
let item2 = dl.children(".item2");
assert_eq!(item2.siblings("").length(), 3);
assert_eq!(item2.siblings("").first().is("dt"), true);
assert_eq!(item2.siblings("dd").first().text(), "item1");
Ok(())
}
sourcepub fn parent(&self, selector: &str) -> Elements<'a>
pub fn parent(&self, selector: &str) -> Elements<'a>
Get the parent of each element in Elements, when the selector is not empty, will filtered by the selector.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<dl>
<dt>Title</dt>
<dd>item1</dd>
<dd class="item2">item2</dd>
<dd class="item3">item3</dd>
</dl>
</body>
</html>
"##;
let doc = Vis::load(html)?;
let dl = doc.find("dl");
let item2 = dl.children(".item2");
assert_eq!(item2.parent("").length(), 1);
assert_eq!(item2.parent("").get(0).unwrap().tag_name(), "DL");
assert_eq!(item2.parent("ul").length(), 0);
Ok(())
}
sourcepub fn parents(&self, selector: &str) -> Elements<'a>
pub fn parents(&self, selector: &str) -> Elements<'a>
Get the ancestors of each element in Elements, when the selector is not empty, will filtered by the selector.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<dl>
<dt>Title</dt>
<dd>item1</dd>
<dd class="item2">item2</dd>
<dd class="item3">item3</dd>
</dl>
</body>
</html>
"##;
let doc = Vis::load(html)?;
let dl = doc.find("dl");
let item2 = dl.children(".item2");
assert_eq!(item2.parents("").length(), 3);
assert_eq!(item2.parents("").first().is("html"), true);
assert_eq!(item2.parents("").last().is("dl"), true);
assert_eq!(item2.parents("dl").length(), 1);
Ok(())
}
sourcepub fn parents_until(
&self,
selector: &str,
filter: &str,
contains: bool
) -> Elements<'a>
pub fn parents_until( &self, selector: &str, filter: &str, contains: bool ) -> Elements<'a>
Get the ancestors of each element in Elements, until the ancestor matched the selector, when contains is true, the matched ancestor will be included, otherwise it will exclude; when the filter is not empty, will filtered by the selector;
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<dl>
<dt>Title</dt>
<dd>item1</dd>
<dd class="item2">item2</dd>
<dd class="item3">item3</dd>
</dl>
</body>
</html>
"##;
let doc = Vis::load(html)?;
let dl = doc.find("dl");
let item2 = dl.children(".item2");
assert_eq!(item2.parents_until("body", "", false).length(), 1);
assert_eq!(item2.parents_until("body", "", true).length(), 2);
assert_eq!(item2.parents_until("body", "dl", true).length(), 1);
Ok(())
}
sourcepub fn closest(&self, selector: &str) -> Elements<'a>
pub fn closest(&self, selector: &str) -> Elements<'a>
Get the first matched element of each element in Elements, traversing from self to it’s ancestors.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<dl>
<dt>Title</dt>
<dd>item1</dd>
<dd class="item2">item2</dd>
<dd class="item3">item3</dd>
</dl>
</body>
</html>
"##;
let doc = Vis::load(html)?;
let dl = doc.find("dl");
let item2 = dl.children(".item2");
assert_eq!(item2.closest("dd").length(), 1);
assert_eq!(item2.closest("dd").is_all_in(&item2), true);
assert_eq!(item2.closest("dl").is_all_in(&item2.parent("")), true);
Ok(())
}
sourcepub fn cloned(&self) -> Elements<'a>
pub fn cloned(&self) -> Elements<'a>
This method will be removed in future versions.
If you want to clone an elements set, please use the clone()
method instead.
This method only clone the element’s Rc
pointer in the elements set.
Any modifications to the cloned elements set will be reflected on the original elements set.
sourcepub fn contains(&self, ele: &BoxDynElement<'_>, comb: &Combinator) -> bool
pub fn contains(&self, ele: &BoxDynElement<'_>, comb: &Combinator) -> bool
pub fn contains
sourcepub fn is(&self, selector: &str) -> bool
pub fn is(&self, selector: &str) -> bool
Check at least one element in Elements is match the selector.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<dl>
<dt>Title</dt>
<dd>item1</dd>
<dd class="item2">item2</dd>
<dd class="item3">item3</dd>
</dl>
</body>
</html>
"##;
let doc = Vis::load(html)?;
let dl = doc.find("dl");
let items = dl.children("");
assert_eq!(items.is("dd"), true);
assert_eq!(items.is("dt"), true);
assert_eq!(items.is(".item2"), true);
assert_eq!(items.is(".item3"), true);
assert_eq!(items.is(":contains('item2')"), true);
Ok(())
}
sourcepub fn is_by<F>(&self, handle: F) -> bool
pub fn is_by<F>(&self, handle: F) -> bool
Check at least one element in Elements call the handle function return true.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<dl>
<dt>Title</dt>
<dd>item1</dd>
<dd class="item2">item2</dd>
<dd class="item3">item3</dd>
</dl>
</body>
</html>
"##;
let doc = Vis::load(html)?;
let dl = doc.find("dl");
let items = dl.children("");
assert_eq!(items.is_by(|_, ele|{
ele.tag_name() == "DT"
}), true);
assert_eq!(items.is_by(|_, ele|{
ele.tag_name() == "DD"
}), true);
assert_eq!(items.is_by(|_, ele|{
Vis::dom(ele).has_class("item2")
}), true);
Ok(())
}
sourcepub fn is_in(&self, search: &Elements<'_>) -> bool
pub fn is_in(&self, search: &Elements<'_>) -> bool
Check at least one element in Elements is also in the other Elements set.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<dl>
<dt>Title</dt>
<dd>item1</dd>
<dd class="item2">item2</dd>
<dd class="item3">item3</dd>
</dl>
</body>
</html>
"##;
let doc = Vis::load(html)?;
let dl = doc.find("dl");
let items = dl.children("");
let dd = items.filter("dd");
let dt = items.filter("dt");
assert_eq!(items.is_in(&dd), true);
assert_eq!(items.is_in(&dt), true);
Ok(())
}
sourcepub fn is_all(&self, selector: &str) -> bool
pub fn is_all(&self, selector: &str) -> bool
Check if each element in Elements are all matched the selector.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<dl>
<dt>Title</dt>
<dd>item1</dd>
<dd class="item2">item2</dd>
<dd class="item3">item3</dd>
</dl>
</body>
</html>
"##;
let doc = Vis::load(html)?;
let dl = doc.find("dl");
let items = dl.children("");
assert_eq!(items.is_all("dd"), false);
assert_eq!(items.is_all("dt"), false);
assert_eq!(items.is_all("dt,dd"), true);
Ok(())
}
sourcepub fn is_all_by<F>(&self, handle: F) -> bool
pub fn is_all_by<F>(&self, handle: F) -> bool
Check if each element in Elements call the handle function are all returned true.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<dl>
<dt>Title</dt>
<dd>item1</dd>
<dd class="item2">item2</dd>
<dd class="item3">item3</dd>
</dl>
</body>
</html>
"##;
let doc = Vis::load(html)?;
let dl = doc.find("dl");
let items = dl.children("");
assert_eq!(items.is_all_by(|_, ele|{
ele.tag_name() == "DT"
}), false);
assert_eq!(items.is_all_by(|_, ele|{
ele.tag_name() == "DD"
}), false);
assert_eq!(items.is_all_by(|_, ele|{
let tag_name = ele.tag_name();
tag_name == "DT" || tag_name == "DD"
}), true);
Ok(())
}
sourcepub fn is_all_in(&self, search: &Elements<'_>) -> bool
pub fn is_all_in(&self, search: &Elements<'_>) -> bool
Check if each element in Elements is also in the other Elements set.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<dl>
<dt>Title</dt>
<dd>item1</dd>
<dd class="item2">item2</dd>
<dd class="item3">item3</dd>
</dl>
</body>
</html>
"##;
let doc = Vis::load(html)?;
let dl = doc.find("dl");
let items = dl.children("");
let dd = items.filter("dd");
let dt = items.filter("dt");
assert_eq!(items.is_all_in(&dd), false);
assert_eq!(items.is_all_in(&dt), false);
assert_eq!(dd.is_all_in(&items), true);
assert_eq!(dt.is_all_in(&items), true);
Ok(())
}
sourcepub fn not(&self, selector: &str) -> Elements<'a>
pub fn not(&self, selector: &str) -> Elements<'a>
Remove elements those that match the selector from the Elements set.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<dl>
<dt>Title</dt>
<dd>item1</dd>
<dd class="item2">item2</dd>
<dd class="item3">item3</dd>
</dl>
</body>
</html>
"##;
let doc = Vis::load(html)?;
let dl = doc.find("dl");
let items = dl.children("");
assert_eq!(items.not("dd").is_all("dt"), true);
assert_eq!(items.not("dt").is_all("dd"), true);
assert_eq!(items.not("dt,dd").is_empty(), true);
Ok(())
}
sourcepub fn not_by<F>(&self, handle: F) -> Elements<'a>
pub fn not_by<F>(&self, handle: F) -> Elements<'a>
Remove elements those that pass the handle function test from the Elements set.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<dl>
<dt>Title</dt>
<dd>item1</dd>
<dd class="item2">item2</dd>
<dd class="item3">item3</dd>
</dl>
</body>
</html>
"##;
let doc = Vis::load(html)?;
let dl = doc.find("dl");
let items = dl.children("");
assert_eq!(items.not_by(|_, ele|ele.tag_name() == "DD").is_all("dt"), true);
assert_eq!(items.not_by(|_, ele|ele.tag_name() == "DT").is_all("dd"), true);
assert_eq!(items.not_by(|_, ele|ele.tag_name() == "DT" || ele.tag_name() == "DD").is_empty(), true);
Ok(())
}
sourcepub fn not_in(&self, search: &Elements<'_>) -> Elements<'a>
pub fn not_in(&self, search: &Elements<'_>) -> Elements<'a>
Remove elements those that also in the elements from the Elements set.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<dl>
<dt>Title</dt>
<dd>item1</dd>
<dd class="item2">item2</dd>
<dd class="item3">item3</dd>
</dl>
</body>
</html>
"##;
let doc = Vis::load(html)?;
let dl = doc.find("dl");
let items = dl.children("");
assert_eq!(items.not_in(&items.filter("dd")).is_all("dt"), true);
assert_eq!(items.not_in(&items.filter("dt")).is_all("dd"), true);
assert_eq!(items.not_in(&items).is_empty(), true);
Ok(())
}
sourcepub fn has(&self, selector: &str) -> Elements<'a>
pub fn has(&self, selector: &str) -> Elements<'a>
Reduce Elements to those that have a descendant that matches the selector.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<dl>
<dt><<strong>T</strong>itle></dt>
<dd><span>item1</span></dd>
<dd class="item2">item2</dd>
<dd class="item3">item3</dd>
</dl>
</body>
</html>
"##;
let doc = Vis::load(html)?;
let dl = doc.find("dl");
let items = dl.children("");
assert_eq!(items.filter("dt").text(), "<Title>");
assert_eq!(items.filter("dd").text(), "item1item2item3");
Ok(())
}
sourcepub fn has_in(&self, search: &Elements<'_>) -> Elements<'a>
pub fn has_in(&self, search: &Elements<'_>) -> Elements<'a>
Reduce Elements to those that have a descendant that matches the selector.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<dl>
<dt>Title</dt>
<dd><span>item1</span></dd>
<dd class="item2"><span>item2</span></dd>
<dd class="item3">item3</dd>
</dl>
</body>
</html>
"##;
let doc = Vis::load(html)?;
let dl = doc.find("dl");
let items = dl.children("");
assert_eq!(items.length(), 4);
assert_eq!(items.has_in(&items.children("span")).length(), 2);
assert_eq!(dl.has_in(&items).length(), 1);
Ok(())
}
source§impl<'a> Elements<'a>
impl<'a> Elements<'a>
sourcepub fn eq(&self, index: usize) -> Elements<'a>
pub fn eq(&self, index: usize) -> Elements<'a>
Get one element from the Elements at the specified index.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<dl>
<dt>Title</dt>
<dd><span>item1</span></dd>
<dd class="item2"><span>item2</span></dd>
<dd class="item3">item3</dd>
</dl>
</body>
</html>
"##;
let doc = Vis::load(html)?;
let dl = doc.find("dl");
let items = dl.children("");
assert_eq!(items.eq(0).is("dt"), true);
assert_eq!(items.eq(2).has_class("item2"), true);
Ok(())
}
sourcepub fn first(&self) -> Elements<'a>
pub fn first(&self) -> Elements<'a>
Get the first element of the Elements set,equal to eq(0).
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<dl>
<dt>Title</dt>
<dd><span>item1</span></dd>
<dd class="item2"><span>item2</span></dd>
<dd class="item3">item3</dd>
</dl>
</body>
</html>
"##;
let doc = Vis::load(html)?;
let dl = doc.find("dl");
let items = dl.children("");
assert_eq!(items.first().is_all_in(&items.eq(0)), true);
assert_eq!(items.first().is("dt"), true);
Ok(())
}
sourcepub fn last(&self) -> Elements<'a>
pub fn last(&self) -> Elements<'a>
Get the last element of the set, equal to eq(length - 1).
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<dl>
<dt>Title</dt>
<dd><span>item1</span></dd>
<dd class="item2"><span>item2</span></dd>
<dd class="item3">item3</dd>
</dl>
</body>
</html>
"##;
let doc = Vis::load(html)?;
let dl = doc.find("dl");
let items = dl.children("");
assert_eq!(items.last().is_all_in(&items.eq(items.length()-1)), true);
assert_eq!(items.last().is(".item3"), true);
Ok(())
}
sourcepub fn slice<T: RangeBounds<usize>>(&self, range: T) -> Elements<'a>
pub fn slice<T: RangeBounds<usize>>(&self, range: T) -> Elements<'a>
Get a subset specified by a range of indices.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<dl>
<dt>Title</dt>
<dd><span>item1</span></dd>
<dd class="item2"><span>item2</span></dd>
<dd class="item3">item3</dd>
</dl>
</body>
</html>
"##;
let doc = Vis::load(html)?;
let dl = doc.find("dl");
let items = dl.children("");
assert_eq!(items.slice(..).length(), 4);
assert_eq!(items.slice(0..3).length(), 3);
assert_eq!(items.slice(0..=3).length(), 4);
assert_eq!(items.slice(0..10).length(), 4);
Ok(())
}
sourcepub fn add(&self, eles: Elements<'a>) -> Elements<'a>
pub fn add(&self, eles: Elements<'a>) -> Elements<'a>
Get a concated element set from Elements and the other parameter elements, it will generate a new element set, take the ownership of the parameter elements, but have no sence with the Elements itself.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<dl>
<dt>Title</dt>
<dd><span>item1</span></dd>
<dd class="item2"><span>item2</span></dd>
<dd class="item3">item3</dd>
</dl>
</body>
</html>
"##;
let doc = Vis::load(html)?;
let dl = doc.find("dl");
let items = dl.children("");
let dt = items.filter("dt");
let class_dd = items.filter("[class]");
assert_eq!(dt.length(), 1);
assert_eq!(class_dd.length(), 2);
let add_dt_dd = dt.add(class_dd);
assert_eq!(add_dt_dd.length(), 3);
Ok(())
}
source§impl<'a> Elements<'a>
impl<'a> Elements<'a>
sourcepub fn val(&self) -> IFormValue
pub fn val(&self) -> IFormValue
Get the form value of input, select, option, textarea.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<input type="text" value="textvalue" />
<select name="single">
<option value="default"></option>
<option value="opt1">opt1</option>
<option value="opt2">opt1</option>
</select>
<select multiple>
<option value="default"></option>
<option value="opt1" selected="selected">opt1</option>
<option value="opt2" selected="selected">opt1</option>
</select>
<textarea><div>hello</div></textarea>
</body>
</html>
"##;
let doc = Vis::load(html)?;
let input = doc.find("input[type='text']");
assert_eq!(input.val().to_string(), "textvalue");
let select = doc.find("select[name='single']");
assert_eq!(select.val().to_string(), "default");
let multi_select = doc.find("select[multiple]");
assert_eq!(multi_select.val().to_string(), "opt1,opt2");
let textarea = doc.find("textarea");
assert_eq!(textarea.val().to_string(), "<div>hello</div>");
Ok(())
}
sourcepub fn text(&self) -> String
pub fn text(&self) -> String
Get the text of each element in Elements,the html entity will auto decoded.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<dl>
<dt>Title</dt>
<dd><span>item1</span></dd>
<dd class="item2"><span>item2</span></dd>
<dd class="item3">item3</dd>
</dl>
</body>
</html>
"##;
let doc = Vis::load(html)?;
let dl = doc.find("dl");
let items = dl.children("");
let dt = items.filter("dt");
let class_dd = items.filter("[class]");
assert_eq!(dt.length(), 1);
assert_eq!(class_dd.length(), 2);
let add_dt_dd = dt.add(class_dd);
assert_eq!(add_dt_dd.length(), 3);
Ok(())
}
sourcepub fn set_text(&mut self, content: &str) -> &mut Self
pub fn set_text(&mut self, content: &str) -> &mut Self
Set the Elements’s text, the html entity in content will auto encoded.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>set_text()</title>
</head>
<body>
<dl>
<dt>Title</dt>
<dd><span>item1</span></dd>
<dd class="item2"><span>item2</span></dd>
<dd class="item3">item3</dd>
</dl>
</body>
</html>
"##;
let doc = Vis::load(html)?;
let dl = doc.find("dl");
let items = dl.children("");
let mut dt = items.filter("dt");
dt.set_text("<Title>");
assert_eq!(dt.text(), "<Title>");
assert_eq!(dt.html(), "<Title>");
let mut item2 = items.filter(".item2");
assert_eq!(item2.html(), "<span>item2</span>");
item2.set_text("item2");
assert_eq!(item2.html(), "item2");
Ok(())
}
sourcepub fn html(&self) -> String
pub fn html(&self) -> String
Get the html of the first element in Elements.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>html()</title>
</head>
<body>
<dl>
<dt>Title</dt>
<dd><span>item1</span></dd>
<dd class="item2"><span>item2</span></dd>
<dd class="item3"><!--comment-->item3</dd>
</dl>
</body>
</html>
"##;
let doc = Vis::load(html)?;
let dl = doc.find("dl");
let items = dl.children("");
let item2 = items.filter(".item2");
assert_eq!(item2.html(), "<span>item2</span>");
let item3 = items.filter(".item3");
assert_eq!(item3.html(), "<!--comment-->item3");
Ok(())
}
sourcepub fn htmls(&self) -> String
pub fn htmls(&self) -> String
Get the combined html of all the elements in Elements.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>htmls()</title>
</head>
<body>
<div><span class="div1span">span1</span></div>
<div><span class="div2span">span2</span></div>
</body>
</html>
"##;
let doc = Vis::load(html)?;
let divs = doc.find("div");
assert_eq!(divs.htmls(), r#"<span class="div1span">span1</span><span class="div2span">span2</span>"#);
assert_eq!(doc.find("p").htmls(), "");
Ok(())
}
sourcepub fn set_html(&mut self, content: &str) -> &mut Self
pub fn set_html(&mut self, content: &str) -> &mut Self
Set the html to content of each element in Elements.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<dl>
<dt>Title</dt>
<dd><span>item1</span></dd>
<dd class="item2"><span>item2</span></dd>
<dd class="item3"><!--comment-->item3</dd>
</dl>
</body>
</html>
"##;
let doc = Vis::load(html)?;
let dl = doc.find("dl");
let items = dl.children("");
let mut item2 = items.filter(".item2");
assert_eq!(item2.html(), "<span>item2</span>");
item2.set_html("set_html:<em>item2</em>");
assert_eq!(item2.html(), "set_html:<em>item2</em>");
Ok(())
}
sourcepub fn outer_html(&self) -> String
pub fn outer_html(&self) -> String
Get the outer html of the first element in Elements.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<dl>
<dt>Title</dt>
<dd><span>item1</span></dd>
<dd class="item2"><span>item2</span></dd>
<dd class="item3"><!--comment-->item3</dd>
</dl>
</body>
</html>
"##;
let doc = Vis::load(html)?;
let dl = doc.find("dl");
let items = dl.children("");
let mut item2 = items.filter(".item2");
assert_eq!(item2.outer_html(), r#"<dd class="item2"><span>item2</span></dd>"#);
Ok(())
}
sourcepub fn outer_htmls(&self) -> String
pub fn outer_htmls(&self) -> String
Get the combined outer html of all the elements in Elements.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>outer_htmls()</title>
</head>
<body>
<div><span class="div1span">span1</span></div>
<div><span class="div2span">span2</span></div>
</body>
</html>
"##;
let doc = Vis::load(html)?;
let divs = doc.find("div");
assert_eq!(divs.outer_htmls(), r#"<div><span class="div1span">span1</span></div><div><span class="div2span">span2</span></div>"#);
assert_eq!(doc.find("p").outer_htmls(), "");
Ok(())
}
sourcepub fn texts(&self, limit_depth: usize) -> Texts<'a>
pub fn texts(&self, limit_depth: usize) -> Texts<'a>
pub fn texts
get the text node of each element
sourcepub fn texts_by(
&self,
limit_depth: usize,
handle: Box<dyn Fn(usize, &BoxDynText<'_>) -> bool>
) -> Texts<'a>
pub fn texts_by( &self, limit_depth: usize, handle: Box<dyn Fn(usize, &BoxDynText<'_>) -> bool> ) -> Texts<'a>
pub fn texts_by
get the text node of each element, filter by the handle
sourcepub fn texts_by_rec(
&self,
limit_depth: usize,
handle: Box<dyn Fn(usize, &BoxDynText<'_>) -> bool>,
rec_handle: Box<dyn Fn(&BoxDynElement<'_>) -> bool>
) -> Texts<'a>
pub fn texts_by_rec( &self, limit_depth: usize, handle: Box<dyn Fn(usize, &BoxDynText<'_>) -> bool>, rec_handle: Box<dyn Fn(&BoxDynElement<'_>) -> bool> ) -> Texts<'a>
pub fn texts_by_rec
get the text node of each element, filter by the handle, and check if need recursive by the result of rec_handle with child element
source§impl<'a> Elements<'a>
impl<'a> Elements<'a>
sourcepub fn attr(&self, attr_name: &str) -> Option<IAttrValue>
pub fn attr(&self, attr_name: &str) -> Option<IAttrValue>
Get an atrribute by name from the first element in Elements.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<input type="text" class="inp inp-txt" readonly />
</body>
</html>
"##;
let doc = Vis::load(html)?;
let input = doc.find("input");
let attr_readonly = input.attr("readonly");
assert!(attr_readonly.is_some() && attr_readonly.unwrap().is_true());
let attr_type = input.attr("type");
assert!(attr_type.is_some() && attr_type.unwrap().to_string() == "text");
let attr_class = input.attr("class");
assert!(attr_class.is_some() && attr_class.unwrap().to_list().contains(&"inp"));
Ok(())
}
sourcepub fn has_attr(&self, attr_name: &str) -> bool
pub fn has_attr(&self, attr_name: &str) -> bool
Check if has an attribute with specified name.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<input type="text" class="inp inp-txt" readonly />
</body>
</html>
"##;
let doc = Vis::load(html)?;
let input = doc.find("input");
assert!(input.has_attr("readonly"));
assert!(input.has_attr("class"));
assert!(input.has_attr("type"));
assert_eq!(input.has_attr("value"), false);
Ok(())
}
sourcepub fn set_attr(&mut self, attr_name: &str, value: Option<&str>) -> &mut Self
pub fn set_attr(&mut self, attr_name: &str, value: Option<&str>) -> &mut Self
Set a specified name attribute with a value who’s type is an Option<&str>
, when the value is None
,that means the attribute does’n have a string value but a bool value of true
.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<input type="text" class="inp inp-txt" readonly />
</body>
</html>
"##;
let doc = Vis::load(html)?;
let mut input = doc.find("input");
assert_eq!(input.has_attr("value"), false);
input.set_attr("value", None);
assert!(input.attr("value").is_some() && input.attr("value").unwrap().is_true());
input.set_attr("value", Some("myinput"));
assert!(input.attr("value").is_some() && input.attr("value").unwrap().to_string() == "myinput");
input.set_attr("value", Some(""));
assert!(input.attr("value").is_some() && input.attr("value").unwrap().is_true() == false);
Ok(())
}
sourcepub fn remove_attr(&mut self, attr_name: &str) -> &mut Self
pub fn remove_attr(&mut self, attr_name: &str) -> &mut Self
Remove a specified name attribute from the each element in the Elements.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<input type="text" class="inp inp-txt" readonly />
</body>
</html>
"##;
let doc = Vis::load(html)?;
let mut input = doc.find("input");
assert_eq!(input.has_attr("value"), false);
input.set_attr("value", None);
assert!(input.attr("value").is_some() && input.attr("value").unwrap().is_true());
input.remove_attr("value");
assert!(input.attr("value").is_none());
assert!(input.attr("readonly").is_some());
input.remove_attr("readonly");
assert!(input.attr("readonly").is_none());
Ok(())
}
sourcepub fn has_class(&self, class_name: &str) -> bool
pub fn has_class(&self, class_name: &str) -> bool
Check if Elements’s ClassList contains the specified class name, multiple classes can be splitted by whitespaces.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<input type="text" class="inp inp-txt" readonly />
</body>
</html>
"##;
let doc = Vis::load(html)?;
let input = doc.find("input");
assert_eq!(input.has_class("inp"), true);
assert_eq!(input.has_class("inp-txt"), true);
assert_eq!(input.has_class("inp-txt inp"), true);
assert_eq!(input.has_class("inp-txt inp noinp"), false);
Ok(())
}
sourcepub fn add_class(&mut self, class_name: &str) -> &mut Self
pub fn add_class(&mut self, class_name: &str) -> &mut Self
Add class to Elements’s ClassList, multiple classes can be splitted by whitespaces.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<input type="text" class="inp inp-txt" readonly />
</body>
</html>
"##;
let doc = Vis::load(html)?;
let mut input = doc.find("input");
assert_eq!(input.has_class("noinp"), false);
assert_eq!(input.has_class("inp-red"), false);
input.add_class("noinp inp-red");
assert_eq!(input.has_class("noinp"), true);
assert_eq!(input.has_class("inp-red"), true);
Ok(())
}
sourcepub fn remove_class(&mut self, class_name: &str) -> &mut Self
pub fn remove_class(&mut self, class_name: &str) -> &mut Self
Remove class from Elements’s ClassList, multiple classes can be splitted by whitespaces.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<input type="text" class="inp inp-txt" readonly />
</body>
</html>
"##;
let doc = Vis::load(html)?;
let mut input = doc.find("input");
assert_eq!(input.has_class("inp"), true);
assert_eq!(input.has_class("inp-txt"), true);
input.remove_class("inp inp-txt");
assert_eq!(input.has_class("inp"), false);
assert_eq!(input.has_class("inp-txt"), false);
assert_eq!(input.has_attr("class"), true);
Ok(())
}
sourcepub fn toggle_class(&mut self, class_name: &str) -> &mut Self
pub fn toggle_class(&mut self, class_name: &str) -> &mut Self
Toggle the class name from Elements’s ClassList, multiple classes can be splitted by whitespaces.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<input type="text" class="inp inp-txt" readonly />
</body>
</html>
"##;
let doc = Vis::load(html)?;
let mut input = doc.find("input");
assert_eq!(input.has_class("inp"), true);
assert_eq!(input.has_class("inp-txt"), true);
input.toggle_class("inp inp-red");
assert_eq!(input.has_class("inp"), false);
assert_eq!(input.has_class("inp-txt"), true);
assert_eq!(input.has_class("inp-red"), true);
Ok(())
}
source§impl<'a> Elements<'a>
impl<'a> Elements<'a>
sourcepub fn remove(self)
pub fn remove(self)
Remove the Elements set.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<dl>
<dt>Title</dt>
<dd><span>item1</span></dd>
<dd class="item2"><span>item2</span></dd>
<dd class="item3"><!--comment-->item3</dd>
</dl>
</body>
</html>
"##;
let doc = Vis::load(html)?;
let dl = doc.find("dl");
let items = dl.children("");
assert_eq!(items.length(), 4);
// remove the dt element
items.filter("dt").remove();
let now_items = dl.children("");
assert_eq!(now_items.length(), 3);
Ok(())
}
sourcepub fn empty(&mut self) -> &mut Self
pub fn empty(&mut self) -> &mut Self
Clear all the nodes in the Elements set.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<dl>
<dt>Title</dt>
<dd><span>item1</span></dd>
<dd class="item2"><span>item2</span></dd>
<dd class="item3"><!--comment-->item3</dd>
</dl>
</body>
</html>
"##;
let doc = Vis::load(html)?;
let mut dl = doc.find("dl");
let items = dl.children("");
assert_eq!(items.length(), 4);
// clear the dl
dl.empty();
let now_items = dl.children("");
assert_eq!(now_items.length(), 0);
Ok(())
}
sourcepub fn append(&mut self, elements: &mut Elements<'_>) -> &mut Self
pub fn append(&mut self, elements: &mut Elements<'_>) -> &mut Self
Append the parameter Elements to the child before the tag end of the current Elements set.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<dl>
<dt>Title</dt>
<dd><span>item1</span></dd>
<dd class="item2"><span>item2</span></dd>
<dd class="item3"><!--comment-->item3</dd>
</dl>
</body>
</html>
"##;
let doc = Vis::load(html)?;
let mut dl = doc.find("dl");
let items = dl.children("");
assert_eq!(items.length(), 4);
assert_eq!(items.last().is(".item3"), true);
// now append item2 to the last child
let mut item2 = items.filter(".item2");
dl.append(&mut item2);
let now_items = dl.children("");
assert_eq!(now_items.last().is(".item2"), true);
// append a new document fragement
let mut append_dd = Vis::load(r#"<dd class="item4">item4</dd>"#)?;
dl.append(&mut append_dd);
let now_items = dl.children("");
assert_eq!(now_items.last().is(".item4"), true);
Ok(())
}
sourcepub fn append_to(&mut self, elements: &mut Elements<'_>) -> &mut Self
pub fn append_to(&mut self, elements: &mut Elements<'_>) -> &mut Self
Same as append
, but exchange the caller and the parameter target.
sourcepub fn prepend(&mut self, elements: &mut Elements<'_>) -> &mut Self
pub fn prepend(&mut self, elements: &mut Elements<'_>) -> &mut Self
Append the parameter Elements to the child after the tag start of the current Elements set.
sourcepub fn prepend_to(&mut self, elements: &mut Elements<'_>) -> &mut Self
pub fn prepend_to(&mut self, elements: &mut Elements<'_>) -> &mut Self
Same as prepend
, but exchange the caller and the parameter target.
sourcepub fn insert_before(&mut self, elements: &mut Elements<'_>) -> &mut Self
pub fn insert_before(&mut self, elements: &mut Elements<'_>) -> &mut Self
Insert the each element in the current Elements set into the other Elements’s element’s before position.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<dl>
<dt>Title</dt>
<dd><span>item1</span></dd>
<dd class="item2"><span>item2</span></dd>
<dd class="item3"><!--comment-->item3</dd>
</dl>
</body>
</html>
"##;
let doc = Vis::load(html)?;
let mut dl = doc.find("dl");
let items = dl.children("");
assert_eq!(items.length(), 4);
assert_eq!(items.last().is(".item3"), true);
// now insert item3 before item2
let mut item2 = items.filter(".item2");
let mut item3 = items.filter(".item3");
item3.insert_before(&mut item2);
let now_items = dl.children("");
assert_eq!(now_items.last().is(".item2"), true);
// insert a new item0
let mut insert_dd = Vis::load(r#"<dd class="item0">item0</dd>"#)?;
let mut first_dd = dl.children("dd").first();
insert_dd.insert_before(&mut first_dd);
let now_dds = dl.children("dd");
assert_eq!(now_dds.first().is(".item0"), true);
Ok(())
}
sourcepub fn before(&mut self, elements: &mut Elements<'_>) -> &mut Self
pub fn before(&mut self, elements: &mut Elements<'_>) -> &mut Self
Same as insert_before
, but exchange the caller and the parameter target.
sourcepub fn insert_after(&mut self, elements: &mut Elements<'_>) -> &mut Self
pub fn insert_after(&mut self, elements: &mut Elements<'_>) -> &mut Self
Insert the each element in the current Elements set into the other Elements’s element’s after position.
sourcepub fn after(&mut self, elements: &mut Elements<'_>) -> &mut Self
pub fn after(&mut self, elements: &mut Elements<'_>) -> &mut Self
Same as insert_after
, but exchange the caller and the parameter target.
sourcepub fn replace_with(&mut self, elements: &mut Elements<'_>) -> &mut Self
pub fn replace_with(&mut self, elements: &mut Elements<'_>) -> &mut Self
Replace each element in the set of matched elements with the provided new set of elements.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<dl>
<dt>Title</dt>
<dd><span>item1</span></dd>
<dd class="item2"><span>item2</span></dd>
<dd class="item3"><!--comment-->item3</dd>
</dl>
</body>
</html>
"##;
let doc = Vis::load(html)?;
let mut dl = doc.find("dl");
let mut dt = dl.children("dt");
assert_eq!(dt.length(), 1);
assert_eq!(dl.children("dd").length(), 3);
// now replace dt with dd
let mut new_dd = Vis::load("<dd>replace</dd>")?;
dt.replace_with(&mut new_dd);
assert_eq!(dl.children("dd").length(), 4);
assert_eq!(dl.children("dt").length(), 0);
assert_eq!(dl.children("dd").eq(0).text(), "replace");
// replace with exist dd
let dds = dl.children("");
let mut first_dd = dds.first();
let mut last_dd = dds.last();
last_dd.replace_with(&mut first_dd);
assert_eq!(dl.children("").length(), 3);
assert_eq!(dl.children("").last().text(), "replace");
Ok(())
}
Trait Implementations§
source§impl<'a> Clone for Elements<'a>
impl<'a> Clone for Elements<'a>
Clone the Elements set.
use visdom::Vis;
use visdom::types::BoxDynError;
fn main()-> Result<(), BoxDynError>{
let html = r##"
<html>
<head>
<title>document</title>
</head>
<body>
<dl>
<dt>Title</dt>
<dd><span class="span">item1</span></dd>
<dd class="item2"><span>item2</span></dd>
<dd class="item3"><!--comment-->item3</dd>
</dl>
</body>
</html>
"##;
let doc = Vis::load(html)?;
let dl = doc.find("dl");
let span = dl.find("span.span");
assert_eq!(span.text(), "item1");
// clone the "dl" Elements
let clone_dl = dl.clone();
let mut clone_span = clone_dl.find("span.span");
clone_span.set_text("span");
assert_eq!(span.text(), "item1");
assert_eq!(clone_dl.find("span.span").text(), "span");
Ok(())
}