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, 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");
} }
} }