Add support for picture elements

This commit is contained in:
Vincent Flyson 2019-08-24 11:21:29 -04:00
parent 625c529cf1
commit d49e825777
2 changed files with 46 additions and 2 deletions

View file

@ -1,6 +1,6 @@
[package]
name = "monolith"
version = "2.0.9"
version = "2.0.10"
authors = [
"Sunshine <sunshine@uberspace.net>",
"Mahdi Robatipoor <mahdi.robatipoor@gmil.com>",

View file

@ -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!()
}
}