implement str!() macro

This commit is contained in:
Sunshine 2019-12-11 01:12:57 -05:00
parent 919e626b5e
commit 9fe913d853
3 changed files with 36 additions and 31 deletions

View File

@ -11,10 +11,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",
@ -35,7 +31,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!(),
} }
} }
@ -104,7 +100,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,
&href_full_url, &href_full_url,
@ -114,7 +110,7 @@ pub fn walk_and_embed_assets(
opt_silent, opt_silent,
opt_insecure, opt_insecure,
) )
.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());
} }
@ -128,7 +124,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,
&href_full_url, &href_full_url,
@ -168,8 +164,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());
} }
@ -182,7 +177,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;
} }
@ -191,7 +186,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,
&src_full_url, &src_full_url,
@ -201,7 +196,7 @@ pub fn walk_and_embed_assets(
opt_silent, opt_silent,
opt_insecure, opt_insecure,
) )
.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());
} }
@ -225,7 +220,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,
&srcset_full_url, &srcset_full_url,
@ -235,7 +230,7 @@ pub fn walk_and_embed_assets(
opt_silent, opt_silent,
opt_insecure, opt_insecure,
) )
.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());
} }
@ -251,8 +246,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());
} }
@ -271,8 +266,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,
&src_full_url, &src_full_url,
@ -282,7 +276,7 @@ pub fn walk_and_embed_assets(
opt_silent, opt_silent,
opt_insecure, opt_insecure,
) )
.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());
} }
@ -319,8 +313,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());
} }
@ -339,12 +332,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,
&src_full_url, &src_full_url,
@ -354,7 +347,7 @@ pub fn walk_and_embed_assets(
opt_silent, opt_silent,
opt_insecure, opt_insecure,
) )
.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,
@ -382,15 +375,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,
&poster_full_url, &poster_full_url,
@ -400,7 +393,7 @@ pub fn walk_and_embed_assets(
opt_silent, opt_silent,
opt_insecure, opt_insecure,
) )
.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());
} }
@ -524,7 +517,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:;";
} }

View File

@ -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
View File

@ -0,0 +1,9 @@
#[macro_export]
macro_rules! str {
() => {
String::new()
};
($val: expr) => {
ToString::to_string(&$val)
};
}