Merge pull request #243 from snshn/fix-embedding-picture-srcset

Fix embedding of srcset assets for PICTURE nodes
This commit is contained in:
Sunshine 2021-02-22 16:27:22 -10:00 committed by GitHub
commit 0f55fb3c49
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 42 deletions

View file

@ -1055,45 +1055,19 @@ pub fn walk_and_embed_assets(
if let Some(source_attr_srcset_value) = get_node_attr(node, "srcset") { if let Some(source_attr_srcset_value) = get_node_attr(node, "srcset") {
if parent_node_name == "picture" { if parent_node_name == "picture" {
if options.no_images { if !source_attr_srcset_value.is_empty() {
set_node_attr(node, "srcset", Some(str!(empty_image!()))); if options.no_images {
} else { set_node_attr(node, "srcset", Some(str!(empty_image!())));
let srcset_full_url = } else {
resolve_url(&url, source_attr_srcset_value).unwrap_or_default(); let resolved_srcset: String = embed_srcset(
let srcset_url_fragment = get_url_fragment(srcset_full_url.clone()); cache,
match retrieve_asset( client,
cache, &url,
client, &source_attr_srcset_value,
&url, options,
&srcset_full_url, depth,
options, );
depth + 1, set_node_attr(node, "srcset", Some(resolved_srcset));
) {
Ok((srcset_data, srcset_final_url, srcset_media_type)) => {
let srcset_data_url = data_to_data_url(
&srcset_media_type,
&srcset_data,
&srcset_final_url,
);
let assembled_url: String = url_with_fragment(
srcset_data_url.as_str(),
srcset_url_fragment.as_str(),
);
set_node_attr(node, "srcset", Some(assembled_url));
}
Err(_) => {
if is_http_url(srcset_full_url.clone()) {
// Keep remote reference if unable to retrieve the asset
let assembled_url: String = url_with_fragment(
srcset_full_url.as_str(),
srcset_url_fragment.as_str(),
);
set_node_attr(node, "srcset", Some(assembled_url));
} else {
// Exclude non-remote URLs
set_node_attr(node, "srcset", None);
}
}
} }
} }
} }

View file

@ -14,10 +14,10 @@ mod passing {
use crate::opts::Options; use crate::opts::Options;
#[test] #[test]
fn replace_with_empty_images() { fn small_medium_large() {
let cache = &mut HashMap::new(); let cache = &mut HashMap::new();
let client = Client::new(); let client = Client::new();
let srcset_value = "small.png 1x, large.png 2x"; let srcset_value = "small.png 1x, medium.png 1.5x, large.png 2x";
let mut options = Options::default(); let mut options = Options::default();
options.no_images = true; options.no_images = true;
options.silent = true; options.silent = true;
@ -25,7 +25,28 @@ mod passing {
assert_eq!( assert_eq!(
embedded_css, embedded_css,
format!("{} 1x, {} 2x", empty_image!(), empty_image!()), format!(
"{} 1x, {} 1.5x, {} 2x",
empty_image!(),
empty_image!(),
empty_image!(),
),
);
}
#[test]
fn small_medium_only_medium_has_scale() {
let cache = &mut HashMap::new();
let client = Client::new();
let srcset_value = "small.png, medium.png 1.5x";
let mut options = Options::default();
options.no_images = true;
options.silent = true;
let embedded_css = html::embed_srcset(cache, &client, "", &srcset_value, &options, 0);
assert_eq!(
embedded_css,
format!("{}, {} 1.5x", empty_image!(), empty_image!()),
); );
} }