Convert action attribute to full URL for FORM tags

pull/10/head
Vincent Flyson 5 years ago
parent cc09c2ba01
commit ffa60a0ce2

@ -1,6 +1,6 @@
[package]
name = "monolith"
version = "2.0.1"
version = "2.0.2"
authors = ["Sunshine <sunshine@uberspace.net>"]
[dependencies]

@ -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 <meta http-equiv="Access-Control-Allow-Origin" content="'self'"/> to the <head> if opt_isolate
serialize(&mut io::stdout(), handle, SerializeOpts::default()).unwrap();
}

@ -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 <sunshine@uberspace.net>")
.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);
}
}

Loading…
Cancel
Save