diff --git a/src/html.rs b/src/html.rs index 61ce6dc..d43c233 100644 --- a/src/html.rs +++ b/src/html.rs @@ -12,10 +12,6 @@ use std::collections::HashMap; use std::default::Default; 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] = [ "icon", "shortcut icon", @@ -36,7 +32,7 @@ pub fn get_parent_node(node: &Handle) -> Handle { pub fn get_node_name(node: &Handle) -> String { match &node.data { 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 { let href_full_url: String = resolve_url(&url, &attr.value.to_string()) - .unwrap_or(EMPTY_STRING.clone()); + .unwrap_or(str!()); let (favicon_dataurl, _) = retrieve_asset( cache, client, @@ -112,7 +108,7 @@ pub fn walk_and_embed_assets( "", opt_silent, ) - .unwrap_or((EMPTY_STRING.clone(), EMPTY_STRING.clone())); + .unwrap_or((str!(), str!())); attr.value.clear(); attr.value.push_slice(favicon_dataurl.as_str()); } @@ -126,7 +122,7 @@ pub fn walk_and_embed_assets( } else { let href_full_url: String = resolve_url(&url, &attr.value.to_string()) - .unwrap_or(EMPTY_STRING.clone()); + .unwrap_or(str!()); let replacement_text = match retrieve_asset( cache, client, @@ -164,8 +160,7 @@ pub fn walk_and_embed_assets( for attr in attrs_mut.iter_mut() { if &attr.name.local == "href" { let href_full_url: String = - resolve_url(&url, &attr.value.to_string()) - .unwrap_or(EMPTY_STRING.clone()); + resolve_url(&url, &attr.value.to_string()).unwrap_or(str!()); attr.value.clear(); 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(); // Ignore images with empty source - if value == EMPTY_STRING.clone() { + if value == str!() { continue; } @@ -187,7 +182,7 @@ pub fn walk_and_embed_assets( attr.value.push_slice(TRANSPARENT_PIXEL); } else { 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( cache, client, @@ -196,7 +191,7 @@ pub fn walk_and_embed_assets( "", opt_silent, ) - .unwrap_or((EMPTY_STRING.clone(), EMPTY_STRING.clone())); + .unwrap_or((str!(), str!())); attr.value.clear(); attr.value.push_slice(img_dataurl.as_str()); } @@ -220,7 +215,7 @@ pub fn walk_and_embed_assets( } else { let srcset_full_url: String = resolve_url(&url, &attr.value.to_string()) - .unwrap_or(EMPTY_STRING.clone()); + .unwrap_or(str!()); let (source_dataurl, _) = retrieve_asset( cache, client, @@ -229,7 +224,7 @@ pub fn walk_and_embed_assets( "", opt_silent, ) - .unwrap_or((EMPTY_STRING.clone(), EMPTY_STRING.clone())); + .unwrap_or((str!(), str!())); attr.value.clear(); attr.value.push_slice(source_dataurl.as_str()); } @@ -245,8 +240,8 @@ pub fn walk_and_embed_assets( continue; } - let href_full_url: String = resolve_url(&url, &attr.value.to_string()) - .unwrap_or(EMPTY_STRING.clone()); + let href_full_url: String = + resolve_url(&url, &attr.value.to_string()).unwrap_or(str!()); attr.value.clear(); 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() { if &attr.name.local == "src" { let src_full_url: String = - resolve_url(&url, &attr.value.to_string()) - .unwrap_or(EMPTY_STRING.clone()); + resolve_url(&url, &attr.value.to_string()).unwrap_or(str!()); let (js_dataurl, _) = retrieve_asset( cache, client, @@ -275,7 +269,7 @@ pub fn walk_and_embed_assets( "application/javascript", opt_silent, ) - .unwrap_or((EMPTY_STRING.clone(), EMPTY_STRING.clone())); + .unwrap_or((str!(), str!())); attr.value.clear(); 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 if !is_valid_url(&attr.value) { let href_full_url: String = - resolve_url(&url, &attr.value.to_string()) - .unwrap_or(EMPTY_STRING.clone()); + resolve_url(&url, &attr.value.to_string()).unwrap_or(str!()); attr.value.clear(); 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(); // Ignore iframes with empty source (they cause infinite loops) - if iframe_src == EMPTY_STRING.clone() { + if iframe_src == str!() { continue; } 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( cache, client, @@ -345,7 +338,7 @@ pub fn walk_and_embed_assets( "text/html", opt_silent, ) - .unwrap_or((EMPTY_STRING.clone(), src_full_url)); + .unwrap_or((str!(), src_full_url)); let dom = html_to_dom(&iframe_data); walk_and_embed_assets( cache, @@ -372,15 +365,15 @@ pub fn walk_and_embed_assets( let video_poster = attr.value.to_string(); // Skip posters with empty source - if video_poster == EMPTY_STRING.clone() { + if video_poster == str!() { continue; } if opt_no_images { attr.value.clear(); } else { - let poster_full_url: String = resolve_url(&url, &video_poster) - .unwrap_or(EMPTY_STRING.clone()); + let poster_full_url: String = + resolve_url(&url, &video_poster).unwrap_or(str!()); let (poster_dataurl, _) = retrieve_asset( cache, client, @@ -389,7 +382,7 @@ pub fn walk_and_embed_assets( "", opt_silent, ) - .unwrap_or((poster_full_url, EMPTY_STRING.clone())); + .unwrap_or((poster_full_url, str!())); attr.value.clear(); attr.value.push_slice(poster_dataurl.as_str()); } @@ -511,7 +504,7 @@ pub fn stringify_document( let doc = dom.get_document(); let html = get_child_node_by_name(&doc, "html"); let head = get_child_node_by_name(&html, "head"); - let mut content_attr = EMPTY_STRING.clone(); + let mut content_attr = str!(); if opt_isolate { content_attr += " default-src 'unsafe-inline' data:;"; } diff --git a/src/lib.rs b/src/lib.rs index 89f168c..d19bed8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,10 +1,13 @@ +extern crate html5ever; #[macro_use] extern crate lazy_static; -extern crate html5ever; extern crate regex; extern crate reqwest; extern crate url; +#[macro_use] +mod macros; + pub mod html; pub mod http; pub mod js; diff --git a/src/macros.rs b/src/macros.rs new file mode 100644 index 0000000..475ce87 --- /dev/null +++ b/src/macros.rs @@ -0,0 +1,9 @@ +#[macro_export] +macro_rules! str { + () => { + String::new() + }; + ($val: expr) => { + ToString::to_string(&$val) + }; +}