commit
9ff9dd0928
53
src/html.rs
53
src/html.rs
@ -12,10 +12,6 @@ use std::collections::HashMap;
|
|||||||
use std::default::Default;
|
use std::default::Default;
|
||||||
use utils::{data_to_dataurl, is_valid_url, resolve_css_imports, resolve_url, url_has_protocol};
|
use utils::{data_to_dataurl, is_valid_url, resolve_css_imports, resolve_url, url_has_protocol};
|
||||||
|
|
||||||
lazy_static! {
|
|
||||||
static ref EMPTY_STRING: String = String::new();
|
|
||||||
}
|
|
||||||
|
|
||||||
const ICON_VALUES: [&str; 5] = [
|
const ICON_VALUES: [&str; 5] = [
|
||||||
"icon",
|
"icon",
|
||||||
"shortcut icon",
|
"shortcut icon",
|
||||||
@ -36,7 +32,7 @@ pub fn get_parent_node(node: &Handle) -> Handle {
|
|||||||
pub fn get_node_name(node: &Handle) -> String {
|
pub fn get_node_name(node: &Handle) -> String {
|
||||||
match &node.data {
|
match &node.data {
|
||||||
NodeData::Element { ref name, .. } => name.local.as_ref().to_string(),
|
NodeData::Element { ref name, .. } => name.local.as_ref().to_string(),
|
||||||
_ => EMPTY_STRING.clone(),
|
_ => str!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,7 +99,7 @@ pub fn walk_and_embed_assets(
|
|||||||
} else {
|
} else {
|
||||||
let href_full_url: String =
|
let href_full_url: String =
|
||||||
resolve_url(&url, &attr.value.to_string())
|
resolve_url(&url, &attr.value.to_string())
|
||||||
.unwrap_or(EMPTY_STRING.clone());
|
.unwrap_or(str!());
|
||||||
let (favicon_dataurl, _) = retrieve_asset(
|
let (favicon_dataurl, _) = retrieve_asset(
|
||||||
cache,
|
cache,
|
||||||
client,
|
client,
|
||||||
@ -112,7 +108,7 @@ pub fn walk_and_embed_assets(
|
|||||||
"",
|
"",
|
||||||
opt_silent,
|
opt_silent,
|
||||||
)
|
)
|
||||||
.unwrap_or((EMPTY_STRING.clone(), EMPTY_STRING.clone()));
|
.unwrap_or((str!(), str!()));
|
||||||
attr.value.clear();
|
attr.value.clear();
|
||||||
attr.value.push_slice(favicon_dataurl.as_str());
|
attr.value.push_slice(favicon_dataurl.as_str());
|
||||||
}
|
}
|
||||||
@ -126,7 +122,7 @@ pub fn walk_and_embed_assets(
|
|||||||
} else {
|
} else {
|
||||||
let href_full_url: String =
|
let href_full_url: String =
|
||||||
resolve_url(&url, &attr.value.to_string())
|
resolve_url(&url, &attr.value.to_string())
|
||||||
.unwrap_or(EMPTY_STRING.clone());
|
.unwrap_or(str!());
|
||||||
let replacement_text = match retrieve_asset(
|
let replacement_text = match retrieve_asset(
|
||||||
cache,
|
cache,
|
||||||
client,
|
client,
|
||||||
@ -164,8 +160,7 @@ pub fn walk_and_embed_assets(
|
|||||||
for attr in attrs_mut.iter_mut() {
|
for attr in attrs_mut.iter_mut() {
|
||||||
if &attr.name.local == "href" {
|
if &attr.name.local == "href" {
|
||||||
let href_full_url: String =
|
let href_full_url: String =
|
||||||
resolve_url(&url, &attr.value.to_string())
|
resolve_url(&url, &attr.value.to_string()).unwrap_or(str!());
|
||||||
.unwrap_or(EMPTY_STRING.clone());
|
|
||||||
attr.value.clear();
|
attr.value.clear();
|
||||||
attr.value.push_slice(&href_full_url.as_str());
|
attr.value.push_slice(&href_full_url.as_str());
|
||||||
}
|
}
|
||||||
@ -178,7 +173,7 @@ pub fn walk_and_embed_assets(
|
|||||||
let value = attr.value.to_string();
|
let value = attr.value.to_string();
|
||||||
|
|
||||||
// Ignore images with empty source
|
// Ignore images with empty source
|
||||||
if value == EMPTY_STRING.clone() {
|
if value == str!() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -187,7 +182,7 @@ pub fn walk_and_embed_assets(
|
|||||||
attr.value.push_slice(TRANSPARENT_PIXEL);
|
attr.value.push_slice(TRANSPARENT_PIXEL);
|
||||||
} else {
|
} else {
|
||||||
let src_full_url: String =
|
let src_full_url: String =
|
||||||
resolve_url(&url, &value).unwrap_or(EMPTY_STRING.clone());
|
resolve_url(&url, &value).unwrap_or(str!());
|
||||||
let (img_dataurl, _) = retrieve_asset(
|
let (img_dataurl, _) = retrieve_asset(
|
||||||
cache,
|
cache,
|
||||||
client,
|
client,
|
||||||
@ -196,7 +191,7 @@ pub fn walk_and_embed_assets(
|
|||||||
"",
|
"",
|
||||||
opt_silent,
|
opt_silent,
|
||||||
)
|
)
|
||||||
.unwrap_or((EMPTY_STRING.clone(), EMPTY_STRING.clone()));
|
.unwrap_or((str!(), str!()));
|
||||||
attr.value.clear();
|
attr.value.clear();
|
||||||
attr.value.push_slice(img_dataurl.as_str());
|
attr.value.push_slice(img_dataurl.as_str());
|
||||||
}
|
}
|
||||||
@ -220,7 +215,7 @@ pub fn walk_and_embed_assets(
|
|||||||
} else {
|
} else {
|
||||||
let srcset_full_url: String =
|
let srcset_full_url: String =
|
||||||
resolve_url(&url, &attr.value.to_string())
|
resolve_url(&url, &attr.value.to_string())
|
||||||
.unwrap_or(EMPTY_STRING.clone());
|
.unwrap_or(str!());
|
||||||
let (source_dataurl, _) = retrieve_asset(
|
let (source_dataurl, _) = retrieve_asset(
|
||||||
cache,
|
cache,
|
||||||
client,
|
client,
|
||||||
@ -229,7 +224,7 @@ pub fn walk_and_embed_assets(
|
|||||||
"",
|
"",
|
||||||
opt_silent,
|
opt_silent,
|
||||||
)
|
)
|
||||||
.unwrap_or((EMPTY_STRING.clone(), EMPTY_STRING.clone()));
|
.unwrap_or((str!(), str!()));
|
||||||
attr.value.clear();
|
attr.value.clear();
|
||||||
attr.value.push_slice(source_dataurl.as_str());
|
attr.value.push_slice(source_dataurl.as_str());
|
||||||
}
|
}
|
||||||
@ -245,8 +240,8 @@ pub fn walk_and_embed_assets(
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let href_full_url: String = resolve_url(&url, &attr.value.to_string())
|
let href_full_url: String =
|
||||||
.unwrap_or(EMPTY_STRING.clone());
|
resolve_url(&url, &attr.value.to_string()).unwrap_or(str!());
|
||||||
attr.value.clear();
|
attr.value.clear();
|
||||||
attr.value.push_slice(href_full_url.as_str());
|
attr.value.push_slice(href_full_url.as_str());
|
||||||
}
|
}
|
||||||
@ -265,8 +260,7 @@ pub fn walk_and_embed_assets(
|
|||||||
for attr in attrs_mut.iter_mut() {
|
for attr in attrs_mut.iter_mut() {
|
||||||
if &attr.name.local == "src" {
|
if &attr.name.local == "src" {
|
||||||
let src_full_url: String =
|
let src_full_url: String =
|
||||||
resolve_url(&url, &attr.value.to_string())
|
resolve_url(&url, &attr.value.to_string()).unwrap_or(str!());
|
||||||
.unwrap_or(EMPTY_STRING.clone());
|
|
||||||
let (js_dataurl, _) = retrieve_asset(
|
let (js_dataurl, _) = retrieve_asset(
|
||||||
cache,
|
cache,
|
||||||
client,
|
client,
|
||||||
@ -275,7 +269,7 @@ pub fn walk_and_embed_assets(
|
|||||||
"application/javascript",
|
"application/javascript",
|
||||||
opt_silent,
|
opt_silent,
|
||||||
)
|
)
|
||||||
.unwrap_or((EMPTY_STRING.clone(), EMPTY_STRING.clone()));
|
.unwrap_or((str!(), str!()));
|
||||||
attr.value.clear();
|
attr.value.clear();
|
||||||
attr.value.push_slice(js_dataurl.as_str());
|
attr.value.push_slice(js_dataurl.as_str());
|
||||||
}
|
}
|
||||||
@ -311,8 +305,7 @@ pub fn walk_and_embed_assets(
|
|||||||
// Modify action to be a full URL
|
// Modify action to be a full URL
|
||||||
if !is_valid_url(&attr.value) {
|
if !is_valid_url(&attr.value) {
|
||||||
let href_full_url: String =
|
let href_full_url: String =
|
||||||
resolve_url(&url, &attr.value.to_string())
|
resolve_url(&url, &attr.value.to_string()).unwrap_or(str!());
|
||||||
.unwrap_or(EMPTY_STRING.clone());
|
|
||||||
attr.value.clear();
|
attr.value.clear();
|
||||||
attr.value.push_slice(href_full_url.as_str());
|
attr.value.push_slice(href_full_url.as_str());
|
||||||
}
|
}
|
||||||
@ -331,12 +324,12 @@ pub fn walk_and_embed_assets(
|
|||||||
let iframe_src: String = attr.value.to_string();
|
let iframe_src: String = attr.value.to_string();
|
||||||
|
|
||||||
// Ignore iframes with empty source (they cause infinite loops)
|
// Ignore iframes with empty source (they cause infinite loops)
|
||||||
if iframe_src == EMPTY_STRING.clone() {
|
if iframe_src == str!() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let src_full_url: String =
|
let src_full_url: String =
|
||||||
resolve_url(&url, &iframe_src).unwrap_or(EMPTY_STRING.clone());
|
resolve_url(&url, &iframe_src).unwrap_or(str!());
|
||||||
let (iframe_data, iframe_final_url) = retrieve_asset(
|
let (iframe_data, iframe_final_url) = retrieve_asset(
|
||||||
cache,
|
cache,
|
||||||
client,
|
client,
|
||||||
@ -345,7 +338,7 @@ pub fn walk_and_embed_assets(
|
|||||||
"text/html",
|
"text/html",
|
||||||
opt_silent,
|
opt_silent,
|
||||||
)
|
)
|
||||||
.unwrap_or((EMPTY_STRING.clone(), src_full_url));
|
.unwrap_or((str!(), src_full_url));
|
||||||
let dom = html_to_dom(&iframe_data);
|
let dom = html_to_dom(&iframe_data);
|
||||||
walk_and_embed_assets(
|
walk_and_embed_assets(
|
||||||
cache,
|
cache,
|
||||||
@ -372,15 +365,15 @@ pub fn walk_and_embed_assets(
|
|||||||
let video_poster = attr.value.to_string();
|
let video_poster = attr.value.to_string();
|
||||||
|
|
||||||
// Skip posters with empty source
|
// Skip posters with empty source
|
||||||
if video_poster == EMPTY_STRING.clone() {
|
if video_poster == str!() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if opt_no_images {
|
if opt_no_images {
|
||||||
attr.value.clear();
|
attr.value.clear();
|
||||||
} else {
|
} else {
|
||||||
let poster_full_url: String = resolve_url(&url, &video_poster)
|
let poster_full_url: String =
|
||||||
.unwrap_or(EMPTY_STRING.clone());
|
resolve_url(&url, &video_poster).unwrap_or(str!());
|
||||||
let (poster_dataurl, _) = retrieve_asset(
|
let (poster_dataurl, _) = retrieve_asset(
|
||||||
cache,
|
cache,
|
||||||
client,
|
client,
|
||||||
@ -389,7 +382,7 @@ pub fn walk_and_embed_assets(
|
|||||||
"",
|
"",
|
||||||
opt_silent,
|
opt_silent,
|
||||||
)
|
)
|
||||||
.unwrap_or((poster_full_url, EMPTY_STRING.clone()));
|
.unwrap_or((poster_full_url, str!()));
|
||||||
attr.value.clear();
|
attr.value.clear();
|
||||||
attr.value.push_slice(poster_dataurl.as_str());
|
attr.value.push_slice(poster_dataurl.as_str());
|
||||||
}
|
}
|
||||||
@ -511,7 +504,7 @@ pub fn stringify_document(
|
|||||||
let doc = dom.get_document();
|
let doc = dom.get_document();
|
||||||
let html = get_child_node_by_name(&doc, "html");
|
let html = get_child_node_by_name(&doc, "html");
|
||||||
let head = get_child_node_by_name(&html, "head");
|
let head = get_child_node_by_name(&html, "head");
|
||||||
let mut content_attr = EMPTY_STRING.clone();
|
let mut content_attr = str!();
|
||||||
if opt_isolate {
|
if opt_isolate {
|
||||||
content_attr += " default-src 'unsafe-inline' data:;";
|
content_attr += " default-src 'unsafe-inline' data:;";
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,13 @@
|
|||||||
|
extern crate html5ever;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate lazy_static;
|
extern crate lazy_static;
|
||||||
extern crate html5ever;
|
|
||||||
extern crate regex;
|
extern crate regex;
|
||||||
extern crate reqwest;
|
extern crate reqwest;
|
||||||
extern crate url;
|
extern crate url;
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
mod macros;
|
||||||
|
|
||||||
pub mod html;
|
pub mod html;
|
||||||
pub mod http;
|
pub mod http;
|
||||||
pub mod js;
|
pub mod js;
|
||||||
|
9
src/macros.rs
Normal file
9
src/macros.rs
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#[macro_export]
|
||||||
|
macro_rules! str {
|
||||||
|
() => {
|
||||||
|
String::new()
|
||||||
|
};
|
||||||
|
($val: expr) => {
|
||||||
|
ToString::to_string(&$val)
|
||||||
|
};
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user