From ffa60a0ce21cb05e48bac09ac9969c0daec6e812 Mon Sep 17 00:00:00 2001 From: Vincent Flyson Date: Fri, 23 Aug 2019 03:26:05 -0400 Subject: [PATCH] Convert action attribute to full URL for FORM tags --- Cargo.toml | 2 +- src/html.rs | 22 ++++++++++++++++++++-- src/main.rs | 6 ++++-- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1af5f2b..51a8d7e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "monolith" -version = "2.0.1" +version = "2.0.2" authors = ["Sunshine "] [dependencies] diff --git a/src/html.rs b/src/html.rs index e5acd45..d701d94 100644 --- a/src/html.rs +++ b/src/html.rs @@ -2,7 +2,7 @@ extern crate html5ever; use std::default::Default; use std::io; -use http::{retrieve_asset, resolve_url}; +use http::{is_url, retrieve_asset, resolve_url}; use self::html5ever::parse_document; use self::html5ever::rcdom::{Handle, NodeData, RcDom}; @@ -15,6 +15,7 @@ enum NodeMatch { StyleSheet, Anchor, Script, + Form, Other, } @@ -78,6 +79,8 @@ pub fn walk_and_embed_assets(url: &str, node: &Handle, opt_no_js: bool) { found = NodeMatch::Anchor; } else if &name.local == "script" { found = NodeMatch::Script; + } else if &name.local == "form" { + found = NodeMatch::Form; } match found { @@ -145,6 +148,20 @@ pub fn walk_and_embed_assets(url: &str, node: &Handle, opt_no_js: bool) { } } }, + NodeMatch::Form => { + for attr in attrs_mut.iter_mut() { + if &attr.name.local == "action" { + // Do not touch action props which are set to a URL + if is_url(&attr.value) { + continue; + } + + let href_full_url = resolve_url(&url, &attr.value.to_string()); + attr.value.clear(); + attr.value.push_slice(href_full_url.unwrap().as_str()); + } + } + }, NodeMatch::Other => {}, } @@ -174,7 +191,8 @@ pub fn html_to_dom(data: &str) -> html5ever::rcdom::RcDom { .unwrap() } -pub fn print_dom(handle: &Handle) { +pub fn print_dom(handle: &Handle, _opt_isolate: bool) { + // TODO: append to the if opt_isolate serialize(&mut io::stdout(), handle, SerializeOpts::default()).unwrap(); } diff --git a/src/main.rs b/src/main.rs index e4c564a..f628495 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ use monolith::html::{walk_and_embed_assets, html_to_dom, print_dom}; fn main() { let command = App::new("monolith") - .version("2.0.1") + .version("2.0.2") .author("Sunshine ") .about("CLI tool to save web pages as single HTML files") .arg(Arg::with_name("url") @@ -16,11 +16,13 @@ fn main() { .index(1) .help("URL to download")) .args_from_usage("-j, --nojs 'Remove JavaScript'") + // .args_from_usage("-i, --isolate 'Isolate the document'") .get_matches(); // Process the command let arg_target = command.value_of("url").unwrap(); let opt_no_js = command.is_present("nojs"); + let opt_isolate = command.is_present("isolate"); if is_url(arg_target) { let data = retrieve_asset(&arg_target, false, ""); @@ -28,6 +30,6 @@ fn main() { walk_and_embed_assets(&arg_target, &dom.document, opt_no_js); - print_dom(&dom.document); + print_dom(&dom.document, opt_isolate); } }