Added support for recursively nested css @imports

This commit is contained in:
Emi Simpson 2019-12-05 18:15:06 -05:00
parent a2bf7e3345
commit 11bbfc0851
No known key found for this signature in database
GPG Key ID: 68FAB2E2E6DFC98B
2 changed files with 39 additions and 24 deletions

View File

@ -142,8 +142,7 @@ pub fn walk_and_embed_assets(
opt_user_agent, opt_user_agent,
opt_silent, opt_silent,
opt_insecure, opt_insecure,
) );
.unwrap_or(css_dataurl);
attr.value.push_slice(css_resolved.as_str()); attr.value.push_slice(css_resolved.as_str());
} }

View File

@ -89,12 +89,12 @@ pub fn resolve_css_imports(
opt_user_agent: &str, opt_user_agent: &str,
opt_silent: bool, opt_silent: bool,
opt_insecure: bool, opt_insecure: bool,
) -> Result<String, String> { ) -> String {
let mut resolved_css = String::from(css_string); let mut resolved_css = String::from(css_string);
let re = Regex::new(r###"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 = dbg!(link.get(1).unwrap().as_str()); let target_link = link.name("url").unwrap().as_str();
// Generate absolute URL for content // Generate absolute URL for content
let embedded_url = match resolve_url(href, target_link) { let embedded_url = match resolve_url(href, target_link) {
@ -102,31 +102,47 @@ pub fn resolve_css_imports(
Err(_) => continue, // Malformed URL Err(_) => continue, // Malformed URL
}; };
let (css_dataurl, _) = retrieve_asset( // Download the asset. If it's more CSS, resolve that too
&embedded_url, let content = match link.name("import") {
true, // true
"", // The link is an @import link
opt_user_agent, Some(_) => retrieve_asset(
opt_silent, &embedded_url,
opt_insecure, false, // Formating as data URL will be done later
) "text/css", // Expect CSS
.unwrap_or((EMPTY_STRING.clone(), EMPTY_STRING.clone())); opt_user_agent,
opt_silent,
opt_insecure,
)
.map(|(content, _)| resolve_css_imports(
&content,
&embedded_url,
opt_user_agent,
opt_silent,
opt_insecure,
)),
// The link is some other, non-@import link
None => retrieve_asset(
&embedded_url,
true, // Format as data URL
"", // Unknown MIME type
opt_user_agent,
opt_silent,
opt_insecure,
).map(|(a, _)| a),
}.unwrap_or_else(|_| EMPTY_STRING.clone());
let replacement = format!("\"{}\"", &content);
let replacement = &[
"\"",
&css_dataurl
.replace("\"", &["\\", "\""].concat())
.to_string(),
"\"",
]
.concat();
let t = resolved_css let t = resolved_css
.replace(&link[0][4..link[0].len() - 1], &replacement) .replace(link.name("to_repl").unwrap().as_str(), &replacement)
.to_string(); .to_string();
resolved_css = t.clone(); resolved_css = t.clone();
} }
let encoded_css = data_to_dataurl("text/css", resolved_css.as_bytes()); let encoded_css = data_to_dataurl("text/css", resolved_css.as_bytes());
Ok(encoded_css.to_string()) encoded_css.to_string()
} }