Lazyloaded images are now loaded at compilation, with placeholders omitted
This commit is contained in:
parent
feb37f5812
commit
292221ea28
57
src/html.rs
57
src/html.rs
@ -2,7 +2,7 @@ use html5ever::interface::QualName;
|
|||||||
use html5ever::parse_document;
|
use html5ever::parse_document;
|
||||||
use html5ever::rcdom::{Handle, NodeData, RcDom};
|
use html5ever::rcdom::{Handle, NodeData, RcDom};
|
||||||
use html5ever::serialize::{serialize, SerializeOpts};
|
use html5ever::serialize::{serialize, SerializeOpts};
|
||||||
use html5ever::tendril::{format_tendril, TendrilSink};
|
use html5ever::tendril::{format_tendril, Tendril, TendrilSink};
|
||||||
use html5ever::tree_builder::{Attribute, TreeSink};
|
use html5ever::tree_builder::{Attribute, TreeSink};
|
||||||
use html5ever::{local_name, namespace_url, ns};
|
use html5ever::{local_name, namespace_url, ns};
|
||||||
use http::retrieve_asset;
|
use http::retrieve_asset;
|
||||||
@ -157,35 +157,50 @@ pub fn walk_and_embed_assets(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
"img" => {
|
"img" => {
|
||||||
for attr in attrs_mut.iter_mut() {
|
// Find source tags
|
||||||
if &attr.name.local == "src" || &attr.name.local == "data-src" {
|
let mut found_src: Option<Attribute> = None;
|
||||||
let value = attr.value.to_string();
|
let mut found_datasrc: Option<Attribute> = None;
|
||||||
|
let mut i = 0;
|
||||||
// Ignore images with empty source
|
while i < attrs_mut.len() {
|
||||||
if value == EMPTY_STRING.clone() {
|
let name = attrs_mut[i].name.local.as_ref();
|
||||||
continue;
|
if name.eq_ignore_ascii_case("src") {
|
||||||
|
found_src = Some(attrs_mut.remove(i));
|
||||||
|
} else if name.eq_ignore_ascii_case("data-src") {
|
||||||
|
found_datasrc = Some(attrs_mut.remove(i));
|
||||||
|
} else {
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If images are disabled, clear both sources
|
||||||
if opt_no_images {
|
if opt_no_images {
|
||||||
attr.value.clear();
|
attrs_mut.push(Attribute {
|
||||||
attr.value.push_slice(TRANSPARENT_PIXEL);
|
name: QualName::new(None, ns!(), local_name!("src")),
|
||||||
} else {
|
value: Tendril::from_slice(TRANSPARENT_PIXEL),
|
||||||
let src_full_url: String =
|
});
|
||||||
resolve_url(&url, &value).unwrap_or(EMPTY_STRING.clone());
|
} else if let Some((dataurl, _)) = (&found_datasrc)
|
||||||
let (img_dataurl, _) = retrieve_asset(
|
.into_iter()
|
||||||
|
.chain(&found_src) // Give dataurl priority
|
||||||
|
.map(|attr| &attr.value)
|
||||||
|
.filter(|src| !src.is_empty()) // Ignore empty srcs
|
||||||
|
.next()
|
||||||
|
.and_then(|src| resolve_url(&url, src).ok()) //Make absolute
|
||||||
|
.and_then(|abs_src| // Download and convert to dataurl
|
||||||
|
retrieve_asset(
|
||||||
cache,
|
cache,
|
||||||
&src_full_url,
|
&abs_src,
|
||||||
true,
|
true,
|
||||||
"",
|
"",
|
||||||
opt_user_agent,
|
opt_user_agent,
|
||||||
opt_silent,
|
opt_silent,
|
||||||
opt_insecure,
|
opt_insecure,
|
||||||
)
|
).ok())
|
||||||
.unwrap_or((EMPTY_STRING.clone(), EMPTY_STRING.clone()));
|
{
|
||||||
attr.value.clear();
|
// Add the new dataurl src attribute
|
||||||
attr.value.push_slice(img_dataurl.as_str());
|
attrs_mut.push(Attribute {
|
||||||
}
|
name: QualName::new(None, ns!(), local_name!("src")),
|
||||||
}
|
value: Tendril::from_slice(dataurl.as_ref()),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
"source" => {
|
"source" => {
|
||||||
|
Loading…
Reference in New Issue
Block a user