Applied rustfmt

This commit is contained in:
Emi Simpson 2019-12-05 20:41:43 -05:00
parent ab65b44f0d
commit de383c94b1
No known key found for this signature in database
GPG key ID: 68FAB2E2E6DFC98B
2 changed files with 40 additions and 43 deletions

View file

@ -7,9 +7,9 @@ use html5ever::tree_builder::{Attribute, TreeSink};
use html5ever::{local_name, namespace_url, ns}; use html5ever::{local_name, namespace_url, ns};
use http::retrieve_asset; use http::retrieve_asset;
use js::attr_is_event_handler; use js::attr_is_event_handler;
use std::fmt::Write as OtherWrite;
use std::collections::HashMap; use std::collections::HashMap;
use std::default::Default; use std::default::Default;
use std::fmt::Write as OtherWrite;
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! { lazy_static! {
@ -139,7 +139,6 @@ pub fn walk_and_embed_assets(
opt_silent, opt_silent,
opt_insecure, opt_insecure,
) { ) {
// On successful retrieval, traverse CSS // On successful retrieval, traverse CSS
Ok((css_data, _)) => resolve_css_imports( Ok((css_data, _)) => resolve_css_imports(
cache, cache,
@ -153,14 +152,11 @@ pub fn walk_and_embed_assets(
// If a network error occured, warn // If a network error occured, warn
Err(e) => { Err(e) => {
eprintln!( eprintln!("Warning: {}", e,);
"Warning: {}",
e,
);
//If failed to resolve, replace with absolute URL //If failed to resolve, replace with absolute URL
href_full_url href_full_url
}, }
}; };
attr.value.clear(); attr.value.clear();
@ -299,7 +295,7 @@ pub fn walk_and_embed_assets(
node.children.borrow_mut().clear(); node.children.borrow_mut().clear();
} else { } else {
for node in node.children.borrow_mut().iter_mut() { for node in node.children.borrow_mut().iter_mut() {
if let NodeData::Text{ref contents} = node.data { if let NodeData::Text { ref contents } = node.data {
let mut tendril = contents.borrow_mut(); let mut tendril = contents.borrow_mut();
let replacement = resolve_css_imports( let replacement = resolve_css_imports(
cache, cache,
@ -311,7 +307,8 @@ pub fn walk_and_embed_assets(
opt_insecure, opt_insecure,
); );
tendril.clear(); tendril.clear();
tendril.write_str(&replacement) tendril
.write_str(&replacement)
.expect("Failed to update DOM"); .expect("Failed to update DOM");
} }
} }

View file

@ -3,8 +3,8 @@ extern crate base64;
use self::base64::encode; use self::base64::encode;
use http::retrieve_asset; use http::retrieve_asset;
use regex::Regex; use regex::Regex;
use url::{ParseError, Url};
use std::collections::HashMap; use std::collections::HashMap;
use url::{ParseError, Url};
lazy_static! { lazy_static! {
static ref HAS_PROTOCOL: Regex = Regex::new(r"^[a-z0-9]+:").unwrap(); static ref HAS_PROTOCOL: Regex = Regex::new(r"^[a-z0-9]+:").unwrap();
@ -88,7 +88,8 @@ pub fn resolve_css_imports(
opt_insecure: bool, opt_insecure: bool,
) -> String { ) -> String {
let mut resolved_css = String::from(css_string); let mut resolved_css = String::from(css_string);
let re = Regex::new(r###"(?P<import>@import )?url\((?P<to_repl>"?(?P<url>[^"]+)"?)\)"###).unwrap(); let re =
Regex::new(r###"(?P<import>@import )?url\((?P<to_repl>"?(?P<url>[^"]+)"?)\)"###).unwrap();
for link in re.captures_iter(&css_string) { for link in re.captures_iter(&css_string) {
let target_link = link.name("url").unwrap().as_str(); let target_link = link.name("url").unwrap().as_str();
@ -101,43 +102,42 @@ pub fn resolve_css_imports(
// Download the asset. If it's more CSS, resolve that too // Download the asset. If it's more CSS, resolve that too
let content = match link.name("import") { let content = match link.name("import") {
// The link is an @import link // The link is an @import link
Some(_) => retrieve_asset( Some(_) => retrieve_asset(
cache, cache,
&embedded_url, &embedded_url,
false, // Formating as data URL will be done later false, // Formating as data URL will be done later
"text/css", // Expect CSS "text/css", // Expect CSS
opt_user_agent, opt_user_agent,
opt_silent, opt_silent,
opt_insecure, opt_insecure,
) )
.map(|(content, _)| resolve_css_imports( .map(|(content, _)| {
cache, resolve_css_imports(
&content, cache,
true, //NOW, convert to data URL &content,
&embedded_url, true, //NOW, convert to data URL
opt_user_agent, &embedded_url,
opt_silent, opt_user_agent,
opt_insecure, opt_silent,
)), opt_insecure,
)
}),
// The link is some other, non-@import link // The link is some other, non-@import link
None => retrieve_asset( None => retrieve_asset(
cache, cache,
&embedded_url, &embedded_url,
true, // Format as data URL true, // Format as data URL
"", // Unknown MIME type "", // Unknown MIME type
opt_user_agent, opt_user_agent,
opt_silent, opt_silent,
opt_insecure, opt_insecure,
).map(|(a, _)| a), )
.map(|(a, _)| a),
}.unwrap_or_else(|e| { }
eprintln!( .unwrap_or_else(|e| {
"Warning: {}", eprintln!("Warning: {}", e,);
e,
);
//If failed to resolve, replace with absolute URL //If failed to resolve, replace with absolute URL
embedded_url embedded_url