Merge pull request #243 from snshn/fix-embedding-picture-srcset
Fix embedding of srcset assets for PICTURE nodes
This commit is contained in:
commit
0f55fb3c49
2 changed files with 37 additions and 42 deletions
36
src/html.rs
36
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 let Some(source_attr_srcset_value) = get_node_attr(node, "srcset") {
|
||||||
if parent_node_name == "picture" {
|
if parent_node_name == "picture" {
|
||||||
|
if !source_attr_srcset_value.is_empty() {
|
||||||
if options.no_images {
|
if options.no_images {
|
||||||
set_node_attr(node, "srcset", Some(str!(empty_image!())));
|
set_node_attr(node, "srcset", Some(str!(empty_image!())));
|
||||||
} else {
|
} else {
|
||||||
let srcset_full_url =
|
let resolved_srcset: String = embed_srcset(
|
||||||
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,
|
cache,
|
||||||
client,
|
client,
|
||||||
&url,
|
&url,
|
||||||
&srcset_full_url,
|
&source_attr_srcset_value,
|
||||||
options,
|
options,
|
||||||
depth + 1,
|
depth,
|
||||||
) {
|
|
||||||
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(
|
set_node_attr(node, "srcset", Some(resolved_srcset));
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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!()),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue