From e41fd6a1c6a7b2359a951c1e303626b0bc52d557 Mon Sep 17 00:00:00 2001 From: Sunshine Date: Mon, 22 Feb 2021 16:14:47 -1000 Subject: [PATCH] fix embedding of srcset for PICTURE nodes --- src/html.rs | 52 +++++++++------------------------- src/tests/html/embed_srcset.rs | 27 ++++++++++++++++-- 2 files changed, 37 insertions(+), 42 deletions(-) diff --git a/src/html.rs b/src/html.rs index 4c2b582..9e9b829 100644 --- a/src/html.rs +++ b/src/html.rs @@ -1055,45 +1055,19 @@ pub fn walk_and_embed_assets( if let Some(source_attr_srcset_value) = get_node_attr(node, "srcset") { if parent_node_name == "picture" { - if options.no_images { - set_node_attr(node, "srcset", Some(str!(empty_image!()))); - } else { - let srcset_full_url = - resolve_url(&url, source_attr_srcset_value).unwrap_or_default(); - let srcset_url_fragment = get_url_fragment(srcset_full_url.clone()); - match retrieve_asset( - cache, - client, - &url, - &srcset_full_url, - options, - depth + 1, - ) { - 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); - } - } + if !source_attr_srcset_value.is_empty() { + if options.no_images { + set_node_attr(node, "srcset", Some(str!(empty_image!()))); + } else { + let resolved_srcset: String = embed_srcset( + cache, + client, + &url, + &source_attr_srcset_value, + options, + depth, + ); + set_node_attr(node, "srcset", Some(resolved_srcset)); } } } diff --git a/src/tests/html/embed_srcset.rs b/src/tests/html/embed_srcset.rs index a2c2f59..704bdfa 100644 --- a/src/tests/html/embed_srcset.rs +++ b/src/tests/html/embed_srcset.rs @@ -14,10 +14,10 @@ mod passing { use crate::opts::Options; #[test] - fn replace_with_empty_images() { + fn small_medium_large() { let cache = &mut HashMap::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(); options.no_images = true; options.silent = true; @@ -25,7 +25,28 @@ mod passing { assert_eq!( 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!()), ); }