Skip to content

Commit de3b588

Browse files
committed
fix embedding mechanics of images linked within SVG
1 parent eb2b384 commit de3b588

File tree

3 files changed

+68
-9
lines changed

3 files changed

+68
-9
lines changed

src/html.rs

+29-8
Original file line numberDiff line numberDiff line change
@@ -860,12 +860,6 @@ pub fn walk_and_embed_assets(
860860
}
861861
}
862862
}
863-
"svg" => {
864-
if options.no_images {
865-
// Remove all children
866-
node.children.borrow_mut().clear();
867-
}
868-
}
869863
"input" => {
870864
if let Some(input_attr_type_value) = get_node_attr(node, "type") {
871865
if input_attr_type_value.eq_ignore_ascii_case("image") {
@@ -892,16 +886,43 @@ pub fn walk_and_embed_assets(
892886
}
893887
}
894888
}
895-
"image" | "use" => {
889+
"svg" => {
890+
if options.no_images {
891+
// Remove all children
892+
node.children.borrow_mut().clear();
893+
}
894+
}
895+
"image" => {
896896
let attr_names: [&str; 2] = ["href", "xlink:href"];
897897

898898
for attr_name in attr_names.into_iter() {
899899
if let Some(image_attr_href_value) = get_node_attr(node, attr_name) {
900+
if options.no_images {
901+
set_node_attr(node, attr_name, None);
902+
} else {
903+
retrieve_and_embed_asset(
904+
cache,
905+
client,
906+
document_url,
907+
node,
908+
attr_name,
909+
&image_attr_href_value,
910+
options,
911+
);
912+
}
913+
}
914+
}
915+
}
916+
"use" => {
917+
let attr_names: [&str; 2] = ["href", "xlink:href"];
918+
919+
for attr_name in attr_names.into_iter() {
920+
if let Some(use_attr_href_value) = get_node_attr(node, attr_name) {
900921
if options.no_images {
901922
set_node_attr(node, attr_name, None);
902923
} else {
903924
let image_asset_url: Url =
904-
resolve_url(document_url, &image_attr_href_value);
925+
resolve_url(document_url, &use_attr_href_value);
905926

906927
match retrieve_asset(
907928
cache,

tests/_data_/svg/image.html

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<html>
2+
<body>
3+
<svg height="24" width="24">
4+
<image href="image.svg" width="24" height="24"></use>
5+
</svg>
6+
</body>
7+
</html>

tests/cli/local_files.rs

+32-1
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ mod passing {
224224
)
225225
);
226226

227-
// STDOUT should contain HTML with date URL for background-image in it
227+
// STDOUT should contain HTML with one symbol extracted from SVG file
228228
assert_eq!(
229229
String::from_utf8_lossy(&out.stdout),
230230
"<html><head></head><body>\n<button class=\"tm-votes-lever__button\" data-test-id=\"votes-lever-upvote-button\" title=\"Like\" type=\"button\">\n <svg class=\"tm-svg-img tm-votes-lever__icon\" height=\"24\" width=\"24\">\n <title>Like</title>\n <use xlink:href=\"#icon-1\"><symbol id=\"icon-1\">\n <path fill-rule=\"evenodd\" clip-rule=\"evenodd\" d=\"M10 20h4V10h3l-5-6.5L7 10h3v10Z\"></path>\n </symbol></use>\n </svg>\n</button>\n\n\n</body></html>\n"
@@ -234,6 +234,37 @@ mod passing {
234234
out.assert().code(0);
235235
}
236236

237+
#[test]
238+
fn embed_svg_local_asset_via_image() {
239+
let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap();
240+
let path_html: &Path = Path::new("tests/_data_/svg/image.html");
241+
let path_svg: &Path = Path::new("tests/_data_/svg/image.svg");
242+
243+
let out = cmd.arg("-M").arg(path_html.as_os_str()).output().unwrap();
244+
245+
// STDERR should list files that got retrieved
246+
assert_eq!(
247+
String::from_utf8_lossy(&out.stderr),
248+
format!(
249+
"\
250+
{file_url_html}\n\
251+
{file_url_svg}\n\
252+
",
253+
file_url_html = Url::from_file_path(fs::canonicalize(path_html).unwrap()).unwrap(),
254+
file_url_svg = Url::from_file_path(fs::canonicalize(path_svg).unwrap()).unwrap(),
255+
)
256+
);
257+
258+
// STDOUT should contain HTML with data URL of SVG file
259+
assert_eq!(
260+
String::from_utf8_lossy(&out.stdout),
261+
"<html><head></head><body>\n <svg height=\"24\" width=\"24\">\n <image href=\"data:image/svg+xml;base64,PHN2ZyB2ZXJzaW9uPSIxLjEiIGJhc2VQcm9maWxlPSJmdWxsIiB3aWR0aD0iMzAwIiBoZWlnaHQ9IjIwMCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KICAgIDxyZWN0IHdpZHRoPSIxMDAlIiBoZWlnaHQ9IjEwMCUiIGZpbGw9InJlZCIgLz4KICAgIDxjaXJjbGUgY3g9IjE1MCIgY3k9IjEwMCIgcj0iODAiIGZpbGw9ImdyZWVuIiAvPgogICAgPHRleHQgeD0iMTUwIiB5PSIxMjUiIGZvbnQtc2l6ZT0iNjAiIHRleHQtYW5jaG9yPSJtaWRkbGUiIGZpbGw9IndoaXRlIj5TVkc8L3RleHQ+Cjwvc3ZnPgo=\" width=\"24\" height=\"24\">\n </image></svg>\n \n\n</body></html>\n"
262+
);
263+
264+
// Exit code should be 0
265+
out.assert().code(0);
266+
}
267+
237268
#[test]
238269
fn discard_integrity_for_local_files() {
239270
let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap();

0 commit comments

Comments
 (0)