fix embedding of srcset for PICTURE nodes
This commit is contained in:
parent
4ad07c0519
commit
e41fd6a1c6
2 changed files with 37 additions and 42 deletions
52
src/html.rs
52
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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!()),
|
||||
);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue