rename dataurl to data_url

pull/119/head
Sunshine 4 years ago
parent ee2055a2a3
commit 5c8d75539b
No known key found for this signature in database
GPG Key ID: B80CA68703CD8AB1

@ -1,7 +1,7 @@
use crate::http::retrieve_asset; use crate::http::retrieve_asset;
use crate::js::attr_is_event_handler; use crate::js::attr_is_event_handler;
use crate::utils::{ use crate::utils::{
data_to_dataurl, is_valid_url, resolve_css_imports, resolve_url, url_has_protocol, data_to_data_url, is_valid_url, resolve_css_imports, resolve_url, url_has_protocol,
}; };
use html5ever::interface::QualName; use html5ever::interface::QualName;
use html5ever::parse_document; use html5ever::parse_document;
@ -130,7 +130,7 @@ pub fn walk_and_embed_assets(
} else { } else {
let href_full_url = resolve_url(&url, attr.value.as_ref()) let href_full_url = resolve_url(&url, attr.value.as_ref())
.unwrap_or_default(); .unwrap_or_default();
let (favicon_dataurl, _) = retrieve_asset( let (favicon_data_url, _) = retrieve_asset(
cache, cache,
client, client,
&href_full_url, &href_full_url,
@ -140,7 +140,7 @@ pub fn walk_and_embed_assets(
) )
.unwrap_or_default(); .unwrap_or_default();
attr.value.clear(); attr.value.clear();
attr.value.push_slice(favicon_dataurl.as_str()); attr.value.push_slice(favicon_data_url.as_str());
} }
} }
} }
@ -229,14 +229,14 @@ pub fn walk_and_embed_assets(
name: QualName::new(None, ns!(), local_name!("src")), name: QualName::new(None, ns!(), local_name!("src")),
value: Tendril::from_slice(TRANSPARENT_PIXEL), value: Tendril::from_slice(TRANSPARENT_PIXEL),
}); });
} else if let Some((dataurl, _)) = found_datasrc } else if let Some((data_url, _)) = found_datasrc
.iter() .iter()
.chain(&found_src) // Give dataurl priority .chain(&found_src) // Give data_url priority
.map(|attr| attr.value.trim()) .map(|attr| attr.value.trim())
.filter(|src| !src.is_empty()) // Ignore empty srcs .filter(|src| !src.is_empty()) // Ignore empty srcs
.next() .next()
.and_then(|src| resolve_url(&url, src).ok()) // Make absolute .and_then(|src| resolve_url(&url, src).ok()) // Make absolute
.and_then(|abs_src| // Download and convert to dataurl .and_then(|abs_src| // Download and convert to data_url
retrieve_asset( retrieve_asset(
cache, cache,
client, client,
@ -246,10 +246,10 @@ pub fn walk_and_embed_assets(
opt_silent, opt_silent,
).ok()) ).ok())
{ {
// Add the new dataurl src attribute // Add the new data_url src attribute
attrs_mut.push(Attribute { attrs_mut.push(Attribute {
name: QualName::new(None, ns!(), local_name!("src")), name: QualName::new(None, ns!(), local_name!("src")),
value: Tendril::from_slice(dataurl.as_ref()), value: Tendril::from_slice(data_url.as_ref()),
}); });
} }
} }
@ -270,7 +270,7 @@ pub fn walk_and_embed_assets(
} else { } else {
let srcset_full_url = let srcset_full_url =
resolve_url(&url, attr.value.trim()).unwrap_or_default(); resolve_url(&url, attr.value.trim()).unwrap_or_default();
let (source_dataurl, _) = retrieve_asset( let (source_data_url, _) = retrieve_asset(
cache, cache,
client, client,
&srcset_full_url, &srcset_full_url,
@ -280,7 +280,7 @@ pub fn walk_and_embed_assets(
) )
.unwrap_or((str!(), str!())); .unwrap_or((str!(), str!()));
attr.value.clear(); attr.value.clear();
attr.value.push_slice(source_dataurl.as_str()); attr.value.push_slice(source_data_url.as_str());
} }
} }
} }
@ -334,7 +334,7 @@ pub fn walk_and_embed_assets(
if &attr.name.local == "src" { if &attr.name.local == "src" {
let src_full_url = let src_full_url =
resolve_url(&url, attr.value.trim()).unwrap_or_default(); resolve_url(&url, attr.value.trim()).unwrap_or_default();
let (js_dataurl, _) = retrieve_asset( let (js_data_url, _) = retrieve_asset(
cache, cache,
client, client,
&src_full_url, &src_full_url,
@ -344,7 +344,7 @@ pub fn walk_and_embed_assets(
) )
.unwrap_or((str!(), str!())); .unwrap_or((str!(), str!()));
attr.value.clear(); attr.value.clear();
attr.value.push_slice(js_dataurl.as_str()); attr.value.push_slice(js_data_url.as_str());
} }
} }
} }
@ -426,9 +426,9 @@ pub fn walk_and_embed_assets(
); );
let mut buf: Vec<u8> = Vec::new(); let mut buf: Vec<u8> = Vec::new();
serialize(&mut buf, &dom.document, SerializeOpts::default()).unwrap(); serialize(&mut buf, &dom.document, SerializeOpts::default()).unwrap();
let iframe_dataurl = data_to_dataurl("text/html", &buf); let iframe_data_url = data_to_data_url("text/html", &buf);
attr.value.clear(); attr.value.clear();
attr.value.push_slice(iframe_dataurl.as_str()); attr.value.push_slice(iframe_data_url.as_str());
} }
} }
} }
@ -447,7 +447,7 @@ pub fn walk_and_embed_assets(
} else { } else {
let poster_full_url = let poster_full_url =
resolve_url(&url, video_poster).unwrap_or_default(); resolve_url(&url, video_poster).unwrap_or_default();
let (poster_dataurl, _) = retrieve_asset( let (poster_data_url, _) = retrieve_asset(
cache, cache,
client, client,
&poster_full_url, &poster_full_url,
@ -457,7 +457,7 @@ pub fn walk_and_embed_assets(
) )
.unwrap_or((poster_full_url, str!())); .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_data_url.as_str());
} }
} }
} }

@ -1,4 +1,4 @@
use crate::utils::{clean_url, data_to_dataurl, is_data_url}; use crate::utils::{clean_url, data_to_data_url, is_data_url};
use reqwest::blocking::Client; use reqwest::blocking::Client;
use reqwest::header::CONTENT_TYPE; use reqwest::header::CONTENT_TYPE;
use std::collections::HashMap; use std::collections::HashMap;
@ -7,7 +7,7 @@ pub fn retrieve_asset(
cache: &mut HashMap<String, String>, cache: &mut HashMap<String, String>,
client: &Client, client: &Client,
url: &str, url: &str,
as_dataurl: bool, as_data_url: bool,
mime: &str, mime: &str,
opt_silent: bool, opt_silent: bool,
) -> Result<(String, String), reqwest::Error> { ) -> Result<(String, String), reqwest::Error> {
@ -38,7 +38,7 @@ pub fn retrieve_asset(
let new_cache_key = clean_url(&res_url); let new_cache_key = clean_url(&res_url);
if as_dataurl { if as_data_url {
// Convert response into a byte array // Convert response into a byte array
let mut data: Vec<u8> = vec![]; let mut data: Vec<u8> = vec![];
response.copy_to(&mut data)?; response.copy_to(&mut data)?;
@ -53,10 +53,10 @@ pub fn retrieve_asset(
} else { } else {
mime mime
}; };
let dataurl = data_to_dataurl(&mimetype, &data); let data_url = data_to_data_url(&mimetype, &data);
// insert in cache // insert in cache
cache.insert(new_cache_key, dataurl.clone()); cache.insert(new_cache_key, data_url.clone());
Ok((dataurl, res_url)) Ok((data_url, res_url))
} else { } else {
let content = response.text().unwrap(); let content = response.text().unwrap();
// insert in cache // insert in cache

@ -1,14 +1,14 @@
use crate::utils::{ use crate::utils::{
clean_url, data_to_dataurl, detect_mimetype, is_data_url, is_valid_url, resolve_url, clean_url, data_to_data_url, detect_mimetype, is_data_url, is_valid_url, resolve_url,
url_has_protocol, url_has_protocol,
}; };
use url::ParseError; use url::ParseError;
#[test] #[test]
fn test_data_to_dataurl() { fn test_data_to_data_url() {
let mime = "application/javascript"; let mime = "application/javascript";
let data = "var word = 'hello';\nalert(word);\n"; let data = "var word = 'hello';\nalert(word);\n";
let datauri = data_to_dataurl(mime, data.as_bytes()); let datauri = data_to_data_url(mime, data.as_bytes());
assert_eq!( assert_eq!(
&datauri, &datauri,
"data:application/javascript;base64,dmFyIHdvcmQgPSAnaGVsbG8nOwphbGVydCh3b3JkKTsK" "data:application/javascript;base64,dmFyIHdvcmQgPSAnaGVsbG8nOwphbGVydCh3b3JkKTsK"

@ -67,7 +67,7 @@ const MAGIC: [[&[u8]; 2]; 19] = [
[b"\x1A\x45\xDF\xA3", b"video/webm"], [b"\x1A\x45\xDF\xA3", b"video/webm"],
]; ];
pub fn data_to_dataurl(mime: &str, data: &[u8]) -> String { pub fn data_to_data_url(mime: &str, data: &[u8]) -> String {
let mimetype = if mime.is_empty() { let mimetype = if mime.is_empty() {
detect_mimetype(data) detect_mimetype(data)
} else { } else {
@ -113,7 +113,7 @@ pub fn resolve_css_imports(
cache: &mut HashMap<String, String>, cache: &mut HashMap<String, String>,
client: &Client, client: &Client,
css_string: &str, css_string: &str,
as_dataurl: bool, as_data_url: bool,
href: &str, href: &str,
opt_no_images: bool, opt_no_images: bool,
opt_silent: bool, opt_silent: bool,
@ -150,7 +150,7 @@ pub fn resolve_css_imports(
cache, cache,
client, client,
&content, &content,
true, // Finally, convert to a dataurl true, // Finally, convert to a data URL
&embedded_url, &embedded_url,
opt_no_images, opt_no_images,
opt_silent, opt_silent,
@ -188,8 +188,8 @@ pub fn resolve_css_imports(
resolved_css.replace_range(target_range, &replacement); resolved_css.replace_range(target_range, &replacement);
} }
if as_dataurl { if as_data_url {
data_to_dataurl("text/css", resolved_css.as_bytes()) data_to_data_url("text/css", resolved_css.as_bytes())
} else { } else {
resolved_css resolved_css
} }

Loading…
Cancel
Save