moar sciter util functions

This commit is contained in:
Jake Howard 2017-09-01 22:37:54 +01:00
parent c6ada504e4
commit c4a074183c
Signed by: jake
GPG key ID: 57AFB45680EDD477
2 changed files with 28 additions and 5 deletions

View file

@ -1,12 +1,12 @@
use renderers::{sciter_start, get_html};
use renderers::{sciter_start, get_html, destroy_matching};
use config::Config;
pub fn html_cleanup(config: Config, input: String) -> Result<String, String> {
let mut root = sciter_start(input);
root.find_first("meta[content='text/css']").unwrap().unwrap().destroy().unwrap();
root.find_first("style").unwrap().unwrap().destroy().unwrap();
root.find_first("title").unwrap().unwrap().destroy().unwrap();
destroy_matching(&mut root, "meta[content='text/css']");
destroy_matching(&mut root, "style");
destroy_matching(&mut root, "title");
let html = get_html(root);
return Ok(html);
}

View file

@ -19,5 +19,28 @@ fn sciter_start(source: String) -> Element {
}
fn get_html(element: Element) -> String {
return String::from_utf8(element.get_html(true)).expect("Failed to get HTML from element");
return String::from_utf8(element.get_html(true)).expect(&format!(
"Failed to get HTML from {}.",
element.get_tag()
));
}
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
)
);
}
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() {
return;
}
for mut ele in matches.unwrap() {
ele.destroy().expect("Failed to delete");
}
}