38 lines
755 B
Rust
38 lines
755 B
Rust
use std::str;
|
|
|
|
use scraper::{ElementRef, Html};
|
|
|
|
pub trait SelectExt {
|
|
fn element(&self) -> ElementRef;
|
|
|
|
fn easy_text(&self) -> String {
|
|
let data = self
|
|
.element()
|
|
.text()
|
|
.map(str::trim)
|
|
.filter(|s| !s.is_empty())
|
|
.collect::<Vec<_>>()
|
|
.join(" ");
|
|
|
|
htmlescape::decode_html(&data).unwrap_or(data)
|
|
}
|
|
|
|
fn easy_inner_html(&self) -> String {
|
|
let data = self.element().inner_html();
|
|
|
|
htmlescape::decode_html(&data).unwrap_or(data)
|
|
}
|
|
}
|
|
|
|
impl SelectExt for Html {
|
|
fn element(&self) -> ElementRef {
|
|
self.root_element()
|
|
}
|
|
}
|
|
|
|
impl<'a> SelectExt for ElementRef<'a> {
|
|
fn element(&self) -> ElementRef {
|
|
*self
|
|
}
|
|
}
|