From d49e825777549f0a56e0c9885cf19b8475af7e5b Mon Sep 17 00:00:00 2001 From: Vincent Flyson Date: Sat, 24 Aug 2019 11:21:29 -0400 Subject: [PATCH] Add support for picture elements --- Cargo.toml | 2 +- src/html.rs | 46 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index debbcc5..c22e10f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "monolith" -version = "2.0.9" +version = "2.0.10" authors = [ "Sunshine ", "Mahdi Robatipoor ", diff --git a/src/html.rs b/src/html.rs index 22052e3..57085fc 100644 --- a/src/html.rs +++ b/src/html.rs @@ -11,6 +11,7 @@ use html5ever::tendril::TendrilSink; enum NodeMatch { Icon, Image, + Source, StyleSheet, Anchor, Script, @@ -50,6 +51,26 @@ const JS_DOM_EVENT_ATTRS: [&str; 21] = [ "onresize", ]; +fn get_parent_node_name(node: &Handle) -> String { + let parent = node.parent.take().clone(); + let parent_node = parent.and_then(|node| node.upgrade()).unwrap(); + + match &parent_node.data { + NodeData::Document => {"".to_string()} + NodeData::Doctype { .. } => {"".to_string()} + NodeData::Text { .. } => {"".to_string()} + NodeData::Comment { .. } => {"".to_string()} + NodeData::Element { + ref name, + attrs: _, + .. + } => { + name.local.as_ref().to_string() + } + NodeData::ProcessingInstruction { .. } => unreachable!() + } +} + pub fn walk_and_embed_assets( url: &str, node: &Handle, @@ -104,6 +125,7 @@ pub fn walk_and_embed_assets( } } "img" => { found = NodeMatch::Image; } + "source" => { found = NodeMatch::Source; } "a" => { found = NodeMatch::Anchor; } "script" => { found = NodeMatch::Script; } "form" => { found = NodeMatch::Form; } @@ -147,6 +169,28 @@ pub fn walk_and_embed_assets( } } } + NodeMatch::Source => { + for attr in attrs_mut.iter_mut() { + if &attr.name.local == "srcset" { + if get_parent_node_name(&node) == "picture" { + if opt_no_images { + attr.value.clear(); + attr.value.push_slice(TRANSPARENT_PIXEL); + } else { + let srcset_full_url = resolve_url(&url, &attr.value.to_string()); + let source_datauri = retrieve_asset( + &srcset_full_url.unwrap(), + true, + "", + opt_user_agent, + ); + attr.value.clear(); + attr.value.push_slice(source_datauri.unwrap().as_str()); + } + } + } + } + } NodeMatch::Anchor => { for attr in attrs_mut.iter_mut() { if &attr.name.local == "href" { @@ -265,7 +309,7 @@ pub fn walk_and_embed_assets( } } - NodeData::ProcessingInstruction { .. } => unreachable!(), + NodeData::ProcessingInstruction { .. } => unreachable!() } }