More sciter util functions

This commit is contained in:
Jake Howard 2017-09-05 21:33:13 +01:00
parent 5724fa8a05
commit 14e46489ab
Signed by: jake
GPG key ID: 57AFB45680EDD477

View file

@ -1,7 +1,4 @@
use config::Config;
use sciter::{Window, Host, Element};
use std::rc::Rc;
use sciter::{Window, Element};
use std::ops::Deref;
pub mod html_cleanup;
@ -19,28 +16,38 @@ fn sciter_start(source: String) -> Element {
}
fn get_html(element: Element) -> String {
element.update(true);
return String::from_utf8(element.get_html(true)).expect(&format!(
"Failed to get HTML from {}.",
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 {
return root.find_first(selector).expect(&format!("Failed to get {}.", selector)).expect(
&format!(
"Couldn't find any {}.",
selector
)
);
let mut all_matches = find_all(root, selector);
all_matches.reverse();
return all_matches.pop().expect(&format!("Failed to find {}.", 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) {
let mut matches = root.find_all(selector).expect(&format!("Failed to get {}.", selector));
if matches.is_none() {
let matches = find_all(root, selector);
if matches.is_empty() {
return;
}
for mut ele in matches.unwrap() {
for mut ele in matches {
ele.destroy().expect("Failed to delete");
}
}