rename dataurl to data_url

This commit is contained in:
Sunshine 2020-02-12 01:51:26 -05:00
parent ee2055a2a3
commit 5c8d75539b
No known key found for this signature in database
GPG key ID: B80CA68703CD8AB1
4 changed files with 30 additions and 30 deletions

View file

@ -1,7 +1,7 @@
use crate::http::retrieve_asset;
use crate::js::attr_is_event_handler;
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::parse_document;
@ -130,7 +130,7 @@ pub fn walk_and_embed_assets(
} else {
let href_full_url = resolve_url(&url, attr.value.as_ref())
.unwrap_or_default();
let (favicon_dataurl, _) = retrieve_asset(
let (favicon_data_url, _) = retrieve_asset(
cache,
client,
&href_full_url,
@ -140,7 +140,7 @@ pub fn walk_and_embed_assets(
)
.unwrap_or_default();
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")),
value: Tendril::from_slice(TRANSPARENT_PIXEL),
});
} else if let Some((dataurl, _)) = found_datasrc
} else if let Some((data_url, _)) = found_datasrc
.iter()
.chain(&found_src) // Give dataurl priority
.chain(&found_src) // Give data_url priority
.map(|attr| attr.value.trim())
.filter(|src| !src.is_empty()) // Ignore empty srcs
.next()
.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(
cache,
client,
@ -246,10 +246,10 @@ pub fn walk_and_embed_assets(
opt_silent,
).ok())
{
// Add the new dataurl src attribute
// Add the new data_url src attribute
attrs_mut.push(Attribute {
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 {
let srcset_full_url =
resolve_url(&url, attr.value.trim()).unwrap_or_default();
let (source_dataurl, _) = retrieve_asset(
let (source_data_url, _) = retrieve_asset(
cache,
client,
&srcset_full_url,
@ -280,7 +280,7 @@ pub fn walk_and_embed_assets(
)
.unwrap_or((str!(), str!()));
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" {
let src_full_url =
resolve_url(&url, attr.value.trim()).unwrap_or_default();
let (js_dataurl, _) = retrieve_asset(
let (js_data_url, _) = retrieve_asset(
cache,
client,
&src_full_url,
@ -344,7 +344,7 @@ pub fn walk_and_embed_assets(
)
.unwrap_or((str!(), str!()));
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();
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.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 {
let poster_full_url =
resolve_url(&url, video_poster).unwrap_or_default();
let (poster_dataurl, _) = retrieve_asset(
let (poster_data_url, _) = retrieve_asset(
cache,
client,
&poster_full_url,
@ -457,7 +457,7 @@ pub fn walk_and_embed_assets(
)
.unwrap_or((poster_full_url, str!()));
attr.value.clear();
attr.value.push_slice(poster_dataurl.as_str());
attr.value.push_slice(poster_data_url.as_str());
}
}
}

View file

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

View file

@ -1,14 +1,14 @@
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,
};
use url::ParseError;
#[test]
fn test_data_to_dataurl() {
fn test_data_to_data_url() {
let mime = "application/javascript";
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!(
&datauri,
"data:application/javascript;base64,dmFyIHdvcmQgPSAnaGVsbG8nOwphbGVydCh3b3JkKTsK"

View file

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