From 14e46489ab6c8a3409c298ac4f75c6f3a8071097 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Tue, 5 Sep 2017 21:33:13 +0100 Subject: [PATCH] More sciter util functions --- src/renderers/mod.rs | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/renderers/mod.rs b/src/renderers/mod.rs index ea6efe5..1f9eeef 100644 --- a/src/renderers/mod.rs +++ b/src/renderers/mod.rs @@ -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 { + 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"); } }