More sciter util functions
This commit is contained in:
parent
5724fa8a05
commit
14e46489ab
1 changed files with 21 additions and 14 deletions
|
@ -1,7 +1,4 @@
|
||||||
use config::Config;
|
use sciter::{Window, Element};
|
||||||
|
|
||||||
use sciter::{Window, Host, Element};
|
|
||||||
use std::rc::Rc;
|
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
|
|
||||||
pub mod html_cleanup;
|
pub mod html_cleanup;
|
||||||
|
@ -19,28 +16,38 @@ fn sciter_start(source: String) -> Element {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_html(element: Element) -> String {
|
fn get_html(element: Element) -> String {
|
||||||
|
element.update(true);
|
||||||
return String::from_utf8(element.get_html(true)).expect(&format!(
|
return String::from_utf8(element.get_html(true)).expect(&format!(
|
||||||
"Failed to get HTML from {}.",
|
"Failed to get HTML from {}.",
|
||||||
element.get_tag()
|
element.get_tag()
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn find_all(root: &mut Element, selector: &str) -> Vec<Element> {
|
||||||
|
let elements = root.find_all(selector).expect(&format!("Failed to get {}.", selector));
|
||||||
|
if elements.is_none() {
|
||||||
|
return Vec::new();
|
||||||
|
}
|
||||||
|
return elements.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
fn find_first(root: &mut Element, selector: &str) -> Element {
|
fn find_first(root: &mut Element, selector: &str) -> Element {
|
||||||
return root.find_first(selector).expect(&format!("Failed to get {}.", selector)).expect(
|
let mut all_matches = find_all(root, selector);
|
||||||
&format!(
|
all_matches.reverse();
|
||||||
"Couldn't find any {}.",
|
return all_matches.pop().expect(&format!("Failed to find {}.", selector));
|
||||||
selector
|
}
|
||||||
)
|
|
||||||
);
|
fn destroy_at(root: &mut Element, index: usize) {
|
||||||
|
let mut ele = root.get(index).expect(&format!("Failed to get element at {}.", index));
|
||||||
|
ele.destroy().expect("Failed to delete.");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn destroy_matching(root: &mut Element, selector: &str) {
|
fn destroy_matching(root: &mut Element, selector: &str) {
|
||||||
let mut matches = root.find_all(selector).expect(&format!("Failed to get {}.", selector));
|
let matches = find_all(root, selector);
|
||||||
if matches.is_none() {
|
if matches.is_empty() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
for mut ele in matches {
|
||||||
for mut ele in matches.unwrap() {
|
|
||||||
ele.destroy().expect("Failed to delete");
|
ele.destroy().expect("Failed to delete");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue